Skip to content

Commit

Permalink
add refresh option
Browse files Browse the repository at this point in the history
  • Loading branch information
xhalo32 committed Oct 9, 2024
1 parent 8182f9e commit fd72c41
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
25 changes: 16 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ struct BuildArgs {
#[arg(long = "flake", name = "FLAKE_URI")]
/// The flake URI defining the system-manager profile
flake_uri: String,

#[arg(long = "refresh", name = "REFRESH")]
/// Refresh flake when building
refresh: bool,
}

#[derive(clap::Args, Debug)]
Expand Down Expand Up @@ -163,8 +167,8 @@ fn go(args: Args) -> Result<()> {
)
.and_then(print_store_path),
Action::Build {
build_args: BuildArgs { flake_uri },
} => build(&flake_uri, &target_host, &nix_options).and_then(print_store_path),
build_args: BuildArgs { flake_uri, refresh },
} => build(&flake_uri, &target_host, &nix_options, refresh).and_then(print_store_path),
Action::Deactivate {
optional_store_path_args: OptionalStorePathArg { maybe_store_path },
} => deactivate(maybe_store_path, &target_host, use_remote_sudo),
Expand All @@ -178,10 +182,10 @@ fn go(args: Args) -> Result<()> {
)
.and_then(print_store_path),
Action::Switch {
build_args: BuildArgs { flake_uri },
build_args: BuildArgs { flake_uri, refresh },
activation_args: ActivationArgs { ephemeral },
} => {
let store_path = do_build(&flake_uri, &nix_options)?;
let store_path = do_build(&flake_uri, &nix_options, refresh)?;
copy_closure(&store_path, &target_host)?;
do_register(&store_path, &target_host, use_remote_sudo, &nix_options)?;
activate(&store_path, ephemeral, &target_host, use_remote_sudo)
Expand All @@ -206,14 +210,15 @@ fn build(
flake_uri: &str,
target_host: &Option<String>,
nix_options: &NixOptions,
refresh: bool,
) -> Result<StorePath> {
let store_path = do_build(flake_uri, nix_options)?;
let store_path = do_build(flake_uri, nix_options, refresh)?;
copy_closure(&store_path, target_host)?;
Ok(store_path)
}

fn do_build(flake_uri: &str, nix_options: &NixOptions) -> Result<StorePath> {
system_manager::register::build(flake_uri, nix_options)
fn do_build(flake_uri: &str, nix_options: &NixOptions, refresh: bool) -> Result<StorePath> {
system_manager::register::build(flake_uri, nix_options, refresh)
}

fn register(
Expand All @@ -233,7 +238,8 @@ fn register(
maybe_flake_uri: Some(flake_uri),
},
} => {
let store_path = do_build(&flake_uri, nix_options)?;
// TODO is refresh = true a sensible default
let store_path = do_build(&flake_uri, nix_options, true)?;
copy_closure(&store_path, target_host)?;
do_register(&store_path, target_host, use_remote_sudo, nix_options)?;
Ok(store_path)
Expand Down Expand Up @@ -325,7 +331,8 @@ fn prepopulate(
maybe_flake_uri: Some(flake_uri),
},
} => {
let store_path = do_build(&flake_uri, nix_options)?;
// TODO is refresh = true a sensible default
let store_path = do_build(&flake_uri, nix_options, true)?;
copy_closure(&store_path, target_host)?;
do_register(&store_path, target_host, use_remote_sudo, nix_options)?;
do_prepopulate(&store_path, ephemeral, target_host, use_remote_sudo)?;
Expand Down
13 changes: 10 additions & 3 deletions src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ fn create_gcroot(gcroot_path: &str, profile_path: &Path) -> Result<()> {
create_store_link(&store_path, Path::new(gcroot_path))
}

pub fn build(flake_uri: &str, nix_options: &NixOptions) -> Result<StorePath> {
pub fn build(flake_uri: &str, nix_options: &NixOptions, refresh: bool) -> Result<StorePath> {
let full_flake_uri = find_flake_attr(flake_uri, nix_options)?;

log::info!("Building new system-manager generation...");
log::info!("Running nix build...");
let store_path =
run_nix_build(full_flake_uri.as_ref(), nix_options).and_then(get_store_path)?;
run_nix_build(full_flake_uri.as_ref(), nix_options, refresh).and_then(get_store_path)?;
log::info!("Built system-manager profile {store_path}");
Ok(store_path)
}
Expand Down Expand Up @@ -152,9 +152,16 @@ fn parse_nix_build_output(output: String) -> Result<StorePath> {
anyhow::bail!("Multiple build results were returned, we cannot handle that yet.")
}

fn run_nix_build(flake_uri: &str, nix_options: &NixOptions) -> Result<process::Output> {
fn run_nix_build(
flake_uri: &str,
nix_options: &NixOptions,
refresh: bool,
) -> Result<process::Output> {
let mut cmd = nix_cmd(nix_options);
cmd.arg("build").arg(flake_uri).arg("--json");
if refresh {
cmd.arg("--refresh");
}

log::debug!("Running nix command: {cmd:?}");

Expand Down

0 comments on commit fd72c41

Please sign in to comment.