Skip to content

Commit

Permalink
Merge pull request #75 from NotAShelf/master
Browse files Browse the repository at this point in the history
modify update arg based on Nix version
  • Loading branch information
viperML authored Mar 13, 2024
2 parents 4e13dc0 + 9f4529a commit 393cffa
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 10 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nh"
version = "3.5.6"
version = "3.5.7"
edition = "2021"
license = "EUPL-1.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down Expand Up @@ -35,6 +35,7 @@ once_cell = "1.18.0"
owo-colors = "4.0.0"
regex = "1.8.4"
reqwest = { version = "0.11.23", features = ["rustls-tls", "blocking", "json"], default-features = false }
semver = "1.0.22"
serde = { version = "1.0.166", features = [
"derive",
] }
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion src/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::*;
use crate::{
interface::NHRunnable,
interface::{FlakeRef, HomeArgs, HomeRebuildArgs, HomeSubcommand},
util::{compare_semver, get_nix_version},
};

#[derive(Error, Debug)]
Expand Down Expand Up @@ -59,8 +60,28 @@ impl HomeRebuildArgs {
);

if self.common.update {
// Get the Nix version
let nix_version = get_nix_version().unwrap_or_else(|_| {
panic!("Failed to get Nix version. Custom Nix fork?");
});

// Default interface for updating flake inputs
let mut update_args = vec!["nix", "flake", "update"];

// If user is on Nix 2.19.0 or above, --flake must be passed
if let Ok(ordering) = compare_semver(&nix_version, "2.19.0") {
if ordering == std::cmp::Ordering::Greater {
update_args.push("--flake");
}
}

update_args.push(&self.common.flakeref);

debug!("nix_version: {:?}", nix_version);
debug!("update_args: {:?}", update_args);

commands::CommandBuilder::default()
.args(["nix", "flake", "update", "--flake", &self.common.flakeref])
.args(&update_args)
.message("Updating flake")
.build()?
.exec()?;
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod interface;
mod logging;
mod nixos;
mod search;
mod util;

use crate::interface::NHParser;
use crate::interface::NHRunnable;
Expand Down
26 changes: 22 additions & 4 deletions src/nixos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tracing::{debug, info};
use crate::interface::NHRunnable;
use crate::interface::OsRebuildType::{self, Boot, Switch, Test};
use crate::interface::{self, OsRebuildArgs};
use crate::util::{compare_semver, get_nix_version};
use crate::*;

const SYSTEM_PROFILE: &str = "/nix/var/nix/profiles/system";
Expand Down Expand Up @@ -48,11 +49,28 @@ impl OsRebuildArgs {
);

if self.common.update {
// Get the Nix version
let nix_version = get_nix_version().unwrap_or_else(|_| {
panic!("Failed to get Nix version. Custom Nix fork?");
});

// Default interface for updating flake inputs
let mut update_args = vec!["nix", "flake", "update"];

// If user is on Nix 2.19.0 or above, --flake must be passed
if let Ok(ordering) = compare_semver(&nix_version, "2.19.0") {
if ordering == std::cmp::Ordering::Greater {
update_args.push("--flake");
}
}

update_args.push(&self.common.flakeref);

debug!("nix_version: {:?}", nix_version);
debug!("update_args: {:?}", update_args);

commands::CommandBuilder::default()
// FIXME: if user is running an older version of Nix (i.e pre-`nix flake lock` change)
// the below command will fail. maybe check for Nix version and decide on the
// command?
.args(["nix", "flake", "update", "--flake", &self.common.flakeref])
.args(&update_args)
.message("Updating flake")
.build()?
.exec()?;
Expand Down
58 changes: 58 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
extern crate semver;

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

use std::process::Command;
use std::str;

/// Compares two semantic versions and returns their order.
///
/// This function takes two version strings, parses them into `semver::Version` objects, and compares them.
/// It returns an `Ordering` indicating whether the current version is less than, equal to, or
/// greater than the target version.
///
/// # Arguments
///
/// * `current` - A string slice representing the current version.
/// * `target` - A string slice representing the target version to compare against.
///
/// # Returns
///
/// * `Result<std::cmp::Ordering>` - The comparison result.
pub fn compare_semver(current: &str, target: &str) -> Result<std::cmp::Ordering> {
let current = Version::parse(current)?;
let target = Version::parse(target)?;

Ok(current.cmp(&target))
}

/// Retrieves the installed Nix version as a string.
///
/// This function executes the `nix --version` command, parses the output to extract the version string,
/// and returns it. If the version string cannot be found or parsed, it returns an error.
///
/// # Returns
///
/// * `Result<String>` - The Nix version string or an error if the version cannot be retrieved.
pub fn get_nix_version() -> Result<String> {
let output = Command::new("nix").arg("--version").output()?;

let output_str = str::from_utf8(&output.stdout)?;
let version_str = output_str
.lines()
.next()
.ok_or_else(|| eyre::eyre!("No version string found"))?;

// Extract the version substring using a regular expression
let re = regex::Regex::new(r"\d+\.\d+\.\d+")?;
if let Some(captures) = re.captures(version_str) {
let version = captures
.get(0)
.ok_or_else(|| eyre::eyre!("No version match found"))?
.as_str();
return Ok(version.to_string());
}

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

0 comments on commit 393cffa

Please sign in to comment.