From df692aa02d7653a7ff005873009d77511ccf5269 Mon Sep 17 00:00:00 2001 From: Collin Diekvoss Date: Wed, 4 Dec 2024 22:38:28 -0600 Subject: [PATCH] PR comments --- .github/workflows/build.yaml | 9 ++++++++- flake.nix | 14 -------------- src/clean.rs | 5 +---- src/commands.rs | 35 +++++++++++++++++++---------------- src/darwin.rs | 23 ++++++++++------------- 5 files changed, 38 insertions(+), 48 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index d7be688..a3123ae 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -40,5 +40,12 @@ jobs: - run: nix build -L --no-link name: Build - - run: nix run -L .#nh -- darwin switch --hostname nh_test --dry --no-nom --verbose . + - run: | + NH_DIR=$(pwd) + NH_FLAKE=/tmp/nh-darwin + mkdir -p $NH_FLAKE + cd $NH_FLAKE + nix flake init -t nix-darwin + cd $NH_DIR + nix run -L .#nh -- darwin switch --hostname simple --dry --no-nom --verbose $NH_FLAKE name: Test Switching to Nix Darwin Configuration diff --git a/flake.nix b/flake.nix index 822c22c..3423dc3 100644 --- a/flake.nix +++ b/flake.nix @@ -1,17 +1,12 @@ { inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05"; - nix-darwin = { - url = "github:LnL7/nix-darwin"; - inputs.nixpkgs.follows = "nixpkgs"; - }; }; outputs = { self, nixpkgs, - nix-darwin, }: let forAllSystems = @@ -39,14 +34,5 @@ }); 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 9dbdfaa..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(not(target_os = "macos"))] - 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 2636c68..9ba8436 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -66,22 +66,25 @@ impl Command { #[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"] - }, - ); + 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 { diff --git a/src/darwin.rs b/src/darwin.rs index c66abda..157929c 100644 --- a/src/darwin.rs +++ b/src/darwin.rs @@ -32,16 +32,7 @@ fn get_hostname(hostname: Option) -> Result { match &hostname { Some(h) => Ok(h.to_owned()), None => { - #[cfg(not(target_os = "macos"))] - { - Ok(hostname::get() - .context("Failed to get hostname")? - .to_str() - .unwrap() - .to_string()) - } - #[cfg(target_os = "macos")] - { + let hostname = if cfg!(target_os = "macos") { use system_configuration::{ core_foundation::{base::TCFType, string::CFString}, sys::dynamic_store_copy_specific::SCDynamicStoreCopyLocalHostName, @@ -52,9 +43,15 @@ fn get_hostname(hostname: Option) -> Result { bail!("Failed to get hostname"); } let name = unsafe { CFString::wrap_under_get_rule(ptr) }; - - Ok(name.to_string()) - } + name.to_string() + } else { + hostname::get() + .context("Failed to get hostname")? + .to_str() + .unwrap() + .to_string() + }; + Ok(hostname) } } }