Copy the closure when running a build with --target_host.

This commit is contained in:
R-VdP 2023-02-21 12:15:46 +01:00
parent d331287116
commit 06bc73beae
No known key found for this signature in database
2 changed files with 32 additions and 20 deletions

View file

@ -57,7 +57,9 @@ pub fn build(flake_uri: &str) -> Result<StorePath> {
log::info!("Building new system-manager generation..."); log::info!("Building new system-manager generation...");
log::info!("Running nix build..."); log::info!("Running nix build...");
run_nix_build(flake_uri, &flake_attr).and_then(get_store_path) let store_path = run_nix_build(flake_uri, &flake_attr).and_then(get_store_path)?;
log::info!("Build system-manager profile {store_path}");
Ok(store_path)
} }
fn get_store_path(nix_build_result: process::Output) -> Result<StorePath> { fn get_store_path(nix_build_result: process::Output) -> Result<StorePath> {

View file

@ -98,20 +98,20 @@ fn go(args: Args) -> Result<()> {
} }
Action::Build { Action::Build {
build_args: BuildArgs { flake_uri }, build_args: BuildArgs { flake_uri },
} => build(flake_uri), } => build(&flake_uri, &target_host),
Action::Deactivate => { Action::Deactivate => {
check_root()?; check_root()?;
// FIXME handle target_host // FIXME handle target_host
deactivate() deactivate()
} }
Action::Generate { generate_args } => { Action::Generate { generate_args } => {
generate(generate_args, &target_host, use_remote_sudo) generate(&generate_args, &target_host, use_remote_sudo)
} }
Action::Switch { Action::Switch {
build_args: BuildArgs { flake_uri }, build_args: BuildArgs { flake_uri },
activation_args: ActivationArgs { ephemeral }, activation_args: ActivationArgs { ephemeral },
} => { } => {
let store_path = do_build(flake_uri)?; let store_path = do_build(&flake_uri)?;
copy_closure(&store_path, &target_host)?; copy_closure(&store_path, &target_host)?;
do_generate(&store_path, &target_host, use_remote_sudo)?; do_generate(&store_path, &target_host, use_remote_sudo)?;
activate(&store_path, ephemeral, &target_host, use_remote_sudo) activate(&store_path, ephemeral, &target_host, use_remote_sudo)
@ -119,7 +119,23 @@ fn go(args: Args) -> Result<()> {
} }
} }
fn generate(args: GenerateArgs, target_host: &Option<String>, use_remote_sudo: bool) -> Result<()> { fn build(flake_uri: &str, target_host: &Option<String>) -> Result<()> {
let store_path = do_build(flake_uri)?;
copy_closure(&store_path, target_host)?;
// Print the raw store path to stdout
println!("{store_path}");
Ok(())
}
fn do_build(flake_uri: &str) -> Result<StorePath> {
system_manager::generate::build(flake_uri)
}
fn generate(
args: &GenerateArgs,
target_host: &Option<String>,
use_remote_sudo: bool,
) -> Result<()> {
match args { match args {
GenerateArgs { GenerateArgs {
flake_uri: Some(flake_uri), flake_uri: Some(flake_uri),
@ -133,8 +149,8 @@ fn generate(args: GenerateArgs, target_host: &Option<String>, use_remote_sudo: b
flake_uri: None, flake_uri: None,
store_path: Some(store_path), store_path: Some(store_path),
} => { } => {
copy_closure(&store_path, target_host)?; copy_closure(store_path, target_host)?;
do_generate(&store_path, target_host, use_remote_sudo) do_generate(store_path, target_host, use_remote_sudo)
} }
_ => { _ => {
anyhow::bail!("Supply either a flake URI or a store path.") anyhow::bail!("Supply either a flake URI or a store path.")
@ -142,18 +158,6 @@ fn generate(args: GenerateArgs, target_host: &Option<String>, use_remote_sudo: b
} }
} }
fn build(flake_uri: String) -> Result<()> {
let store_path = do_build(flake_uri)?;
log::info!("Build system-manager profile {store_path}");
// Print the raw store path to stdout
println!("{store_path}");
Ok(())
}
fn do_build(flake_uri: String) -> Result<StorePath> {
system_manager::generate::build(&flake_uri)
}
fn do_generate( fn do_generate(
store_path: &StorePath, store_path: &StorePath,
target_host: &Option<String>, target_host: &Option<String>,
@ -194,7 +198,8 @@ fn copy_closure(store_path: &StorePath, target_host: &Option<String>) -> Result<
} }
fn do_copy_closure(store_path: &StorePath, target_host: &str) -> Result<()> { fn do_copy_closure(store_path: &StorePath, target_host: &str) -> Result<()> {
process::Command::new("nix-copy-closure") log::info!("Copying closure to target host...");
let status = process::Command::new("nix-copy-closure")
.arg("--to") .arg("--to")
.arg(target_host) .arg(target_host)
.arg("--use-substitutes") .arg("--use-substitutes")
@ -202,6 +207,11 @@ fn do_copy_closure(store_path: &StorePath, target_host: &str) -> Result<()> {
.stdout(process::Stdio::inherit()) .stdout(process::Stdio::inherit())
.stderr(process::Stdio::inherit()) .stderr(process::Stdio::inherit())
.status()?; .status()?;
if status.success() {
log::info!("Successfully copied closure to target host");
} else {
log::error!("Error copying closure, {}", status);
}
Ok(()) Ok(())
} }