diff --git a/src/cli/cmd/apply/home_manager.rs b/src/cli/cmd/apply/home_manager.rs index 4c84ef2..55d2dc0 100644 --- a/src/cli/cmd/apply/home_manager.rs +++ b/src/cli/cmd/apply/home_manager.rs @@ -1,7 +1,5 @@ use clap::Parser; -pub(super) const HOME_MANAGER_SCRIPT: &str = "activate"; - #[derive(Parser)] pub(super) struct HomeManager { /// The FlakeHub output reference for the Home Manager configuration. @@ -14,7 +12,24 @@ impl super::ApplyType for HomeManager { fn get_ref(&self) -> &str { &self.output_ref } + fn default_ref(&self) -> String { format!("homeConfigurations.{}", whoami::username()) } + + fn profile_path(&self) -> Option<&std::path::Path> { + None + } + + fn requires_root(&self) -> bool { + false + } + + fn relative_path(&self) -> &std::path::Path { + std::path::Path::new("activate") + } + + fn action(&self) -> Option { + None + } } diff --git a/src/cli/cmd/apply/mod.rs b/src/cli/cmd/apply/mod.rs index c38403c..d1bfa58 100644 --- a/src/cli/cmd/apply/mod.rs +++ b/src/cli/cmd/apply/mod.rs @@ -12,16 +12,9 @@ use clap::{Parser, Subcommand}; use color_eyre::eyre::Context; use tempfile::{tempdir, TempDir}; -use crate::{ - cli::{cmd::nix_command, error::FhError}, - path, -}; +use crate::cli::{cmd::nix_command, error::FhError}; -use self::{ - home_manager::{HomeManager, HOME_MANAGER_SCRIPT}, - nix_darwin::{NixDarwin, DARWIN_REBUILD_ACTION, NIX_DARWIN_SCRIPT}, - nixos::{NixOs, NIXOS_PROFILE, NIXOS_SCRIPT}, -}; +use self::{home_manager::HomeManager, nix_darwin::NixDarwin, nixos::NixOs}; use super::{CommandExecute, FlakeHubClient}; @@ -53,19 +46,28 @@ enum System { pub trait ApplyType { fn get_ref(&self) -> &str; + fn default_ref(&self) -> String; + + fn profile_path(&self) -> Option<&Path>; + + fn requires_root(&self) -> bool; + + fn relative_path(&self) -> &Path; + + fn action(&self) -> Option; } #[async_trait::async_trait] impl CommandExecute for ApplySubcommand { async fn execute(self) -> color_eyre::Result { - let output_ref = { - let applyer: Box<&dyn ApplyType> = match &self.system { - System::HomeManager(home_manager) => Box::new(home_manager), - System::NixOs(nixos) => Box::new(nixos), - System::NixDarwin(nix_darwin) => Box::new(nix_darwin), - }; + let applyer: Box<&(dyn ApplyType + Send + Sync)> = match &self.system { + System::HomeManager(home_manager) => Box::new(home_manager), + System::NixOs(nixos) => Box::new(nixos), + System::NixDarwin(nix_darwin) => Box::new(nix_darwin), + }; + let output_ref = { parse_output_ref( &self.frontend_addr, applyer.get_ref(), @@ -83,52 +85,25 @@ impl CommandExecute for ApplySubcommand { &resolved_path.store_path ); - match self.system { - System::HomeManager(HomeManager { .. }) => { - let (profile_path, _tempdir) = apply_path_to_profile( - None, - &resolved_path.store_path, - false, // don't sudo when running `nix build` - ) - .await?; - - // /nix/store/{path}/activate - let script_path = path!(&profile_path, HOME_MANAGER_SCRIPT); - - run_script(script_path, None, HOME_MANAGER_SCRIPT).await?; - } - System::NixDarwin(NixDarwin { profile, .. }) => { - apply_path_to_profile( - Some(&profile), - &resolved_path.store_path, - true, // sudo if necessary when running `nix build` - ) - .await?; - - // {path}/sw/bin/darwin-rebuild - let script_path = path!(&profile, "sw", "bin", NIX_DARWIN_SCRIPT); - - run_script( - script_path, - Some(DARWIN_REBUILD_ACTION.to_string()), - NIX_DARWIN_SCRIPT, - ) - .await?; - } - System::NixOs(NixOs { action, .. }) => { - let profile_path = Path::new(NIXOS_PROFILE); - apply_path_to_profile( - Some(Path::new(NIXOS_PROFILE)), - &resolved_path.store_path, - true, // sudo if necessary when running `nix build` - ) - .await?; - - let script_path = path!(&profile_path, "bin", NIXOS_SCRIPT); - - run_script(script_path, Some(action.to_string()), NIXOS_SCRIPT).await?; - } - } + let (profile_path, _tempdir) = apply_path_to_profile( + applyer.profile_path(), + &resolved_path.store_path, + applyer.requires_root(), + ) + .await?; + + let script_path = profile_path.join(applyer.relative_path()); + + run_script( + script_path, + applyer.action(), + &applyer + .relative_path() + .file_name() + .expect("The apply type should absolutely have a file name.") + .to_string_lossy(), + ) + .await?; Ok(ExitCode::SUCCESS) } diff --git a/src/cli/cmd/apply/nix_darwin.rs b/src/cli/cmd/apply/nix_darwin.rs index b25f722..c984154 100644 --- a/src/cli/cmd/apply/nix_darwin.rs +++ b/src/cli/cmd/apply/nix_darwin.rs @@ -1,8 +1,5 @@ use clap::Parser; -pub(super) const DARWIN_REBUILD_ACTION: &str = "activate"; -pub(super) const NIX_DARWIN_SCRIPT: &str = "darwin-rebuild"; - #[derive(Parser)] pub(super) struct NixDarwin { /// The FlakeHub output reference for the nix-darwin configuration. @@ -28,4 +25,20 @@ impl super::ApplyType for NixDarwin { fn default_ref(&self) -> String { format!("darwinConfigurations.{}", whoami::devicename(),) } + + fn profile_path(&self) -> Option<&std::path::Path> { + Some(&self.profile) + } + + fn requires_root(&self) -> bool { + true + } + + fn relative_path(&self) -> &std::path::Path { + std::path::Path::new("sw/bin/darwin-rebuild") + } + + fn action(&self) -> Option { + Some("activate".to_string()) + } } diff --git a/src/cli/cmd/apply/nixos.rs b/src/cli/cmd/apply/nixos.rs index 6aeca15..155cce8 100644 --- a/src/cli/cmd/apply/nixos.rs +++ b/src/cli/cmd/apply/nixos.rs @@ -2,9 +2,6 @@ use std::fmt::Display; use clap::{Parser, ValueEnum}; -pub(super) const NIXOS_PROFILE: &str = "/nix/var/nix/profiles/system"; -pub(super) const NIXOS_SCRIPT: &str = "switch-to-configuration"; - #[derive(Parser)] pub(super) struct NixOs { /// The FlakeHub output reference to apply to the system profile. @@ -29,6 +26,22 @@ impl super::ApplyType for NixOs { gethostname::gethostname().to_string_lossy() ) } + + fn profile_path(&self) -> Option<&std::path::Path> { + Some(std::path::Path::new("/nix/var/nix/profiles/system")) + } + + fn requires_root(&self) -> bool { + true + } + + fn relative_path(&self) -> &std::path::Path { + std::path::Path::new("bin/switch-to-configuration") + } + + fn action(&self) -> Option { + Some(self.action.to_string()) + } } // For available commands, see diff --git a/src/cli/cmd/mod.rs b/src/cli/cmd/mod.rs index 22ffeab..2c91050 100644 --- a/src/cli/cmd/mod.rs +++ b/src/cli/cmd/mod.rs @@ -382,19 +382,6 @@ macro_rules! flakehub_url { }}; } -#[macro_export] -macro_rules! path { - ($root:expr, $($segment:expr),+ $(,)?) => {{ - let mut path = PathBuf::from($root); - - $( - path.push($segment); - )+ - - path - }}; -} - fn is_root_user() -> bool { nix::unistd::getuid().is_root() }