-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-macos.sh
executable file
·253 lines (215 loc) · 5.97 KB
/
setup-macos.sh
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
#!/usr/bin/env bash
gpg_primary_key=0x2B7340DB13C85766
gpg_encryption_subkey=0x79C70BBE4865D828
base_path=$PWD
data_path="$base_path"/data
git_submodule_path="$base_path"/.git_submodules
exit_setup() {
rv=$?
printf "\n\nExiting setup...\n"
exit $rv
}
set -euo pipefail
trap 'exit_setup' ERR INT
symlink() {
if [[ -e "${2}" ]]; then
if [[ ! -L "${2}" ]]; then
echo "${2} already exists and is an irregular type. Check manually whether this is safe to replace with ${1}."
return 1
fi
echo "Removing existing symlink at ${2}"
rmrf "${2}" || {
echo "failed to remove old symlink: ${2}"
return 1
}
fi
# Stow only supports relative symlinks
ln -s "${1}" "${2}" || {
echo "failed to symlink: ${1} to ${2}"
return 1
}
}
rmrf() {
rm -rf "${1}" || {
echo "failed to recursively remove ${1}"
return 1
}
}
declare -a brew_pkgs=(
bat
font-hack-nerd-font
gnupg
go
helix
mpv
neovim
node
npm
pinentry-mac
python
ruby
rust
stow
theseal/ssh-askpass/ssh-askpass
wget
ykman
yubikey-personalization
)
brew update || {
echo "failed to update homebrew"
}
brew install "${brew_pkgs[@]}" || {
echo "failed to install brew packages"
}
git submodule update --init --recursive --remote --progress || {
echo "failed to update git submodules"
exit 1
}
git submodule foreach --recursive git clean -xfd || {
echo "failed to clean git submodules"
exit 1
}
git submodule foreach --recursive git reset --hard || {
echo "failed to reset git submodules"
exit 1
}
echo "Setting up GPG/SSH"
gpg --list-keys >/dev/null
declare -a mk_dirs=(
~/.cargo/
~/.config/
~/.continue/
~/.local/bin/
~/.local/share/
~/.ssh/
~/Library/KeyBindings/
~/Library/LaunchAgents/
)
for mk_dir in "${mk_dirs[@]}"; do
mkdir -p "${mk_dir}"
done
declare -a conflict_paths=(
~/.bashrc
~/.continue/config.json
~/.gnupg/common.conf
~/.zshenv
~/.zshrc
)
rm_if_not_stowed() {
if [[ -L "${1}" ]]; then
local symlink_path
symlink_path=$(readlink -f "${1}")
if [[ $symlink_path == *"${base_path}"* ]]; then
return 0
fi
fi
rm -rfv "${1}" || {
echo "failed to remove conflict path ${1}"
return 1
}
}
echo "Checking for files/directories that will conflict with stow"
for conflict_path in "${conflict_paths[@]}"; do
rm_if_not_stowed "${conflict_path}"
done
echo "Setting up stow"
stow -t ~/ stow || {
echo "failed to setup stow"
exit 1
}
echo "Appending custom pinentry script to gpg-agent.conf"
# GNUPG is ridiculous and only allows env-vars in some of the options here, so we have to do this the convoluted way with a line append
cp -v "$data_path"/gpg/gpg-agent.conf "$base_path"/gpg/.gnupg/gpg-agent.conf || {
echo "failed to copy gpg-agent.conf from data dir"
exit 1
}
echo "pinentry-program $HOME/.local/bin/pinentry-auto" | tee -a "$base_path"/gpg/.gnupg/gpg-agent.conf
stow_config() {
stow -v "$1" || {
echo "Failed to stow ${1} config"
exit 1
}
}
declare -a stow_dirs_setup=(
bash
git
gpg
macos
ssh
zsh
)
echo "Stowing setup configs"
for stow_dir in "${stow_dirs_setup[@]}"; do
stow_config "$stow_dir"
done
declare -a launch_agents=(
"$HOME"/Library/LaunchAgents/gnupg.gpg-agent.plist
"$HOME"/Library/LaunchAgents/gnupg.gpg-agent-symlink.plist
)
for launch_agent_dir in "${launch_agents[@]}"; do
launchctl unload "$launch_agent_dir"
launchctl load "$launch_agent_dir" || {
echo "failed to load $launch_agent_dir"
exit 1
}
echo "Loaded $launch_agent_dir"
done
gpg-connect-agent reloadagent /bye || {
echo "failed to reload gpg-agent"
exit 1
}
# If our primary GPG key is not yet imported, import it
if [[ ! $(gpg --list-keys "$gpg_primary_key") ]]; then
gpg --import "$data_path"/gpg/2B7340DB13C85766.asc || {
echo "failed to import GPG pubkey"
exit 1
}
gpg --tofu-policy good "$gpg_primary_key" || {
echo "failed to set gpg tofu policy"
exit 1
}
fi
echo "Decrypting data"
declare -a decrypt_data_paths_tuples=(
"${data_path}/ssh/config.asc.gpg ${base_path}/ssh/.ssh/config"
)
for decrypt_data_paths_tuple in "${decrypt_data_paths_tuples[@]}"; do
read -ra decrypt_data_paths <<<"$decrypt_data_paths_tuple"
if [[ -f "${decrypt_data_paths[0]}" ]]; then
gpg --quiet --no-verbose --local-user "${gpg_encryption_subkey}" --armor --decrypt --yes --output "${decrypt_data_paths[1]}" "${decrypt_data_paths[0]}" >/dev/null || {
echo "failed to decrypt file ${decrypt_data_paths[0]} to ${decrypt_data_paths[1]}"
exit 1
}
fi
done
# Fix for spacing breaking the space delimited tuples
mv "${git_submodule_path}"/catppuccin-bat/themes/Catppuccin\ Mocha.tmTheme "${git_submodule_path}"/catppuccin-bat/themes/Catppuccin-Mocha.tmTheme || {
echo "failed to move catppuccin-bat theme"
exit 1
}
declare -a symlink_paths_tuples=(
"${git_submodule_path}/alacritty-theme/themes ${base_path}/alacritty/.config/alacritty/themes"
"${git_submodule_path}/catppuccin-bat/themes/Catppuccin-Mocha.tmTheme ${base_path}/bat/.config/bat/themes/Catppuccin-Mocha.tmTheme"
"${git_submodule_path}/catppuccin-helix/themes/default/catppuccin_mocha.toml ${base_path}/helix/.config/helix/themes/catppuccin_mocha.toml"
)
for symlink_paths_tuple in "${symlink_paths_tuples[@]}"; do
read -ra symlink_paths <<<"$symlink_paths_tuple"
symlink "${symlink_paths[0]}" "${symlink_paths[1]}"
done
declare -a stow_dirs_general=(
alacritty
bat
continue
helix
nvim
omz
rust
)
echo "Stowing general configs"
for stow_dir in "${stow_dirs_general[@]}"; do
stow_config "$stow_dir"
done
defaults write com.apple.finder AppleShowAllFiles -boolean true || {
echo "failed to enable hidden files in Finder"
exit 1
}