-
Notifications
You must be signed in to change notification settings - Fork 2
/
naive
executable file
·124 lines (110 loc) · 2.94 KB
/
naive
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/run/current-system/sw/bin/bash
set -e
check_root() {
if [[ $(id -u) -ne 0 ]]; then
echo "Please run as root." && exit 1
fi
}
check_os() {
case "$OSTYPE" in
darwin*) rebuild='darwin-rebuild' ;;
linux*) rebuild='nixos-rebuild --use-remote-sudo --upgrade' ;;
*) echo "Unknown OS type." && exit 1 ;;
esac
}
check_machine() {
case "$OSTYPE" in
darwin*) machine='macbook' ;;
linux*)
if [[ -f /sys/firmware/devicetree/base/model ]]; then
model=$(tr -d '\0' </sys/firmware/devicetree/base/model)
elif [[ -f /sys/devices/virtual/dmi/id/board_name ]]; then
model=$(tr -d '\0' </sys/devices/virtual/dmi/id/board_name)
elif [[ -f /sys/devices/virtual/dmi/id/sys_vendor ]]; then
model=$(tr -d '\0' </sys/devices/virtual/dmi/id/sys_vendor)
else
echo "Unknown machine." && exit 1
fi
case "$model" in
P9D-M*) machine='kimsufi' ;;
Microsoft*) machine='hyperv' ;;
Parallels*) machine='parallels' ;;
Pine64*) machine='pinephone' ;;
Raspberry*) machine='raspberrypi' ;;
*) echo "Unknown machine." && exit 1 ;;
esac
;;
*) echo "Unknown machine." && exit 1 ;;
esac
}
format_and_mount() {
umount /dev/sda* || true
umount -R /mnt || true
parted -s /dev/sda mklabel gpt
parted -s /dev/sda mkpart ESP fat32 1MiB 512MiB
parted -s /dev/sda set 1 esp on
parted -s /dev/sda mkpart primary btrfs 512MiB 100%
yes | mkfs.fat -F 32 -n boot /dev/sda1
yes | mkfs.btrfs -L nixos /dev/sda2
sleep 1
mkdir -p /mnt
mount /dev/sda2 /mnt
mkdir -p /mnt/boot
mount /dev/sda1 /mnt/boot
}
install() {
nix-shell -p nixUnstable --run \
"nixos-install --no-root-passwd --root /mnt --flake .#$1"
}
netinstall() {
nix-shell -p nixUnstable --run \
"nixos-install --no-root-passwd --root /mnt --flake github:wegank/nixos-config#$1"
}
clean() {
nix store gc
}
update() {
nix flake update
}
upgrade() {
$rebuild switch --flake ".#$1" --no-write-lock-file
}
full_upgrade() {
$rebuild switch --flake ".#$1" --recreate-lock-file
}
trap 'exit 1' ERR
if [[ $1 == "clean" ]] && [[ $2 == "" ]]; then
clean
exit
elif [[ $1 == "update" ]] && [[ $2 == "" ]]; then
update
exit
elif [[ $1 == "upgrade" ]] && [[ $3 == "" ]]; then
check_os
check_machine
upgrade "${2:-$machine}"
exit
elif [[ $1 == "full-upgrade" ]] && [[ $3 == "" ]]; then
check_os
check_machine
full_upgrade "${2:-$machine}"
exit
elif [[ $1 == "install" ]] && [[ $3 == "" ]]; then
check_root
check_machine
format_and_mount
sleep 1
install "${2:-$machine}"
umount -R /mnt || true
exit
elif [[ $1 == "netinstall" ]] && [[ $3 == "" ]]; then
check_root
check_machine
format_and_mount
sleep 1
netinstall "${2:-$machine}"
umount -R /mnt || true
exit
fi
echo "Unrecognized command."
exit 1