Skip to content

Commit

Permalink
Add --out-link flag
Browse files Browse the repository at this point in the history
Closes #137
  • Loading branch information
viperML committed Aug 18, 2024
1 parent 24d7b24 commit 6878bc3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
24 changes: 15 additions & 9 deletions src/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ impl NHRunnable for HomeArgs {

impl HomeRebuildArgs {
fn rebuild(&self, action: &HomeSubcommand) -> Result<()> {
let out_dir = tempfile::Builder::new().prefix("nh-home-").tempdir()?;
let out_link = out_dir.path().join("result");
let out_link_str = out_link.to_str().unwrap();
debug!("out_dir: {:?}", out_dir);
debug!("out_link {:?}", out_link);
let out_path: Box<dyn crate::util::MaybeTempPath> = match self.common.out_link {
Some(ref p) => Box::new(p.clone()),
None => Box::new({
let dir = tempfile::Builder::new().prefix("nh-home").tempdir()?;
(dir.as_ref().join("result"), dir)
}),
};

debug!(?out_path);

let username = std::env::var("USER").expect("Couldn't get username");

Expand Down Expand Up @@ -91,7 +95,8 @@ impl HomeRebuildArgs {

commands::BuildCommandBuilder::default()
.flakeref(&flakeref)
.extra_args(["--out-link", out_link_str])
.extra_args(["--out-link"])
.extra_args([out_path.get_path()])
.extra_args(&self.extra_args)
.message("Building home configuration")
.nom(!self.common.no_nom)
Expand All @@ -115,7 +120,8 @@ impl HomeRebuildArgs {
if let Some(prev_gen) = prev_generation {
commands::CommandBuilder::default()
.args(self.common.diff_provider.split_ascii_whitespace())
.args([(prev_gen.to_str().unwrap()), out_link_str])
.args([(prev_gen.to_str().unwrap())])
.args([out_path.get_path()])
.message("Comparing changes")
.build()?
.exec()?;
Expand All @@ -140,13 +146,13 @@ impl HomeRebuildArgs {
}

commands::CommandBuilder::default()
.args([&format!("{}/activate", out_link_str)])
.args([out_path.get_path().join("activate")])
.message("Activating configuration")
.build()?
.exec()?;

// Drop the out dir *only* when we are finished
drop(out_dir);
drop(out_path);

Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ pub struct CommonRebuildArgs {
default_value = "nvd diff"
)]
pub diff_provider: String,

/// Path to save the result link. Defaults to using a temporary directory.
#[arg(long, short)]
pub out_link: Option<PathBuf>
}

#[derive(Args, Debug)]
Expand Down
30 changes: 19 additions & 11 deletions src/nixos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ impl OsRebuildArgs {
None => hostname::get().context("Failed to get hostname")?,
};

let out_dir = tempfile::Builder::new().prefix("nh-os-").tempdir()?;
let out_link = out_dir.path().join("result");
let out_link_str = out_link.to_str().unwrap();
debug!("out_dir: {:?}", out_dir);
debug!("out_link {:?}", out_link);
let out_path: Box<dyn crate::util::MaybeTempPath> = match self.common.out_link {
Some(ref p) => Box::new(p.clone()),
None => Box::new({
let dir = tempfile::Builder::new().prefix("nh-os").tempdir()?;
(dir.as_ref().join("result"), dir)
}),
};

debug!(?out_path);

let flake_output = format!(
"{}#nixosConfigurations.\"{:?}\".config.system.build.toplevel",
Expand Down Expand Up @@ -79,7 +83,8 @@ impl OsRebuildArgs {
commands::BuildCommandBuilder::default()
.flakeref(flake_output)
.message("Building NixOS configuration")
.extra_args(["--out-link", out_link_str])
.extra_args(["--out-link"])
.extra_args([out_path.get_path()])
.extra_args(&self.extra_args)
.nom(!self.common.no_nom)
.build()?
Expand All @@ -96,8 +101,8 @@ impl OsRebuildArgs {
debug!("target_specialisation: {target_specialisation:?}");

let target_profile = match &target_specialisation {
None => out_link.to_owned(),
Some(spec) => out_link.join("specialisation").join(spec),
None => out_path.get_path().to_owned(),
Some(spec) => out_path.get_path().join("specialisation").join(spec),
};

target_profile.try_exists().context("Doesn't exist")?;
Expand Down Expand Up @@ -143,13 +148,16 @@ impl OsRebuildArgs {
"--profile",
SYSTEM_PROFILE,
"--set",
out_link_str,
])
.args([out_path.get_path()])
.build()?
.exec()?;

// !! Use the base profile aka no spec-namespace
let switch_to_configuration = out_link.join("bin").join("switch-to-configuration");
let switch_to_configuration = out_path
.get_path()
.join("bin")
.join("switch-to-configuration");
let switch_to_configuration = switch_to_configuration.to_str().unwrap();

commands::CommandBuilder::default()
Expand All @@ -160,7 +168,7 @@ impl OsRebuildArgs {
}

// Drop the out dir *only* when we are finished
drop(out_dir);
drop(out_path);

Ok(())
}
Expand Down
18 changes: 18 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ extern crate semver;

use color_eyre::{eyre, Result};
use semver::Version;
use tempfile::TempDir;

use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;

Expand Down Expand Up @@ -56,3 +58,19 @@ pub fn get_nix_version() -> Result<String> {

Err(eyre::eyre!("Failed to extract version"))
}

pub trait MaybeTempPath: std::fmt::Debug {
fn get_path(&self) -> &Path;
}

impl MaybeTempPath for PathBuf {
fn get_path(&self) -> &Path {
self.as_ref()
}
}

impl MaybeTempPath for (PathBuf, TempDir) {
fn get_path(&self) -> &Path {
self.0.as_ref()
}
}

0 comments on commit 6878bc3

Please sign in to comment.