Rename generate to register and make the activate subcommand hidden.

This commit is contained in:
r-vdp 2023-06-06 01:25:57 +02:00
parent bf0e22a14b
commit 7789f31548
No known key found for this signature in database
3 changed files with 51 additions and 44 deletions

View file

@ -1,5 +1,5 @@
pub mod activate; pub mod activate;
pub mod generate; pub mod register;
mod systemd; mod systemd;
use anyhow::Result; use anyhow::Result;

View file

@ -27,7 +27,7 @@ struct Args {
/// Only useful in combination with --target-host /// Only useful in combination with --target-host
use_remote_sudo: bool, use_remote_sudo: bool,
#[clap(long = "nix-option", num_args = 2)] #[clap(long = "nix-option", num_args = 2, global = true)]
nix_options: Option<Vec<String>>, nix_options: Option<Vec<String>>,
} }
@ -39,7 +39,7 @@ struct BuildArgs {
} }
#[derive(clap::Args, Debug)] #[derive(clap::Args, Debug)]
struct GenerateArgs { struct RegisterArgs {
#[arg(long = "flake", name = "FLAKE_URI")] #[arg(long = "flake", name = "FLAKE_URI")]
/// The flake URI defining the system-manager profile /// The flake URI defining the system-manager profile
flake_uri: Option<String>, flake_uri: Option<String>,
@ -83,23 +83,19 @@ struct StoreOrFlakeArgs {
#[derive(clap::Subcommand, Debug)] #[derive(clap::Subcommand, Debug)]
enum Action { enum Action {
/// Activate a given system-manager profile /// Build a new system-manager generation, register it as the active profile, and activate it
Activate { Switch {
#[arg(long)] #[command(flatten)]
/// The store path containing the system-manager profile to activate build_args: BuildArgs,
store_path: StorePath,
#[command(flatten)] #[command(flatten)]
activation_args: ActivationArgs, activation_args: ActivationArgs,
}, },
/// Put all files defined by the given generation in place, but do not start /// Build a new system-manager generation and register is as the active system-manager profile
/// services. Useful in build scripts. Register {
PrePopulate {
#[command(flatten)] #[command(flatten)]
store_or_flake_args: StoreOrFlakeArgs, store_or_flake_args: StoreOrFlakeArgs,
#[command(flatten)]
activation_args: ActivationArgs,
}, },
/// Build a new system-manager profile without registering it as a nix profile /// Build a new system-manager profile without registering it as a profile
Build { Build {
#[command(flatten)] #[command(flatten)]
build_args: BuildArgs, build_args: BuildArgs,
@ -109,16 +105,21 @@ enum Action {
#[command(flatten)] #[command(flatten)]
optional_store_path_args: OptionalStorePathArg, optional_store_path_args: OptionalStorePathArg,
}, },
/// Generate a new system-manager profile and /// Put all files defined by the given generation in place, but do not start
/// register is as the active system-manager profile /// services. Useful in build scripts.
Generate { PrePopulate {
#[command(flatten)] #[command(flatten)]
store_or_flake_args: StoreOrFlakeArgs, store_or_flake_args: StoreOrFlakeArgs,
},
/// Generate a new system-manager profile and activate it
Switch {
#[command(flatten)] #[command(flatten)]
build_args: BuildArgs, activation_args: ActivationArgs,
},
/// Activate a given system-manager profile.
/// This is a low-level action that should not be used directly.
#[clap(hide = true)]
Activate {
#[arg(long)]
/// The store path containing the system-manager profile to activate
store_path: StorePath,
#[command(flatten)] #[command(flatten)]
activation_args: ActivationArgs, activation_args: ActivationArgs,
}, },
@ -150,13 +151,6 @@ fn go(args: Args) -> Result<()> {
})); }));
match action { match action {
Action::Activate {
store_path,
activation_args: ActivationArgs { ephemeral },
} => {
copy_closure(&store_path, &target_host)?;
activate(&store_path, ephemeral, &target_host, use_remote_sudo)
}
Action::PrePopulate { Action::PrePopulate {
store_or_flake_args, store_or_flake_args,
activation_args: ActivationArgs { ephemeral }, activation_args: ActivationArgs { ephemeral },
@ -174,9 +168,9 @@ fn go(args: Args) -> Result<()> {
Action::Deactivate { Action::Deactivate {
optional_store_path_args: OptionalStorePathArg { maybe_store_path }, optional_store_path_args: OptionalStorePathArg { maybe_store_path },
} => deactivate(maybe_store_path, &target_host, use_remote_sudo), } => deactivate(maybe_store_path, &target_host, use_remote_sudo),
Action::Generate { Action::Register {
store_or_flake_args, store_or_flake_args,
} => generate( } => register(
store_or_flake_args, store_or_flake_args,
&target_host, &target_host,
use_remote_sudo, use_remote_sudo,
@ -189,7 +183,14 @@ fn go(args: Args) -> Result<()> {
} => { } => {
let store_path = do_build(&flake_uri, &nix_options)?; let store_path = do_build(&flake_uri, &nix_options)?;
copy_closure(&store_path, &target_host)?; copy_closure(&store_path, &target_host)?;
do_generate(&store_path, &target_host, use_remote_sudo)?; do_register(&store_path, &target_host, use_remote_sudo, &nix_options)?;
activate(&store_path, ephemeral, &target_host, use_remote_sudo)
}
Action::Activate {
store_path,
activation_args: ActivationArgs { ephemeral },
} => {
copy_closure(&store_path, &target_host)?;
activate(&store_path, ephemeral, &target_host, use_remote_sudo) activate(&store_path, ephemeral, &target_host, use_remote_sudo)
} }
} }
@ -212,10 +213,10 @@ fn build(
} }
fn do_build(flake_uri: &str, nix_options: &NixOptions) -> Result<StorePath> { fn do_build(flake_uri: &str, nix_options: &NixOptions) -> Result<StorePath> {
system_manager::generate::build(flake_uri, nix_options) system_manager::register::build(flake_uri, nix_options)
} }
fn generate( fn register(
args: StoreOrFlakeArgs, args: StoreOrFlakeArgs,
target_host: &Option<String>, target_host: &Option<String>,
use_remote_sudo: bool, use_remote_sudo: bool,
@ -234,7 +235,7 @@ fn generate(
} => { } => {
let store_path = do_build(&flake_uri, nix_options)?; let store_path = do_build(&flake_uri, nix_options)?;
copy_closure(&store_path, target_host)?; copy_closure(&store_path, target_host)?;
do_generate(&store_path, target_host, use_remote_sudo)?; do_register(&store_path, target_host, use_remote_sudo, nix_options)?;
Ok(store_path) Ok(store_path)
} }
StoreOrFlakeArgs { StoreOrFlakeArgs {
@ -248,7 +249,7 @@ fn generate(
}, },
} => { } => {
copy_closure(&store_path, target_host)?; copy_closure(&store_path, target_host)?;
do_generate(&store_path, target_host, use_remote_sudo)?; do_register(&store_path, target_host, use_remote_sudo, nix_options)?;
Ok(store_path) Ok(store_path)
} }
_ => { _ => {
@ -257,10 +258,11 @@ fn generate(
} }
} }
fn do_generate( fn do_register(
store_path: &StorePath, store_path: &StorePath,
target_host: &Option<String>, target_host: &Option<String>,
use_remote_sudo: bool, use_remote_sudo: bool,
nix_options: &NixOptions,
) -> Result<()> { ) -> Result<()> {
if let Some(target_host) = target_host { if let Some(target_host) = target_host {
let status = invoke_remote_script( let status = invoke_remote_script(
@ -281,7 +283,7 @@ fn do_generate(
} }
} else { } else {
check_root()?; check_root()?;
system_manager::generate::generate(store_path) system_manager::register::register(store_path, nix_options)
} }
} }
@ -325,7 +327,7 @@ fn prepopulate(
} => { } => {
let store_path = do_build(&flake_uri, nix_options)?; let store_path = do_build(&flake_uri, nix_options)?;
copy_closure(&store_path, target_host)?; copy_closure(&store_path, target_host)?;
do_generate(&store_path, target_host, use_remote_sudo)?; do_register(&store_path, target_host, use_remote_sudo, nix_options)?;
do_prepopulate(&store_path, ephemeral, target_host, use_remote_sudo)?; do_prepopulate(&store_path, ephemeral, target_host, use_remote_sudo)?;
Ok(store_path) Ok(store_path)
} }
@ -339,7 +341,7 @@ fn prepopulate(
let store_path = StorePath::try_from(store_path_or_active_profile(maybe_store_path))?; let store_path = StorePath::try_from(store_path_or_active_profile(maybe_store_path))?;
copy_closure(&store_path, target_host)?; copy_closure(&store_path, target_host)?;
//TODO: this currently fails in the VM test, need to figure out why //TODO: this currently fails in the VM test, need to figure out why
//do_generate(&store_path, target_host, use_remote_sudo)?; do_register(&store_path, target_host, use_remote_sudo, nix_options)?;
do_prepopulate(&store_path, ephemeral, target_host, use_remote_sudo)?; do_prepopulate(&store_path, ephemeral, target_host, use_remote_sudo)?;
Ok(store_path) Ok(store_path)
} }

View file

@ -17,12 +17,12 @@ struct NixBuildOutput {
outputs: HashMap<String, String>, outputs: HashMap<String, String>,
} }
pub fn generate(store_path: &StorePath) -> Result<()> { pub fn register(store_path: &StorePath, nix_options: &NixOptions) -> Result<()> {
let profile_dir = Path::new(PROFILE_DIR); let profile_dir = Path::new(PROFILE_DIR);
let profile_name = Path::new(PROFILE_NAME); let profile_name = Path::new(PROFILE_NAME);
log::info!("Creating new generation from {store_path}"); log::info!("Creating new generation from {store_path}");
let status = install_nix_profile(store_path, profile_dir, profile_name)?; let status = install_nix_profile(store_path, profile_dir, profile_name, nix_options)?;
if !status.success() { if !status.success() {
anyhow::bail!("Error installing the nix profile, see above for details."); anyhow::bail!("Error installing the nix profile, see above for details.");
} }
@ -38,16 +38,21 @@ fn install_nix_profile(
store_path: &StorePath, store_path: &StorePath,
profile_dir: &Path, profile_dir: &Path,
profile_name: &Path, profile_name: &Path,
nix_options: &NixOptions,
) -> Result<process::ExitStatus> { ) -> Result<process::ExitStatus> {
DirBuilder::new() DirBuilder::new()
.recursive(true) .recursive(true)
.create(profile_dir) .create(profile_dir)
.context("While creating the profile dir.")?; .context("While creating the profile dir.")?;
let status = process::Command::new("nix-env") let mut cmd = process::Command::new("nix-env");
.arg("--profile") cmd.arg("--profile")
.arg(profile_dir.join(profile_name)) .arg(profile_dir.join(profile_name))
.arg("--set") .arg("--set")
.arg(&store_path.store_path) .arg(&store_path.store_path);
nix_options.options.iter().for_each(|option| {
cmd.arg("--option").arg(&option.0).arg(&option.1);
});
let status = cmd
.stdout(process::Stdio::inherit()) .stdout(process::Stdio::inherit())
.stderr(process::Stdio::inherit()) .stderr(process::Stdio::inherit())
.status() .status()