Skip to content

Commit bd93a0c

Browse files
committed
check: ignore environment checks if NH_NO_CHECKS is set
1 parent 2b5a5ac commit bd93a0c

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

src/check.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ use crate::util;
1313
///
1414
/// * `Result<()>` - Ok if version requirements are met, error otherwise
1515
pub fn check_nix_version() -> Result<()> {
16+
if env::var("NH_NO_CHECKS").is_ok() {
17+
return Ok(());
18+
}
19+
1620
let version = util::get_nix_version()?;
1721
let is_lix_binary = util::is_lix()?;
1822

@@ -63,6 +67,10 @@ pub fn check_nix_version() -> Result<()> {
6367
///
6468
/// * `Result<()>` - Ok if all required features are enabled, error otherwise
6569
pub fn check_nix_features() -> Result<()> {
70+
if env::var("NH_NO_CHECKS").is_ok() {
71+
return Ok(());
72+
}
73+
6674
let mut required_features = vec!["nix-command", "flakes"];
6775

6876
// Lix still uses repl-flake, which is removed in the latest version of Nix.
@@ -115,6 +123,10 @@ pub fn setup_environment() -> Result<bool> {
115123
///
116124
/// * `Result<()>` - Ok if all checks pass, error otherwise
117125
pub fn verify_nix_environment() -> Result<()> {
126+
if env::var("NH_NO_CHECKS").is_ok() {
127+
return Ok(());
128+
}
129+
118130
check_nix_version()?;
119131
check_nix_features()?;
120132
Ok(())
@@ -134,6 +146,7 @@ fn cleanup_env_vars() {
134146
env::remove_var("NH_OS_FLAKE");
135147
env::remove_var("NH_HOME_FLAKE");
136148
env::remove_var("NH_DARWIN_FLAKE");
149+
env::remove_var("NH_NO_CHECKS");
137150
}
138151

139152
#[test]
@@ -276,3 +289,23 @@ fn test_setup_environment_flake_set_no_nh_flake_nh_darwin_flake_set() -> Result<
276289
cleanup_env_vars(); // Clean up after test
277290
Ok(())
278291
}
292+
293+
#[test]
294+
fn test_checks_skip_when_no_checks_set() -> Result<()> {
295+
let _lock = ENV_LOCK.lock().unwrap();
296+
cleanup_env_vars();
297+
298+
env::set_var("NH_NO_CHECKS", "1");
299+
300+
// These should succeed even with invalid environment
301+
let version_check = check_nix_version();
302+
let features_check = check_nix_features();
303+
let verify_check = verify_nix_environment();
304+
305+
assert!(version_check.is_ok(), "Version check should be skipped");
306+
assert!(features_check.is_ok(), "Features check should be skipped");
307+
assert!(verify_check.is_ok(), "Verify check should be skipped");
308+
309+
cleanup_env_vars();
310+
Ok(())
311+
}

src/util.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::collections::HashSet;
22
use std::path::{Path, PathBuf};
3-
use std::process::Command;
43
use std::str;
54

65
use color_eyre::{eyre, Result};
76
use tempfile::TempDir;
87

8+
use crate::commands::Command;
9+
910
/// Retrieves the installed Nix version as a string.
1011
///
1112
/// This function executes the `nix --version` command, parses the output to extract the version string,
@@ -15,10 +16,12 @@ use tempfile::TempDir;
1516
///
1617
/// * `Result<String>` - The Nix version string or an error if the version cannot be retrieved.
1718
pub fn get_nix_version() -> Result<String> {
18-
let output = Command::new("nix").arg("--version").output()?;
19+
let output = Command::new("nix")
20+
.arg("--version")
21+
.run_capture()?
22+
.ok_or_else(|| eyre::eyre!("No output from command"))?;
1923

20-
let output_str = str::from_utf8(&output.stdout)?;
21-
let version_str = output_str
24+
let version_str = output
2225
.lines()
2326
.next()
2427
.ok_or_else(|| eyre::eyre!("No version string found"))?;
@@ -42,10 +45,12 @@ pub fn get_nix_version() -> Result<String> {
4245
///
4346
/// * `Result<bool>` - True if the binary is Lix, false if it's standard Nix
4447
pub fn is_lix() -> Result<bool> {
45-
let output = Command::new("nix").arg("--version").output()?;
46-
let output_str = str::from_utf8(&output.stdout)?.to_lowercase();
48+
let output = Command::new("nix")
49+
.arg("--version")
50+
.run_capture()?
51+
.ok_or_else(|| eyre::eyre!("No output from command"))?;
4752

48-
Ok(output_str.contains("lix"))
53+
Ok(output.to_lowercase().contains("lix"))
4954
}
5055

5156
/// Represents an object that may be a temporary path
@@ -109,16 +114,14 @@ pub fn get_hostname() -> Result<String> {
109114
pub fn get_nix_experimental_features() -> Result<HashSet<String>> {
110115
let output = Command::new("nix")
111116
.args(["config", "show", "experimental-features"])
112-
.output()?;
117+
.run_capture()?;
113118

114-
if !output.status.success() {
115-
return Err(eyre::eyre!(
116-
"Failed to get experimental features: {}",
117-
String::from_utf8_lossy(&output.stderr)
118-
));
119-
}
119+
// If running with dry=true, output might be None
120+
let output_str = match output {
121+
Some(output) => output,
122+
None => return Ok(HashSet::new()),
123+
};
120124

121-
let output_str = str::from_utf8(&output.stdout)?;
122125
let enabled_features: HashSet<String> =
123126
output_str.split_whitespace().map(String::from).collect();
124127

0 commit comments

Comments
 (0)