-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap
More file actions
executable file
·211 lines (177 loc) · 6.34 KB
/
Copy pathbootstrap
File metadata and controls
executable file
·211 lines (177 loc) · 6.34 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
#!/usr/bin/env bash
#
# Set up an Arch machine from this repo. Every step is idempotent -- re-running
# is always safe, and each subcommand can be run on its own.
#
# ./bootstrap packages, tools, symlinks, repos (in that order)
# ./bootstrap --full ...and restore the full package snapshot too
# ./bootstrap packages essential packages (+ snapshot with --full)
# ./bootstrap save record installed packages back into packages/
set -euo pipefail
DOTFILES="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PKG_DIR="$DOTFILES/packages"
ESSENTIAL_FILE="$PKG_DIR/packages-essential.txt"
OFFICIAL_FILE="$PKG_DIR/packages-official.txt"
AUR_FILE="$PKG_DIR/packages-aur.txt"
# Kernel and base packages are owned by the system, not by this repo.
FILTER_REGEX='^(linux|linux-headers|linux-cachyos|linux-firmware|base|base-devel|mkinitcpio|grub|efibootmgr|systemd)'
FULL=0
AUR_HELPER=''
# All logging goes to stderr so command substitution stays clean.
log() { printf '\n\033[1;34m==>\033[0m \033[1m%s\033[0m\n' "$*" >&2; }
info() { printf ' %s\n' "$*" >&2; }
warn() { printf '\033[1;33m ! %s\033[0m\n' "$*" >&2; }
die() { printf '\033[1;31m x %s\033[0m\n' "$*" >&2; exit 1; }
usage() {
sed -n '3,9p' "$0" | sed 's/^# \?//'
cat <<'EOF'
Commands:
packages Install packages-essential.txt; with --full also restore the
packages-official.txt and packages-aur.txt snapshots.
tools tmux tpm, mise runtimes, claude-code, dokku client.
symlinks Run ./update-symlinks. Needs ruby, so run `tools` first.
repos gh auth login, then clone the ~/code projects.
save Snapshot installed packages into packages/*.txt.
all packages, tools, symlinks, repos. The default.
Flags:
--full Include the full package snapshot in `packages`.
-h Show this help.
EOF
}
# Strip comments and blank lines from a package list.
read_list() {
[ -f "$1" ] || die "missing package list: $1"
grep -vE '^[[:space:]]*(#|$)' "$1" || true
}
# Fresh Arch has no AUR helper, and neither yay nor paru is in the official
# repos -- so bootstrap paru from source. makepkg refuses to run as root, which
# is why this is never prefixed with sudo.
ensure_aur_helper() {
if [ -n "$AUR_HELPER" ]; then return; fi
if command -v paru >/dev/null 2>&1; then
AUR_HELPER=paru
elif command -v yay >/dev/null 2>&1; then
AUR_HELPER=yay
else
log "No AUR helper found -- building paru from the AUR"
sudo pacman -S --needed --noconfirm base-devel git
local tmp
tmp="$(mktemp -d)"
git clone --depth 1 https://aur.archlinux.org/paru.git "$tmp/paru"
(cd "$tmp/paru" && makepkg -si --noconfirm)
rm -rf "$tmp"
command -v paru >/dev/null 2>&1 || die "paru build failed"
AUR_HELPER=paru
fi
info "AUR helper: $AUR_HELPER"
}
cmd_packages() {
local pkgs=()
log "Installing essential packages"
mapfile -t pkgs < <(read_list "$ESSENTIAL_FILE")
[ ${#pkgs[@]} -gt 0 ] || die "$ESSENTIAL_FILE is empty"
sudo pacman -Syu --needed --noconfirm "${pkgs[@]}"
if [ "$FULL" -eq 0 ]; then
info "skipping full snapshot (pass --full to restore it)"
return
fi
log "Restoring official package snapshot"
mapfile -t pkgs < <(read_list "$OFFICIAL_FILE")
[ ${#pkgs[@]} -gt 0 ] && sudo pacman -S --needed --noconfirm "${pkgs[@]}"
log "Restoring AUR package snapshot"
ensure_aur_helper
mapfile -t pkgs < <(read_list "$AUR_FILE")
[ ${#pkgs[@]} -gt 0 ] && "$AUR_HELPER" -S --needed --noconfirm "${pkgs[@]}"
}
cmd_tools() {
log "Installing tools"
if [ -d "$HOME/.tmux/plugins/tpm" ]; then
info "tpm already installed"
else
git clone --depth 1 https://github.com/tmux-plugins/tpm "$HOME/.tmux/plugins/tpm"
fi
# Versions come from mise.toml, so this needs no arguments.
if command -v mise >/dev/null 2>&1; then
(cd "$DOTFILES" && mise install)
else
warn "mise not found -- run './bootstrap packages' first"
fi
if command -v npm >/dev/null 2>&1; then
npm install -g @anthropic-ai/claude-code
else
warn "npm not found -- mise did not provide node, skipping claude-code"
fi
# Not on PATH: bin/dokku is the client that actually gets used. Kept because
# the alias that pointed here was dropped, not because this became unused.
# https rather than ssh so this works before any keys are on the machine.
if [ -d "$HOME/.dokku" ]; then
info "dokku client already cloned"
else
git clone --depth 1 https://github.com/dokku/dokku.git "$HOME/.dokku"
fi
# vimrc picks its background from this file's existence.
touch "$HOME/.darkmode"
}
cmd_symlinks() {
log "Linking dotfiles"
command -v ruby >/dev/null 2>&1 \
|| die "update-symlinks is a ruby script and ruby isn't installed -- run './bootstrap tools' first"
"$DOTFILES/update-symlinks"
}
cmd_repos() {
log "Cloning projects"
command -v gh >/dev/null 2>&1 \
|| die "github-cli not installed -- run './bootstrap packages' first"
if gh auth status >/dev/null 2>&1; then
info "gh already authenticated"
else
gh auth login
fi
mkdir -p "$HOME/code"
local repo
for repo in budgie measurehub meet_the_danes; do
if [ -d "$HOME/code/$repo" ]; then
info "$repo already cloned"
else
gh repo clone "thejspr/$repo" "$HOME/code/$repo"
fi
done
}
cmd_save() {
log "Saving installed packages"
pacman -Qqen | { grep -vE "$FILTER_REGEX" || true; } | sort >"$OFFICIAL_FILE"
pacman -Qqem | { grep -vE "$FILTER_REGEX" || true; } | sort >"$AUR_FILE"
info "official: $(wc -l <"$OFFICIAL_FILE")"
info "AUR: $(wc -l <"$AUR_FILE")"
info "essentials: hand-maintained, left untouched"
}
main() {
local cmds=()
while [ $# -gt 0 ]; do
case "$1" in
--full) FULL=1 ;;
-h|--help) usage; exit 0 ;;
packages|tools|symlinks|repos|save|all) cmds+=("$1") ;;
*) die "unknown argument: $1 (try --help)" ;;
esac
shift
done
if [ ${#cmds[@]} -eq 0 ]; then
cmds=(all)
fi
command -v pacman >/dev/null 2>&1 || die "this bootstrap is Arch-only (no pacman found)"
[ "$(id -u)" -ne 0 ] || die "don't run this as root -- it sudos only where needed"
local cmd
for cmd in "${cmds[@]}"; do
case "$cmd" in
all) cmd_packages; cmd_tools; cmd_symlinks; cmd_repos ;;
packages) cmd_packages ;;
tools) cmd_tools ;;
symlinks) cmd_symlinks ;;
repos) cmd_repos ;;
save) cmd_save ;;
esac
done
log "Done"
}
main "$@"