Skip to content

Commit 9b45936

Browse files
committed
feat: 添加错误处理和通知功能到nixos-switch函数
1 parent 3af4210 commit 9b45936

1 file changed

Lines changed: 93 additions & 32 deletions

File tree

utils.nu

Lines changed: 93 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,111 @@ export def nixos-switch [
1515
] {
1616
print $"nixos-switch '($name)' in '($mode)' mode..."
1717
print (repeat-str "=" 50)
18+
let timestamp = (date now | format date "%Y-%m-%d %H:%M:%S")
1819
if "debug" == $mode {
1920
# show details via nix-output-monitor
2021
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nom build $\".#nixosConfigurations.($name).config.system.build.toplevel\" --show-trace --verbose"
2122
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild switch --sudo --flake $\".#($name)\" --show-trace --verbose --impure"
22-
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nom build $".#nixosConfigurations.($name).config.system.build.toplevel" --show-trace --verbose
23-
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild switch --sudo --flake $".#($name)" --show-trace --verbose --impure
23+
try {
24+
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nom build $".#nixosConfigurations.($name).config.system.build.toplevel" --show-trace --verbose
25+
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild switch --sudo --flake $".#($name)" --show-trace --verbose --impure
26+
} catch {|error|
27+
let msg = $"NixOS switch failed for '($name)': ($error.msg)"
28+
print $msg
29+
notify-send -u critical -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg
30+
return
31+
}
32+
let msg = $"NixOS switch completed successfully for '($name)'"
33+
print $msg
34+
notify-send -u normal -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg -w
2435
} else if "boot" == $mode {
25-
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild boot --sudo --flake $\".#($name)\" --impure"
26-
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild boot --sudo --flake $".#($name)" --impure
36+
try {
37+
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild boot --sudo --flake $\".#($name)\" --impure"
38+
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild boot --sudo --flake $".#($name)" --impure
39+
} catch {|error|
40+
let msg = $"NixOS boot failed for '($name)': ($error.msg)"
41+
print $msg
42+
notify-send -u critical -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg
43+
return
44+
}
45+
let msg = $"NixOS boot completed successfully for '($name)'"
46+
print $msg
47+
notify-send -u normal -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg -w
2748
} else if "switch" == $mode {
28-
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild switch --sudo --flake $\".#($name)\" --impure"
29-
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild switch --sudo --flake $".#($name)" --impure
49+
try {
50+
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild switch --sudo --flake $\".#($name)\" --impure"
51+
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild switch --sudo --flake $".#($name)" --impure
52+
} catch {|error|
53+
let msg = $"NixOS switch failed for '($name)': ($error.msg)"
54+
print $msg
55+
notify-send -u critical -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg
56+
return
57+
}
58+
let msg = $"NixOS switch completed successfully for '($name)'"
59+
print $msg
60+
notify-send -u normal -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg -w
3061
} else if "boot-notify" == $mode {
3162
let new_dir_name = $"result-(date now | format date "%Y-%m-%d_%H:%M:%S")"
32-
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $\".#($name)\" --impure"
33-
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $".#($name)" --impure
34-
mv result $new_dir_name
35-
let msg = "NixOS boot image built successfully. sudo password is required now"
36-
print $msg
37-
notify-send -u critical -a NIXOS_REBUILD -p 0 $msg -w
38-
sudo nix-env -p /nix/var/nix/profiles/system --set $"./($new_dir_name)"
39-
rm $new_dir_name
63+
try {
64+
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $\".#($name)\" --impure"
65+
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $".#($name)" --impure
66+
mv result $new_dir_name
67+
let msg = "NixOS boot image built successfully. sudo password is required now"
68+
print $msg
69+
notify-send -u critical -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg -w
70+
sudo nix-env -p /nix/var/nix/profiles/system --set $"./($new_dir_name)"
71+
rm $new_dir_name
72+
} catch {|error|
73+
let msg = $"NixOS boot build failed for '($name)': ($error.msg)"
74+
print $msg
75+
notify-send -u critical -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg
76+
if ($new_dir_name | path exists) {
77+
rm $new_dir_name
78+
}
79+
return
80+
}
4081
} else if "switch-notify" == $mode {
4182
let new_dir_name = $"result-(date now | format date "%Y-%m-%d_%H:%M:%S")"
42-
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $\".#($name)\" --impure"
43-
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $".#($name)" --impure
44-
mv result $new_dir_name
45-
let msg = "NixOS system configuration built successfully. sudo password is required now"
46-
print $msg
47-
notify-send -u critical -a NIXOS_REBUILD -p 0 $msg -w
48-
sudo nix-env -p /nix/var/nix/profiles/system --set $"./($new_dir_name)"
49-
sudo systemd-run -E LOCALE_ARCHIVE -E NIXOS_INSTALL_BOOTLOADER --collect --no-ask-password --pipe --quiet --service-type=exec --unit=nixos-rebuild-switch-to-configuration $"($new_dir_name)/bin/switch-to-configuration" switch
50-
rm $new_dir_name
83+
try {
84+
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $\".#($name)\" --impure"
85+
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $".#($name)" --impure
86+
mv result $new_dir_name
87+
let msg = "NixOS system configuration built successfully. sudo password is required now"
88+
print $msg
89+
notify-send -u critical -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg -w
90+
sudo nix-env -p /nix/var/nix/profiles/system --set $"./($new_dir_name)"
91+
sudo systemd-run -E LOCALE_ARCHIVE -E NIXOS_INSTALL_BOOTLOADER --collect --no-ask-password --pipe --quiet --service-type=exec --unit=nixos-rebuild-switch-to-configuration $"($new_dir_name)/bin/switch-to-configuration" switch
92+
rm $new_dir_name
93+
} catch {|error|
94+
let msg = $"NixOS switch build failed for '($name)': ($error.msg)"
95+
print $msg
96+
notify-send -u critical -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg
97+
if ($new_dir_name | path exists) {
98+
rm $new_dir_name
99+
}
100+
return
101+
}
51102
} else {
52103
let new_dir_name = $"result-(date now | format date "%Y-%m-%d_%H:%M:%S")"
53-
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $\".#($name)\" --impure"
54-
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $".#($name)" --impure
55-
mv result $new_dir_name
56-
let msg = "NixOS system configuration built successfully. sudo password is required now"
57-
print $msg
58-
notify-send -u critical -a NIXOS_REBUILD -p 0 $msg -w
59-
sudo nix-env -p /nix/var/nix/profiles/system --set $"./($new_dir_name)"
60-
sudo systemd-run -E LOCALE_ARCHIVE -E NIXOS_INSTALL_BOOTLOADER --collect --no-ask-password --pipe --quiet --service-type=exec --unit=nixos-rebuild-switch-to-configuration $"($new_dir_name)/bin/switch-to-configuration" switch
61-
rm $new_dir_name
104+
try {
105+
print $"NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $\".#($name)\" --impure"
106+
NIXPKGS_ALLOW_BROKEN=1 NIXPKGS_ALLOW_INSECURE=1 NIXPKGS_ALLOW_UNFREE=1 nixos-rebuild build --flake $".#($name)" --impure
107+
mv result $new_dir_name
108+
let msg = "NixOS system configuration built successfully. sudo password is required now"
109+
print $msg
110+
notify-send -u critical -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg -w
111+
sudo nix-env -p /nix/var/nix/profiles/system --set $"./($new_dir_name)"
112+
sudo systemd-run -E LOCALE_ARCHIVE -E NIXOS_INSTALL_BOOTLOADER --collect --no-ask-password --pipe --quiet --service-type=exec --unit=nixos-rebuild-switch-to-configuration $"($new_dir_name)/bin/switch-to-configuration" switch
113+
rm $new_dir_name
114+
} catch {|error|
115+
let msg = $"NixOS switch build failed for '($name)': ($error.msg)"
116+
print $msg
117+
notify-send -u critical -a "nixos-rebuild" -p $"NIXOS_REBUILD_($timestamp)" $msg
118+
if ($new_dir_name | path exists) {
119+
rm $new_dir_name
120+
}
121+
return
122+
}
62123
}
63124
}
64125

0 commit comments

Comments
 (0)