VB6RBT2WV6GJI4JH63FLILCSYYCAYT4UZAMHYTMZGQW3PLEKUMCAC CFWMBZPLUD5VRYC3HKBRANMLMXRRPIL6HZJR3AK4UUUZUDXSYPXAC C6RBGGMQXBPZ5VUFD3NL6COMHPI4IUQZXW77Z6JWEARLRBJVVESQC AX75Q7H4YF5EMPIZA5WYHIIEXN633EI3ZF7LF2HCLBT6OYJC53BAC 3CJ46F2EWV5MKVQODGD2O4UX6QKMUW6UO7HUSOGNG7WE3MRFL2QQC I6S2HUTI2OOWFXGSGQFYE5LMYYOLR2GQNAWW2X7DHF2XD76UCPVAC 5BHIHEZYYNTL3XVMDYRYUGB5RCCV6D7BDNEDS6PA4LIYVCZKDOJAC # nu-lint-ignore: print_and_return_datadef print-notify [message: string]: nothing -> nothing {print $"(ansi purple)[Rebuilder](ansi rst) ($message)\r"
def print-notify [message: string, --error (-e)] {if $error {print $"(ansi red)[Rebuilder](ansi rst) ($message)"} else {print $"(ansi purple)[Rebuilder](ansi rst) ($message)"}
def remote-build [target: string--quiet]: nothing -> record {print-notify $"Attempting to start remote build process on ($target)."if not $quiet { print-notify $"Removing old configuration files on ($target)." }let result = ssh -qtt $"jam@($target)" "rm --recursive --force nixos" | completeif not $quiet { print-notify $"Copying new configuration files to ($target)." }let result = jj file list | rsync-files --files-from - ./ $"jam@($target):nixos" | complete
if not $quiet { print-notify $"Starting rebuild on ($target)." }let result = ssh -qtt $"jam@($target)" ./nixos/rebuild.nu | complete$result}
# Rebuild the current or a remote NixOS/nix-darwin host@example "Rebuild the current host" rebuild@example "Rebuild a remote host" { rebuild plum }@example "Rebuild all hosts sequentially" { rebuild all }def --wrapped main [target: string = "" # The host to build (defaults to current)--help (-h) # Show this help message...rest: string # Extra arguments to pass to nh]: nothing -> nothing {if $help { help main; exit 0 }
def build [cmd: string...args: string--all]: nothing -> record {let nh = if (which nh | is-not-empty) {[ nh ]} else {print-notify "Command 'nh' not found, falling back to 'nix run nixpkgs#nh'."[ nix run nixpkgs#nh -- ]}print-notify $"Rebuilding (sys host | get hostname)."if $all {try { sudo ...$nh $cmd ...$args | complete } catch { false }} else {try { sudo ...$nh $cmd ...$args; true } catch { false }}}# nu-lint-ignore: max_function_body_lengthdef start-progress-bar []: nothing -> int { # nu-lint-ignore: print_and_return_datajob spawn {sleep 100ms # Give time to spawn before starting animation.const WIDTH = 10mut s = 1.0mut m = 0mut pos = 0mut dir = 1while $env.REBUILD_IN_PROGRESS {$s += 0.1$pos += $dirif $pos >= $WIDTH or $pos <= 0 { $dir *= -1 }let l = ('' | fill --width $pos --character ' ')let r = ('' | fill --width ($WIDTH - $pos) --character ' ')if $s == 60.0 { $m += 1 }let s = $s | into intlet msg = $"(ansi p)($l)█($r)(ansi rst) Elapsed: ($s)s\r"print --no-newline $msgjob send 0sleep 100ms}}}def --wrapped main [ # nu-lint-ignore: max_function_body_length--remote: string = "" # Build a remote host--all # Attempt to rebuild all hosts...rest: string # Extra arguments to pass to nh]: nothing -> nothing {
let remote = $remote | str trim | str downcaselet is_remote = $remote | is-not-emptyconst HOSTS = [blackwelldatekiwilimepearplumsloeyuzu]let target = if $is_remote {if $remote == $hostname {print-notify "Error: Attempting to build the current systems configuration as a remote system."exit 1}$remote} else { $hostname }
let remote = ($target | is-not-empty) and ($target != $hostname)
if $all {print-notify $"Rebuilding all hosts in parallel: ($HOSTS)."print-notify "Results will be collected and shown upon completion."print-notify "This will take some time."
let result = if $remote {print-notify $"Attempting to start remote build process on ($target)."try {print-notify $"Removing old configuration files on ($target)."ssh -o ConnectTimeout=10 -tt $"jam@($target)" "rm --recursive --force nixos"
let results = $HOSTS | par-each --keep-order {|h|let remote = $h != $hostnameif $remote {let result = remote-build --quiet $h{host: $h, success: ($result.exit_code == 0), stderr: $result.stderr}} else {let result = build $config.cmd ...$nh_args --all{host: $h, success: ($result.exit_code == 0), stderr: $result.stderr}}
true} catch {|e|print-notify --error $"Something went wrong: ($e.msg)"print-notify --error "See above for more information."false}} else {print-notify $"Rebuilding (sys host | get hostname)."let nh = if (which nh | is-not-empty) {[ nh ]} else {print-notify "Command 'nh' not found, falling back to 'nix run nixpkgs#nh'."[ nix run nixpkgs#nh -- ]
print-notify "All builds complete."for r in $results {if $r.success {print-notify $"✓ ($r.host)"} else {print-notify $"✗ ($r.host) failed (if $r.stderr != null { $"\(($r.stderr)\)" })"}
if not $remote {if $result {print-notify $"Rebuild for ($target) succeeded."} else {print-notify $"Rebuild for ($target) failed."