summaryrefslogtreecommitdiff
path: root/static/thesiah-debian.sh
blob: 53f5f71e43480a1ca2d5357b9b763e5eeacfe982 (plain)
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
#!/bin/sh

# Soomin's Auto Rice Bootstrapping Script (THESIAH) adapted for debian
# Adaptation by: Soomin Im <si@thesiah.xyz>
# License: GNU GPLv3


### VARIABLES ###
dotfilesrepo="https://github.com/TheSiahxyz/.dotfiles.git"
progsfile="https://raw.githubusercontent.com/thesiah/THESIAH/main/static/debianprogs.csv"
repobranch="master"
export TERM=ansi


### FUNCTIONS ###
installpkg() {
    DEBIAN_FRONTEND=noninteractive apt-get install -y "$1" >/dev/null 2>&1 || error "Failed to install $1"
}

error() {
    whiptail --title "Error" --msgbox "An error occurred: $1" 8 78
    exit 1
}

welcomemsg() {
    whiptail --title "Welcome!" \
      --msgbox "Welcome to Soomin's Auto-Rice Bootstrapping Script for Debian! This script will automatically install a fully-featured Linux desktop, which I use as my main machine." 12 60
}

getuserandpass() {
    name=$(whiptail --inputbox "Please enter a name for the user account." 10 60 3>&1 1>&2 2>&3 3>&1) || error "User exited."
    pass=$(whiptail --passwordbox "Enter a password for the user." 10 60 3>&1 1>&2 2>&3 3>&1) || error "User exited."
    pass2=$(whiptail --passwordbox "Retype password." 10 60 3>&1 1>&2 2>&3 3>&1) || error "User exited."
    while ! [ "$pass" = "$pass2" ]; do
        pass=$(whiptail --passwordbox "Passwords do not match.\\n\\nEnter password again." 10 60 3>&1 1>&2 2>&3 3>&1)
        pass2=$(whiptail --passwordbox "Retype password." 10 60 3>&1 1>&2 2>&3 3>&1)
    done
}

usercheck() {
    if id "$name" &>/dev/null; then
        whiptail --yesno "The user $name already exists. Do you want to continue?" 8 78 || error "User exited."
    fi
}

preinstallmsg() {
    whiptail --title "Ready to start?" --yesno "The rest of the installation will now be totally automated. Press Yes to continue." 8 78 || error "User exited."
}

adduserandpass() {
	whiptail --infobox "Adding user \"$name\"..." 7 50
	adduser --disabled-password --gecos "" "$name" || error "Failed to add user $name"
	echo "$name:$pass1" | chpasswd
	usermod -aG sudo "$name"

	export repodir="/home/$name/.local/src"
	mkdir -p "$repodir"
	chown -R "$name": "$repodir"

	unset pass1 pass2

    trap 'rm -f /etc/sudoers.d/thesiah-temp' HUP INT QUIT TERM PWR EXIT
    echo "$name ALL=(ALL) NOPASSWD: ALL Defaults:$name runcwd=*" >/etc/sudoers.d/thesiah-temp
}

installationloop() {
    ([ -f "$progsfile" ] && cp "$progsfile" /tmp/progs.csv) || curl -Ls "$progsfile" | sed '/^#/d' >/tmp/progs.csv
    total=$(wc -l </tmp/progs.csv)
    while IFS=, read -r tag program comment; do
        case "$tag" in
            "G") gitmakeinstall "$program" "$comment" ;;
            "P") pipinstall "$program" ;;
            *) installpkg "$program" ;;
        esac
    done </tmp/progs.csv
}

putgitrepo() {
    whiptail --infobox "Downloading and installing config files..." 7 60
    [ -z "$3" ] && branch="main" || branch="$3"
    dir=$(mktemp -d)
    [ ! -d "$2" ] && mkdir -p "$2"
	sudo -u "$name" git -C "$repodir" clone --depth 1 \
		--single-branch --no-tags -q --recursive -b "$branch" \
		--recurse-submodules "$1" "$dir"
	sudo -u "$name" cp -rfT "$dir" "$2"
}

gitmakeinstall() {
	progname="${1##*/}"
	progname="${progname%.git}"
	dir="$repodir/$progname"
	whiptail --title "SI Installation" \
		--infobox "Installing \`$progname\` ($n of $total) via \`git\` and \`make\`. $(basename "$1") $2" 8 70
	sudo -u "$name" git -C "$repodir" clone --depth 1 --single-branch \
		--no-tags -q "$1" "$dir" ||
		{
			cd "$dir" || return 1
			sudo -u "$name" git pull --force origin master
		}
    # Check the repository name and act accordingly for suckless
    case "$progname" in
        "suckless")
            for sub in "${dir}/"*; do
                cd "$sub" || continue
                make >/dev/null 2>&1
                make install >/dev/null 2>&1
            done
            ;;
        *)
            cd "$dir" || exit 1
            make >/dev/null 2>&1
            make install >/dev/null 2>&1
            ;;
    esac
	cd /tmp || return 1
}

pipinstall() {
	whiptail --title "SI Installation" \
		--infobox "Installing the Python package \`$1\` ($n of $total). $1 $2" 9 70
	[ -x "$(command -v "pip")" ] || installpkg python-pip >/dev/null 2>&1
	yes | pip install "$1"
}

vimplugininstall() {
	# Installs vim plugins.
	whiptail --infobox "Installing vim plugins..." 7 60
	mkdir -p "/home/$name/.config/vim/autoload"
	curl -Ls "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim" >"/home/$name/.config/vim/autoload/plug.vim"
	chown -R "$name:$name" "/home/$name/.config/vim" # Changed to use user's group
	sudo -u "$name" vim -c "PlugInstall|q|q"
}

lfinstall() {
    env CGO_ENABLED=0 go install -ldflags="-s -w" github.com/gokcehan/lf@latest
}

zoxideinstall() {
    git clone https://github.com/ajeetdsouza/zoxide.git
    cd zoxide
    ./install.sh
    cd /tmp
}

nviminstall() {
    latest_version=$(curl -s https://api.github.com/repos/neovim/neovim/releases/latest | grep -o '"tag_name": ".*"' | cut -d'"' -f4)
    download_url="https://github.com/neovim/neovim/releases/download/$latest_version/nvim.appimage"
    curl -LO "$download_url"
}

addsudo() {
    echo "$name ALL=(ALL:ALL) ALL" >/etc/sudoers.d/00-thesiah-user-can-sudo
    echo "$name ALL=(ALL:ALL) NOPASSWD: /usr/bin/shutdown,/usr/bin/reboot,/usr/bin/systemctl suspend,/usr/bin/wifi-menu,/usr/bin/mount,/usr/bin/umount,/usr/bin/apt update,/usr/bin/apt upgrade,/usr/bin/apt upgrade -y,/usr/bin/loadkeys,/usr/bin/apt-get --download-only upgrade" >/etc/sudoers.d/01-thesiah-cmds-without-password
    echo "Defaults editor=/usr/bin/nvim" >/etc/sudoers.d/02-thesiah-visudo-editor
    mkdir -p /etc/sysctl.d
    echo "kernel.dmesg_restrict = 0" >/etc/sysctl.d/dmesg.conf
}

checksystem() {
    if [ "$1" = "ufw" ]; then
        ufw --force reload >/dev/null 2>&1
        ufw default deny incoming >/dev/null 2>&1
        ufw allow 80 >/dev/null 2>&1
        ufw allow 443 >/dev/null 2>&1
        ufw allow 25/tcp >/dev/null 2>&1
        ufw allow 587/tcp >/dev/null 2>&1
        ufw allow 3478/udp >/dev/null 2>&1
        ufw allow 5349/tcp >/dev/null 2>&1
        ufw allow 10000/udp >/dev/null 2>&1
        ufw allow in ssh >/dev/null 2>&1
        ufw allow in IMAPS >/dev/null 2>&1
        ufw allow in POP3 >/dev/null 2>&1
        ufw allow in SMTP >/dev/null 2>&1
        ufw allow in 'WWW Full' >/dev/null 2>&1
        ufw allow in 'Postfix SMTPS' >/dev/null 2>&1
        ufw allow in 'Mail Submission' >/dev/null 2>&1
        ufw enable >/dev/null 2>&1 || ufw --force enable >/dev/null 2>&1
    fi
    if [ "$1" = "smbd" ]; then
        ufw --force reload >/dev/null 2>&1
        ufw allow 137/tcp >/dev/null 2>&1
        ufw allow 137/udp >/dev/null 2>&1
        ufw allow 138/tcp >/dev/null 2>&1
        ufw allow 138/udp >/dev/null 2>&1
        ufw allow 139/tcp >/dev/null 2>&1
        ufw allow 445/tcp >/dev/null 2>&1
        [ -f /etc/samba/smb.conf ] && touch /etc/samba/smb.conf
        sleep 1
        printf "$pass1" | smbpasswd -a "$name" >/dev/null 2>&1
    fi
    if ! systemctl is-active --quiet "$1"; then
        systemctl enable "$1"
        systemctl start "$1"
    fi
}

finalize() {
	whiptail --title "All done!" \
		--msgbox "Congrats! Provided there were no hidden errors, the script completed successfully and all the programs and configuration files should be in place.\\n\\nTo run the new graphical environment, log out and log back in as your new user, then run the command \"startx\" to start the graphical environment (it will start automatically in tty1).\\n\\n.t Soomin" 13 80
}


### MAIN SCRIPT ###
if [ "$(id -u)" -ne 0 ]; then
    echo "This script must be run as root." >&2
    exit 1
fi

essential_packages="curl ca-certificates build-essential git network-manager ntp zsh stow"
whiptail --title "SI Installation" --infobox "Installing essential packages required to install and configure other programs." 8 70
for package in $essential_packages; do
    installpkg $package || error "Failed to install $package"
done

welcomemsg || error "User exited."

getuserandpass || error "User exited."

usercheck || error "User exited."

preinstallmsg || error "User exited."

adduserandpass || error "Error adding username and/or password."

installationloop

lfinstall || error "User exited."

zoxideinstall || error "User exited."

nviminstall || error "User exited."

putgitrepo "$dotfilesrepo" "/home/$name" "$repobranch"
rm -rf "/home/$name/.git/" "/home/$name/README.md" "/home/$name/LICENSE" "/home/$name/FUNDING.yml"
cd "/home/$name/.dotfiles" && stow --no-folding -S debian && stow --no-folding -S default || exit 1

sudo -u "$name" ln -sf "/home/$name/.dotfiles/debian/.config/shell/profile" "/home/$name/.zprofile"
sudo -u "$name" ln -sf "/home/$name/.dotfiles/debian/.config/x11/xprofile" "/home/$name/.xprofile"
sudo -u "$name" ln -sf "/home/$name/.dotfiles/debian/.config/bash/bash_profile" "/home/$name/.bash_profile"
sudo -u "$name" ln -sf "/home/$name/.dotfiles/debian/.config/bash/bashrc" "/home/$name/.bashrc"

[ ! -f "/home/$name/.config/vim/autoload/plug.vim" ] && vimplugininstall

rmmod pcspkr
echo "blacklist pcspkr" >/etc/modprobe.d/nobeep.conf

chsh -s /bin/zsh "$name" >/dev/null 2>&1
sudo -u "$name" mkdir -p "/home/$name/.cache/zsh/"
sudo -u "$name" mkdir -p "/home/$name/.config/mpd/playlists/"

# dbus UUID must be generated for Artix runit.
dbus-uuidgen >/var/lib/dbus/machine-id

# Use system notifications for Brave on Artix
echo "export \$(dbus-launch)" >/etc/profile.d/dbus.sh

# Enable tap to click
[ ! -f /etc/X11/xorg.conf.d/40-libinput.conf ] && printf 'Section "InputClass"
        Identifier "libinput touchpad catchall"
        MatchIsTouchpad "on"
        MatchDevicePath "/dev/input/event*"
        Driver "libinput"
        # Enable left mouse button by tapping
        Option "Tapping" "on"
EndSection' >/etc/X11/xorg.conf.d/40-libinput.conf

# Allow wheel users to sudo with password and allow several system commands
# (like `shutdown` to run without password).
addsudo

systems="NetworkManager ssh bluetooth ufw"
for system in $systems; do
    checksystem $system || error "Failed to start $system"
done

finalize