-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx
executable file
·356 lines (311 loc) · 8.98 KB
/
x
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env bash
set -e
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
pushd "$ROOT"
HOSTNAME=$(uname -n | tr '[:upper:]' '[:lower:] | sed 's/\.local//'')
if [[ -z "${MACHINE_KEY}" ]]; then
MACHINE_KEY="${USER}@${HOSTNAME}"
fi
HOME_CONFIG=""
NIXOS=0
if [[ "${MACHINE_KEY}" == "appaquet@deskapp"* ]]; then
HOME_CONFIG="appaquet@deskapp"
HOSTNAME="deskapp"
NIXOS=1
elif [[ "${MACHINE_KEY}" == "appaquet@servapp"* ]]; then
HOME_CONFIG="appaquet@servapp"
HOSTNAME="servapp"
NIXOS=1
elif [[ "${MACHINE_KEY}" == "appaquet@utm"* ]]; then
HOME_CONFIG="appaquet@utm"
HOSTNAME="utm"
NIXOS=1
elif [[ "${MACHINE_KEY}" == "appaquet@piapp"* ]]; then
HOME_CONFIG="appaquet@piapp"
HOSTNAME="piapp"
NIXOS=1
elif [[ "${MACHINE_KEY}" == "appaquet@mbpapp"* || "${MACHINE_KEY}" == "appaquet@mbpvmapp"* ]]; then
HOME_CONFIG="appaquet@mbpapp"
fi
NIX_BUILDER="nix"
if [[ -x ~/.nix-profile/bin/nom ]]; then
NIX_BUILDER="nom"
fi
if [[ ! -f "$ROOT/secrets/flake.nix" ]]; then
echo "Secrets aren't checked out. Make sure you checkout & decrypt them after activation"
sleep 2
fi
SECRETS_CHANGED=$(git diff --submodule=log secrets | grep "Submodule" || true)
if [[ -n "$SECRETS_CHANGED" ]]; then
echo "Secrets submodule has changes. Make sure to update prior to building"
sleep 1
fi
check_eval() {
nix eval --raw "${1}"
}
check_home() {
echo "Checking home ${1}"
check_eval ".#homeConfigurations.${1}.activationPackage"
}
prime_sudo() {
sudo echo # prime pw for nom redirects to work
}
copy_files() {
local host="$1"
local file
find "${ROOT}/files/${host}" -type f | while read -r file; do
local target_file="${file/${ROOT}\/files\/${host}/}"
local target_path="${target_file}"
echo "Copy ${file} to ${target_path}"
# if outside of home, use sudo
if [[ "${target_path}" != "${HOME}/"* ]]; then
sudo mkdir -p "$(dirname "${target_path}")"
sudo cp "${file}" "${target_path}"
else
mkdir -p "$(dirname "${target_path}")"
cp "${file}" "${target_path}"
fi
done
}
COMMAND=$1
case $COMMAND in
home)
shift
if [[ -z "$HOME_CONFIG" ]]; then
echo "No home configuration for ${MACHINE_KEY}"
exit 1
fi
SUBCOMMAND=$1
case $SUBCOMMAND in
check)
shift
check_home $HOME_CONFIG
;;
build)
shift
${NIX_BUILDER} build ".#homeConfigurations.${HOME_CONFIG}.activationPackage"
;;
switch)
shift
GENERATION="${1:-}"
if [[ -n "$GENERATION" ]]; then
GEN_PATH=$(home-manager generations | grep "id ${GENERATION}" | awk '{print $7}')
if [[ -z "$GEN_PATH" ]]; then
echo "Generation $GENERATION not found"
exit 1
fi
echo "Activating generation $GENERATION at $GEN_PATH"
$GEN_PATH/activate
else
echo "Activating latest generation"
./result/activate
fi
;;
diff)
shift
nvd diff ~/.local/state/nix/profiles/home-manager ./result
;;
tree)
shift
nix-tree ~/.local/state/nix/profiles/home-manager
;;
generations)
shift
home-manager generations
;;
diff-generations)
shift
nix profile diff-closures --profile ~/.local/state/nix/profiles/home-manager
;;
*)
echo "$0 $COMMAND check: check home" >&2
echo "$0 $COMMAND build: build home" >&2
echo "$0 $COMMAND switch: switch home" >&2
echo "$0 $COMMAND diff: diff last build with current" >&2
echo "$0 $COMMAND tree: show home tree" >&2
echo "$0 $COMMAND generations: list generations" >&2
echo "$0 $COMMAND diff-generations: diff last generations" >&2
exit 1
;;
esac
;;
darwin)
shift
SUBCOMMAND=$1
case $SUBCOMMAND in
check)
shift
check_eval ".#darwinConfigurations.mbpapp.system"
;;
build)
shift
${NIX_BUILDER} build ".#darwinConfigurations.mbpapp.system"
;;
switch)
shift
./result/sw/bin/darwin-rebuild switch --flake .
;;
tree)
shift
nix-tree ~/.nix-profile
;;
*)
echo "$0 $COMMAND check: check home" >&2
echo "$0 $COMMAND build: build home" >&2
echo "$0 $COMMAND switch: switch home" >&2
echo "$0 $COMMAND tree: show home tree" >&2
exit 1
;;
esac
;;
nixos)
shift
SUBCOMMAND=$1
case $SUBCOMMAND in
check)
shift
check_eval ".#nixosConfigurations.${HOSTNAME}.config.system.build.toplevel"
;;
build)
shift
nixos-rebuild build --flake ".#${HOSTNAME}"
;;
boot)
shift
prime_sudo
sudo nixos-rebuild boot --flake ".#${HOSTNAME}"
;;
switch)
shift
prime_sudo
GENERATION="${1:-}"
if [[ -n "$GENERATION" ]]; then
GEN_PATH="/nix/var/nix/profiles/system-${GENERATION}-link"
if [[ ! -d "$GEN_PATH" ]]; then
echo "Generation $GENERATION not found"
exit 1
fi
echo "Activating generation $GENERATION at $GEN_PATH"
sudo $GEN_PATH/activate
else
echo "Activating latest generation"
sudo nixos-rebuild switch --flake ".#${HOSTNAME}"
fi
;;
diff)
shift
nvd diff /run/current-system result
;;
tree)
shift
nix-tree /run/current-system
;;
kernel-versions)
shift
BOOTED=$(readlink /run/booted-system/{initrd,kernel,kernel-modules} | tr '\n' ' ')
CURRENT=$(readlink /run/current-system/{initrd,kernel,kernel-modules} | tr '\n' ' ')
RESULT=$(readlink ./result/{initrd,kernel,kernel-modules} | tr '\n' ' ')
HAS_DIFF=0
if [[ "$BOOTED" != "$CURRENT" ]]; then
HAS_DIFF=1
echo "Booted kernel isn't the same as last generation"
echo " Booted: $BOOTED"
echo " Current: $CURRENT"
echo " "
fi
if [[ "$BOOTED" != "$RESULT" ]]; then
HAS_DIFF=1
echo "Booted kernel isn't the same as the one in ./result"
echo " Booted: $BOOTED"
echo " Result: $RESULT"
fi
if [[ "$HAS_DIFF" -eq 0 ]]; then
echo "Kernel versions are consistent"
fi
;;
generations)
shift
nix profile history --profile /nix/var/nix/profiles/system
;;
*)
echo "$0 $COMMAND check: check nixos" >&2
echo "$0 $COMMAND build: build nixos" >&2
echo "$0 $COMMAND boot: rebuild boot (remove old gens)" >&2
echo "$0 $COMMAND diff: diff nixos" >&2
echo "$0 $COMMAND tree: show nixos tree" >&2
echo "$0 $COMMAND kernel-versions: show kernel versions" >&2
echo "$0 $COMMAND generations: diff nixos" >&2
echo "$0 $COMMAND switch: switch nixos" >&2
exit 1
;;
esac
;;
check)
shift
check_home "appaquet@deskapp"
check_eval ".#nixosConfigurations.deskapp.config.system.build.toplevel"
check_home "appaquet@servapp"
check_home "appaquet@mbpapp"
check_eval ".#darwinConfigurations.mbpapp.system"
;;
update)
shift
PACKAGE="$1"
if [[ -z "$PACKAGE" ]]; then
nix-channel --update
nix flake update
for shell in $(ls shells); do
pushd "shells/${shell}"
nix flake update
popd
done
else
nix flake lock --update-input $PACKAGE
fi
;;
link)
shift
if [[ ! -d "${ROOT}/files/${HOSTNAME}" ]]; then
echo "No files for ${HOSTNAME}"
exit 1
fi
prime_sudo
copy_files "$HOSTNAME"
;;
gc)
shift
echo "Garbage collecting..."
nix-collect-garbage --delete-older-than "7d"
home-manager expire-generations "-7 days"
# Cleaning as root collects more stuff as well
# See https://www.reddit.com/r/NixOS/comments/10107km/how_to_delete_old_generations_on_nixos/?s=8
ncg=$(which nix-collect-garbage)
sudo ${ncg} -d --delete-older-than "7d"
;;
fmt)
shift
find . -name "*.nix" -not -path "./humanfirst-dots/*" -exec nixfmt "${@}" {} \;
;;
optimize)
shift
echo "Optimizing store..."
nix store optimise
;;
fetch-deskapp)
shift
rsync -avz --delete [email protected]:dotfiles/ ~/dotfiles/
;;
*)
echo "$0 home: home manager sub commands" >&2
echo "$0 darwin: darwin sub commands" >&2
echo "$0 nixos: nixos sub commands" >&2
echo "$0 check: eval home & nixos & darwin configs for all hosts" >&2
echo "$0 update: update nix channels" >&2
echo "$0 link: link system files" >&2
echo "$0 gc: run garbage collection" >&2
echo "$0 fmt: format nix files" >&2
echo "$0 optimize: optimize store" >&2
echo "$0 fetch-deskapp: fetch latest dotfiles from deskapp" >&2
exit 1
;;
esac