-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
executable file
·560 lines (486 loc) · 16.2 KB
/
setup
File metadata and controls
executable file
·560 lines (486 loc) · 16.2 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env bash
set -e
# Master identity pubkeys for each group
declare -A group_pubkeys
# TPM-protected identity paths (can be overridden in private/setup-env)
AGE_TPM_IDENTITY="${AGE_TPM_IDENTITY:-${HOME}/.age/tpm-identity.txt}"
AGE_MASTER_KEY_TPM="${AGE_MASTER_KEY_TPM:-${HOME}/.age/master-key.age}"
# Runtime decryption path - must match defaults in age.nix module
if [[ "$(uname -s)" == "Darwin" ]]; then
AGE_RUNTIME_KEY="/tmp/age-master-key"
else
AGE_RUNTIME_KEY="/dev/shm/age-master-key"
fi
AGE_DECRYPTED_KEY="" # Set during rekey
THISHOST="$(hostname -s)"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOTS=/nix/var/nix/gcroots/my-builds
# Source private environment if it exists
[[ -f "$SCRIPT_DIR/private/setup-env" ]] && source "$SCRIPT_DIR/private/setup-env"
# Options
HOSTS=()
DO_REFRESH=false
DO_SWITCH=false
DO_REKEY=false
DO_GIT=true
DO_PUSH=true
DO_CACHE_PUSH=true
DO_LIST=false
DO_ALLOW_SHORT_HOST=false
DO_OFFLINE=false
MESSAGE=""
VERBOSE=false
# Cache for host metadata
declare -A HOST_META
usage() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS] [HOST...]
Build and deploy NixOS/nix-darwin configurations.
If no HOST is specified, builds the current host (${THISHOST}).
Options:
-s, --switch Switch to the new configuration after building
-k, --rekey Rekey age secrets for the specified hosts before building
-r, --refresh Update flake inputs before building
-n, --no-git Skip git commit and push
-np, --no-push Commit locally but skip git push
-nc, --no-cache Skip pushing closures to attic cache (still inhibits per-build hooks)
-m, --message=MSG Custom git commit message (default: "autosave (<host> / <os>)")
-l, --list List known hosts and exit
--allow-short-host
Allow using short host name for remote switch when FQDN is missing
--offline Pass --offline to nix build (no network fetches)
-v, --verbose Enable verbose output (set -x)
-h, --help Show this help message
Host specifiers:
HOST Individual host name (e.g., o1, pavel-fw)
+GROUP All hosts in a group (e.g., +infra, +pavel)
Examples:
$(basename "$0") # Build current host
$(basename "$0") -s # Build and switch current host
$(basename "$0") -k -s # Rekey, build, and switch current host
$(basename "$0") o1 o2 # Build o1 and o2
$(basename "$0") +infra # Build all hosts in 'infra' group
$(basename "$0") -k +infra # Rekey and build all infra hosts
$(basename "$0") -r -s # Update flake, build, and switch
$(basename "$0") pavel-fw -s --allow-short-host
# Remote switch via short host name when no FQDN is defined
$(basename "$0") -n # Build without git operations
EOF
exit 0
}
load_host_meta() {
echo "Loading host metadata from flake..."
add_all
local json
local errfile
errfile="$(mktemp)"
if ! json=$(nix eval ".?submodules=1#hostMeta" --json 2>"$errfile"); then
echo "Error: nix eval failed for host metadata:" >&2
cat "$errfile" >&2
rm -f "$errfile"
exit 1
fi
rm -f "$errfile"
# Parse JSON and populate HOST_META
while IFS="=" read -r key value; do
HOST_META["$key"]="$value"
done < <(echo "$json" | jq -r 'to_entries[] | "\(.key)=\(.value | @json)"')
}
get_host_attr() {
local host="$1"
local attr="$2"
local meta="${HOST_META[$host]}"
if [[ -z "$meta" ]]; then
echo ""
return
fi
echo "$meta" | jq -r ".$attr // empty"
}
get_hosts_by_group() {
local target_group="$1"
local hosts=()
for host in "${!HOST_META[@]}"; do
local group
group=$(get_host_attr "$host" "group")
if [[ "$group" == "$target_group" ]]; then
hosts+=("$host")
fi
done
echo "${hosts[@]}"
}
get_current_platform() {
case "$(uname -s)" in
Darwin) echo "darwin" ;;
Linux) echo "linux" ;;
*) echo "unknown" ;;
esac
}
expand_host_specifiers() {
local current_platform
current_platform=$(get_current_platform)
local expanded=()
for spec in "${HOSTS[@]}"; do
if [[ "$spec" == +* ]]; then
local group="${spec:1}"
local group_hosts=()
local skipped=()
for host in "${!HOST_META[@]}"; do
local host_group host_platform
host_group=$(get_host_attr "$host" "group")
host_platform=$(get_host_attr "$host" "platform")
if [[ "$host_group" == "$group" ]]; then
if [[ "$host_platform" == "$current_platform" ]]; then
group_hosts+=("$host")
else
skipped+=("$host")
fi
fi
done
if [[ ${#group_hosts[@]} -eq 0 ]]; then
echo "Error: No buildable hosts found in group '$group' for platform '$current_platform'" >&2
[[ ${#skipped[@]} -gt 0 ]] && echo " (skipped incompatible: ${skipped[*]})" >&2
echo "Use --list to see known hosts and their groups." >&2
exit 1
fi
echo " Expanding +$group: ${group_hosts[*]}"
[[ ${#skipped[@]} -gt 0 ]] && echo " (skipped incompatible: ${skipped[*]})"
for h in "${group_hosts[@]}"; do
expanded+=("$h")
done
else
expanded+=("$spec")
fi
done
HOSTS=("${expanded[@]}")
}
list_hosts() {
load_host_meta
echo "Known hosts:"
printf " %-16s %-8s %-8s %s\n" "HOST" "PLATFORM" "GROUP" "FQN"
printf " %-16s %-8s %-8s %s\n" "----" "--------" "-----" "---"
for host in "${!HOST_META[@]}"; do
local platform group fqn
platform=$(get_host_attr "$host" "platform")
group=$(get_host_attr "$host" "group")
fqn=$(get_host_attr "$host" "fqn")
[[ "$fqn" == "null" || -z "$fqn" ]] && fqn="(local)"
printf " %-16s %-8s %-8s %s\n" "$host" "$platform" "$group" "$fqn"
done | sort
exit 0
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
-s|--switch)
DO_SWITCH=true
shift
;;
-k|--rekey)
DO_REKEY=true
shift
;;
-r|--refresh)
DO_REFRESH=true
shift
;;
-n|--no-git)
DO_GIT=false
shift
;;
-np|--no-push)
DO_PUSH=false
shift
;;
-nc|--no-cache)
DO_CACHE_PUSH=false
shift
;;
-m|--message)
MESSAGE="$2"
shift 2
;;
--message=*)
MESSAGE="${1#*=}"
shift
;;
-m=*)
MESSAGE="${1#*=}"
shift
;;
-l|--list)
DO_LIST=true
shift
;;
--allow-short-host)
DO_ALLOW_SHORT_HOST=true
shift
;;
--offline)
DO_OFFLINE=true
shift
;;
-v|--verbose)
VERBOSE=true
shift
;;
-h|--help)
usage
;;
-*)
echo "Error: Unknown option: $1" >&2
echo "Use --help for usage information." >&2
exit 1
;;
*)
HOSTS+=("$1")
shift
;;
esac
done
# Default to current host if none specified
if [[ ${#HOSTS[@]} -eq 0 ]]; then
HOSTS=("$THISHOST")
fi
# Default commit message
if [[ -z "$MESSAGE" ]]; then
MESSAGE="autosave ($(hostname) / $(uname))"
fi
}
validate_hosts() {
for host in "${HOSTS[@]}"; do
if [[ -z "${HOST_META[$host]}" ]]; then
echo "Error: Unknown host '$host'" >&2
echo "Use --list to see known hosts." >&2
exit 1
fi
done
}
add_all() {
git add .
git submodule foreach git add .
}
commit_and_push() {
if [[ "$DO_GIT" == "true" ]]; then
git submodule foreach "git diff-index --quiet HEAD || (git add -A && git commit -m \"${MESSAGE}\")"
git diff-index --quiet HEAD || (git add -A && git commit -m "${MESSAGE}")
if [[ "$DO_PUSH" == "true" ]]; then
git push --recurse-submodules=on-demand
fi
fi
}
do_refresh() {
if [[ "$DO_REFRESH" == "true" ]]; then
echo "==> Updating flake inputs..."
nix flake update
fi
}
decrypt_password_key() {
local password_key="${HOME}/age-key.age"
if [[ ! -f "$password_key" ]]; then
return 1
fi
echo " Decrypting master key (enter password)..." >&2
if age -d -o "$AGE_DECRYPTED_KEY" "$password_key"; then
chmod 600 "$AGE_DECRYPTED_KEY"
echo "$AGE_DECRYPTED_KEY"
return 0
else
echo "Error: Failed to decrypt master key" >&2
rm -f "$AGE_DECRYPTED_KEY"
return 1
fi
}
decrypt_tpm_key() {
if [[ ! -f "$AGE_MASTER_KEY_TPM" || ! -f "$AGE_TPM_IDENTITY" ]]; then
return 1
fi
if ! command -v age-plugin-tpm &>/dev/null; then
echo " Warning: age-plugin-tpm not found, skipping TPM" >&2
return 1
fi
echo " Decrypting master key via TPM..." >&2
if age -d -i "$AGE_TPM_IDENTITY" -o "$AGE_DECRYPTED_KEY" "$AGE_MASTER_KEY_TPM" 2>/dev/null; then
chmod 600 "$AGE_DECRYPTED_KEY"
echo "$AGE_DECRYPTED_KEY"
return 0
else
echo " Warning: TPM decryption failed, trying password key..." >&2
rm -f "$AGE_DECRYPTED_KEY"
return 1
fi
}
get_master_identity() {
# Returns path to the master identity file
# Tries TPM first (Linux only), then falls back to password-protected key
AGE_DECRYPTED_KEY="$AGE_RUNTIME_KEY"
trap 'rm -f "$AGE_DECRYPTED_KEY"' EXIT
# Linux: try TPM first
if [[ "$(uname -s)" != "Darwin" ]]; then
if decrypt_tpm_key; then
return 0
fi
fi
# Fallback (all platforms): password-protected key
if decrypt_password_key; then
return 0
fi
echo "Error: No usable master key found" >&2
echo "Either run ./enroll-tpm for TPM protection," >&2
echo "or create a password-protected key: age -p -o ~/age-key.age ~/age-key.txt" >&2
return 1
}
cleanup_decrypted_key() {
if [[ -n "$AGE_DECRYPTED_KEY" && -f "$AGE_DECRYPTED_KEY" ]]; then
rm -f "$AGE_DECRYPTED_KEY"
AGE_DECRYPTED_KEY=""
fi
}
do_rekey() {
if [[ "$DO_REKEY" == "true" ]]; then
echo "==> Rekeying secrets for: ${HOSTS[*]}"
add_all
# Decrypt master key to the path expected by Nix config
get_master_identity >/dev/null || exit 1
local system
system="$(uname -m)-$(uname -s | tr '[:upper:]' '[:lower:]')"
for host in "${HOSTS[@]}"; do
local group
group=$(get_host_attr "$host" "group")
local pubkey="${group_pubkeys[$group]}"
if [[ -z "$pubkey" ]]; then
echo "Warning: No pubkey configured for group '$group', skipping rekey for $host"
continue
fi
echo " Rekeying $host (group: $group)..."
nix run ".?submodules=1#agenix-rekey-hosts.${host}.${system}.rekey"
done
cleanup_decrypted_key
fi
}
attic_inhibit_start() {
ATTIC_INHIBIT="/tmp/attic-push-inhibit"
touch "$ATTIC_INHIBIT"
trap 'rm -f "$ATTIC_INHIBIT"' EXIT
}
attic_push_closures() {
if [[ "$DO_CACHE_PUSH" != "true" ]]; then
echo "==> Skipping attic cache push (-nc)"
return
fi
if command -v attic-push-closure &>/dev/null; then
local -a closures=()
for host in "${HOSTS[@]}"; do
[[ -L "${ROOTS}/${host}" ]] && closures+=("$(readlink -f "${ROOTS}/${host}")")
done
if [[ ${#closures[@]} -gt 0 ]]; then
echo "==> Pushing ${#closures[@]} closure(s) to attic..."
attic-push-closure "${closures[@]}" || echo " Warning: attic push failed (non-fatal)"
fi
fi
}
attic_inhibit_stop() {
rm -f "$ATTIC_INHIBIT"
trap - EXIT
}
do_build() {
echo "==> Building: ${HOSTS[*]}"
add_all
mkdir -p "$ROOTS"
local -a build_args=()
if [[ "$DO_OFFLINE" == "true" ]]; then
build_args+=(--offline)
fi
for host in "${HOSTS[@]}"; do
local platform
platform=$(get_host_attr "$host" "platform")
echo " Building $host ($platform)..."
case "$platform" in
darwin)
nix build ".?submodules=1#darwinConfigurations.${host}.system" \
--out-link "${ROOTS}/${host}" \
"${build_args[@]}"
;;
linux)
nix build ".?submodules=1#nixosConfigurations.${host}.config.system.build.toplevel" \
--out-link "${ROOTS}/${host}" \
"${build_args[@]}"
;;
*)
echo "Error: Unknown platform '$platform' for host '$host'" >&2
exit 1
;;
esac
done
commit_and_push
}
do_switch() {
if [[ "$DO_SWITCH" == "true" ]]; then
echo "==> Switching: ${HOSTS[*]}"
add_all
for host in "${HOSTS[@]}"; do
local platform fqn closure remote_target
platform=$(get_host_attr "$host" "platform")
fqn=$(get_host_attr "$host" "fqn")
closure=$(readlink -f "${ROOTS}/${host}")
# null fqn means local deployment
[[ "$fqn" == "null" || -z "$fqn" ]] && fqn=""
remote_target="$fqn"
if [[ "$THISHOST" != "$host" && -z "$remote_target" ]]; then
if [[ "$DO_ALLOW_SHORT_HOST" == "true" ]]; then
remote_target="$host"
echo " Warning: '$host' has no FQDN; using short host name for remote switch"
fi
fi
if [[ "$THISHOST" != "$host" && -z "$remote_target" ]]; then
echo "Error: Host '$host' has no FQDN (smind.host.fqn/networking.domain), refusing local fallback switch" >&2
echo "Use --allow-short-host to opt in to short-name remote switching for this run." >&2
exit 1
fi
echo " Switching $host..."
case "$platform" in
darwin)
if [[ "$THISHOST" == "$host" ]]; then
"${closure}/sw/bin/darwin-rebuild" switch --flake .
else
nix copy --to "ssh://${remote_target}" "${ROOTS}/${host}"
ssh -t "${remote_target}" sudo -H --preserve-env=PATH env nix-env -p /nix/var/nix/profiles/system --set "$closure"
ssh -t "${remote_target}" "sudo ${closure}/sw/bin/darwin-rebuild" activate
fi
;;
linux)
if [[ "$THISHOST" == "$host" ]]; then
sudo nix-env -p /nix/var/nix/profiles/system --set "$closure"
sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch
else
nix copy --to "ssh://root@${remote_target}" "${ROOTS}/${host}"
ssh "root@${remote_target}" nix-env -p /nix/var/nix/profiles/system --set "$closure"
ssh "root@${remote_target}" /nix/var/nix/profiles/system/bin/switch-to-configuration switch
fi
;;
esac
done
commit_and_push
fi
}
main() {
parse_args "$@"
if [[ "$DO_LIST" == "true" ]]; then
list_hosts
fi
if [[ "$VERBOSE" == "true" ]]; then
set -x
fi
cd "$SCRIPT_DIR"
load_host_meta
expand_host_specifiers
validate_hosts
do_refresh
do_rekey
attic_inhibit_start
do_build
attic_push_closures
do_switch
attic_inhibit_stop
echo "==> Done!"
}
main "$@"