Skip to content

Commit

Permalink
Flatten the implementation into a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamc committed Jul 30, 2024
1 parent 69dfff8 commit ad8672c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 82 deletions.
19 changes: 17 additions & 2 deletions src/cli/cmd/apply/home_manager.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<String> {
None
}
}
97 changes: 36 additions & 61 deletions src/cli/cmd/apply/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<String>;
}

#[async_trait::async_trait]
impl CommandExecute for ApplySubcommand {
async fn execute(self) -> color_eyre::Result<ExitCode> {
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(),
Expand All @@ -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)
}
Expand Down
19 changes: 16 additions & 3 deletions src/cli/cmd/apply/nix_darwin.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<String> {
Some("activate".to_string())
}
}
19 changes: 16 additions & 3 deletions src/cli/cmd/apply/nixos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<String> {
Some(self.action.to_string())
}
}

// For available commands, see
Expand Down
13 changes: 0 additions & 13 deletions src/cli/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit ad8672c

Please sign in to comment.