Skip to content

Commit

Permalink
darwin updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ToyVo committed Dec 5, 2024
1 parent 24293d6 commit c71c396
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 22 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
};

outputs =
{ self, nixpkgs }:
{
self,
nixpkgs,
}:
let
forAllSystems =
function:
Expand Down
5 changes: 1 addition & 4 deletions src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() } {
Expand Down
22 changes: 21 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
29 changes: 13 additions & 16 deletions src/darwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,27 @@ fn get_hostname(hostname: Option<String>) -> Result<String> {
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")]
{
use system_configuration::{
core_foundation::{base::TCFType, string::CFString},
sys::dynamic_store_copy_specific::SCDynamicStoreCopyLocalHostName,
};

let ptr = unsafe { SCDynamicStoreCopyLocalHostName(std::ptr::null()) };
if ptr.is_null() {
bail!("Failed to get hostname");
}
let name = unsafe { CFString::wrap_under_get_rule(ptr) };

Ok(name.to_string())
}
#[cfg(not(target_os = "macos"))]
{
return Ok(hostname::get()
.context("Failed to get hostname")?
.to_str()
.unwrap()
.to_string());
};
}
}
}
Expand Down Expand Up @@ -88,7 +86,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());
Expand Down Expand Up @@ -116,11 +114,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()?;

Expand All @@ -134,19 +128,22 @@ 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");

Command::new(switch_to_configuration)
.elevate(true)
.message("Activating configuration")
.dry(self.common.dry)
.run()?;
}

Expand Down

0 comments on commit c71c396

Please sign in to comment.