-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·444 lines (366 loc) · 12.2 KB
/
setup
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
#!/usr/bin/env bash
set -Eeu -o pipefail
user_config() {
# Uncomment the desired installation mode below
# INSTALL_MODE="user,system"
# INSTALL_MODE="user-only"
# INSTALL_MODE="system-only"
# NOTE! Change the values below to your own
# Or comment out to disable installation
# SSH_REMOTE_ACCESS_PUBKEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID38541mtP7fDpTxoq7kfRlLdCKV0TrBLwht1vyut3+5 (none)"
# Change the value below to change the hostname
CHANGE_HOSTNAME_TO=""
# Set a static IP address for an Interface
# STATIC_IFACE="eno1"
# STATIC_ADDRESS="192.168.250.302/24"
}
main() {
user_config
if [[ ! -v INSTALL_MODE || -z $INSTALL_MODE ]]; then
die "must customize user_config before running; hint: ${EDITOR:-nano} $0"
fi
check_sudo
check_ubuntu_version
if [[ $INSTALL_MODE == *system* ]]; then
info "configuring system settings"
change_hostname
remove_package thunderbird
remove_package rhythmbox
setup_static_ip_address
make_journald_persistent
allow_user_to_view_journalctl
remove_update_messages_from_motd
enable_sudo_without_password
disable_automatic_updates
disable_unwanted_services
disable_login_messages
disable_graphical_grub
cloudflare_dns
disable_sleep
fi
if [[ $INSTALL_MODE == *user* ]]; then
setup_git
setup_zsh_with_prezto
setup_ssh
setup_screen
setup_nvim
install_development_packages
install_wireless_build_packages
install_ripgrep
install_fd
install_rofi_greenclip
install_kitty
cleanup_user_home
configure_gnome_settings
disable_suspend_on_lid_closed
remap_capslock_to_control
setup_dialout_group
install_pscripts
fi
info "prestobuntu setup complete!"
}
mute() { "$@" &>/dev/null; }
check_sudo() {
if (( UID == 0 )); then
die "must not run as root (tip: don't use sudo)"
fi
if ! sudo -n true &>/dev/null; then
info "root privileges are needed for installation"
sudo true
fi
}
apt_update() {
if [[ ! -v HAVE_APT_UPDATE ]]; then
kill_apt_inhibitors
sudo apt-get update
HAVE_APT_UPDATE=1
fi
}
install_package() {
for pkg in "$@"; do
if ! dpkg -s "$pkg" | grep -q "Status: install ok installed"; then
apt_update
info "installing $pkg"
sudo apt-get install -y "$pkg"
fi
done
}
remove_package() {
if dpkg -s "$1" 2>&1 | grep -vq "is not installed"; then
info "uninstalling $1"
sudo apt-get purge -y "$1"
fi
}
check_ubuntu_version() {
UBUNTU_VERSION=$(grep RELEASE /etc/lsb-release | grep -Eo '[0-9.]+')
UBUNTU_VERSION=${UBUNTU_VERSION%%.*} # keep major version only, e.g. 18
if (( UBUNTU_VERSION < 22 )); then
die "prestobuntu only supported on Ubuntu 22.04 or higher"
fi
}
change_hostname() {
if [[ -v CHANGE_HOSTNAME_TO && $CHANGE_HOSTNAME_TO ]]; then
sudo hostnamectl set-hostname "$CHANGE_HOSTNAME_TO"
fi
}
kill_apt_inhibitors() {
unwanted='apt|aptd|unattended-upgrade'
while pgrep -f "$unwanted"; do
sudo pkill -f "$unwanted" ||:
sleep 0.5
done
}
make_journald_persistent() {
sudo mkdir -p /var/log/journal
}
allow_user_to_view_journalctl() {
sudo usermod -G systemd-journal -a "$USER"
}
remove_update_messages_from_motd() (
cd /etc/update-motd.d
sudo chmod -x 10-help-text 90-updates-available 91-release-upgrade
)
remap_capslock_to_control() {
dconf write "/org/gnome/desktop/input-sources/xkb-options" "['ctrl:nocaps']"
}
setup_dialout_group() {
: allow user to access serial/USB devices such as /dev/ttyACM0 or /dev/ttyUSB0
sudo usermod -G dialout -a "$USER"
}
install_pscripts() {
local dest="$HOME/.local/bin"
mkdir -p "$dest"
for script in lastf psc psg; do
if [[ ! -f "$dest" ]]; then
get_internet_file "https://raw.githubusercontent.com/presto8/pscripts/main/$script/$script" "$dest/$script"
chmod a+x "$_"
fi
done
}
enable_sudo_without_password() {
if ! sudo grep -q NOPASSWD /etc/sudoers; then
: Allow sudo without password
sudo sed -E -i.bak 's|^(%sudo.+ALL:ALL\)) (ALL)|\1 NOPASSWD:SETENV: \2|' /etc/sudoers
fi
}
setup_ssh() {
install_package openssh-server mosh
if [[ -v SSH_REMOTE_ACCESS_PUBKEY ]]; then
mkdir -p "$HOME"/.ssh --mode 700
touch "$HOME"/.ssh/authorized_keys
_require_line "$SSH_REMOTE_ACCESS_PUBKEY" "$HOME"/.ssh/authorized_keys
chmod 600 "$HOME"/.ssh/authorized_keys
fi
# make sure this system has ssh keys
if ! test -f ~/.ssh/id_rsa; then
ssh-keygen -N "" -f "$_"
fi
}
setup_git() {
install_package git git-lfs
get_prestobuntu_file gitconfig $HOME/.gitconfig
}
get_internet_file() {
wget "$1" --output-document "$2"
}
get_prestobuntu_file() {
base="https://raw.githubusercontent.com/presto8/prestobuntu/master"
get_internet_file "$base/$1" "$2"
}
setup_zsh_with_prezto() {
install_package zsh
sudo chsh -s /usr/bin/zsh "$USER"
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
local ZDOTDIR=$XDG_CONFIG_HOME/zsh
local ZPREZTODIR=$ZDOTDIR/.zprezto
# prezto
if [[ -d "$HOME/.zshenv" || -d "$ZPREZTODIR" ]]; then
return
fi
mkdir -p "$ZDOTDIR"
cat >"$HOME/.zshenv" <<EOF
export ZDOTDIR="$ZDOTDIR"
source "$ZDOTDIR/.zshenv"
EOF
git clone --recursive https://github.com/sorin-ionescu/prezto.git "$ZPREZTODIR"
mapfile -d $'\0' rcfiles < <(find "$ZPREZTODIR/runcoms" -name 'z*' -type f -print0)
for rcfile in "${rcfiles[@]}"; do
ln -s "$rcfile" "$ZDOTDIR/.$(basename "$rcfile")"
done
# zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions "$ZDOTDIR"/zsh-autosuggestions
_require_line "source $ZDOTDIR/zsh-autosuggestions/zsh-autosuggestions.zsh" "$ZDOTDIR/.zshrc"
# autoscreen
get_prestobuntu_file "autoscreen.zsh" "$ZDOTDIR/autoscreen.zsh"
_require_line "source $ZDOTDIR/autoscreen.zsh" "$ZDOTDIR/.zshrc"
# disable zprezto corrections
_require_line "unsetopt correct" "$ZDOTDIR/.zshrc"
# fully complete on tab even if ambiguous
_require_line "setopt menu_complete" "$ZDOTDIR/.zshrc"
}
setup_screen() {
install_package screen
tee $HOME/.screenrc <<'EOF'
startup_message off
msgwait 0 # don't show "New screen..." message
hardstatus alwayslastline
hardstatus string "%{= KW} %H %{= Kw}%{-} %-Lw%{= bW}%n%f %t%{-}%+Lw %=%h %M %d %c"
# https://gist.github.com/martijnvermaat/8070533#gistcomment-1317075
setenv SSH_AUTH_SOCK $HOME/.ssh/ssh_auth_sock
EOF
}
install_development_packages() {
install_package exuberant-ctags
install_package flake8
install_package python3-pip
install_package curl
install_ccache
hash -r
}
install_ccache() {
install_package ccache
local dest="$HOME/.local/bin"
mkdir -p "$dest"
for i in gcc g++ cc c++; do
local targ="$dest/$i"
sudo rm -f "$targ"
sudo ln -s "$(which ccache)" "$targ"
done
}
install_ripgrep() {
# ripgrep not available via apt install so have to install this way
get_internet_file "https://github.com/BurntSushi/ripgrep/releases/download/11.0.2/ripgrep_11.0.2_amd64.deb" /tmp/ripgrep.deb
sudo dpkg -i "$_"
}
install_fd() {
install_package fd-find
}
install_rofi_greenclip() {
install_package rofi
get_internet_file "https://github.com/erebe/greenclip/releases/download/v4.2/greenclip" /tmp/greenclip
sudo install /tmp/greenclip /usr/local/bin
}
install_kitty() {
: install kitty mostly to get terminfo file
install_package kitty
}
setup_nvim() {
local nvimdir="$HOME/.config/nvim"
install_package neovim
mkdir -p "$nvimdir"
get_prestobuntu_file "init.lua" "$nvimdir/init.lua"
install_package fzf
}
setup_static_ip_address() {
if [[ -v STATIC_IFACE && $STATIC_IFACE ]]; then
connection_name=$(nmcli -g general.connection device show "$STATIC_IFACE")
nmcli connection modify "$connection_name" ipv4.addresses "$STATIC_ADDRESS"
nmcli connection modify "$connection_name" ipv4.method manual
fi
}
disable_service() {
for service in "$@"; do
if systemctl is-enabled "$service" | grep -q enabled ; then
_systemctl disable --now "$service" ||:
fi
done
}
disable_unwanted_services() {
disable_service cups cups-browsed whoopsie colord
# note: can't uninstall evolution since it will break Ubuntu; prevent the processes from starting
sudo find /usr/libexec -type f -name 'evolution-*' -exec chmod -x "{}" \;
}
disable_login_messages() {
touch "$HOME"/.hushlogin
}
disable_graphical_grub() {
sudo sed -E -i.bak -e 's|^(GRUB_CMDLINE_LINUX_DEFAULT=")quiet splash(")|\1text\2|' \
-e 's|^(GRUB_TIMEOUT_STYLE=).*|\1menu|' \
-e 's|^(GRUB_TIMEOUT=).*|\110|' \
/etc/default/grub
sudo update-grub
}
_sed() {
local from=$1 to=$2 file=$3
sudo sed -E -i.bak "s|$from|$to|g" "$file"
}
_require_line() {
# adds line if not present
local line="$1" file="$2"
if ! grep -qs --line-regexp --fixed-strings "$line" "$file"; then
echo "$line" | sudo tee -a "$file" >/dev/null
fi
}
cloudflare_dns() {
: configure CloudFlare to take priority over DHCP provided DNS
local resolv
resolv=$(head -1 /etc/resolv.conf)
if [[ $resolv =~ resolvconf ]]; then
_require_line "nameserver 1.1.1.1" /etc/resolvconf/resolv.conf.d/head
elif [[ $resolv =~ systemd-resolved ]]; then
_sed "^#DNS=" "DNS=1.1.1.1" /etc/systemd/resolved.conf
_systemctl restart systemd-resolved
fi
}
install_wireless_build_packages() {
info "install packages needed to compile wpa_supplicant, hostapd, and iwlwifi"
for i in build-essential libnl-3-dev libnl-genl-3-dev bison flex libssl-dev libreadline-dev libncurses-dev pkg-config; do
install_package "$i"
done
}
disable_automatic_updates() {
disable_service apt-daily.timer unattended-upgrades packagekit apt-daily-upgrade.timer
sudo sed -E -i.bak 's|"1"|"0"|' /etc/apt/apt.conf.d/10periodic
sudo sed -E -i.bak 's|"1"|"0"|' /etc/apt/apt.conf.d/20auto-upgrades
sudo sed -E -i.bak 's|^([^#])|#\1|g' /etc/apt/apt.conf.d/99update-notifier
sudo pkill -9 update-notifier ||:
sudo pkill -9 update-manager ||:
sudo chmod 000 /etc/xdg/autostart/update-notifier.desktop
}
cleanup_user_home() {
find "$HOME" -maxdepth 1 -type d -empty '!' -name Desktop -delete ||:
find "$HOME" -name examples.desktop -delete ||:
}
configure_gnome_settings() {
gsettings="gsettings"
$gsettings set org.gnome.desktop.wm.preferences focus-mode 'sloppy'
$gsettings set org.gnome.desktop.screensaver lock-enabled false
$gsettings set org.gnome.desktop.screensaver ubuntu-lock-on-suspend false
$gsettings set org.gnome.desktop.peripherals.touchpad natural-scroll true
$gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action 'nothing'
if (( UBUNTU_VERSION >= 20 )); then
: disable ubuntu/gnome welcome wizard
touch "$HOME"/.config/gnome-initial-setup-done
pkill -9 -f gnome-initial-setup ||:
fi
}
disable_sleep() {
_systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
}
disable_if_installed() {
for unit in "$@"; do
if systemctl list-unit-files "$@" &>/dev/null; then
sudo systemctl disable "$unit"
fi
done
}
disable_suspend_on_lid_closed() {
_require_line "HandleLidSwitch=ignore" /etc/systemd/logind.conf
_require_line "HandleLidSwitchExternalPower=ignore" /etc/systemd/logind.conf
# TODO: below command causes session to end and user logout
# systemctl restart systemd-logind
}
auto_start_terminal() {
local autostart="$HOME/.config/autostart"
local file="startup-terminal.desktop"
mkdir -p "$autostart"
get_prestobuntu_file "$file" "$autostart/$file"
}
_systemctl() { sudo systemctl "$@" ; }
_info_helper() {
local color=$1 prefix=$2 ; shift 2
echo -e "\033[${color}m${prefix}$*\e[0m"
}
info() { _info_helper 36 ":: " "$@" ; }
die() { _info_helper 31 "!! " "$@" ; exit 1; }
main "$@"