-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·453 lines (393 loc) · 15 KB
/
install.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env bash
# A cross platform installation script for basic setup including tools and other
# configurations.
# TODO
# Make a runner user for Arch to test AUR package installs - https://blog.ganssle.io/articles/2019/12/gitlab-ci-arch-pkg.html
# Make the install() function more DRY - reusable approach to passing different options for OSes
# Caching for packages
set -eou pipefail
ALPINE_TOOLS="yq docker python3-dev py3-pip fd colordiff ca-certificates openssl ncurses coreutils python2 make gcc g++ libgcc linux-headers grep util-linux binutils findutils libressl-dev openssl-dev musl-dev libffi-dev rust cargo sudo zsh libstdc++ direnv bat pass shfmt"
ARCH_TOOLS="python-pip fd go unzip base-devel fakeroot sudo bat shfmt"
COMMON_TOOLS="git jq shellcheck fzf ripgrep yamllint highlight pandoc zip exa vim curl wget zoxide"
DEBIAN_TOOLS="fd-find colordiff python3-pip ondir build-essential locales"
LINUX_TOOLS="pass tmux zsh"
NODE_TOOLS="bash-language-server fixjson"
PY_TOOLS="ansible ansible-lint pylint flake8 bashate pre-commit isort virtualenvwrapper commitizen"
ARCH_EXTRAS="docker ondir-git hadolint-bin colordiff yq direnv-bin bat bat-extras \
kubectl kubectx kube-linter k9s helm krew-bin \
tfenv tgenv terraform-ls tfsec-bin tflint-bin"
# DEBIAN_EXTRAS="terraform-ls kubectx yq docker hadolint bat direnv"
NVM_VERSION="v0.39.0"
COMPOSE_VERSION="v2.0.1"
set_env() {
CI=${CI:-false}
# Set options for running in CI
if [[ $CI = true ]]; then
if grep ID=ubuntu /etc/os-release; then
# Ubuntu runs on VM as non root user
sudo="sudo"
else
# No sudo in containers
sudo=""
fi
# github runner path
RUNNER_PATH="$HOME/work/configs/configs"
else
sudo="sudo"
fi
}
set_env_paths() {
if [[ ${REMOTE_CONTAINERS-} ]] || [[ ${CODESPACES-} ]]; then
# Set the home dir to our remote containers path
INSTALLER_PATH="$HOME/github.com/configs"
elif [[ ${CODESPACES-} ]]; then
# Set the home dir to our codespaces path
INSTALLER_PATH="/workspaces/.codespaces/.persistedshare/dotfiles"
elif [[ ${CI-} ]]; then
# Set the home dir to custom path if we're running in CI
INSTALLER_PATH="${RUNNER_PATH}"
else
# Set the default dir to home
INSTALLER_PATH="${HOME}"
fi
mkdir -p "$HOME/.config"
}
_alpine() {
$sudo apk update --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing
if [[ $UPDATE ]]; then
$sudo apk upgrade --available --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing
return
fi
touch "$HOME/.bashrc"
install_cmd="apk add --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing"
echo "Installing tools: $COMMON_TOOLS $ALPINE_TOOLS"
$sudo $install_cmd $COMMON_TOOLS $ALPINE_TOOLS
if [[ -z ${REMOTE_CONTAINERS-} ]] || [[ -z ${CODESPACES-} ]]; then
echo "Installing Python tools: $PY_TOOLS"
pip install wheel
pip install $PY_TOOLS
fi
}
_arch() {
# Assume we have yay install if we're trying to update
if [[ $UPDATE ]]; then
yay -Syu --noconfirm
return
fi
$sudo pacman -Syu --noconfirm
echo "Installing tools: $COMMON_TOOLS $LINUX_TOOLS $ARCH_TOOLS"
$sudo pacman -S --needed --noconfirm $COMMON_TOOLS $LINUX_TOOLS $ARCH_TOOLS
# Skip yay install for now if we are running as the root user (CI)
if [[ $EUID != 0 ]]; then
echo "Installing extras: $ARCH_EXTRAS"
yay_cmd="yay -S --needed --noconfirm"
if ! yay -V &>/dev/null; then install_yay; fi
# Update package list
$yay_cmd $ARCH_EXTRAS
fi
echo "Installing Python tools: $PY_TOOLS"
pip install $PY_TOOLS
}
_bsd() {
echo
}
_fonts() {
if [[ ! -d $HOME/.local/share/fonts ]]; then
$sudo apt install fontconfig
cd ~
wget https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/Meslo.zip
mkdir -p .local/share/fonts
unzip Meslo.zip -d .local/share/fonts
cd .local/share/fonts
rm *Windows*
cd ~
rm Meslo.zip
fc-cache -fv
fi
}
_debian() {
$sudo apt update -y
if [[ $UPDATE ]]; then
$sudo apt upgrade
return
fi
install_cmd="$sudo apt install -y"
echo "Installing tools: $COMMON_TOOLS $LINUX_TOOLS $DEBIAN_TOOLS"
$install_cmd $COMMON_TOOLS $LINUX_TOOLS $DEBIAN_TOOLS
echo "Installing Python tools: $PY_TOOLS"
pip install $PY_TOOLS
# Install fonts for extra glyphs
_fonts
# Set the default locale otherwise the installer stops to configure it
$sudo sh -c "echo \"en_US.UTF-8 UTF-8\" >> /etc/locale.gen"
$sudo locale-gen
}
_gentoo() {
echo
}
_macos() {
brew analytics off
brew update
if [[ $UPDATE ]]; then
brew upgrade
return
fi
echo "Installing tools from Brewfile"
brew bundle install
brew bundle install
# AWS CLI
if ! command -v aws &>/dev/null; then
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /
fi
# vim key repeating settings https://vimforvscode.com/enable-key-repeat-vim
defaults write -g ApplePressAndHoldEnabled -bool false
}
_nix() {
# Link configs
rm -rf ~/.zshrc && ln -s ~/configs/.zshrc ~/.zshrc
rm -rf ~/.config/starship.toml && ln -s ~/configs/config/starship/starship.toml ~/.config/starship.toml
# Vim
curl -fLo "$HOME/.vim/autoload/plug.vim" --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
rm -rf ~/.vimrc && ln -s ~/configs/.vimrc ~/.vimrc
# OSX
if [[ "$(uname -s)" = "Darwin" ]]; then
# This step needs sudo
if ! command -v nix-build; then
# TODO: run the original script to update the config file needed to map /run dir
#curl -L https://nixos.org/nix/install | sh -s -- --daemon --darwin-use-unencrypted-nix-store-volume
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sudo sh -s -- install
fi
if [ -e '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh' ]; then
set +u
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
set -u
fi
mkdir -p ~/.nixpkgs && rm -rf ~/.nixpkgs/darwin-configuration.nix && ln -s ~/configs/nix/darwin-configuration.nix ~/.nixpkgs/darwin-configuration.nix
nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer
if ! command -v darwin-rebuild; then
./result/bin/darwin-installer
exec zsh
fi
# Either move or chown config files created by nix installer
sudo mv /etc/bashrc /etc/bashrc.old
sudo mv /etc/zshrc /etc/zshrc.old
sudo chown $(id -un) /etc/nix/nix.conf
darwin-rebuild switch
exec zsh
else
# NIXOS
ln -s ~/configs/nix/configuration.nix ~/.nixpkgs/configuration.nix
nixos-rebuild switch
fi
# Extra channels
nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager
nix-channel --add https://nixos.org/channels/nixpkgs-unstable unstable
nix-channel --update
sudo nix-channel --update
# Home manager
nix-shell '<home-manager>' -A install
# mkdir -p ~/.config/nixpkgs && ln -s ~/configs/nix/home.nix ~/.config/nixpkgs/home.nix
mkdir -p ~/.config/home-manager && rm -rf ~/.config/home-manager/home.nix && ln -s ~/configs/nix/home.nix ~/.config/home-manager/home.nix
home-manager switch
}
_ubuntu() {
$sudo apt update -y
if [[ $UPDATE ]]; then
$sudo apt upgrade
return
fi
install_cmd="$sudo apt install -y --ignore-missing"
echo "Installing tools: ${COMMON_TOOLS//exa/} $LINUX_TOOLS $DEBIAN_TOOLS"
$install_cmd ${COMMON_TOOLS//exa/} $LINUX_TOOLS $DEBIAN_TOOLS
echo "Installing Python tools: $PY_TOOLS"
pip install $PY_TOOLS
# Install fonts for extra glyphs
_fonts
}
install() {
set_env
mkdir -p "$HOME/.config"
mkdir -p "$HOME/.nvm"
if grep ID=arch /etc/os-release >/dev/null 2>&1; then
_arch
elif grep ID=debian /etc/os-release >/dev/null 2>&1; then
_debian
elif grep ID=ubuntu /etc/os-release >/dev/null 2>&1; then
_ubuntu
elif grep ID=pop /etc/os-release >/dev/null 2>&1; then
_ubuntu
elif grep ID=alpine /etc/os-release >/dev/null 2>&1; then
_alpine
elif [[ "$(uname -s)" = "Darwin" ]]; then
echo "Sudo password is required for installing rosetta and homebrew"
sudo softwareupdate --install-rosetta --agree-to-license
PATH=$PATH:/opt/homebrew/bin
if ! command -v brew &>/dev/null; then
NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
fi
_macos
#_nix
# No need to run custom installs for below Linux systems
return
else
echo "Unkown OS"
exit 0
fi
# Install starship across all systems
curl -sS https://starship.rs/install.sh | sh -s -- -y
if [[ -z ${REMOTE_CONTAINERS-} ]] || [[ -z ${CODESPACES-} ]]; then
install_nvm
install_awscli
echo "Finished installing packages and tools"
fi
}
### Non-packaged tools
install_yay() {
git clone https://aur.archlinux.org/yay-bin.git
pushd yay-bin
makepkg -si
popd
rm -rf yay-bin
}
install_awscli() {
if ! aws --version &>/dev/null; then
echo "Installing AWS CLI"
# Grab the newest version by default
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip -qq awscliv2.zip
$sudo ./aws/install
rm -rf aws*
fi
}
install_nvm() {
if [[ ! -f $HOME/.nvm/nvm.sh ]]; then
echo "Installing NVM"
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/${NVM_VERSION}/install.sh | bash
. "$HOME/.nvm/nvm.sh"
# We need to do some shenanigans to override the function to retrieve
# the correct architecture and point to a musl version of the nodejs
# binary when we are using Alpine
if grep ID=alpine /etc/os-release; then
nvm_get_arch() { nvm_echo "x64-musl"; }
export NVM_NODEJS_ORG_MIRROR=https://unofficial-builds.nodejs.org/download/release
fi
nvm install --lts
nvm alias default stable
# shellcheck disable=SC2086
npm install -g $NODE_TOOLS
fi
}
install_docker_compose() {
# https://docs.docker.com/compose/cli-command/#installing-compose-v2
mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose
}
configure() {
# git pull
echo "Configuring environment"
# Create extra directories if they don't exist
mkdir -p "$HOME/.terragrunt/plugins"
mkdir -p "$HOME/.aws"
mkdir -p "$HOME/.ssh"
mkdir -p "$HOME/git"
mkdir -p "$HOME/hack"
mkdir -p "$HOME/tmp"
set_env_paths
# oh-my-zsh
if [[ ! -d $"$HOME/.oh-my-zsh" ]]; then
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
fi
ZSH_PATH=${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}
rm -rf "${ZSH_PATH}/plugins/zsh-you-should-use" || true
git clone --depth=1 https://github.com/MichaelAquilina/zsh-you-should-use "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"/plugins/zsh-you-should-use || true
rm -rf "${ZSH_PATH}/plugins/zsh-nvm" || true
git clone --depth=1 https://github.com/lukechilds/zsh-nvm "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"/plugins/zsh-nvm || true
rm -rf "${ZSH_PATH}/plugins/zsh-syntax-highlighting" || true
git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"/plugins/zsh-syntax-highlighting || true
rm -rf "${ZSH_PATH}/plugins/zsh-autosuggestions" || true
git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"/plugins/zsh-autosuggestions || true
# Link configs
rm -rf "$HOME/.zshrc" || true && ln -s "$INSTALLER_PATH/configs/.zshrc" "$HOME/.zshrc"
rm -rf "$HOME/.vimrc" || true && ln -s "$INSTALLER_PATH/configs/.vimrc" "$HOME/.vimrc"
rm -rf "$HOME/.tmux.conf" || true && ln -s "$INSTALLER_PATH/configs/.tmux.conf" "$HOME/.tmux.conf"
rm -rf "$HOME/.gitconfig" || true && ln -s "$INSTALLER_PATH/configs/.gitconfig" "$HOME/.gitconfig"
rm -rf "$HOME/.ssh/config" || true && ln -s "$INSTALLER_PATH/configs/config/ssh" "$HOME/.ssh/config"
rm -rf "$HOME/.config/starship.toml" || true && ln -s "$INSTALLER_PATH/configs/config/starship/starship.toml" "$HOME/.config/starship.toml"
# i3/wayland configurations
# kitty configuration
# Disgusting hack to get node installed and available for vim to bootstrap
NVM_DIR="/tmp/.nvm"
git clone https://github.com/nvm-sh/nvm.git "$NVM_DIR" || true
LATEST_TAG="$(builtin cd "$NVM_DIR" && git fetch --quiet --tags origin && git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1))"
builtin cd "$NVM_DIR" && git checkout --quiet "${LATEST_TAG}"
source "$NVM_DIR/nvm.sh"
nvm install node
export PATH=$PATH:$NVM_DIR/versions/node/v23.3.0/bin/node
echo "Configuring Vim"
if [[ ! -d $HOME/.vim/plugged ]]; then
# Vim 8.2+ tools/plugins + coc plugins
curl -fLo "$HOME/.vim/autoload/plug.vim" --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
mkdir -p "$HOME/.config/coc"
vim --not-a-term +PlugInstall +qall
vim --not-a-term +'CocInstall coc-json coc-sh coc-yaml coc-go coc-pyright coc-go coc-docker coc-markdownlint' +qall
else
vim --not-a-term +'PlugInstall --sync' +qall
fi
# Quick check if configs are linked
ls -lah "$HOME"
rm -rf /tmp/.nvm
echo "Open a new zsh shell to finish configuration."
}
switch_shell() {
if [[ ! $CI ]]; then
if [[ $SHELL != "/usr/bin/zsh" ]]; then
echo "Switching to zsh"
chsh -s "$(which zsh)"
fi
echo "Non CI environment detected, reloading shell"
exec zsh
fi
}
main() {
set +u
option=$1
set -u
UPDATE=""
# Handle automated installs with no args
if [[ $# -eq 0 ]]; then
install
configure
switch_shell
exit 0
fi
case $option in
--install)
install
;;
--configure)
configure
;;
--update)
UPDATE=true
install
;;
--all)
install
configure
# Change the shell as the last step because it is interactive
switch_shell
;;
--extras)
# TODO: install extras separately to speed up base installs
;;
*)
echo "'$option' not a recognized option"
exit 1
;;
esac
}
main "$@"
echo "Finished bootstrapping environment"