-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.sh
323 lines (273 loc) · 8.11 KB
/
installer.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/sh
SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)"
OSTYPE="$(uname -s)"
DRY_RUN=0
INTERACTIVE=0
LAST_DIR=""
trap '[ "$PWD" = "/tmp" ] && popd; trap - EXIT; exit' EXIT
command -v pushd >/dev/null 2>&1 || {
pushd() {
if [ -z "$LAST_DIR" ]; then
LAST_DIR="$PWD"
else
# we assume newlines won't appear in paths
LAST_DIR="$PWD
$LAST_DIR"
fi
cd "$1" || exit 1
}
popd() {
if [ -z "$LAST_DIR" ]; then
echo "directory stack empty"
exit 1
else
cd "${LAST_DIR%%
*}" || exit 1
LAST_DIR="${LAST_DIR#*
}"
fi
}
}
prompt() {
printf '\e[1;34mContinue\e[0m [y/N]: '
cont=''
read -r cont
[ "$cont" = "y" ]
}
execute() {
printf '\e[37m%s\e[0m\n' "COMMAND: $*"
if [ $INTERACTIVE -eq 1 ]; then
prompt || {
echo "Command execution rejected. Quitting..."
exit 1
}
fi
if [ $DRY_RUN -eq 1 ]; then
return 0
fi
"$@"
}
islinux() {
[ "$OSTYPE" = "Linux" ]
}
isdarwin() {
[ "$OSTYPE" = "Darwin" ]
}
iswsl() {
islinux && [ -f /proc/version ] && grep ".*[Mm]icrosoft.*" /proc/version >/dev/null
}
isnixos() {
islinux && grep NixOS /etc/os-release >/dev/null
}
nixinstalled() {
[ -d /nix ]
}
BASIC_DARWIN_CONFIG='{ config, pkgs, ... }:\n\n{ imports = [ ./simple-darwin.nix ]; }\n'
install_nixdarwin() {
echo "Installing nix-darwin"
prompt || {
printf '%s\n' "Skipping nix-darwin install..."
return 0
}
execute eval "printf '%b\n' '$BASIC_DARWIN_CONFIG' > '$HOME/.nixpkgs/darwin-configuration.nix'"
execute sudo mv /etc/nix/nix.conf /etc/nix/nix.conf.old
# install nix-darwin
execute nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer
execute ./result/bin/darwin-installer
printf '%s\n' "Remember to open a new shell, edit ~/.nixpkgs/darwin-configuration.nix and run darwin-rebuild switch"
}
install_homemanager() {
if [ -z "$HOME_VER" ] && [ -n "$host_dir" ]; then
# this matches a line like `home.stateVersion = "23.11";`, accounting for spaces.
# It doesn't handle the expression being split over multiple lines, using different quotes
# or using an attrset instead of straight assignment (e.g. `home = { stateVersion = "23.11"; };`)
HOME_VER="$(sed -n 's/.*home\.stateVersion[[:blank:]]\{0,1\}=[[:blank:]]\{0,1\}"\(.*\)"[[:blank:]]\{0,1\};/\1/p' "$SCRIPT_DIR/machine/$host_dir/home.nix")"
fi
if [ -z "$HOME_VER" ]; then
HOME_VER="master"
else
HOME_VER="release-$HOME_VER"
fi
echo "Installing home-manager $HOME_VER"
prompt || {
printf '%s\n' "Skipping home-manager install..."
return 0
}
# install system home-manager if we are in nixos or macos
if isdarwin || isnixos; then
printf '%s\n' "Installing system home-manager"
execute sudo -i nix-channel --add "https://github.com/nix-community/home-manager/archive/$HOME_VER.tar.gz" home-manager
execute sudo -i nix-channel --update
else
printf '%s\n' "Installing standalone home-manager"
execute nix-channel --add "https://github.com/nix-community/home-manager/archive/$HOME_VER.tar.gz" home-manager
execute nix-channel --update
export NIX_PATH="$HOME/.nix-defexpr/channels:/nix/var/nix/profiles/per-user/root/channels${NIX_PATH:+:$NIX_PATH}"
execute nix-shell "<home-manager>" -A install
fi
}
install_nix() {
if nixinstalled; then
printf '%s\n' "Nix is already installed. Skipping..."
return 0
fi
echo "Installing nix using the Determinate Systems installer."
prompt || {
printf '%s\n' "Cancelling install..."
exit 0
}
execute eval 'curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install'
printf '%s\n' "Restart your shell and rerun this script."
exit 0
}
bootstrap_nixos() {
echo "Bootstrapping nixos config"
prompt || {
echo "Skipping nixos boostrap..."
return 0
}
printf '%s' "Provide machine config: "
read -r host_dir
while [ ! -d "$SCRIPT_DIR/machine/$host_dir" ]; do
echo "Invalid machine. Machine must be the name of a directory in $SCRIPT_DIR/machine"
echo "Options are: "
# shellcheck disable=SC2038
find "$SCRIPT_DIR/machine" -maxdepth 1 -mindepth 1 -type d -exec basename '{}' \;
printf '%s' "Try again or quit with ctrl-c: "
read -r host_dir
done
echo "Backing up existing nixos configuration in /etc/nixos."
if prompt; then
for f in /etc/nixos/*; do
execute sudo mv "$f" "$f.bak"
done
echo "Creating configuration.nix for $host_dir"
execute eval "echo '{...}: { imports = [ $SCRIPT_DIR/machine/$host_dir ]; }' | sudo tee /etc/nixos/configuration.nix"
else
echo "Skipping nixos boostrap..."
fi
echo "Adding nixos-hardware channel"
if prompt; then
execute sudo -i nix-channel --add https://github.com/NixOS/nixos-hardware/archive/master.tar.gz nixos-hardware
execute sudo -i nix-channel --update
else
echo "Skipping nixos-hardware channel..."
fi
}
do_install() {
execute pushd /tmp
if isnixos; then
bootstrap_nixos
else
install_nix
fi
install_homemanager
if isdarwin; then
install_nixdarwin
fi
}
do_uninstall() {
if isnixos; then
echo "Cannot uninstall nixos in place. Quitting..."
exit 1
fi
if isdarwin; then
echo "Uninstalling nix-darwin"
if prompt; then
execute pushd /tmp
execute nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A uninstaller
execute ./result/bin/darwin-uninstaller
else
printf '%s\n' "Skipping nix-darwin uninstall..."
fi
echo "Uninstalling nix."
if prompt; then
printf '%s\n' "Restoring original shell rc files"
execute sudo mv /etc/zshrc.backup-before-nix /etc/zshrc
execute sudo mv /etc/bashrc.backup-before-nix /etc/bashrc
execute sudo mv /etc/zprofile.orig /etc/zprofile
execute sudo rm -i "/etc/*.backup-before-nix"
printf '%s\n' "Removing nix store volume from fstab"
execute sudo vifs
printf '%s\n' "Remove nix from /etc/synthetic.conf"
execute sudo vim /etc/synthetic.conf
printf '%s\n' "Stopping nix daemon service"
execute sudo launchctl unload /Library/LaunchDaemon/org.nixos.nix-daemon.plist
execute sudo rm /Library/LaunchDaemons/org.nixos.nix-daemon.plist
execute sudo launchctl unload /Library/LaunchDaemons/org.nixos.activate-system.plist
execute sudo rm /Library/LaunchDaemons/org.nixos.activate-system.plist
printf '%s\n' "Removing nix files"
execute sudo rm -rf /etc/nix /var/root/.nix-profile /var/root/.nix-defexpr /var/root/.nix-channels ~/.nix-profile ~/.nix-defexpr ~/.nix-channels
printf '%s\n' "Removing nixbld groups"
execute sudo dscl . delete /Groups/nixbld
for i in $(seq 1 32); do
execute sudo dscl . -delete "/Users/_nixbld$i"
done
printf '%s\n' "Removing nix store volume"
execute sudo diskutil apfs deleteVolume /nix
execute sudo rm -rf /nix/
printf '%s\n' "Removing user nix temp files"
execute rm -rf "$HOME/.local/state/nix"
execute rm -rf "$HOME/.local/state/home-manager"
execute rm -rf "$HOME/Applications/Nix Apps"
execute rm -rf "$HOME/Applications/Home Manager Apps"
echo "Rebooting."
if prompt; then
execute sudo reboot
else
printf '%s\n' "Uninstall complete. Reboot needed"
exit 0
fi
else
printf '%s\n' "Cancelling uninstall..."
exit 0
fi
elif islinux; then
echo "TODO: uninstall linux"
else
printf '%s\n' "System $OSTYPE not supported"
exit 1
fi
}
show_help() {
echo "installer.sh [-h|--help] [--dry-run] [--home-ver <VERSION>] (install | uninstall)"
}
ACTION=
HOME_VER=
while :; do
case $1 in
-h|--help)
show_help
exit
;;
--dry-run)
DRY_RUN=1
echo "---Dry Run---"
;;
-i|--interactive)
INTERACTIVE=1
echo "---Interactive---"
;;
--home-ver)
shift
HOME_VER="$1"
;;
install)
ACTION="install"
;;
uninstall)
ACTION="uninstall"
;;
*)
break
esac
shift
done
if [ "$ACTION" = "install" ]; then
do_install
elif [ "$ACTION" = "uninstall" ]; then
do_uninstall
else
show_help
exit 1
fi