Skip to content

Commit 395d3c9

Browse files
committed
fix: avoid break normal command execution that expects a valid HOME directory.
1 parent d8654cb commit 395d3c9

File tree

1 file changed

+12
-36
lines changed

1 file changed

+12
-36
lines changed

src/commands.rs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl Command {
288288
}
289289

290290
// Only propagate HOME for non-elevated commands
291-
if self.elevate.is_none() && cfg!(not(target_os = "macos")) {
291+
if self.elevate.is_none() {
292292
if let Ok(home) = std::env::var("HOME") {
293293
self
294294
.env_vars
@@ -298,7 +298,7 @@ impl Command {
298298

299299
// INFO: Setting HOME to "" for macos
300300
// ref: https://github.com/NixOS/nix/blob/d5d7ca01b3dcf48f43819012c580cfb57cb08e47/src/libutil/unix/users.cc#L52
301-
if cfg!(target_os = "macos") {
301+
if self.elevate.is_some() && cfg!(target_os = "macos") {
302302
self
303303
.env_vars
304304
.insert("HOME".to_string(), EnvAction::Set("".to_string()));
@@ -489,13 +489,12 @@ impl Command {
489489
std_cmd.args(&sudo_parts[1..]);
490490
}
491491

492-
if let Some(ElevationStrategy::Force("sudo")) = cmd_builder.elevate.as_ref()
493-
{
492+
// check if using SUDO_ASKPASS
493+
if sudo_parts[1] == "-A" {
494494
if let Ok(askpass) = std::env::var("NH_SUDO_ASKPASS") {
495495
std_cmd.env("SUDO_ASKPASS", askpass);
496496
}
497497
}
498-
499498
Ok(std_cmd)
500499
}
501500

@@ -891,17 +890,9 @@ mod tests {
891890
let cmd = Command::new("test").with_required_env();
892891

893892
// Should preserve HOME and USER as Set actions
894-
if cfg!(target_os = "macos") {
895-
// macOS sets HOME to "" in Nix environment
896-
assert!(
897-
matches!(cmd.env_vars.get("HOME"), Some(EnvAction::Set(val)) if val.is_empty())
898-
);
899-
} else {
900-
// Other OSes should have the actual HOME value
901-
assert!(
902-
matches!(cmd.env_vars.get("HOME"), Some(EnvAction::Set(val)) if val == "/test/home")
903-
);
904-
}
893+
assert!(
894+
matches!(cmd.env_vars.get("HOME"), Some(EnvAction::Set(val)) if val == "/test/home")
895+
);
905896
assert!(
906897
matches!(cmd.env_vars.get("USER"), Some(EnvAction::Set(val)) if val == "testuser")
907898
);
@@ -936,15 +927,7 @@ mod tests {
936927
let cmd = Command::new("test").with_required_env();
937928

938929
// Should not have HOME or USER in env_vars if they're not set
939-
if cfg!(target_os = "macos") {
940-
// macOS sets HOME to "" in Nix environment
941-
assert!(
942-
matches!(cmd.env_vars.get("HOME"), Some(EnvAction::Set(val)) if val.is_empty())
943-
);
944-
} else {
945-
// Other OSes should not have HOME set
946-
assert!(!cmd.env_vars.contains_key("HOME"));
947-
}
930+
assert!(!cmd.env_vars.contains_key("HOME"));
948931
assert!(!cmd.env_vars.contains_key("USER"));
949932

950933
// Should preserve Nix-related variables if present
@@ -997,17 +980,10 @@ mod tests {
997980
.preserve_envs(["EXTRA_VAR"]);
998981

999982
// Should have HOME from with_nix_env
1000-
if cfg!(target_os = "macos") {
1001-
// macOS sets HOME to "" in Nix environment
1002-
assert!(
1003-
matches!(cmd.env_vars.get("HOME"), Some(EnvAction::Set(val)) if val.is_empty())
1004-
);
1005-
} else {
1006-
// Other OSes should have the actual HOME value
1007-
assert!(
1008-
matches!(cmd.env_vars.get("HOME"), Some(EnvAction::Set(val)) if val == "/test/home")
1009-
);
1010-
}
983+
assert!(
984+
matches!(cmd.env_vars.get("HOME"), Some(EnvAction::Set(val)) if val == "/test/home")
985+
);
986+
1011987
// Should have NH variables from with_nh_env
1012988
assert!(
1013989
matches!(cmd.env_vars.get("NH_TEST"), Some(EnvAction::Set(val)) if val == "nh_value")

0 commit comments

Comments
 (0)