-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·383 lines (327 loc) · 10.2 KB
/
install.sh
File metadata and controls
executable file
·383 lines (327 loc) · 10.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
#!/bin/bash
cur_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
platform=$(uname)
install_xcode_command_line_tools() {
output=$(xcode-select --install 2>&1)
status=$?
if [ $status != 0 ]; then
echo "${output}"
case "${output}" in
*"already installed"*)
;;
*)
exit 1
;;
esac
fi
}
HOMEBREW_PREFIX=/opt/homebrew
ensure_sshd_config_option() {
local file="$1"
local key="$2"
local value="$3"
local desired="${key} ${value}"
local tmp
tmp="$(mktemp)"
local root_cmd=""
if [ ! -w "$file" ]; then
root_cmd="sudo"
fi
# Global section only (before first Match):
# - Replace first occurrence of (possibly commented) key with desired
# - Remove any other duplicate occurrences of key in global section
# - If not found globally, insert desired right before first Match (or EOF)
$root_cmd awk -v key="$key" -v desired="$desired" '
BEGIN {
found = 0
in_match = 0
keyre = "^[[:space:]]*#?[[:space:]]*" key "([[:space:]]+|$)"
}
/^[[:space:]]*Match([[:space:]]+|$)/ {
if (!found) {
print desired
found = 1
}
in_match = 1
print $0
next
}
{
if (!in_match && $0 ~ keyre) {
if (!found) {
print desired
found = 1
}
next
}
print $0
}
END {
if (!found) {
print desired
}
}
' "$file" > "$tmp" || { rm -f "$tmp"; return 1; }
$root_cmd dd if="$tmp" of="$file" bs=4096 conv=fsync status=none || { rm -f "$tmp"; return 1; }
rm -f "$tmp"
}
ensure_sshd_google_auth_settings() {
local file="$1"
ensure_sshd_config_option "$file" "UsePAM" "yes" || return 1
ensure_sshd_config_option "$file" "ChallengeResponseAuthentication" "yes" || return 1
ensure_sshd_config_option "$file" "PasswordAuthentication" "no" || return 1
}
ensure_pam_google_authenticator() {
local file="$1"
local module="/opt/homebrew/opt/google-authenticator-libpam/lib/security/pam_google_authenticator.so"
local line="auth required ${module}"
if [ ! -f "$module" ]; then
return 1
fi
if [ ! -f "$file" ]; then
return 1
fi
local root_cmd=""
if [ ! -w "$file" ]; then
root_cmd="sudo"
fi
if $root_cmd grep -Fq "pam_google_authenticator.so" "$file"; then
return 0
fi
local tmp
tmp="$(mktemp)"
$root_cmd awk -v newline="$line" '
NR == 1 {
if ($0 ~ /^#%PAM/) {
print $0
print newline
} else {
print newline
print $0
}
next
}
{ print $0 }
' "$file" > "$tmp" || { rm -f "$tmp"; return 1; }
$root_cmd dd if="$tmp" of="$file" bs=4096 conv=fsync status=none || { rm -f "$tmp"; return 1; }
rm -f "$tmp"
}
change_mac_settings() {
defaults write -g KeyRepeat -int 2
defaults write -g InitialKeyRepeat -int 9
defaults write -g ApplePressAndHoldEnabled -bool false
# defaults -currentHost delete -globalDomain NSStatusItemSpacing
# defaults -currentHost write -globalDomain NSStatusItemSpacing -int 8
# defaults -currentHost delete -globalDomain NSStatusItemSelectionPadding
# defaults -currentHost write -globalDomain NSStatusItemSelectionPadding -int 8
}
install_command_line_tools() {
# TODO: brew bundle --file=Brewfile
brew install wget
brew install llvm
brew install readline xz
brew install gnupg pinentry-mac
# brew install ctags
brew install fzf
$HOMEBREW_PREFIX/opt/fzf/install
brew install pyenv pyenv-virtualenv # pyenv-virtualenvwrapper
brew install tmux reattach-to-user-namespace
brew install coreutils findutils gnu-getopt
brew ln gnu-getopt --force
brew install node yarn # Nodejs and..
brew install watch
brew install go # Golang
brew install --cask swiftformat-for-xcode
brew install roswell # Common Lisp(sbcl) version manager
brew install rlwrap
brew install difftastic
brew install git git-lfs
brew install ripgrep fd bat jq
brew install tree
brew install htop iftop
brew install fortune
brew install direnv
brew install zoxide
brew install wireguard-tools
# /etc/ssh/sshd_config (managed by ensure_sshd_google_auth_settings)
brew install google-authenticator-libpam
ensure_sshd_google_auth_settings "/etc/ssh/sshd_config" || return 1
ensure_pam_google_authenticator "/etc/pam.d/sshd" || return 1
brew install colima docker docker-credential-helper # colima start --cpu 4 --memory 8
brew install docker-buildx
mkdir -p ~/.docker/cli-plugins && ln -sfn $(which docker-buildx) ~/.docker/cli-plugins/docker-buildx
docker buildx install
go install golang.org/x/perf/cmd/benchstat@latest
GLOBAL_PYTHON=3.11.0
# https://github.com/pyenv/pyenv/issues/1219
pyenv install --skip-existing -v $GLOBAL_PYTHON
pyenv global $GLOBAL_PYTHON
pyenv rehash
$(pyenv which pip) install ipython httpie
if ! command -v rustup &> /dev/null; then
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup toolchain add nightly
rustup component add rust-src
fi
# Tmux
# git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
}
link_configuration_files() {
echo "-> link configuration files .."
files=(\
bin \
gitconfig \
tmux.conf \
zshrc \
config \
)
for f in "${files[@]}"
do
target="$HOME/.$f"
if [[ -e "$target" && ! -L "$target" ]]; then
echo "backup: $target -> $target-backup"
mv "$target" "${target}-backup-$(date '+%Y%m%d%H%M%S')"
fi
ln -vsfn "$cur_dir/$f" "$target"
done
mkdir -vp "$HOME/.gnupg"
cp -rfv "$cur_dir/gnupg/" "$HOME/.gnupg"
# shellcheck source=/dev/null
source "$cur_dir/third_configs/install.sh"
}
link_llm_agent_rules() {
echo "-> link LLM agent rules .."
local src="$cur_dir/config/agents/AGENTS.md"
if [[ ! -f "$src" ]]; then
echo "missing: $src" >&2
return 1
fi
mkdir -vp "$HOME/.claude" "$HOME/.codex"
ln -vsfn "$src" "$HOME/.claude/CLAUDE.md"
ln -vsfn "$src" "$HOME/.codex/AGENTS.md"
}
setup_vim() {
echo "-> setup vim .."
brew install luajit
# brew install neovim
cargo install bob-nvim
bob install v0.11.5
bob install nightly
bob use nightly
# lint for bash_ls
brew install shellcheck
# Swift
brew install swiftformat
# https://github.com/wojciech-kulik/xcodebuild.nvim
brew install xcode-build-server
brew install xcbeautify
brew install ruby
gem install xcodeproj
# Python
pip install pynvim black
# Lua
cargo install stylua
# Go
go install golang.org/x/tools/cmd/goimports@latest
nvim --headless "+Lazy! sync" +qa
}
install_nerd_font() {
local name="$1"
local url="https://github.com/ryanoasis/nerd-fonts/releases/latest/download/${name}.zip"
local tmpdir
tmpdir=$(mktemp -d)
local zip="$tmpdir/${name}.zip"
local fontdir="$HOME/Library/Fonts"
curl -L -o "$zip" "$url"
unzip -q "$zip" -d "$tmpdir"
cp -f "${tmpdir}"/"${name}"NerdFont-*.ttf "${fontdir}/"
cp -f "${tmpdir}"/"${name}"NerdFontMono-*.ttf "${fontdir}/"
}
install_fonts() {
echo "-> install fonts .."
unzip config/_asserts/fonts.zip -d /tmp/dotfiles-fonts
mv "/tmp/dotfiles-fonts/fonts/Monaco Nerd Font Complete.ttf" "$HOME/Library/Fonts/"
rm -rf /tmp/dotfiles-fonts
install_nerd_font "JetBrainsMono"
}
init_mac(){
if [[ "$platform" != "Darwin" ]]; then # Mac
echo "unsupported platform $platform"
exit 1
fi
echo "-> initialize mac .."
install_xcode_command_line_tools
set -e
change_settings_for_mac
install_fonts
link_configuration_files
# https://docs.brew.sh/Installation
if ! command -v brew &> /dev/null; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brewzprofile="eval $($HOMEBREW_PREFIX/bin/brew shellenv)"
grep -qxF "$brewzprofile" ~/.zprofile || echo "$brewzprofile" >> ~/.zprofile
eval "$($HOMEBREW_PREFIX/bin/brew shellenv)"
fi
brew install zsh # Default on macOS
if [ "$SHELL" != "$(which zsh)" ]; then
grep -qxF "$(which zsh)" /etc/shells || echo "$(which zsh)" | sudo tee -a /etc/shells
chsh -s "$(which zsh)"
sudo chsh -s "$(which zsh)" # Install for root as well.
echo "PLEASE RESTART THE TERMINAL SESSION"
echo " .. and run this command again"
exit 0
else
antidote-update-and-force-compinit
fi
install_command_line_tools
setup_vim
}
usage() {
echo "Usage: $0 [-i] [-t] [-l] [-v] [-f] [-a]" >&2
echo " -i initialize a new mac" >&2
echo " -t install command line tools" >&2
echo " -l link configuration files" >&2
echo " -v setup vim" >&2
echo " -f install patched https://www.nerdfonts.com/" >&2
echo " -a link agent rules for Claude/Codex" >&2
exit 1
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
while getopts ":itlvfa" opt; do
case "${opt}" in
i )
noargs=1
init_mac
;;
t )
noargs=1
install_command_line_tools
;;
l )
noargs=1
link_configuration_files
;;
v )
noargs=1
setup_vim
;;
f )
noargs=1
install_fonts
;;
a )
noargs=1
link_llm_agent_rules
;;
* )
echo "invalid option -${opt}"
usage
;;
esac
done
if [ -z "${noargs}" ]; then
usage
else
exec "$SHELL"
fi
fi