-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall
More file actions
executable file
·373 lines (322 loc) · 9.63 KB
/
Copy pathinstall
File metadata and controls
executable file
·373 lines (322 loc) · 9.63 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
#!/bin/sh
# Bootstrap this dotfiles checkout.
#
# With Nix available, this applies the Home Manager flake, which installs tools
# and links configs. Without Nix, it falls back to the legacy manifest symlinks
# and prints the official Nix install command for the next run.
set -eu
REPO_ROOT="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
MANIFEST="$REPO_ROOT/MANIFEST"
NVIM_SOURCE="$REPO_ROOT/external/nvim"
NVIM_TARGET="${HOME:-}/.config/nvim"
OVERWRITE_FROM_HOME=0
FORCE=0
usage() {
printf 'Usage: %s [--overwrite-from-home] [--force]\n' "$0" >&2
}
for arg in "$@"; do
case "$arg" in
--overwrite-from-home) OVERWRITE_FROM_HOME=1 ;;
--force) FORCE=1 ;;
-h|--help)
usage
exit 0
;;
*)
printf '%s: unknown option %s\n' "$0" "$arg" >&2
usage
exit 1
;;
esac
done
if [ ! -f "$MANIFEST" ]; then
printf '%s: missing MANIFEST at %s\n' "$0" "$MANIFEST" >&2
exit 1
fi
if [ -z "${HOME:-}" ]; then
printf '%s: HOME is not set\n' "$0" >&2
exit 1
fi
backup_existing_path() {
_path=$1
_bak="${_path}.pre-dotfiles-$(date +%Y%m%d-%H%M%S).bak"
mv "$_path" "$_bak"
printf 'backed up: %s -> %s\n' "$_path" "$_bak"
}
resolve_symlink_chain() {
_path=$1
_remaining=32
while [ -L "$_path" ]; do
if [ "$_remaining" -eq 0 ]; then
return 1
fi
_link=$(readlink "$_path") || return 1
case "$_link" in
/*) _path=$_link ;;
*) _path="$(dirname "$_path")/$_link" ;;
esac
_remaining=$((_remaining - 1))
done
printf '%s\n' "$_path"
}
# $1 repo relative path, $2 path relative to $HOME (no leading slash)
link_one() {
_repo_rel=$1
_target_rel=$2
_repo_file="$REPO_ROOT/$_repo_rel"
_target="$HOME/$_target_rel"
mkdir -p "$(dirname "$_repo_file")"
# Bootstrap: copy from home into repo when repo file is missing
if [ ! -e "$_repo_file" ]; then
if [ -f "$_target" ] || [ -L "$_target" ]; then
if [ -L "$_target" ]; then
cp -pL "$_target" "$_repo_file" 2>/dev/null || cp -p "$_target" "$_repo_file"
else
cp -p "$_target" "$_repo_file"
fi
printf 'copied into repo: %s (from %s)\n' "$_repo_rel" "$_target"
elif [ ! -e "$_target" ]; then
printf '%s: error: neither repo file nor home file exists:\n %s\n %s\n' "$0" "$_repo_file" "$_target" >&2
return 1
else
printf '%s: error: cannot bootstrap %s from unsupported type at %s\n' "$0" "$_repo_file" "$_target" >&2
return 1
fi
fi
# Already linked to this repo
if [ -L "$_target" ]; then
_link_dest=$(readlink "$_target" || true)
_resolved_target=$(resolve_symlink_chain "$_target" || true)
_resolved_repo=$(resolve_symlink_chain "$_repo_file" || true)
if [ "$_link_dest" = "$_repo_file" ] || {
[ -n "$_resolved_target" ] && [ "$_resolved_target" = "$_resolved_repo" ]
}; then
printf 'unchanged (already linked): %s\n' "$_target"
return 0
fi
if [ "$FORCE" -eq 1 ]; then
rm -f "$_target"
printf 'removed foreign symlink (--force): %s -> %s\n' "$_target" "$_link_dest"
else
printf '%s: error: %s is a symlink to %s, not %s. Use --force to replace.\n' "$0" "$_target" "$_link_dest" "$_repo_file" >&2
return 1
fi
fi
# Replace regular file with symlink
if [ -e "$_target" ]; then
if [ ! -f "$_target" ]; then
printf '%s: error: %s exists and is not a regular file\n' "$0" "$_target" >&2
return 1
fi
if ! cmp -s "$_target" "$_repo_file" 2>/dev/null; then
if [ "$OVERWRITE_FROM_HOME" -eq 1 ]; then
cp -p "$_target" "$_repo_file"
printf 'overwrote repo from home (--overwrite-from-home): %s\n' "$_repo_rel"
elif [ "$FORCE" -eq 1 ]; then
printf 'keeping repo version (--force): %s\n' "$_repo_rel"
else
printf '%s: error: %s differs from %s\n' "$0" "$_target" "$_repo_file" >&2
printf ' Run with --overwrite-from-home to copy home -> repo, or --force to keep the repo version.\n' >&2
return 1
fi
fi
backup_existing_path "$_target"
fi
mkdir -p "$(dirname "$_target")"
ln -sf "$_repo_file" "$_target"
printf 'linked: %s -> %s\n' "$_target" "$_repo_file"
}
link_manifest() {
_status=0
while IFS="$(printf '\t')" read -r repo_rel target_rel || [ -n "${repo_rel:-}" ]; do
case "${repo_rel:-}" in
''|'#'*) continue ;;
esac
if ! link_one "$repo_rel" "$target_rel"; then
_status=1
fi
done < "$MANIFEST"
return "$_status"
}
init_submodules() {
if [ ! -f "$REPO_ROOT/.gitmodules" ]; then
return 0
fi
if ! command -v git >/dev/null 2>&1; then
printf '%s: error: git is required to initialize submodules\n' "$0" >&2
return 1
fi
git -C "$REPO_ROOT" submodule update --init --recursive
}
known_nvim_remote() {
_remote=$1
case "$_remote" in
git@github.com:connorcarpenter15/nvim.git) return 0 ;;
git@github.com:connorcarpenter15/nvim) return 0 ;;
https://github.com/connorcarpenter15/nvim.git) return 0 ;;
https://github.com/connorcarpenter15/nvim) return 0 ;;
../nvim.git) return 0 ;;
*) return 1 ;;
esac
}
prepare_nvim_target() {
if [ ! -d "$NVIM_SOURCE" ]; then
printf '%s: error: missing Neovim submodule at %s\n' "$0" "$NVIM_SOURCE" >&2
printf ' Run: git submodule update --init --recursive\n' >&2
return 1
fi
if [ ! -e "$NVIM_TARGET" ] && [ ! -L "$NVIM_TARGET" ]; then
return 0
fi
if [ -L "$NVIM_TARGET" ]; then
_link_dest=$(readlink "$NVIM_TARGET" || true)
if [ "$_link_dest" = "$NVIM_SOURCE" ]; then
printf 'unchanged (already linked): %s\n' "$NVIM_TARGET"
return 0
fi
if [ "$FORCE" -eq 1 ]; then
rm -f "$NVIM_TARGET"
printf 'removed foreign symlink (--force): %s -> %s\n' "$NVIM_TARGET" "$_link_dest"
return 0
fi
printf '%s: error: %s is a symlink to %s, not %s. Use --force to replace.\n' "$0" "$NVIM_TARGET" "$_link_dest" "$NVIM_SOURCE" >&2
return 1
fi
if [ -d "$NVIM_TARGET" ]; then
if command -v git >/dev/null 2>&1 && git -C "$NVIM_TARGET" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
_remote=$(git -C "$NVIM_TARGET" remote get-url origin 2>/dev/null || true)
if known_nvim_remote "$_remote"; then
_dirty=$(git -C "$NVIM_TARGET" status --porcelain 2>/dev/null || true)
if [ -n "$_dirty" ] && [ "$FORCE" -ne 1 ]; then
printf '%s: error: %s has uncommitted changes; refusing to replace it with the dotfiles submodule.\n' "$0" "$NVIM_TARGET" >&2
printf ' Commit/stash those changes, move the directory aside, or run with --force to back it up first.\n' >&2
return 1
fi
backup_existing_path "$NVIM_TARGET"
return 0
fi
fi
if [ "$FORCE" -eq 1 ]; then
backup_existing_path "$NVIM_TARGET"
return 0
fi
printf '%s: error: %s already exists and is not the expected Neovim repo. Use --force to back it up and replace it.\n' "$0" "$NVIM_TARGET" >&2
return 1
fi
if [ "$FORCE" -eq 1 ]; then
backup_existing_path "$NVIM_TARGET"
return 0
fi
printf '%s: error: %s already exists and cannot be replaced safely. Use --force to back it up and replace it.\n' "$0" "$NVIM_TARGET" >&2
return 1
}
link_nvim_fallback() {
if [ -L "$NVIM_TARGET" ]; then
_link_dest=$(readlink "$NVIM_TARGET" || true)
if [ "$_link_dest" = "$NVIM_SOURCE" ]; then
return 0
fi
fi
mkdir -p "$(dirname "$NVIM_TARGET")"
ln -s "$NVIM_SOURCE" "$NVIM_TARGET"
printf 'linked: %s -> %s\n' "$NVIM_TARGET" "$NVIM_SOURCE"
}
detect_nix_system() {
_arch=$(uname -m 2>/dev/null || true)
_os=$(uname -s 2>/dev/null || true)
case "$_arch" in
arm64|aarch64) _cpu=aarch64 ;;
x86_64|amd64) _cpu=x86_64 ;;
*)
printf '%s: error: unsupported CPU architecture: %s\n' "$0" "$_arch" >&2
return 1
;;
esac
case "$_os" in
Darwin) _platform=darwin ;;
Linux) _platform=linux ;;
*)
printf '%s: error: unsupported operating system: %s\n' "$0" "$_os" >&2
return 1
;;
esac
printf '%s-%s\n' "$_cpu" "$_platform"
}
find_nix() {
if command -v nix >/dev/null 2>&1; then
command -v nix
return 0
fi
for nix_bin in \
/nix/var/nix/profiles/default/bin/nix \
"$HOME/.nix-profile/bin/nix"
do
if [ -x "$nix_bin" ]; then
printf '%s\n' "$nix_bin"
return 0
fi
done
return 1
}
run_home_manager() {
_nix_bin=$1
_system=$(detect_nix_system) || return 1
_run_user=${USER:-$(id -un 2>/dev/null || printf dotfiles)}
_nix_config='extra-experimental-features = nix-command flakes'
if [ -n "${NIX_CONFIG:-}" ]; then
_nix_config="${NIX_CONFIG}
${_nix_config}"
fi
printf 'applying Home Manager configuration: %s\n' "$_system"
DOTFILES_DIR=$REPO_ROOT USER=$_run_user HOME=$HOME NIX_CONFIG=$_nix_config \
"$_nix_bin" --extra-experimental-features nix-command \
--extra-experimental-features flakes \
run "$REPO_ROOT#home-manager" -- \
switch --flake "$REPO_ROOT#$_system" --impure
}
print_nix_install_hint() {
printf '\nNix is not installed, so only dotfile symlinks were applied.\n' >&2
printf 'Install Nix, open a new shell, then rerun ./install to install packages with Home Manager.\n\n' >&2
printf 'Recommended installer:\n' >&2
printf " curl --proto '=https' --tlsv1.2 -L https://nixos.org/nix/install | sh -s -- --daemon\n\n" >&2
printf 'On Linux hosts where you cannot use the daemon installer:\n' >&2
printf " curl --proto '=https' --tlsv1.2 -L https://nixos.org/nix/install | sh -s -- --no-daemon\n\n" >&2
}
install_ghostty_terminfo() {
if [ -x "$REPO_ROOT/bin/install-ghostty-terminfo" ]; then
"$REPO_ROOT/bin/install-ghostty-terminfo"
else
printf 'warning: skipping Ghostty terminfo install (missing helper)\n' >&2
return 0
fi
}
status=0
if ! init_submodules; then
status=1
fi
if ! link_manifest; then
status=1
fi
if ! prepare_nvim_target; then
status=1
fi
if NIX_BIN=$(find_nix); then
if [ "$status" -eq 0 ]; then
if ! run_home_manager "$NIX_BIN"; then
status=1
fi
else
printf '%s: skipping Home Manager because earlier setup checks failed\n' "$0" >&2
fi
else
print_nix_install_hint
if [ "$status" -eq 0 ]; then
if ! link_nvim_fallback; then
status=1
fi
fi
fi
if ! install_ghostty_terminfo; then
status=1
fi
exit "$status"