-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.sh
executable file
·564 lines (457 loc) · 17.2 KB
/
bootstrap.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#!/usr/bin/env bash
set -Eeuo pipefail
#############
# CONSTANTS #
#############
orange='\e[0;33m'
nc='\e[0m'
red='\e[0;31m'
green='\e[0;32m'
purple='\e[0;35m'
grey='\e[0;37m'
bold='\e[1m'
underline='\e[4m'
####################
# HELPER FUNCTIONS #
####################
print_header()
{
local HEADER="$1"
printf "\n$purple$bold • $HEADER\n\n$nc"
}
print_subheader()
{
local SUBHEADER="$1"
printf "$purple ${underline}$SUBHEADER\n\n$nc"
}
execute()
{
local COMMAND="$1"
local MESSAGE="${2:-$1}"
local errfile="$(mktemp /tmp/bootstrap-XXXXXXXX.err)"
local exitcode=0
# Remember start time
local starttime=$(date +%s)
# Run the command
eval $COMMAND > /dev/null 2> "$errfile" &
# Show spinner
local pid="$!"
local i=1
local sp="/-\|"
echo -n ' '
while kill -0 "$pid" &> /dev/null; do
spinner="${sp:i++%${#sp}:1}"
printf "\r $grey[$spinner] $MESSAGE$nc"
sleep 0.2
done
printf "\r"
# Check return status
wait "$pid" || exitcode="$?"
# Calculate running time (in seconds)
local endtime=$(date +%s)
local runtime=$((endtime-starttime))
if [ "$exitcode" -eq 0 ]; then
printf "$green [✔] $MESSAGE ($runtime s)\n$nc"
else
printf "$red [X] $MESSAGE ($runtime s)\n$nc"
printf "$red ┌ \n$nc"
while read -r line; do
printf "$red │ %s\n$nc" "$line"
done < "$errfile"
printf "$red └ \n$nc"
fi
rm $errfile
return $exitcode
}
ask_question()
{
local MESSAGE="$1"
while true; do
printf "$orange [?] $MESSAGE (y/n) $nc"
read -n1 response
printf '\n'
case $response in
[Yy]) return 0;;
[Nn]) return 1;;
*) ;;
esac
done
}
prompt_sudo()
{
# Prompt for sudo password
print_header "Prompt for sudo password"
sudo -v &> /dev/null
# Keep sudo session alive
while true; do
sleep 300
sudo -n true
kill -0 "$$" || exit
done &> /dev/null &
}
# Join array
# Source: https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-an-array-in-bash
join_by() { local IFS="$1"; shift; printf "$*"; }
############
# FEATURES #
############
feature_dualboot()
{
# Make Linux store the time in local timezone instead of UTC, so the time does not jump
# when rebooting into a different OS. This is a better solution than configuring Windows
# to store the time in UTC.
timedatectl set-local-rtc 1
}
feature_gnome()
{
# Install Cascadia font
sudo apt-get install -y fonts-cascadia-code
# Enable click-to-minimize
gsettings set org.gnome.shell.extensions.dash-to-dock click-action 'minimize-or-previews'
# Show battery percentage on the top bar
gsettings set org.gnome.desktop.interface show-battery-percentage true
# Change print screen keys
gsettings set org.gnome.shell.keybindings screenshot "['Print']"
gsettings set org.gnome.shell.keybindings screenshot-window "['<Alt>Print']"
gsettings set org.gnome.shell.keybindings show-screenshot-ui "['<Shift>Print']"
# Unmap default CTRL+ALT+T binding
gsettings set org.gnome.settings-daemon.plugins.media-keys terminal '@as []'
# Remap CTRL+ALT+T to launch terminal maximised
# Only remap when the user has not added any mappings himself
local current_mappings=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
if [[ "${current_mappings}" == "@as []" ]]; then
# Add two custom keybinds
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/']"
# Set the properties of the new keybind 1 (name, command, shortcut)
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name 'Launch terminal (maximized)'
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command 'gnome-terminal --window --maximize'
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding '<Primary><Alt>t'
# Set the properties of the new keybind 2 (name, command, shortcut)
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ name 'Iconify'
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ command 'bash -ci "iconify"'
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding '<Super>i'
fi
# Favourite applications
local favorites=(
"'firefox_firefox.desktop'"
"'org.gnome.Nautilus.desktop'"
"'nvim-qt.desktop'"
"'slack_slack.desktop'"
)
gsettings set org.gnome.shell favorite-apps $(printf '['; join_by ',' "${favorites[@]}"; printf ']')
# Get default terminal profile ID
profile=$(gsettings get org.gnome.Terminal.ProfilesList default)
# Remove leading and trailing single quotes
profile=${profile:1:-1}
# Set terminal font size
gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:${profile}/ use-system-font false
gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:${profile}/ font 'Cascadia Code 12'
# Disable 'Use colors from system theme'
gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:${profile}/ use-theme-colors false
# Set foreground and background colour to mimic VSCode's dark theme
gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:${profile}/ foreground-color 'rgb(212,212,212)'
gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:${profile}/ background-color 'rgb(30,30,30)'
# Use 'CTRL+=' in GNOME terminal to zoom in (i.e. CTRL++, but without SHIFT)
gsettings set org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/ zoom-in '<Primary>equal'
# Configure dash to dock
gsettings set org.gnome.shell.extensions.dash-to-dock show-apps-at-top true
gsettings set org.gnome.shell.extensions.dash-to-dock dash-max-icon-size 32
gsettings set org.gnome.shell.extensions.dash-to-dock dock-position 'BOTTOM'
# Only show windows of current workspace in dash to dock
gsettings set org.gnome.shell.extensions.dash-to-dock isolate-workspaces true
# Make workspaces span multiple displays
gsettings set org.gnome.mutter workspaces-only-on-primary false
# Use Super+CTRL+arrow to switch workspaces
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-left "['<Primary><Super>Up','<Primary><Super>Left']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-right "['<Primary><Super>Down','<Primary><Super>Right']"
}
feature_nvim()
{
# Install neovim
NEOVIM_URL="https://github.com/neovim/neovim/releases/latest/download/nvim.appimage"
TMPDIR="$(mktemp -d)"
(
cd "$TMPDIR" && \
wget "$NEOVIM_URL" -O nvim.appimage && \
chmod +x nvim.appimage && \
./nvim.appimage --appimage-extract && \
mkdir -p ~/bin/store && \
rm -rf ~/bin/store/nvim && \
mv squashfs-root ~/bin/store/nvim && \
ln -sf ~/bin/store/nvim/usr/bin/nvim ~/bin/nvim
)
rm -r "$TMPDIR"
# Install python support for neovim
sudo apt-get install -y python3-pip python3-venv
python3 -m venv ~/.local --system-site-packages
~/.local/bin/pip install neovim
# Install Python language server
sudo apt-get install -y python3-pylsp
# Install Neovim GUI
sudo apt-get install -y neovim-qt
# Install ripgrep (for Telescope grep)
sudo apt-get install -y ripgrep
}
feature_dotfiles()
{
# Install dependencies
sudo apt-get install -y \
curl \
xclip \
jq \
python3-pip \
python3-venv \
entr \
fzf
python3 -m venv ~/.local --system-site-packages
# Install delta
DELTA_VERSION="$(curl -s https://api.github.com/repos/dandavison/delta/releases/latest | jq -r '.name')"
DELTA_URL="https://github.com/dandavison/delta/releases/download/${DELTA_VERSION}/git-delta_${DELTA_VERSION}_amd64.deb"
TMPDIR="$(mktemp -d)"
(
cd "$TMPDIR" && \
wget "$DELTA_URL" -O delta.deb && \
sudo apt-get install -y ./delta.deb
)
rm -r "$TMPDIR"
# Install nbstripout
~/.local/bin/pip install --upgrade nbstripout
# Install zoxide
curl -sS https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | bash
# Install dotfiles
./config_unix.sh
}
feature_skeleton()
{
# Create directories in home
mkdir -p ~/bin/store
mkdir -p ~/dev/{personal,work,deps}
}
feature_keepassxc()
{
# Install KeepassXC
sudo apt-get install -y keepassxc
}
feature_vlc()
{
# Install VLC
sudo apt-get install -y vlc
# Get MIME types for audio and video that vlc supports
local mimetypes=$(grep '^MimeType=' /usr/share/applications/vlc.desktop | sed 's/^MimeType=//; s/;/\n/g' | grep '^\(audio\|video\)/' | tr '\n' ' ')
# Set VLC as default audio and video player for the supported filetypes
xdg-mime default vlc.desktop ${mimetypes}
}
feature_slack()
{
# Install Slack
sudo snap install slack --classic
}
feature_documents()
{
sudo apt-get install -y \
texlive-full \
pandoc
}
feature_docker()
{
sudo apt-get install -y \
docker.io \
docker-compose
# Run docker without sudo
sudo groupadd -f docker
sudo usermod -aG docker $USER
}
feature_kvm()
{
sudo apt-get install -y \
qemu-kvm \
libvirt-daemon-system \
libvirt-clients \
bridge-utils \
virt-manager
}
feature_direnv()
{
# Install direnv
sudo apt-get install -y direnv
}
feature_virtualcam()
{
# Install dependencies
sudo apt-get install -y ffmpeg
sudo apt-get install -y v4l2loopback-dkms
# Install OBS
sudo add-apt-repository -y ppa:obsproject/obs-studio
sudo apt-get update -y
sudo apt-get install -y obs-studio
# Load v4l2loopback on boot
if ! grep -q '^v4l2loopback' /etc/modules; then
printf "\nv4l2loopback\n" | sudo tee -a /etc/modules >/dev/null
fi
# Set default v4l2loopback options
# NOTE: exclusive_caps is necessary to allow connecting to webcam before producer (= OBS) is connected.
echo 'options v4l2loopback devices=1 video_nr=10 card_label="OBS Virtualcam" exclusive_caps=0' | sudo tee /etc/modprobe.d/v4l2loopback.conf
# Update initramfs
sudo update-initramfs -c -k $(uname -r)
# Create a launcher to automatically start OBS virtual camera.
cat <<EOF > ~/.local/share/applications/virtualcamera.desktop
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=obs --scene "Virtual Camera" --startvirtualcam --minimize-to-tray
Icon=com.obsproject.Studio
Name=Start OBS Virtual Camera
EOF
}
feature_zathura()
{
sudo apt-get install -y zathura
}
feature_obsidian()
{
OBSIDIAN_VERSION="$(curl -s https://api.github.com/repos/obsidianmd/obsidian-releases/releases/latest | jq -r '.name')"
OBSIDIAN_URL="https://github.com/obsidianmd/obsidian-releases/releases/download/v${OBSIDIAN_VERSION}/obsidian_${OBSIDIAN_VERSION}_amd64.deb"
TMPDIR="$(mktemp -d)"
(
cd "$TMPDIR" && \
wget "$OBSIDIAN_URL" -O obsidian.deb && \
sudo apt-get install -y ./obsidian.deb
)
rm -r "$TMPDIR"
}
feature_zotero()
{
sudo snap install zotero-snap
}
feature_onedrive()
{
sudo apt-get install -y onedrive
}
feature_syncthing()
{
sudo apt-get install -y syncthing
# Automatically start syncthing
systemctl --user enable syncthing.service
systemctl --user start syncthing.service
}
feature_tasks()
{
sudo apt-get install -y taskwarrior tasksh timewarrior gnome-shell-pomodoro
mkdir -p ~/.task/hooks
cp /usr/share/doc/timewarrior/ext/on-modify.timewarrior ~/.task/hooks/on-modify.00.timewarrior
chmod +x ~/.task/hooks/on-modify.00.timewarrior
# Enable 'Custom actions' in Pomodoro.
gsettings set org.gnome.pomodoro.preferences enabled-plugins "['sounds', 'actions']"
# Add hook action.
gsettings set org.gnome.pomodoro.plugins.actions actions-list "['/org/gnome/pomodoro/plugins/actions/action0/']"
gsettings set org.gnome.pomodoro.plugins.actions.action:/org/gnome/pomodoro/plugins/actions/action0/ name "Pomodoro TimeWarrior Integration"
gsettings set org.gnome.pomodoro.plugins.actions.action:/org/gnome/pomodoro/plugins/actions/action0/ command "$HOME/.dotfiles/pomodoro-hook.sh \$(triggers)"
gsettings set org.gnome.pomodoro.plugins.actions.action:/org/gnome/pomodoro/plugins/actions/action0/ states "['pomodoro']"
gsettings set org.gnome.pomodoro.plugins.actions.action:/org/gnome/pomodoro/plugins/actions/action0/ triggers "['start', 'complete', 'skip', 'pause', 'resume']"
# Start Pomodoro timer on boot.
mkdir -p ~/.config/autostart
cat <<EOF >~/.config/autostart/pomodoro.desktop
[Desktop Entry]
Name=Pomodoro
Exec=gnome-pomodoro --no-default-window
Type=Application
EOF
}
########
# MAIN #
########
usage()
{
cat <<EOF >&2
Usage: $0 [OPTIONS]
Bootstrap script to install all software on a fresh Linux install.
Options:
-h, --help Show this help.
-o, --off Do not select any options by default.
EOF
}
# Default selection state for items (ON/OFF)
DEFAULT_SELECTION="ON"
parse_args()
{
local positional=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage; exit 0
;;
-o|--off)
DEFAULT_SELECTION="OFF"
shift
;;
-*)
echo "Unknown command-line option '$1'."
echo "Try '$0 --help' for more information."
exit 1
;;
*)
positional+=("$1")
shift
;;
esac
done
set -- "${positional[@]}"
if [[ $# -ne 0 ]]; then
echo "Expected 0 positional arguments, but got $#."
echo "Try '$0 --help' for more information."
exit 1
fi
}
ask_for_reboot()
{
if ask_question "Do you want to reboot?"; then
sudo reboot
fi
}
main()
{
# Kill all background jobs when the shell script exits
trap 'kill $(jobs -p) &>/dev/null' EXIT
# Ask for sudo password
prompt_sudo
# Ask the user what they want to install
features=$(
whiptail --title "Select Features" --checklist --notags --separate-output \
"Choose the features to install:" 25 35 19 \
dualboot "Dual boot fixes" "$DEFAULT_SELECTION" \
gnome "GNOME config" "$DEFAULT_SELECTION" \
nvim "Neovim" "$DEFAULT_SELECTION" \
dotfiles "Dotfiles" "$DEFAULT_SELECTION" \
skeleton "Home directory skeleton" "$DEFAULT_SELECTION" \
keepassxc "KeepassXC" "$DEFAULT_SELECTION" \
vlc "VLC" "$DEFAULT_SELECTION" \
slack "Slack" "$DEFAULT_SELECTION" \
documents "Document creation" "$DEFAULT_SELECTION" \
docker "Docker" "$DEFAULT_SELECTION" \
kvm "KVM" "$DEFAULT_SELECTION" \
direnv "direnv" "$DEFAULT_SELECTION" \
virtualcam "Virtual webcam using OBS" "$DEFAULT_SELECTION" \
zathura "Zathura PDF reader" "$DEFAULT_SELECTION" \
obsidian "Obsidian.md" "$DEFAULT_SELECTION" \
zotero "Zotero" "$DEFAULT_SELECTION" \
onedrive "OneDrive" "$DEFAULT_SELECTION" \
syncthing "Syncthing" "$DEFAULT_SELECTION" \
tasks "Task Management" "$DEFAULT_SELECTION" \
3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
exit
fi
print_header "Install selected features"
local i=1
local numfeatures=$(wc -w <<< "$features")
for feature in $features; do
execute feature_${feature} "Feature $i of $numfeatures: $feature" || true
i=$((i+1))
done
# Ask if user wants to reboot
ask_for_reboot
}
parse_args "$@"
main