-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·302 lines (238 loc) · 8.65 KB
/
setup.sh
File metadata and controls
executable file
·302 lines (238 loc) · 8.65 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
#! /usr/bin/env bash
# shellcheck disable=SC2155
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
readonly yellow='\e[0;33m'
readonly green='\e[0;32m'
readonly red='\e[0;31m'
readonly reset='\e[0m'
readonly dotfiles='https://github.com/martaver/dotfiles'
readonly LocalHostName="$(scutil --get LocalHostName)"
readonly cmPath="$HOME/.local/share/chezmoi"
readonly nixDarwinDir="$cmPath/nix-darwin"
# Usage: log MESSAGE
#
# Prints all arguments on the standard output stream
log() {
printf "${yellow}>> %s${reset}\n" "${*}"
}
# Usage: success MESSAGE
#
# Prints all arguments on the standard output stream
success() {
printf "${green} %s${reset}\n" "${*}"
}
# Usage: error MESSAGE
#
# Prints all arguments on the standard error stream
error() {
printf "${red}!!! %s${reset}\n" "${*}" 1>&2
}
# Usage: die MESSAGE
# Prints the specified error message and exits with an error status
die() {
error "${*}"
exit 1
}
tryInstall() {
local name=${1}
local executable=${2}
log "'${name}' not detected. Installing..."
${executable}
success "'${name}' successfully installed!"
}
# Usage: checkDep NAME CONDITION IFNOTEXISTS IFEXISTS?
checkDep() {
local name=${1}
local condition=${2}
local executable=${3}
local ifexists=${4:-log "Skipping..."}
if ! ${condition} -p &>/dev/null; then
tryInstall "${name}" "${executable}"
else
log "'${name}' detected. Already installed..."
${ifexists}
fi
}
chomp() {
printf "%s" "${1/"$'\n'"/}"
}
# Source: https://github.com/Homebrew/install/blob/master/install.sh#L846
installXcodeCommandLineTools() {
log "Searching online for the Command Line Tools"
# This temporary file prompts the 'softwareupdate' utility to list the Command Line Tools
clt_placeholder="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
/usr/bin/sudo /usr/bin/touch "${clt_placeholder}"
clt_label_command="/usr/sbin/softwareupdate -l |
grep -B 1 -E 'Command Line Tools' |
awk -F'*' '/^ *\\*/ {print \$2}' |
sed -e 's/^ *Label: //' -e 's/^ *//' |
sort -V |
tail -n1"
clt_label="$(chomp "$(/bin/bash -c "${clt_label_command}")")"
if [[ -n "${clt_label}" ]]; then
log "Installing ${clt_label}"
/usr/bin/sudo "/usr/sbin/softwareupdate" "-i" "${clt_label}"
/usr/bin/sudo "/usr/bin/xcode-select" "--switch" "/Library/Developer/CommandLineTools"
fi
/usr/bin/sudo "/bin/rm" "-f" "${clt_placeholder}"
}
installNix() {
log "Running (determinate) nix installer (distro: Determinate)..."
# Use Determinate Nix Installer: https://github.com/DeterminateSystems/nix-installer
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm --determinate
success "Nix installed successfully"
log 'Configuring nix environment...'
set +o errexit
set +o nounset
set +o pipefail
# shellcheck disable=SC1091
# Load nix cmds into the current shell, so that we don't have to open a new one
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
set -o errexit
set -o nounset
set -o pipefail
nix --version
log 'Configured nix environment successfully'
}
bootstrapNixDarwin() {
log "Bootstrapping nix-darwin with 'default' configuration..."
{
sudo chown $(id -nu):$(id -ng) "$nixDarwinDir"
# Backup existing /etc files, they'll get replaced during the first switch
log "Backing up /etc/* files..."
[ ! -f /etc/zshenv ] || sudo mv /etc/zshenv /etc/zshenv.bak
[ ! -f /etc/zshrc ] || sudo mv /etc/zshrc /etc/zshrc.bak
[ ! -f /etc/zprofile ] || sudo mv /etc/zprofile /etc/zprofile.bak
# To use flake.nix and default configuration stored in chezmoi dotfiles repo:
log "Running darwin-rebuild switch..."
sudo DEFAULT_USER="$USER" nix run nix-darwin/master#darwin-rebuild -- switch --impure --flake "$nixDarwinDir#default"
log "Configuring nix-darwin environment..."
set +o errexit
set +o nounset
set +o pipefail
# shellcheck disable=SC1091
source /etc/static/bashrc
set -o errexit
set -o nounset
set -o pipefail
success "Bootstrapped nix-darwin successfully"
} || {
error "Bootstrapping nix-darwin failed, restoring /etc/* files"
[ ! -f /etc/zshenv.bak ] || sudo mv /etc/zshenv.bak /etc/zshenv
[ ! -f /etc/zshrc.bak ] || sudo mv /etc/zshrc.bak /etc/zshrc
[ ! -f /etc/zprofile.bak ] || sudo mv /etc/zprofile.bak /etc/zprofile
}
}
applyNixDarwin() {
log "Applying (flake) nix-darwin configuration for user ($USER)..."
sudo DEFAULT_USER="$USER" darwin-rebuild switch --impure --flake "$nixDarwinDir#default"
}
installBrew() {
log "Configuring shell profiles for brew..."
# shellcheck disable=SC2016
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >>"${HOME}/.bash_profile"
# shellcheck disable=SC2016
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >>"${HOME}/.zprofile"
eval "$(/opt/homebrew/bin/brew shellenv)"
log "Downloading install script and running non-interactively..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
log "Configuring brew environment..."
set +o errexit
set +o nounset
set +o pipefail
# shellcheck disable=SC1091
source "${HOME}/.bash_profile"
set -o errexit
set -o nounset
set -o pipefail
brew --version
log "Configured brew environment successfully"
}
# check Touch ID is configured
# configure sudo with Touch ID
setupSudoTouchID() {
if [ ! -f /etc/pam.d/sudo_local ];
then
sed "s/^#auth/auth/" /etc/pam.d/sudo_local.template | sudo tee /etc/pam.d/sudo_local
fi
}
# Verify every account described in contexts/*/account.json is configured in
# the 1Password CLI. Lists missing accounts and opens the 1Password app so the
# user can add them, then halts so they can re-run setup.sh.
check1PasswordAccounts() {
log "Checking 1Password accounts..."
local configured
configured=$(op account list --format=json 2>/dev/null || echo '[]')
local missing=()
for accountFile in "$cmPath"/contexts/*/account.json; do
[[ -f "$accountFile" ]] || continue
local address email host
address=$(jq -r '.address' "$accountFile")
email=$(jq -r '.email' "$accountFile")
host="${address#https://}"
host="${host%/}"
if ! echo "$configured" | jq -e --arg url "$host" --arg email "$email" \
'any(.[]; .url == $url and .email == $email)' >/dev/null; then
missing+=("$host ($email)")
fi
done
if (( ${#missing[@]} > 0 )); then
error "The following 1Password accounts still need to be added:"
printf ' - %s\n' "${missing[@]}" 1>&2
log "In 1Password, also configure Settings > Developer:"
log " - Integrate with 1Password CLI"
log " - Set up the SSH Agent"
log "Launching 1Password — complete the steps above, then re-run setup.sh"
open -a 1Password
exit 1
fi
success "All 1Password accounts configured"
}
# install 1password
# install 1password-cli
# check to ensure 1password ssh-agent is set up
# ssh-agent -l
# check $SSH_AUTH_SOCK
# xcode is needed for building most software from source
checkDep 'xcode-cli-tools' '/usr/bin/xcode-select' 'installXcodeCommandLineTools'
# brew is needed for installing GUI applications (casks)
checkDep 'brew' 'command -v brew' 'installBrew'
# rosetta is needed for running x86_64 applications
checkDep 'rosetta' '/usr/bin/pgrep oahd' 'sudo softwareupdate --install-rosetta --agree-to-license'
# install docker
# ./install-docker.sh
# nix is needed to configure the entire system
checkDep 'nix' 'command -v nix' 'installNix'
# init chezmoi from dotfiles repo, to load nix, nix-darwin and home-manager configuration
if [[ ! -d "$cmPath" ]]; then
log "Initialising dotfiles..."
nix shell nixpkgs#chezmoi -c chezmoi init "${dotfiles}"
else
log "Fetching dotfiles..."
nix shell nixpkgs#chezmoi -c chezmoi git -- reset --hard
nix shell nixpkgs#chezmoi -c chezmoi git pull "${dotfiles}"
fi
# install base tools required to work with env locally
brew bundle --file="$cmPath/Brewfile"
cd "$cmPath"
# if transcrypt has not yet been initialised for the repo, do it now.
if ! git config --local --get transcrypt.version &>/dev/null; then
log "Transcrypt not initialised. Enter passphrase to decrypt encrypted files."
read -rs -p "Passphrase: " transcryptPassphrase
echo
transcrypt -c aes-256-cbc -p "$transcryptPassphrase" -y
unset transcryptPassphrase
success "Transcrypt initialised"
else
log "Transcrypt already initialised. Skipping..."
fi
# check 1password accounts are set up
check1PasswordAccounts
code "$cmPath"
# # nix-darwin is what actually does the configuration
# checkDep 'nix-darwin' 'command -v darwin-rebuild' 'bootstrapNixDarwin' 'applyNixDarwin'
# log "Applying dotfiles..."
# nix shell nixpkgs#chezmoi -c chezmoi update
# success 'Done!'