From 8d552c04c47edce8ab792bd0a5af216e1d2d67a9 Mon Sep 17 00:00:00 2001 From: Collin Diekvoss Date: Thu, 5 Dec 2024 01:19:55 -0600 Subject: [PATCH] rebase --- .github/workflows/build.yaml | 22 ++++++++++++++++++++++ src/clean.rs | 5 +---- src/commands.rs | 22 +++++++++++++++++++++- src/darwin.rs | 11 +++++------ 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index f1af325..d5890f4 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -26,3 +26,25 @@ jobs: ./fix.sh git diff-index --quiet HEAD name: Check formatting + + Test_Darwin: + runs-on: macos-latest + + steps: + - uses: cachix/install-nix-action@master + with: + github_access_token: ${{ secrets.GITHUB_TOKEN }} + + - uses: actions/checkout@v4 + + - run: nix build -L --no-link + name: Build + + - run: | + mkdir flake + cd flake + nix flake init -t nix-darwin + git add flake.nix + cd .. + nix run .#nh -- darwin switch --hostname simple --dry --no-nom --verbose ./flake + name: Test Switching to Nix Darwin Configuration diff --git a/src/clean.rs b/src/clean.rs index 163bfeb..0133601 100644 --- a/src/clean.rs +++ b/src/clean.rs @@ -59,10 +59,7 @@ impl interface::CleanMode { // Most unix systems start regular users at uid 1000+, but macos is special at 501+ // https://en.wikipedia.org/wiki/User_identifier - #[cfg(target_os = "linux")] - let uid_min = 1000; - #[cfg(target_os = "macos")] - let uid_min = 501; + let uid_min = if cfg!(target_os = "macos") { 501 } else { 1000 }; let uid_max = uid_min + 100; debug!("Scanning XDG profiles for users 0, ${uid_min}-${uid_max}"); for user in unsafe { uzers::all_users() } { diff --git a/src/commands.rs b/src/commands.rs index b1f9357..4297d98 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -63,7 +63,27 @@ impl Command { pub fn run(&self) -> Result<()> { let cmd = if self.elevate { - Exec::cmd("sudo").arg(&self.command).args(&self.args) + let cmd = if cfg!(target_os = "macos") { + // Check for if sudo has the preserve-env flag + Exec::cmd("sudo").args( + if Exec::cmd("sudo") + .args(&["--help"]) + .stderr(Redirection::None) + .stdout(Redirection::Pipe) + .capture()? + .stdout_str() + .contains("--preserve-env") + { + &["--set-home", "--preserve-env=PATH", "env"] + } else { + &["--set-home"] + }, + ) + } else { + Exec::cmd("sudo") + }; + + cmd.arg(&self.command).args(&self.args) } else { Exec::cmd(&self.command).args(&self.args) } diff --git a/src/darwin.rs b/src/darwin.rs index 99252f8..c66abda 100644 --- a/src/darwin.rs +++ b/src/darwin.rs @@ -88,7 +88,7 @@ impl DarwinRebuildArgs { ref mut attribute, .. } = installable { - // If user explicitely selects some other attribute, don't push darwinConfigurations + // If user explicitly selects some other attribute, don't push darwinConfigurations if attribute.is_empty() { attribute.push(String::from("darwinConfigurations")); attribute.push(hostname.clone()); @@ -116,11 +116,7 @@ impl DarwinRebuildArgs { .message("Comparing changes") .run()?; - if self.common.dry || matches!(variant, Build) { - return Ok(()); - } - - if self.common.ask { + if self.common.ask && !self.common.dry && !matches!(variant, Build) { info!("Apply the config?"); let confirmation = dialoguer::Confirm::new().default(false).interact()?; @@ -134,12 +130,14 @@ impl DarwinRebuildArgs { .args(["build", "--profile", SYSTEM_PROFILE]) .arg(out_path.get_path()) .elevate(true) + .dry(self.common.dry) .run()?; let switch_to_configuration = out_path.get_path().join("activate-user"); Command::new(switch_to_configuration) .message("Activating configuration for user") + .dry(self.common.dry) .run()?; let switch_to_configuration = out_path.get_path().join("activate"); @@ -147,6 +145,7 @@ impl DarwinRebuildArgs { Command::new(switch_to_configuration) .elevate(true) .message("Activating configuration") + .dry(self.common.dry) .run()?; }