Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·799 lines (722 loc) · 23.4 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·799 lines (722 loc) · 23.4 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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#!/usr/bin/env bash
set -euo pipefail
FLAKE_DIR="$(cd "$(dirname "$(realpath "$0")")" && pwd)"
NIXOS_USER="${NIXOS_USER:-muddyblack}"
STATE_FILE="${FLAKE_DIR}/.deploy-state"
SUCCESS_SOUND="${FLAKE_DIR}/assets/sounds/success.wav"
ERROR_SOUND="${FLAKE_DIR}/assets/sounds/error.wav"
_play_sound() {
local file="$1"
if [[ -f "$file" && -s "$file" ]]; then
if command -v pw-play &>/dev/null; then
pw-play "$file" &>/dev/null &
elif command -v paplay &>/dev/null; then
paplay "$file" &>/dev/null &
elif command -v aplay &>/dev/null; then
aplay "$file" &>/dev/null &
fi
fi
}
load_saved_profile() {
if [[ -f "$STATE_FILE" ]]; then
# shellcheck source=/dev/null
source "$STATE_FILE"
fi
}
save_profile() {
echo "FLAKE_TARGET=${1}" > "$STATE_FILE"
}
load_saved_profile
FLAKE_TARGET="${FLAKE_TARGET:-muddyblack}"
ensure_symlink() {
local target="$1"
if [[ "$(readlink /etc/nixos 2>/dev/null)" == "$target" ]]; then
return 0
fi
if [[ -e /etc/nixos && ! -L /etc/nixos ]]; then
echo "/etc/nixos exists as a directory — removing to replace with symlink"
sudo rm -rf /etc/nixos
fi
sudo ln -sfn "$target" /etc/nixos
echo "Symlink updated: /etc/nixos -> $target"
}
usage() {
cat <<EOF
NixOS deployment helper
Usage: $0 <command> [options]
Commands:
fresh Partition disk (disko) + install NixOS on LOCAL machine
(Run this while booted from a Live ISO)
fresh --remote <host>
Partition + install on a REMOTE machine via nixos-anywhere
(Builds locally, pushes over SSH)
install Run nixos-install on LOCAL machine (disk already partitioned)
switch Rebuild & switch config on the RUNNING system (day-to-day)
switch --offline Rebuild without fetching anything from the network
switch --remote <host>
Rebuild & switch on a REMOTE running NixOS machine
sops-setup First-time SOPS setup: generate age key, update .sops.yaml, create secrets
secrets-edit Edit encrypted secrets
secrets-rotate Re-encrypt after adding new age keys to .sops.yaml
Options:
--profile <name> Host profile: muddyblack (default), muddyblack-lite
--remote <host> Target host (e.g. root@192.168.1.100)
--nh Use 'nh' wrapper instead of 'nixos-rebuild' directly
--device <dev> Target disk (e.g. /dev/sda, /dev/nvme0n1) — prompted interactively if omitted
--bootloader <bl> Bootloader: grub or refind — prompted interactively if omitted
--dual-boot Enable dual-boot Windows partitions — prompted interactively if omitted
--no-dual-boot Disable dual-boot Windows partitions
--plymouth-theme <t> Plymouth boot theme (dotted, flower, icy, matrix) — prompted interactively if omitted
--desktops <list> Comma-separated desktop sessions to enable: hyprland,plasma,cosmic,gnome
— prompted interactively if omitted
--keyboard <layout> xkb keyboard layout for every session (de, us, fr, ...)
— prompted interactively if omitted
Examples:
$0 fresh # interactive setup
$0 fresh --device /dev/nvme0n1 # skip device prompt only
$0 fresh --device /dev/sda --bootloader grub --no-dual-boot # fully non-interactive
$0 fresh --remote root@192.168.1.100 # remote install (interactive)
$0 install # local, after manual disko
$0 switch # rebuild current system
$0 switch --remote root@192.168.1.100
EOF
exit 1
}
# Best x86-64 psABI level this CPU can run. v4 is deliberately not a possible
# answer: the kernel builds with -mno-avx, so no AVX-512 reaches kernel code and
# a v4 build is a v3 build with a narrower CPU requirement. Read from
# /proc/cpuinfo rather than `ld.so --help` because the loader on PATH is nix-ld's
# shim here, and cpuinfo answers the same CPUID bits over SSH just as well.
detect_march() {
local host="${1:-}"
local cpuinfo flags vendor f
if [[ -n "$host" ]]; then
cpuinfo="$(ssh -o StrictHostKeyChecking=no "$host" 'cat /proc/cpuinfo' 2>/dev/null)" || return 1
else
cpuinfo="$(cat /proc/cpuinfo 2>/dev/null)" || return 1
fi
flags=" $(printf '%s\n' "$cpuinfo" | sed -n 's/^flags[[:space:]]*: //p' | head -1) "
vendor="$(printf '%s\n' "$cpuinfo" | sed -n 's/^vendor_id[[:space:]]*: //p' | head -1)"
[[ -n "${flags// /}" ]] || return 1
for f in avx avx2 bmi1 bmi2 f16c fma movbe xsave popcnt sse4_1 sse4_2 ssse3 cx16 lahf_lm; do
[[ "$flags" == *" $f "* ]] || { echo "generic"; return 0; }
done
# Zen 4/5 are the AMD parts with AVX-512, and the one cached variant tuned for
# a microarchitecture instead of a psABI level.
if [[ "$vendor" == "AuthenticAMD" && "$flags" == *" avx512f "* ]]; then
echo "zen4"
return 0
fi
echo "x86_64-v3"
}
# Keep the detected level in deploy-config.nix. Nix cannot probe the CPU itself:
# flake evaluation is pure, so reading /proc/cpuinfo at eval time would need
# --impure and would break `nix flake check` and every remote build. Detecting
# once at deploy time and writing the answer down keeps evaluation reproducible.
sync_deploy_march() {
local host="${1:-}"
local out="$FLAKE_DIR/hosts/deploy-config.nix"
local march
[[ -f "$out" ]] || return 0
if ! march="$(detect_march "$host")"; then
echo "CPU detection failed — leaving features.kernel.cachyos.march unchanged."
return 0
fi
if grep -q 'features\.kernel\.cachyos\.march' "$out"; then
grep -q "features\.kernel\.cachyos\.march = \"${march}\";" "$out" && return 0
sed -i "s|features\.kernel\.cachyos\.march = \".*\";|features.kernel.cachyos.march = \"${march}\";|" "$out"
else
sed -i "\$i\\ features.kernel.cachyos.march = \"${march}\";" "$out"
fi
echo "Detected CPU level: ${march} (hosts/deploy-config.nix updated)"
}
write_deploy_config() {
local device="$1"
local bl="$2"
local dual_boot="$3"
local theme="$4"
local march="${5:-generic}"
local desktops="${6:-hyprland,plasma,cosmic}"
local kb_layout="${7:-de}"
local out="$FLAKE_DIR/hosts/deploy-config.nix"
if [[ ! "$device" =~ ^/dev/ ]]; then
echo "Error: device must be a path like /dev/sda or /dev/nvme0n1"
exit 1
fi
if [[ ! "$bl" =~ ^(grub|refind|systemd-boot)$ ]]; then
echo "Error: bootloader must be grub, refind, or systemd-boot"
exit 1
fi
if [[ ! "$dual_boot" =~ ^(true|false)$ ]]; then
echo "Error: dual_boot must be true or false"
exit 1
fi
if ! compgen -G "$FLAKE_DIR/assets/plymouth/$theme/*.mp4" > /dev/null; then
echo "Error: Plymouth theme '$theme' not found in assets/plymouth/"
exit 1
fi
if [[ ! "$kb_layout" =~ ^[a-z][a-z0-9_-]*$ ]]; then
echo "Error: keyboard layout must be an xkb layout name like de, us, fr"
exit 1
fi
local all_desktops=(hyprland plasma cosmic gnome)
local desktop_lines=""
IFS=',' read -ra chosen_desktops <<< "$desktops"
for d in "${chosen_desktops[@]}"; do
if [[ ! " ${all_desktops[*]} " =~ " ${d} " ]]; then
echo "Error: unknown desktop session '${d}' (known: ${all_desktops[*]})"
exit 1
fi
done
for d in "${all_desktops[@]}"; do
if [[ " ${chosen_desktops[*]} " =~ " ${d} " ]]; then
desktop_lines+=" features.desktops.${d}.enable = true;"$'\n'
else
desktop_lines+=" features.desktops.${d}.enable = false;"$'\n'
fi
done
cat > "$out" <<EOF
# Auto-generated by deploy.sh — do not edit manually.
{
diskLayout.device = "${device}";
diskLayout.withDualBoot = ${dual_boot};
bootloader = "${bl}";
plymouthTheme = "${theme}";
keyboardLayout = "${kb_layout}";
features.kernel.cachyos.march = "${march}";
${desktop_lines}}
EOF
echo "Wrote deploy config: hosts/deploy-config.nix (device=${device}, bootloader=${bl}, dualBoot=${dual_boot}, plymouth=${theme}, march=${march}, desktops=${desktops}, keyboard=${kb_layout})"
}
prompt_interactive_setup() {
local bootloader_ref="$1" # nameref
local dual_boot_ref="$2" # nameref
local device_ref="$3" # nameref
local plymouth_theme_ref="$4" # nameref
local desktops_ref="$5" # nameref
local kb_layout_ref="$6" # nameref
local -n _bootloader="$bootloader_ref"
local -n _dual_boot="$dual_boot_ref"
local -n _device="$device_ref"
local -n _plymouth_theme="$plymouth_theme_ref"
local -n _desktops="$desktops_ref"
local -n _kb_layout="$kb_layout_ref"
echo ""
echo "=== Interactive setup ==="
# Device
if [[ -z "$_device" ]]; then
echo "Available block devices:"
lsblk -d -o NAME,SIZE,MODEL 2>/dev/null | grep -v "^loop" || true
echo ""
local disks=()
while IFS= read -r name; do
disks+=("/dev/$name")
done < <(lsblk -d -o NAME,TYPE 2>/dev/null | awk '$2=="disk"{print $1}' | grep -v "^loop")
local default_device=""
if [[ ${#disks[@]} -eq 1 ]]; then
default_device="${disks[0]}"
fi
if [[ -n "$default_device" ]]; then
read -rp "Target disk device [default: ${default_device}]: " _device
_device="${_device:-$default_device}"
else
read -rp "Target disk device [e.g. /dev/sda, /dev/nvme0n1]: " _device
if [[ -z "$_device" ]]; then
echo "Error: device is required"
exit 1
fi
fi
fi
# Bootloader
if [[ -z "$_bootloader" ]]; then
local uefi_supported=false
if [[ -d /sys/firmware/efi ]]; then
uefi_supported=true
fi
echo ""
echo "Available bootloaders:"
if $uefi_supported; then
echo " 1) refind (UEFI — recommended)"
echo " 2) grub"
read -rp "Select bootloader [1-2, default: 1]: " bl_choice
bl_choice="${bl_choice:-1}"
case "$bl_choice" in
1) _bootloader="refind" ;;
2) _bootloader="grub" ;;
*) echo "Invalid choice, using refind"; _bootloader="refind" ;;
esac
else
echo " 1) grub (legacy BIOS — only option)"
echo " Note: rEFInd requires UEFI, not available on this system."
read -rp "Select bootloader [1, default: 1]: " bl_choice
_bootloader="grub"
fi
fi
# Dual boot
if [[ -z "$_dual_boot" ]]; then
read -rp "Enable dual-boot Windows partitions? [y/N]: " db_answer
if [[ "$db_answer" =~ ^[Yy]$ ]]; then
_dual_boot="true"
else
_dual_boot="false"
fi
fi
# Plymouth theme
if [[ -z "$_plymouth_theme" ]]; then
echo ""
echo "Available Plymouth boot themes:"
local themes=()
local i=1
for theme_dir in "$FLAKE_DIR"/assets/plymouth/*/; do
if [[ -d "$theme_dir" ]]; then
theme_name=$(basename "$theme_dir")
if compgen -G "$theme_dir/*.mp4" > /dev/null; then
themes+=("$theme_name")
echo " $i) $theme_name"
((i++))
fi
fi
done
echo ""
read -rp "Select Plymouth theme [1-${#themes[@]}, default: 1]: " theme_choice
theme_choice="${theme_choice:-1}"
if [[ "$theme_choice" =~ ^[0-9]+$ ]] && [[ "$theme_choice" -ge 1 ]] && [[ "$theme_choice" -le "${#themes[@]}" ]]; then
_plymouth_theme="${themes[$((theme_choice-1))]}"
else
echo "Invalid choice, using first theme: ${themes[0]}"
_plymouth_theme="${themes[0]}"
fi
fi
# Desktop sessions
if [[ -z "$_desktops" ]]; then
echo ""
echo "Which desktop sessions do you want enabled? (switchable at the login screen)"
local all_desktops=(hyprland plasma cosmic gnome)
local defaults=(y y y n)
local chosen=()
for i in "${!all_desktops[@]}"; do
local name="${all_desktops[$i]}"
local def="${defaults[$i]}"
local prompt_suffix="[y/N]"
[[ "$def" == "y" ]] && prompt_suffix="[Y/n]"
read -rp " Enable ${name}? ${prompt_suffix}: " answer
answer="${answer:-$def}"
if [[ "$answer" =~ ^[Yy]$ ]]; then
chosen+=("$name")
fi
done
if [[ ${#chosen[@]} -eq 0 ]]; then
echo "Error: at least one desktop session must be enabled"
exit 1
fi
_desktops="$(IFS=,; echo "${chosen[*]}")"
fi
# Keyboard layout — one xkb layout name for console, greeter, Hyprland,
# and Plasma (keyboardLayout in hosts/deploy-config.nix).
if [[ -z "$_kb_layout" ]]; then
echo ""
echo "Keyboard layout:"
local kb_names=(de us gb fr es it ch)
local kb_labels=("German" "US English" "UK English" "French (AZERTY)" "Spanish" "Italian" "Swiss")
for i in "${!kb_names[@]}"; do
echo " $((i+1))) ${kb_names[$i]} — ${kb_labels[$i]}"
done
echo " $(( ${#kb_names[@]} + 1 ))) other — type any xkb layout name"
read -rp "Select keyboard layout [1-$(( ${#kb_names[@]} + 1 )), default: 1]: " kb_choice
kb_choice="${kb_choice:-1}"
if [[ "$kb_choice" =~ ^[0-9]+$ ]] && [[ "$kb_choice" -ge 1 ]] && [[ "$kb_choice" -le "${#kb_names[@]}" ]]; then
_kb_layout="${kb_names[$((kb_choice-1))]}"
elif [[ "$kb_choice" == "$(( ${#kb_names[@]} + 1 ))" ]]; then
read -rp " xkb layout name (see 'localectl list-x11-keymap-layouts'): " _kb_layout
if [[ ! "$_kb_layout" =~ ^[a-z][a-z0-9_-]*$ ]]; then
echo "Invalid layout name, using de"
_kb_layout="de"
fi
else
echo "Invalid choice, using de"
_kb_layout="de"
fi
# console.keyMap reuses this string; a handful of layouts spell their
# console keymap differently. Warn rather than silently install a console
# with no keymap.
case "$_kb_layout" in
gb) echo " Note: xkb 'gb' has no console keymap of that name — set console.keyMap = \"uk\" in hosts/common.nix." ;;
esac
fi
echo ""
echo " Profile: $FLAKE_TARGET"
echo " Device: $_device"
echo " Bootloader: $_bootloader"
echo " Dual boot: $_dual_boot"
echo " Plymouth theme: $_plymouth_theme"
echo " Desktops: $_desktops"
echo " Keyboard: $_kb_layout"
echo ""
read -rp "Proceed with these settings? [y/N]: " confirm
[[ "$confirm" =~ ^[Yy]$ ]] || { echo "Aborted."; exit 1; }
}
prompt_luks_password() {
local pw pw2
while true; do
read -rsp "Enter LUKS disk encryption password: " pw; echo
read -rsp "Confirm LUKS password: " pw2; echo
if [[ "$pw" == "$pw2" ]]; then
break
fi
echo "Passwords do not match, try again."
done
LUKS_PASSWORD="$pw"
}
cmd_fresh() {
local host=""
local bootloader=""
local dual_boot=""
local device=""
local plymouth_theme=""
local desktops=""
local kb_layout=""
local pw_file=""
while [[ $# -gt 0 ]]; do
case "$1" in
--remote)
host="$2"
shift 2
;;
--profile)
FLAKE_TARGET="$2"
shift 2
;;
--bootloader)
bootloader="$2"
shift 2
;;
--dual-boot)
dual_boot="true"
shift
;;
--no-dual-boot)
dual_boot="false"
shift
;;
--device)
device="$2"
shift 2
;;
--plymouth-theme)
plymouth_theme="$2"
shift 2
;;
--desktops)
desktops="$2"
shift 2
;;
--keyboard)
kb_layout="$2"
shift 2
;;
*)
if [[ ! "$1" =~ ^-- ]]; then
host="$1"
shift
else
echo "Unknown option: $1"
exit 1
fi
;;
esac
done
prompt_interactive_setup bootloader dual_boot device plymouth_theme desktops kb_layout
write_deploy_config "$device" "$bootloader" "$dual_boot" "$plymouth_theme" \
"$(detect_march "$host" || echo generic)" "$desktops" "$kb_layout"
if [[ -z "$host" ]]; then
# LOCAL: booted from live ISO
echo "WARNING: This will WIPE the local disk and install NixOS!"
read -rp "Are you absolutely sure? [y/N] " confirm
[[ "$confirm" =~ ^[Yy]$ ]] || { echo "Aborted."; exit 1; }
prompt_luks_password
pw_file="/tmp/luks.key"
echo -n "$LUKS_PASSWORD" > "$pw_file"
chmod 600 "$pw_file"
trap 'rm -f "$pw_file"' EXIT
echo "Running Disko (partitioning)..."
sudo --preserve-env=pw_file \
nix --extra-experimental-features "nix-command flakes" run github:nix-community/disko/latest -- \
--mode disko --flake "${FLAKE_DIR}#${FLAKE_TARGET}"
echo "Disko done. Running install..."
cmd_install --skip-setup
else
# REMOTE: build locally, deploy via nixos-anywhere
echo "Fresh install to ${host} via nixos-anywhere (will WIPE disk!)..."
read -rp "Are you sure? [y/N] " confirm
[[ "$confirm" =~ ^[Yy]$ ]] || { echo "Aborted."; exit 1; }
prompt_luks_password
pw_file="$(mktemp)"
echo -n "$LUKS_PASSWORD" > "$pw_file"
trap 'rm -f "$pw_file"' EXIT
nix --extra-experimental-features "nix-command flakes" run github:nix-community/nixos-anywhere -- \
--flake "${FLAKE_DIR}#${FLAKE_TARGET}" \
--target-host "$host" \
--disk-encryption-keys /tmp/luks.key "$pw_file" \
--generate-hardware-config nixos-generate-config \
"${FLAKE_DIR}/hosts/${FLAKE_TARGET}/hardware-configuration.nix"
fi
}
cmd_install() {
local bootloader=""
local dual_boot=""
local device=""
local plymouth_theme=""
local desktops=""
local kb_layout=""
local skip_setup=""
while [[ $# -gt 0 ]]; do
case "$1" in
--profile)
FLAKE_TARGET="$2"
shift 2
;;
--bootloader)
bootloader="$2"
shift 2
;;
--dual-boot)
dual_boot="true"
shift
;;
--no-dual-boot)
dual_boot="false"
shift
;;
--device)
device="$2"
shift 2
;;
--plymouth-theme)
plymouth_theme="$2"
shift 2
;;
--desktops)
desktops="$2"
shift 2
;;
--keyboard)
kb_layout="$2"
shift 2
;;
--skip-setup)
skip_setup="1"
shift
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
if [[ -z "$skip_setup" ]]; then
prompt_interactive_setup bootloader dual_boot device plymouth_theme desktops kb_layout
write_deploy_config "$device" "$bootloader" "$dual_boot" "$plymouth_theme" \
"$(detect_march || echo generic)" "$desktops" "$kb_layout"
fi
echo "Running nixos-install (disk must already be partitioned/mounted)..."
sudo nixos-install --flake "${FLAKE_DIR}#${FLAKE_TARGET}"
save_profile "$FLAKE_TARGET"
echo "Copying config repo to installed system..."
if ! mountpoint -q /mnt; then
sudo mount -o subvol=@root /dev/mapper/crypted /mnt
fi
if ! mountpoint -q /mnt/home; then
sudo mount -o subvol=@home /dev/mapper/crypted /mnt/home
fi
sudo mkdir -p "/mnt/home/${NIXOS_USER}"
sudo cp -r "${FLAKE_DIR}" "/mnt/home/${NIXOS_USER}/nixos-config"
sudo chown -R 1000:100 "/mnt/home/${NIXOS_USER}/nixos-config"
sudo ln -sfn "/home/${NIXOS_USER}/nixos-config" /mnt/etc/nixos
echo "Done! You can now reboot."
}
cmd_switch() {
local host=""
local offline=""
local profile_explicit=""
local use_nh=""
while [[ $# -gt 0 ]]; do
case "$1" in
--remote)
host="$2"
shift 2
;;
--profile)
FLAKE_TARGET="$2"
profile_explicit="1"
shift 2
;;
--offline)
offline="--offline"
shift
;;
--nh)
use_nh="1"
shift
;;
*)
if [[ ! "$1" =~ ^-- ]]; then
host="$1"
shift
else
echo "Unknown option: $1"
exit 1
fi
;;
esac
done
[[ -n "$profile_explicit" ]] && save_profile "$FLAKE_TARGET"
sync_deploy_march "$host"
if [[ -z "$host" ]]; then
ensure_symlink "$FLAKE_DIR"
echo "Rebuilding & switching local config (profile: ${FLAKE_TARGET})..."
sudo systemctl stop nixos-rebuild-switch-to-configuration.service 2>/dev/null || true
local success=false
if [[ -n "$use_nh" ]] && command -v nh &>/dev/null; then
if NIX_CONFIG="experimental-features = nix-command flakes" \
nh os switch "${FLAKE_DIR}" --hostname "${FLAKE_TARGET}" $offline; then
success=true
else
success=false
fi
else
if sudo -E HOME=/root NIX_CONFIG="experimental-features = nix-command flakes" \
nixos-rebuild switch --install-bootloader --flake "${FLAKE_DIR}#${FLAKE_TARGET}" $offline; then
success=true
else
success=false
fi
fi
if [[ "$success" == "true" ]]; then
echo "Build successful!"
_play_sound "$SUCCESS_SOUND"
else
echo "Build failed!"
_play_sound "$ERROR_SOUND"
exit 1
fi
else
echo "Rebuilding & switching config on ${host} (profile: ${FLAKE_TARGET})..."
if NIX_SSHOPTS="-o StrictHostKeyChecking=no" \
NIX_CONFIG="experimental-features = nix-command flakes" \
nixos-rebuild switch --flake "${FLAKE_DIR}#${FLAKE_TARGET}" \
--target-host "$host" \
--use-remote-sudo \
--show-trace $offline; then
echo "Build successful!"
_play_sound "$SUCCESS_SOUND"
else
echo "Build failed!"
_play_sound "$ERROR_SOUND"
exit 1
fi
fi
}
cmd_secrets_edit() {
SOPS_AGE_KEY_FILE="${SOPS_AGE_KEY_FILE:-$HOME/.config/sops/age/keys.txt}" \
sops "${FLAKE_DIR}/secrets/secrets.yaml"
}
cmd_secrets_rotate() {
SOPS_AGE_KEY_FILE="${SOPS_AGE_KEY_FILE:-$HOME/.config/sops/age/keys.txt}" \
sops updatekeys "${FLAKE_DIR}/secrets/secrets.yaml"
}
cmd_sops_setup() {
local key_file="$HOME/.config/sops/age/keys.txt"
local secrets_file="$FLAKE_DIR/secrets/secrets.yaml"
local sops_yaml="$FLAKE_DIR/.sops.yaml"
echo "=== SOPS Setup ==="
# 1. Generate user age key if missing
if [[ -f "$key_file" ]]; then
echo "Age key already exists: $key_file"
else
echo "Generating age key..."
mkdir -p "$(dirname "$key_file")"
age-keygen -o "$key_file"
chmod 600 "$key_file"
fi
# 2. Read user public key
local user_pub
user_pub=$(age-keygen -y "$key_file")
echo "User public key : $user_pub"
# 3. Get host public key from SSH host key
local host_pub=""
if [[ -f /etc/ssh/ssh_host_ed25519_key.pub ]]; then
host_pub=$(ssh-to-age < /etc/ssh/ssh_host_ed25519_key.pub)
echo "Host public key : $host_pub"
else
echo "Warning: /etc/ssh/ssh_host_ed25519_key.pub not found — host key skipped"
fi
# 4. Write .sops.yaml
if [[ -n "$host_pub" ]]; then
cat > "$sops_yaml" <<EOF
keys:
- &user ${user_pub}
- &host ${host_pub}
creation_rules:
- path_regex: secrets/.*\\.yaml\$
key_groups:
- age:
- *user
- *host
EOF
else
cat > "$sops_yaml" <<EOF
keys:
- &user ${user_pub}
creation_rules:
- path_regex: secrets/.*\\.yaml\$
key_groups:
- age:
- *user
EOF
fi
echo "Updated .sops.yaml"
# 5. Handle secrets.yaml
if [[ -f "$secrets_file" ]]; then
if SOPS_AGE_KEY_FILE="$key_file" sops -d "$secrets_file" &>/dev/null; then
echo "Existing secrets.yaml is valid — opening editor..."
else
echo "Cannot decrypt existing secrets.yaml (old keys) — recreating..."
rm -f "$secrets_file"
fi
fi
if [[ ! -f "$secrets_file" ]]; then
echo "Creating secrets.yaml — add your secrets in the editor that opens."
echo "See secrets/secrets.yaml.example for available keys."
echo ""
fi
SOPS_AGE_KEY_FILE="$key_file" sops "$secrets_file"
echo ""
echo "Done. Run 'upnix' to apply."
}
[[ $# -lt 1 ]] && usage
case "$1" in
fresh)
shift
cmd_fresh "$@"
;;
install)
shift
cmd_install "$@"
;;
switch|rebuild)
shift
cmd_switch "$@"
;;
secrets-edit)
cmd_secrets_edit
;;
secrets-rotate)
cmd_secrets_rotate
;;
sops-setup)
cmd_sops_setup
;;
*)
usage
;;
esac