From 3fbc5eeb11e6b9abf66e38ab32d85a2edf9f76e1 Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Wed, 17 Aug 2022 11:56:30 -0600 Subject: [PATCH] repl: Add arguments depending on Nix version --- src/command/repl.rs | 23 +++++++++++++++++------ src/nix/info.rs | 10 +++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/command/repl.rs b/src/command/repl.rs index b2c5436..a642367 100644 --- a/src/command/repl.rs +++ b/src/command/repl.rs @@ -5,6 +5,7 @@ use tempfile::Builder as TempFileBuilder; use tokio::process::Command; use crate::error::ColmenaError; +use crate::nix::info::NixCheck; use crate::util; pub fn subcommand() -> ClapCommand<'static> { @@ -20,6 +21,9 @@ attribute set."#, } pub async fn run(_global_args: &ArgMatches, local_args: &ArgMatches) -> Result<(), ColmenaError> { + let nix_check = NixCheck::detect().await; + let nix_version = nix_check.version().expect("Could not detect Nix version"); + let hive = util::hive_from_args(local_args).await?; let expr = hive.get_repl_expression(); @@ -31,14 +35,21 @@ pub async fn run(_global_args: &ArgMatches, local_args: &ArgMatches) -> Result<( expr_file.write_all(expr.as_bytes())?; - let status = Command::new("nix") - .arg("repl") + let mut repl_cmd = Command::new("nix"); + + repl_cmd.arg("repl"); + + if nix_version.at_least(2, 4) { // `nix repl` is expected to be marked as experimental: // - .args(&["--experimental-features", "nix-command flakes"]) - .arg(expr_file.path()) - .status() - .await?; + repl_cmd.args(&["--experimental-features", "nix-command flakes"]); + } + + if nix_version.at_least(2, 10) { + repl_cmd.arg("--file"); + } + + let status = repl_cmd.arg(expr_file.path()).status().await?; if !status.success() { return Err(status.into()); diff --git a/src/nix/info.rs b/src/nix/info.rs index 906e658..16f4ccd 100644 --- a/src/nix/info.rs +++ b/src/nix/info.rs @@ -7,7 +7,7 @@ use tokio::process::Command; use super::{ColmenaError, ColmenaResult}; -struct NixVersion { +pub struct NixVersion { major: usize, minor: usize, string: String, @@ -37,6 +37,10 @@ impl NixVersion { fn has_flakes(&self) -> bool { self.major > 2 || (self.major == 2 && self.minor >= 4) } + + pub fn at_least(&self, major: usize, minor: usize) -> bool { + self.major >= major && self.minor >= minor + } } impl fmt::Display for NixVersion { @@ -147,4 +151,8 @@ impl NixCheck { pub fn flakes_supported(&self) -> bool { self.flakes_supported } + + pub fn version(&self) -> Option<&NixVersion> { + self.version.as_ref() + } }