-
Notifications
You must be signed in to change notification settings - Fork 1
/
nix-switch.sh
executable file
·51 lines (41 loc) · 1 KB
/
nix-switch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
dir=$(realpath $(dirname "$0"))
if grep '^NAME' /etc/os-release | grep NixOS >/dev/null; then
dir=$dir/machines/$(hostname)
fi
mode=$1
function switch_home() {
conf="$dir/home.nix"
if [ ! -f "$conf" ]; then
echo "error: $conf does not exist."
exit 1
fi
echo "switching home configuration from..."
echo "using: $conf"
exec home-manager switch -f "$conf" -b bak "$@"
}
function switch_system() {
if [[ $(whoami) != "root" ]]; then
echo "error: use sudo to switch system configuration"
exit 1
fi
conf="$dir/configuration.nix"
if [ ! -f "$conf" ]; then
echo "error: $conf does not exist."
exit 1
fi
echo "switching system configuration..."
echo "using: $conf"
exec env NIXOS_CONFIG="$conf" nixos-rebuild switch "$@"
}
case "$mode" in
"home")
switch_home "${@:2}"
;;
"system")
switch_system "${@:2}"
;;
*)
echo "error: first parameter should be home or system"
;;
esac