Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·968 lines (848 loc) · 33.8 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·968 lines (848 loc) · 33.8 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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
#!/bin/bash
#
# dotfiles setup script
# KH
#
set -euo pipefail
DOTS_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# shellcheck source=lib/dotfiles-setup-data.bash
source "${DOTS_HOME}/lib/dotfiles-setup-data.bash"
# The prism checkout, reached the same way bin/prism reaches it: resolved via
# the tasks project registry (`dotfiles_project_root`), falling back to `~/d`.
PRISM_ROOT="$(dotfiles_project_root prism)"
# The wali checkout, reached the same way: bin/walictl execs into it and the
# systemd and Noctalia-plugin phases link out of it.
WALI_ROOT="$(dotfiles_project_root wali)"
RESOURCE_ALERT_ROOT="$(dotfiles_project_root pressure)"
# The ops checkout, reached the same way: the obs-index, git-index-retention and
# git-index-health systemd user units link out of it.
OPS_ROOT="$(dotfiles_project_root ops)"
LOOKOUT_ROOT="$(dotfiles_project_root look)"
# The hostkit checkout, reached the same way: hostkit-links converges manifests and units.
HOSTKIT_ROOT="$(dotfiles_project_root hostkit)"
FAMILIAR_ROOT="$(dotfiles_project_root fam)"
# Parse command line arguments
HEADLESS=false
UBUNTU=false
MACOS=false
DRY_RUN=false
LINK_ONLY=false
SKIP_PACKAGES=false
SKIP_EXTERNAL_CLONES=false
ENABLE_USER_TIMERS=false
ONLY_PHASES=()
CHECK_ONLY=false
COMPLETED_PHASES=()
FAILED_PHASES=()
VALID_PHASES=(
preflight
external-clones
shell
graphical-config
common-config
systemd
kitty
lookout
app-config
noctalia-plugins
mime
dropbox-ignore
tmux
packages
)
function is_valid_phase() {
local candidate="$1"
local phase_name
for phase_name in "${VALID_PHASES[@]}"; do
[[ "$phase_name" == "$candidate" ]] && return 0
done
return 1
}
function print_valid_phases() {
printf 'Valid phases: %s\n' "${VALID_PHASES[*]}"
}
while [[ $# -gt 0 ]]; do
case $1 in
--headless)
HEADLESS=true
shift
;;
--ubuntu|-u)
UBUNTU=true
shift
;;
--macos|-m)
MACOS=true
shift
;;
--check)
CHECK_ONLY=true
ONLY_PHASES=(preflight)
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
--link-only)
LINK_ONLY=true
SKIP_PACKAGES=true
SKIP_EXTERNAL_CLONES=true
shift
;;
--no-packages)
SKIP_PACKAGES=true
shift
;;
--no-external-clones)
SKIP_EXTERNAL_CLONES=true
shift
;;
--enable-user-timers)
ENABLE_USER_TIMERS=true
shift
;;
--only)
if [[ $# -lt 2 ]]; then
echo "--only requires a comma-separated phase list"
print_valid_phases
exit 1
fi
IFS=',' read -r -a _requested_phases <<< "$2"
for phase_name in "${_requested_phases[@]}"; do
if [[ -z "$phase_name" ]] || ! is_valid_phase "$phase_name"; then
echo "Unknown setup phase: $phase_name"
print_valid_phases
exit 1
fi
ONLY_PHASES+=("$phase_name")
done
unset _requested_phases phase_name
shift 2
;;
--help)
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " --headless Install only non-graphical components"
echo " -u, --ubuntu Configure for Ubuntu (default: Arch Linux)"
echo " -m, --macos Configure for macOS with Homebrew"
echo " --check Report unmet per-machine prerequisites and exit; changes nothing"
echo " --dry-run Print planned filesystem and command actions without running them"
echo " --link-only Link dotfiles only; skip package installation and external clones"
echo " --no-packages Skip package installation"
echo " --no-external-clones"
echo " Skip cloning external tools such as zinit and tmux TPM"
echo " --enable-user-timers"
echo " Enable linked systemd user timers on Linux"
echo " --only PHASES Run only comma-separated setup phases"
print_valid_phases
echo " noctalia-plugins links the plugins in every run; enabling them needs a running Noctalia v5"
echo " --help Show this help message"
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# macOS has no Wayland/X11, so skip graphical configs
if [[ "$MACOS" == "true" ]]; then
HEADLESS=true
fi
echo "Installing dotfiles relative to: $DOTS_HOME..."
echo "Headless mode: $HEADLESS"
echo "Ubuntu mode: $UBUNTU"
echo "macOS mode: $MACOS"
echo "Dry run: $DRY_RUN"
echo "Link only: $LINK_ONLY"
echo "Skip packages: $SKIP_PACKAGES"
echo "Skip external clones: $SKIP_EXTERNAL_CLONES"
echo "Enable user timers: $ENABLE_USER_TIMERS"
if [[ "${#ONLY_PHASES[@]}" -gt 0 ]]; then
echo "Only phases: ${ONLY_PHASES[*]}"
fi
# Define package lists based on distribution
if [[ "$MACOS" == "true" ]]; then
PACKAGES=("bat" "coreutils" "dust" "fd" "figlet" "fzf" "btop"
"lolcat" "lsd" "neovim" "ripgrep" "thefuck" "tldr"
"tmux" "tre-command" "visidata" "zoxide")
FONT_PACKAGES=("font-hack-nerd-font" "font-symbols-only-nerd-font")
PACKAGE_INSTALL_CMD="brew install"
PACKAGE_UPDATE_CMD="brew update"
elif [[ "$UBUNTU" == "true" ]]; then
PACKAGES=("bat" "btop" "fd-find" "fzf" "lsd" "ripgrep" "thefuck" "tldr" "visidata" "zoxide")
FONT_PACKAGES=("fonts-nerd-fonts" "fonts-weather-icons")
PACKAGE_INSTALL_CMD="sudo apt install -y"
PACKAGE_UPDATE_CMD="sudo apt update"
else
# just and shellcheck are this repo's own gate dependencies: without them a
# machine set up by setup.sh cannot run `just check`.
PACKAGES=("bat" "dust" "fd" "fzf" "glow" "btop" "just" "lolcat" "lsd" "moor" "ripgrep" "sd" "shellcheck" "tre-command" "thefuck" "tldr" "visidata" "xan" "zoxide")
FONT_PACKAGES=("ttf-nerd-fonts-symbols" "ttf-hack-nerd" "ttf-weather-icons")
PACKAGE_INSTALL_CMD="yay -S"
PACKAGE_UPDATE_CMD=""
fi
NOCTALIA_GENERATED=("${DOTFILES_NOCTALIA_GENERATED[@]}")
# Check for configuration directory
if [ -z "${XDG_CONFIG_HOME:-}" ]; then
XDG_CONFIG_HOME=$HOME/.config
fi
function describe_cmd() {
printf '%q ' "$@"
echo ""
}
function run() {
if [[ "$DRY_RUN" == "true" ]]; then
echo "[DRY-RUN] $(describe_cmd "$@")"
else
"$@"
fi
}
function ensure_dir() {
local dir="$1"
if [[ -d "$dir" ]]; then
return
fi
run mkdir -p "$dir"
}
function generate_zsh_completion() {
local command_name="$1"
local output="$2"
shift 2
command -v "$command_name" >/dev/null || return 0
if [[ "$DRY_RUN" == "true" ]]; then
echo "[DRY-RUN] $command_name $(describe_cmd "$@")> $output"
else
"$command_name" "$@" > "$output"
fi
}
function phase() {
echo "==> $1"
}
function should_run_phase() {
local requested="$1"
local phase_name
[[ "${#ONLY_PHASES[@]}" -eq 0 ]] && return 0
for phase_name in "${ONLY_PHASES[@]}"; do
[[ "$phase_name" == "$requested" ]] && return 0
done
return 1
}
# Checks for file or directory and creates a sym link if it doesn't already exist
function ln_s() {
# Existing symlinks are replaced; a real file/dir is moved aside to .bak so
# we never nest a link inside it (e.g. an app-generated ~/.config/kitty) or
# clobber real data.
local src="$1"
local dest="$2"
if [[ ! -e "$src" ]]; then
echo "Link source does not exist: $src"
exit 1
fi
if [[ -L "$dest" && "$(readlink "$dest")" == "$src" ]]; then
echo "[SKIPPING] \"$dest\" already points to \"$src\""
return
fi
ensure_dir "$(dirname "$dest")"
if [[ -e "$dest" && ! -L "$dest" && -e "${dest}.bak" ]]; then
echo "Refusing to overwrite existing backup: ${dest}.bak"
exit 1
fi
if [[ -L "$2" ]]; then
run rm "$dest"
elif [[ -e "$2" ]]; then
echo "[BACKUP] \"$dest\" -> \"$dest.bak\""
run mv "$dest" "$dest.bak"
fi
echo "[CREATING] \"$dest\""
run ln -sf "$src" "$dest"
}
function migrate_legacy_directory_symlinks() {
local lsd_config_dir="${XDG_CONFIG_HOME}/lsd"
local yazi_config_dir="${XDG_CONFIG_HOME}/yazi"
local legacy_yazi_dir="" path
if [[ -L "$lsd_config_dir" ]]; then
run rm "$lsd_config_dir"
ensure_dir "$lsd_config_dir"
fi
if [[ -L "$yazi_config_dir" ]]; then
legacy_yazi_dir="$(cd "$yazi_config_dir" && pwd -P)"
run rm "$yazi_config_dir"
ensure_dir "$yazi_config_dir"
if [[ -n "$legacy_yazi_dir" ]]; then
for path in theme.toml flavors; do
if [[ -e "${legacy_yazi_dir}/${path}" ]]; then
run mv "${legacy_yazi_dir}/${path}" "${yazi_config_dir}/${path}"
fi
done
fi
fi
}
function backup_manifest_conflicts() {
local manifest_path="$1"
local check_out status dest detail
if [[ "$DRY_RUN" == "true" && ! -f "${HOSTKIT_ROOT}/bin/hostkit-links" ]]; then
return 0
fi
check_out="$(python3 "${HOSTKIT_ROOT}/bin/hostkit-links" --check \
--manifest "$manifest_path" --root "${DOTS_HOME}" --home "${HOME}" 2>/dev/null || true)"
while IFS=$'\t' read -r status dest detail; do
[[ "$status" == "refuse" ]] || continue
case "$detail" in
"a real file is there"|"a real directory is there") ;;
*) continue ;;
esac
if [[ "$dest" == "~" ]]; then
dest="${HOME}"
elif [[ "$dest" == \~/* ]]; then
dest="${HOME}/${dest#\~/}"
fi
if [[ -e "${dest}.bak" || -L "${dest}.bak" ]]; then
echo "Refusing to overwrite existing backup: ${dest}.bak"
return 1
fi
echo "[BACKUP] \"$dest\" -> \"$dest.bak\""
run mv "$dest" "$dest.bak"
done <<<"$check_out"
}
function apply_manifest() {
local manifest_path="$1"
if [[ "$(basename "$manifest_path")" == "install.toml" ]]; then
migrate_legacy_directory_symlinks
fi
backup_manifest_conflicts "$manifest_path"
if [[ "$DRY_RUN" == "true" ]]; then
run python3 "${HOSTKIT_ROOT}/bin/hostkit-links" --apply \
--manifest "$manifest_path" --root "${DOTS_HOME}" --home "${HOME}"
return 0
fi
local output rc=0
output="$(python3 "${HOSTKIT_ROOT}/bin/hostkit-links" --apply \
--manifest "$manifest_path" --root "${DOTS_HOME}" --home "${HOME}" 2>&1)" || rc=$?
[[ -n "$output" ]] && printf '%s\n' "$output"
if [[ "$rc" -ne 0 || "$output" == $'skipped\t'* || "$output" == *$'\nskipped\t'* ]]; then
return 1
fi
}
# Function to install packages
function install_packages() {
if [[ "$SKIP_PACKAGES" == "true" ]]; then
echo "Skipping package installation..."
return
fi
local packages=("$@")
local distro_name
if [[ "$MACOS" == "true" ]]; then
distro_name="macOS (Homebrew)"
elif [[ "$UBUNTU" == "true" ]]; then
distro_name="Ubuntu"
else
distro_name="Arch"
fi
echo "$distro_name packages:"
printf '%s ' "${packages[@]}"
echo ""
printf '%s ' "${FONT_PACKAGES[@]}"
echo ""
while true; do
read -r -p "Install $distro_name packages? [Y/n] " input
case $input in
[yY][eE][sS]|[yY])
if [[ "$MACOS" == "true" ]]; then
echo "Installing macOS packages via Homebrew..."
run brew update
run brew install "${packages[@]}"
run brew install --cask "${FONT_PACKAGES[@]}"
elif [[ "$UBUNTU" == "true" ]]; then
echo "Installing Ubuntu packages..."
if [[ -n "$PACKAGE_UPDATE_CMD" ]]; then
# shellcheck disable=SC2086
run $PACKAGE_UPDATE_CMD
fi
# shellcheck disable=SC2086
run $PACKAGE_INSTALL_CMD "${packages[@]}"
# shellcheck disable=SC2086
run $PACKAGE_INSTALL_CMD "${FONT_PACKAGES[@]}"
else
if [[ ! -x "/usr/bin/yay" && ! -x "$HOME/.local/bin/yay" ]]; then
echo "Installing yay AUR helper..."
local cwd
cwd="$(pwd)"
run git clone https://aur.archlinux.org/yay.git /tmp/yay
if [[ "$DRY_RUN" != "true" ]]; then
cd /tmp/yay
run makepkg -si
cd "$cwd"
fi
echo "Finished installing yay..."
fi
echo "Installing Arch packages..."
# shellcheck disable=SC2086
run $PACKAGE_INSTALL_CMD "${packages[@]}"
# shellcheck disable=SC2086
run $PACKAGE_INSTALL_CMD "${FONT_PACKAGES[@]}"
fi
break
;;
[nN][oO]|[nN])
echo "Skipping $distro_name package installation..."
break
;;
*)
echo "Invalid input..."
;;
esac
done
}
function run_phase() {
local phase_name="$1"
local setup_function="$2"
local rc=0
should_run_phase "$phase_name" || return 0
# Each phase runs in its own subshell so a failure stops that phase at its
# first bad command without taking the rest of the run with it. errexit must
# be off in this shell for the subshell's own `set -e` to be honoured: bash
# suppresses errexit inside an `if` condition, and that suppression reaches
# into subshells.
set +e
( set -e; "$setup_function" )
rc=$?
set -e
if [[ "$rc" -eq 0 ]]; then
COMPLETED_PHASES+=("$phase_name")
else
FAILED_PHASES+=("$phase_name")
echo "Phase failed (exit ${rc}): ${phase_name}" >&2
fi
return 0
}
function report_phases() {
local phase_name
echo
echo "==> Summary"
for phase_name in "${COMPLETED_PHASES[@]}"; do
echo " ok ${phase_name}"
done
for phase_name in "${FAILED_PHASES[@]}"; do
echo " FAILED ${phase_name}"
done
if [[ "${#FAILED_PHASES[@]}" -gt 0 ]]; then
printf '%s of %s phase(s) failed: %s\n' \
"${#FAILED_PHASES[@]}" \
"$(( ${#COMPLETED_PHASES[@]} + ${#FAILED_PHASES[@]} ))" \
"${FAILED_PHASES[*]}" >&2
return 1
fi
return 0
}
# prism's node_modules is gitignored and carries com.dropbox.ignored, so it is
# per-machine and never arrives with a clone or a Dropbox sync. The
# graphical-config phase runs bin/prism, which needs the yaml package at
# runtime, so the bootstrap has to land before that phase.
function setup_prism_dependencies() {
local prism_root="${PRISM_ROOT}"
[[ -f "${prism_root}/package.json" ]] || return 0
if [[ -d "${prism_root}/node_modules" ]]; then
echo "[SKIPPING] prism dependencies already installed"
return
fi
if ! command -v npm >/dev/null; then
echo "npm not found; prism needs it before the graphical-config phase"
return 1
fi
echo "Installing prism Node dependencies..."
run npm ci --prefix "$prism_root"
# Mark the new tree now rather than waiting for the dropbox-ignore-flux
# timer, which is opt-in and may not be enabled on this machine yet.
if command -v attr >/dev/null; then
run attr -s com.dropbox.ignored -V 1 "${prism_root}/node_modules"
fi
}
# Every prerequisite below is per-machine: none of it is in git and none of it
# syncs, so each one used to surface as an opaque crash partway through a run,
# one at a time. Report them together and change nothing.
function preflight_check() {
local label="$1" fix="$2"
shift 2
if "$@" >/dev/null 2>&1; then
echo " ok ${label}"
return 0
fi
echo " MISSING ${label}"
echo " fix: ${fix}"
return 1
}
function setup_preflight() {
phase "Preflight"
local missing=0
local tasks_registry="${XDG_CONFIG_HOME}/tasks/projects.toml"
local prism_root="${PRISM_ROOT}"
local requirements line
if [[ -f "${prism_root}/package.json" ]]; then
preflight_check "npm" "install nodejs-npm" \
command -v npm || missing=1
preflight_check "prism node dependencies" "npm ci --prefix ${prism_root}" \
test -d "${prism_root}/node_modules" || missing=1
fi
# ln_s stops the systemd and plugin phases on a missing checkout; this names
# the fix up front, with the rest of the per-machine prerequisites.
preflight_check "wali checkout" "git clone git@github.com:khughitt/wali.git ${WALI_ROOT}" \
test -x "${WALI_ROOT}/bin/walictl" || missing=1
preflight_check "ops checkout" "git clone git@github.com:khughitt/ops.git ${OPS_ROOT}" \
test -x "${OPS_ROOT}/bin/obs-index" || missing=1
preflight_check "hostkit checkout" "ensure ${HOSTKIT_ROOT} is synced or cloned (see ops/docs/tooling.md)" \
test -x "${HOSTKIT_ROOT}/bin/hostkit-links" || missing=1
# Familiar's hooks run on every agent tool call and die on a missing
# node_modules. Name the missing local install before hooks crash.
# Guarded on the checkout the same way prism's dependencies are.
if [[ -f "${FAMILIAR_ROOT}/package.json" ]]; then
preflight_check "familiar node dependencies" "npm install --prefix ${FAMILIAR_ROOT}" \
test -d "${FAMILIAR_ROOT}/node_modules" || missing=1
fi
preflight_check "tasks binary" "install tasks and run 'tasks init' in each project" \
command -v tasks || missing=1
preflight_check "tasks registry" "run 'tasks init' in each project to populate ${tasks_registry}" \
test -s "$tasks_registry" || missing=1
if [[ "$HEADLESS" == "false" ]]; then
preflight_check "niri" "install niri" \
command -v niri || missing=1
preflight_check "noctalia" "install the noctalia-qs package" \
command -v noctalia || missing=1
# What each sink needs (quickshell for the debug backdrop, a niri that
# accepts the material node for glass) is declared in the sink's own
# manifest and evaluated by prism against this machine's resolved
# params, so nothing is restated here. Only when prism can run: the
# node_modules finding above already covers the other case.
if [[ -d "${prism_root}/node_modules" ]]; then
if requirements=$("${prism_root}/bin/prism" requirements 2>&1); then
echo " ok prism sink requirements"
else
echo " MISSING prism sink requirements"
while IFS= read -r line; do
echo " ${line#requirements: }"
done <<<"$requirements"
missing=1
fi
fi
# The phase links its plugins with Noctalia down and says what is left to
# do, so this reports the deferred work rather than a blocked run.
if should_run_phase noctalia-plugins; then
preflight_check "noctalia is running" \
"start noctalia; until then the plugins are linked but not enabled" \
noctalia msg plugins list || missing=1
fi
fi
if [[ "$missing" -gt 0 ]]; then
# A finding is a report, not a gate: the phase that actually needs the
# prerequisite fails on its own and is named in the summary. --check is
# the form that exists to be a gate, so only it turns findings into an
# exit code.
echo "Preflight found ${missing} unmet prerequisite(s); each is listed above with its fix." >&2
[[ "$CHECK_ONLY" == "true" ]] && return 1
fi
return 0
}
function setup_external_clones() {
phase "External clone setup"
if [[ "$SKIP_EXTERNAL_CLONES" == "true" ]]; then
echo "Skipping external clone setup..."
return
fi
ensure_dir "$HOME/.cache/zinit/completions"
local zinit_home="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"
if [[ ! -d "$zinit_home/.git" ]]; then
ensure_dir "$(dirname "$zinit_home")"
run git clone https://github.com/zdharma-continuum/zinit.git "$zinit_home"
fi
setup_prism_dependencies
}
function setup_shell_links() {
phase "Shell links"
apply_manifest "${DOTS_HOME}/install.toml"
local completion_dir="${XDG_DATA_HOME:-${HOME}/.local/share}/zsh/site-functions"
ensure_dir "$completion_dir"
generate_zsh_completion xan "${completion_dir}/_xan" completions zsh
generate_zsh_completion crush "${completion_dir}/_crush" completion zsh
generate_zsh_completion codex "${completion_dir}/_codex" completion zsh
}
function link_noctalia_generated() {
local entry tree_path state_path
for entry in "${NOCTALIA_GENERATED[@]}"; do
tree_path="${DOTS_HOME}/${entry%%:*}"
state_path="${XDG_STATE_HOME:-${HOME}/.local/state}/${entry##*:}"
ensure_dir "$(dirname "$state_path")"
ensure_dir "$(dirname "$tree_path")"
# Whatever this machine already generated is the right seed; a regular
# file here is the pre-migration layout, not something to discard.
if [[ ! -e "$state_path" && -f "$tree_path" && ! -L "$tree_path" ]]; then
run mv "$tree_path" "$state_path"
fi
[[ -e "$state_path" ]] || run touch "$state_path"
run ln -sfT "$state_path" "$tree_path"
done
}
# link_noctalia_generated leaves four empty files on a machine that has never had a
# theme, and nothing else fills them: on titan they stayed empty until a wallpaper
# rotation or a hand toggle happened to fire. `templates-apply` renders the current
# palette into every template output -- verified on 2026-09-09 to rewrite an output
# that was emptied and one holding wrong content -- so ask for it here, while setup is
# still running, and name it when Noctalia is not up to be asked.
function seed_noctalia_generated_output() {
local entry state_path unthemed=false
for entry in "${NOCTALIA_GENERATED[@]}"; do
state_path="${XDG_STATE_HOME:-${HOME}/.local/state}/${entry##*:}"
[[ -s "$state_path" ]] || unthemed=true
done
[[ "$unthemed" == "true" ]] || return 0
# Ask whether Noctalia is there before planning to talk to it: a dry run has to
# report which of these two it would do, and only the probe can tell it.
if ! noctalia msg theme-mode-get >/dev/null 2>&1; then
echo "Noctalia is not running; its generated theme output is still empty." \
"Run 'noctalia msg templates-apply' once it is up."
return 0
fi
run noctalia msg templates-apply
[[ "$DRY_RUN" != "true" ]] || return 0
for entry in "${NOCTALIA_GENERATED[@]}"; do
state_path="${XDG_STATE_HOME:-${HOME}/.local/state}/${entry##*:}"
[[ -s "$state_path" ]] || echo "Still empty after templates-apply: ${state_path}." \
"Noctalia renders the palette it has; set a wallpaper, then run 'noctalia msg templates-apply'."
done
}
function setup_graphical_config_links() {
[[ "$HEADLESS" == "false" ]] || return 0
phase "Graphical config links"
local wali_config
local prism_generated="${XDG_STATE_HOME:-${HOME}/.local/state}/prism/generated"
local niri_generated="${prism_generated}/prism.kdl"
local kitty_generated="${prism_generated}/kitty.conf"
local niri_generated_before=""
apply_manifest "${DOTS_HOME}/install-graphical.toml"
ensure_dir "${DOTS_HOME}/prism/$(hostname)/contexts"
ln_s "${DOTS_HOME}/prism/$(hostname)" "${XDG_CONFIG_HOME}/prism"
wali_config="${DOTS_HOME}/wali/$(hostname)/config.toml"
if [[ -f "$wali_config" ]]; then
ln_s "$wali_config" "${XDG_CONFIG_HOME}/wali/config.toml"
else
echo "No wali config for $(hostname); skipping ${XDG_CONFIG_HOME}/wali/config.toml"
fi
# prism apply validates the real niri config, which includes these links, so
# the links must already exist when the sinks run. Create their targets
# first: aborting anywhere in between then leaves an empty include rather
# than an unresolvable one, which niri refuses to load.
ensure_dir "$prism_generated"
[[ -e "$niri_generated" ]] || run touch "$niri_generated"
[[ -e "$kitty_generated" ]] || run touch "$kitty_generated"
run ln -sfT "$kitty_generated" "${DOTS_HOME}/kitty/prism-generated.conf"
run ln -sfT "$niri_generated" "${DOTS_HOME}/niri/prism.kdl"
run env NIRI_DIR="${DOTS_HOME}/niri" "${DOTS_HOME}/niri/host_specific.sh"
link_noctalia_generated
seed_noctalia_generated_output
if [[ "$DRY_RUN" != "true" ]]; then
niri_generated_before="$(stat -Lc '%d:%i' "$niri_generated" 2>/dev/null || true)"
fi
if ! run env NIRI_CONFIG="${DOTS_HOME}/niri/config.kdl" \
"${PRISM_ROOT}/bin/prism" apply niri
then
local niri_generated_after
niri_generated_after="$(stat -Lc '%d:%i' "$niri_generated" 2>/dev/null || true)"
if [[ -n "${NIRI_SOCKET:-}" || ! -s "$niri_generated" ||
-z "$niri_generated_after" || "$niri_generated_after" == "$niri_generated_before" ]]; then
return 1
fi
echo "Niri is not running; prism.kdl was generated and reload is deferred."
fi
# setup.sh links kitty at this sink's output, so it has to be applied too;
# nothing else ran it, and the link sat dangling on a fresh machine.
run "${PRISM_ROOT}/bin/prism" apply kitty
run "${PRISM_ROOT}/bin/prism" apply debug-backdrop
run niri validate -c "${DOTS_HOME}/niri/config.kdl"
if [[ -e "${XDG_CONFIG_HOME}/niri" && ! -L "${XDG_CONFIG_HOME}/niri" ]]; then
if [[ -e "${XDG_CONFIG_HOME}/niri.bak" ]]; then
echo "Refusing to overwrite existing ${XDG_CONFIG_HOME}/niri.bak"
exit 1
fi
run mv "${XDG_CONFIG_HOME}/niri" "${XDG_CONFIG_HOME}/niri.bak"
fi
ln_s "${DOTS_HOME}/niri" "${XDG_CONFIG_HOME}/niri"
if [[ -n "${NIRI_SOCKET:-}" ]]; then
run niri msg action load-config-file
else
echo "Niri is not running; the validated config will load on first start. Run 'prism apply niri' after Niri starts to reload and clear the failed sink status."
fi
}
function setup_common_config_links() {
phase "Common config links"
local fzf_theme
apply_manifest "${DOTS_HOME}/install.toml"
if [[ -f "${DOTS_HOME}/hosts/$(hostname)/budget.toml" ]]; then
ensure_dir "${XDG_CONFIG_HOME}/hostkit"
ln_s "${DOTS_HOME}/hosts/$(hostname)/budget.toml" "${XDG_CONFIG_HOME}/hostkit/budget.toml"
fi
fzf_theme="${XDG_CACHE_HOME:-${HOME}/.cache}/noctalia/fzf.conf"
ensure_dir "${fzf_theme%/*}"
[[ -e "$fzf_theme" ]] || run touch "$fzf_theme"
run "${DOTS_HOME}/bin/opencode-config-migrate" \
"${DOTS_HOME}/opencode" \
"${DOTFILES_OPENCODE_RUNTIME_SOURCE:-${DOTS_HOME}/opencode}" \
"${XDG_CONFIG_HOME}/opencode" \
"${XDG_CONFIG_HOME}/opencode.local" \
"${HOME}/.cache/noctalia/nvim-glass/current/opencode-theme.json"
}
function setup_systemd_user_units() {
[[ "$MACOS" != "true" ]] || return 0
phase "Systemd user units"
apply_manifest "${DOTS_HOME}/install-linux.toml"
if [[ "$ENABLE_USER_TIMERS" == "true" ]]; then
run systemctl --user daemon-reload
run systemctl --user enable --now dropbox-ignore-flux.timer
run systemctl --user enable --now work-link.timer
run systemctl --user enable --now kernel-gate-nudge.timer
run systemctl --user enable --now wali-rotate.timer
run systemctl --user enable --now obs-index.timer
fi
}
# The OS overrides themselves are selected by each host's kitty/<hostname>.conf,
# so nothing here writes into the tree.
function setup_kitty_overrides() {
[[ "$MACOS" == "true" ]] || return 0
phase "Kitty OS overrides"
# macOS ships no xterm-kitty in its terminfo db, so git/less/etc. warn the
# "terminal is not fully functional". Compile kitty's bundled entry into
# ~/.terminfo, which ncurses auto-searches. (Linux gets it from the
# kitty-terminfo package.)
local kitty_terminfo="/Applications/kitty.app/Contents/Resources/kitty/terminfo/kitty.terminfo"
if [[ -f "$kitty_terminfo" ]]; then
run tic -x -o "${HOME}/.terminfo" "$kitty_terminfo"
else
echo "kitty.app terminfo not found at ${kitty_terminfo}; install kitty, then rerun this phase"
fi
}
function setup_lookout() {
phase "Lookout"
if [[ ! -x "${LOOKOUT_ROOT}/bin/lookout" ]]; then
echo "Lookout checkout missing; clone it into ${LOOKOUT_ROOT}, then rerun setup.sh --only lookout"
return 1
fi
if [[ "$LINK_ONLY" != "true" ]]; then
run just --working-directory "$LOOKOUT_ROOT" setup
fi
ln_s "${LOOKOUT_ROOT}/bin/lookout" "${HOME}/.local/bin/lookout"
if [[ "$LINK_ONLY" != "true" ]]; then
run "${HOME}/.local/bin/lookout" --version
fi
}
function setup_application_config_links() {
phase "Application config links"
if [[ "$HEADLESS" == "false" ]]; then
apply_manifest "${DOTS_HOME}/install-graphical.toml"
fi
apply_manifest "${DOTS_HOME}/install.toml"
run python3 "${DOTS_HOME}/lib/git-packing-policy.py" write "$HOME" "$DOTS_HOME"
}
function setup_noctalia_plugins() {
[[ "$HEADLESS" == "false" ]] || return 0
phase "Noctalia local plugins"
local plugin_dir="${XDG_DATA_HOME:-${HOME}/.local/share}/noctalia/plugins"
ensure_dir "$plugin_dir"
ln_s "${WALI_ROOT}/integrations/noctalia-plugin" "${plugin_dir}/wali-panel"
ln_s "${PRISM_ROOT}/integrations/noctalia-plugin" "${plugin_dir}/prism"
ln_s "${RESOURCE_ALERT_ROOT}" "${plugin_dir}/resource-pressure-alert"
# The links are what dotfiles-health requires and they need nothing running, so
# every pass makes them. Only enabling talks to Noctalia; when it is not up, name
# the commands rather than leaving a phase that never ran in a full pass.
if ! noctalia msg theme-mode-get >/dev/null 2>&1; then
echo "Noctalia is not running; its plugins are linked but not enabled." \
"Run 'noctalia msg plugins enable khughitt/wali-panel' and" \
"'noctalia msg plugins enable khughitt/prism' and" \
"'noctalia msg plugins enable khughitt/resource-pressure-alert' once it is up."
return 0
fi
run noctalia msg plugins enable khughitt/wali-panel
run noctalia msg plugins enable khughitt/prism
run noctalia msg plugins enable khughitt/resource-pressure-alert
}
function setup_mime_links() {
[[ "$MACOS" != "true" ]] || return 0
phase "MIME links"
apply_manifest "${DOTS_HOME}/install-linux.toml"
if [[ "$LINK_ONLY" != "true" ]] && command -v update-mime-database >/dev/null; then
run update-mime-database "${HOME}/.local/share/mime"
fi
}
# The paths in DOTFILES_DROPBOX_IGNORED are per-machine state written into the
# shared tree. The xattr is itself per-machine and never syncs, so every machine
# runs this; marking a directory before the application's first write is what
# keeps that write local.
function setup_dropbox_ignores() {
[[ "$MACOS" != "true" ]] || return 0
phase "Dropbox ignores"
if ! command -v attr >/dev/null; then
echo "attr is required to mark per-machine paths com.dropbox.ignored"
return 1
fi
# Git replaces its index inode; native rules also ignore each replacement.
local rules_file="${HOME}/d/rules.dropboxignore" existing_rules=""
local git_rules=$'**/.git/index*\n**/.git/sharedindex*\n**/.git/next-index-*'
if [[ -d "${HOME}/d" ]]; then
if [[ -e "$rules_file" ]]; then
existing_rules=$(tail -n 3 -- "$rules_file")
fi
if [[ "$existing_rules" != "$git_rules" ]]; then
run bash -c 'printf "\n%s\n" "$2" >> "$1"' bash "$rules_file" "$git_rules"
fi
else
echo "[SKIPPING] Dropbox root not present: ${HOME}/d"
fi
local entry path
for entry in "${DOTFILES_DROPBOX_IGNORED[@]}"; do
path="${DOTS_HOME}/${entry%/}"
if [[ "$entry" == */ ]]; then
ensure_dir "$path"
elif [[ ! -e "$path" ]]; then
echo "[SKIPPING] not present yet: ${entry}"
continue
fi
if [[ "$(attr -q -g com.dropbox.ignored "$path" 2>/dev/null)" == "1" ]]; then
echo "[SKIPPING] already ignored: ${entry}"
continue
fi
run attr -s com.dropbox.ignored -V 1 "$path"
done
}
function setup_tmux_plugin_manager() {
phase "Tmux plugin manager"
if [[ "$SKIP_EXTERNAL_CLONES" == "true" ]]; then
return
fi
if [[ ! -d "${HOME}/.tmux/plugins/tpm/.git" ]]; then
ensure_dir "${HOME}/.tmux/plugins"
run git clone https://github.com/tmux-plugins/tpm "${HOME}/.tmux/plugins/tpm"
fi
}
function setup_package_installation() {
phase "Package installation"
install_packages "${PACKAGES[@]}"
}
echo "Setting up dotfiles..."
ensure_dir "$XDG_CONFIG_HOME"
run_phase preflight setup_preflight
run_phase external-clones setup_external_clones
run_phase shell setup_shell_links
run_phase graphical-config setup_graphical_config_links
run_phase common-config setup_common_config_links
run_phase systemd setup_systemd_user_units
run_phase kitty setup_kitty_overrides
run_phase lookout setup_lookout
run_phase app-config setup_application_config_links
run_phase noctalia-plugins setup_noctalia_plugins
run_phase mime setup_mime_links
run_phase dropbox-ignore setup_dropbox_ignores
run_phase tmux setup_tmux_plugin_manager
run_phase packages setup_package_installation
if ! report_phases; then
exit 1
fi
echo "Done!"