Files
nix-config-ryan4yin/utils.nu
T
Ryan Yin 5a4a1d0c20 fix(darwin): run the rollback as root
darwin-rebuild rejects switch, activate, rollback and check unless it runs as
root, so the recipe could only ever print an error.
2026-10-10 08:42:38 +08:00

76 lines
2.3 KiB
Nu

def repeat-str [s: string, n: int] {
(1..$n | each { $s } | str join)
}
# ================= NixOS related =========================
export def nixos-switch [
name: string
mode: string
verbosity: string
] {
if $mode not-in ["switch" "boot"] {
error make { msg: $"unsupported deployment mode '($mode)'; expected switch or boot" }
}
if $verbosity not-in ["normal" "debug"] {
error make { msg: $"unsupported verbosity '($verbosity)'; expected normal or debug" }
}
print $"nixos-switch '($name)' in '($mode)' mode with '($verbosity)' verbosity..."
print (repeat-str "=" 50)
if $verbosity == "debug" {
# show details via nix-output-monitor
nom build $".#nixosConfigurations.($name).config.system.build.toplevel" --accept-flake-config --show-trace --verbose
nixos-rebuild $mode --sudo --flake $".#($name)" --accept-flake-config --show-trace --verbose
} else {
nixos-rebuild $mode --sudo --flake $".#($name)" --accept-flake-config
}
}
# ====================== Misc =============================
export def make-editable [
path: string
] {
print (repeat-str "=" 50)
let tmpdir = (mktemp -d)
rsync -avz --copy-links $"($path)/" $tmpdir
rsync -avz --copy-links --chmod=D2755,F744 $"($tmpdir)/" $path
}
# ================= macOS related =========================
export def darwin-build [
name: string
verbosity: string
] {
print $"darwin-build '($name)' with '($verbosity)' verbosity..."
print (repeat-str "=" 50)
let target = $".#darwinConfigurations.($name).system"
if "debug" == $verbosity {
nom build $target --extra-experimental-features "nix-command flakes" --show-trace --verbose
} else {
nix build $target --extra-experimental-features "nix-command flakes"
}
}
export def darwin-switch [
name: string
verbosity: string
] {
print $"darwin-switch '($name)' with '($verbosity)' verbosity..."
print (repeat-str "=" 50)
if "debug" == $verbosity {
sudo -E ./result/sw/bin/darwin-rebuild switch --flake $".#($name)" --show-trace --verbose
} else {
sudo -E ./result/sw/bin/darwin-rebuild switch --flake $".#($name)"
}
}
export def darwin-rollback [] {
# darwin-rebuild refuses switch/activate/rollback/check unless it runs as root.
sudo -E ./result/sw/bin/darwin-rebuild --rollback
}