From 1e458a5e4ed225881a686167712afefeadcca3d3 Mon Sep 17 00:00:00 2001 From: Collin Diekvoss Date: Sun, 1 Dec 2024 16:34:44 -0600 Subject: [PATCH] darwin updates --- .github/workflows/build.yaml | 16 ++++++++++++++++ flake.lock | 21 +++++++++++++++++++++ flake.nix | 19 ++++++++++++++++++- src/clean.rs | 2 +- src/commands.rs | 22 +++++++++++++++++++++- src/darwin.rs | 9 ++++++--- 6 files changed, 83 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index f1af325..d7be688 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -26,3 +26,19 @@ 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: nix run -L .#nh -- darwin switch --hostname nh_test --dry --no-nom --verbose . + name: Test Switching to Nix Darwin Configuration diff --git a/flake.lock b/flake.lock index b4fa215..3c8d185 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,25 @@ { "nodes": { + "nix-darwin": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1733047432, + "narHash": "sha256-fQUKxgxAEHlL5bevRkdsQB7sSpAMhlvxf7Zw0KK8QIg=", + "owner": "LnL7", + "repo": "nix-darwin", + "rev": "e30a3622b606dffc622305b4bbe1cfe37e78fa40", + "type": "github" + }, + "original": { + "owner": "LnL7", + "repo": "nix-darwin", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1731386116, @@ -18,6 +38,7 @@ }, "root": { "inputs": { + "nix-darwin": "nix-darwin", "nixpkgs": "nixpkgs" } } diff --git a/flake.nix b/flake.nix index 2cb7946..822c22c 100644 --- a/flake.nix +++ b/flake.nix @@ -1,10 +1,18 @@ { inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05"; + nix-darwin = { + url = "github:LnL7/nix-darwin"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; outputs = - { self, nixpkgs }: + { + self, + nixpkgs, + nix-darwin, + }: let forAllSystems = function: @@ -31,5 +39,14 @@ }); formatter = forAllSystems (pkgs: pkgs.nixfmt-rfc-style); + + darwinConfigurations.nh_test = nix-darwin.lib.darwinSystem { + modules = [ + { + nixpkgs.hostPlatform = "aarch64-darwin"; + system.stateVersion = 5; + } + ]; + }; }; } diff --git a/src/clean.rs b/src/clean.rs index 163bfeb..9dbdfaa 100644 --- a/src/clean.rs +++ b/src/clean.rs @@ -59,7 +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")] + #[cfg(not(target_os = "macos"))] let uid_min = 1000; #[cfg(target_os = "macos")] let uid_min = 501; diff --git a/src/commands.rs b/src/commands.rs index b1f9357..2636c68 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) + #[cfg(not(target_os = "macos"))] + let cmd = Exec::cmd("sudo"); + + // Check for if sudo has the preserve-env flag + #[cfg(target_os = "macos")] + let cmd = 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"] + }, + ); + + 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..e6dab26 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()); @@ -133,20 +133,23 @@ impl DarwinRebuildArgs { Command::new("nix") .args(["build", "--profile", SYSTEM_PROFILE]) .arg(out_path.get_path()) - .elevate(true) + .elevate(!self.common.dry) + .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"); Command::new(switch_to_configuration) - .elevate(true) + .elevate(!self.common.dry) .message("Activating configuration") + .dry(self.common.dry) .run()?; }