larbs.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #!/bin/sh
  2. # Luke's Auto Rice Bootstrapping Script (LARBS)
  3. # by Luke Smith <luke@lukesmith.xyz>
  4. # License: GNU GPLv3
  5. ### OPTIONS AND VARIABLES ###
  6. dotfilesrepo="https://github.com/lukesmithxyz/voidrice.git"
  7. progsfile="https://raw.githubusercontent.com/LukeSmithxyz/LARBS/master/static/progs.csv"
  8. aurhelper="yay"
  9. repobranch="master"
  10. export TERM=ansi
  11. rssurls="https://lukesmith.xyz/rss.xml
  12. https://videos.lukesmith.xyz/feeds/videos.xml?videoChannelId=2 \"~Luke Smith (Videos)\"
  13. https://www.youtube.com/feeds/videos.xml?channel_id=UC2eYFnH61tmytImy1mTYvhA \"~Luke Smith (YouTube)\"
  14. https://lindypress.net/rss
  15. https://notrelated.xyz/rss
  16. https://landchad.net/rss.xml
  17. https://based.cooking/index.xml
  18. https://artixlinux.org/feed.php \"tech\"
  19. https://www.archlinux.org/feeds/news/ \"tech\"
  20. https://github.com/LukeSmithxyz/voidrice/commits/master.atom \"~LARBS dotfiles\""
  21. ### FUNCTIONS ###
  22. installpkg() {
  23. pacman --noconfirm --needed -S "$1" >/dev/null 2>&1
  24. }
  25. error() {
  26. # Log to stderr and exit with failure.
  27. printf "%s\n" "$1" >&2
  28. exit 1
  29. }
  30. welcomemsg() {
  31. whiptail --title "Welcome!" \
  32. --msgbox "Welcome to Luke's Auto-Rice Bootstrapping Script!\\n\\nThis script will automatically install a fully-featured Linux desktop, which I use as my main machine.\\n\\n-Luke" 10 60
  33. whiptail --title "Important Note!" --yes-button "All ready!" \
  34. --no-button "Return..." \
  35. --yesno "Be sure the computer you are using has current pacman updates and refreshed Arch keyrings.\\n\\nIf it does not, the installation of some programs might fail." 8 70
  36. }
  37. getuserandpass() {
  38. # Prompts user for new username and password.
  39. name=$(whiptail --inputbox "First, please enter a name for the user account." 10 60 3>&1 1>&2 2>&3 3>&1) || exit 1
  40. while ! echo "$name" | grep -q "^[a-z_][a-z0-9_-]*$"; do
  41. name=$(whiptail --nocancel --inputbox "Username not valid. Give a username beginning with a letter, with only lowercase letters, - or _." 10 60 3>&1 1>&2 2>&3 3>&1)
  42. done
  43. pass1=$(whiptail --nocancel --passwordbox "Enter a password for that user." 10 60 3>&1 1>&2 2>&3 3>&1)
  44. pass2=$(whiptail --nocancel --passwordbox "Retype password." 10 60 3>&1 1>&2 2>&3 3>&1)
  45. while ! [ "$pass1" = "$pass2" ]; do
  46. unset pass2
  47. pass1=$(whiptail --nocancel --passwordbox "Passwords do not match.\\n\\nEnter password again." 10 60 3>&1 1>&2 2>&3 3>&1)
  48. pass2=$(whiptail --nocancel --passwordbox "Retype password." 10 60 3>&1 1>&2 2>&3 3>&1)
  49. done
  50. }
  51. usercheck() {
  52. ! { id -u "$name" >/dev/null 2>&1; } ||
  53. whiptail --title "WARNING" --yes-button "CONTINUE" \
  54. --no-button "No wait..." \
  55. --yesno "The user \`$name\` already exists on this system. LARBS can install for a user already existing, but it will OVERWRITE any conflicting settings/dotfiles on the user account.\\n\\nLARBS will NOT overwrite your user files, documents, videos, etc., so don't worry about that, but only click <CONTINUE> if you don't mind your settings being overwritten.\\n\\nNote also that LARBS will change $name's password to the one you just gave." 14 70
  56. }
  57. preinstallmsg() {
  58. whiptail --title "Let's get this party started!" --yes-button "Let's go!" \
  59. --no-button "No, nevermind!" \
  60. --yesno "The rest of the installation will now be totally automated, so you can sit back and relax.\\n\\nIt will take some time, but when done, you can relax even more with your complete system.\\n\\nNow just press <Let's go!> and the system will begin installation!" 13 60 || {
  61. clear
  62. exit 1
  63. }
  64. }
  65. adduserandpass() {
  66. # Adds user `$name` with password $pass1.
  67. whiptail --infobox "Adding user \"$name\"..." 7 50
  68. useradd -m -g wheel -s /bin/zsh "$name" >/dev/null 2>&1 ||
  69. usermod -a -G wheel "$name" && mkdir -p /home/"$name" && chown "$name":wheel /home/"$name"
  70. export repodir="/home/$name/.local/src"
  71. mkdir -p "$repodir"
  72. chown -R "$name":wheel "$(dirname "$repodir")"
  73. echo "$name:$pass1" | chpasswd
  74. unset pass1 pass2
  75. }
  76. refreshkeys() {
  77. case "$(readlink -f /sbin/init)" in
  78. *systemd*)
  79. whiptail --infobox "Refreshing Arch Keyring..." 7 40
  80. pacman --noconfirm -S archlinux-keyring >/dev/null 2>&1
  81. ;;
  82. *)
  83. whiptail --infobox "Enabling Arch Repositories for more a more extensive software collection..." 7 40
  84. pacman --noconfirm --needed -S \
  85. artix-keyring artix-archlinux-support >/dev/null 2>&1
  86. grep -q "^\[extra\]" /etc/pacman.conf ||
  87. echo "[extra]
  88. Include = /etc/pacman.d/mirrorlist-arch" >>/etc/pacman.conf
  89. pacman -Sy --noconfirm >/dev/null 2>&1
  90. pacman-key --populate archlinux >/dev/null 2>&1
  91. ;;
  92. esac
  93. }
  94. manualinstall() {
  95. # Installs $1 manually. Used only for AUR helper here.
  96. # Should be run after repodir is created and var is set.
  97. pacman -Qq "$1" && return 0
  98. whiptail --infobox "Installing \"$1\" manually." 7 50
  99. sudo -u "$name" mkdir -p "$repodir/$1"
  100. sudo -u "$name" git -C "$repodir" clone --depth 1 --single-branch \
  101. --no-tags -q "https://aur.archlinux.org/$1.git" "$repodir/$1" ||
  102. {
  103. cd "$repodir/$1" || return 1
  104. sudo -u "$name" git pull --force origin master
  105. }
  106. cd "$repodir/$1" || exit 1
  107. sudo -u "$name" \
  108. makepkg --noconfirm -si >/dev/null 2>&1 || return 1
  109. }
  110. maininstall() {
  111. # Installs all needed programs from main repo.
  112. whiptail --title "LARBS Installation" --infobox "Installing \`$1\` ($n of $total). $1 $2" 9 70
  113. installpkg "$1"
  114. }
  115. gitmakeinstall() {
  116. progname="${1##*/}"
  117. progname="${progname%.git}"
  118. dir="$repodir/$progname"
  119. whiptail --title "LARBS Installation" \
  120. --infobox "Installing \`$progname\` ($n of $total) via \`git\` and \`make\`. $(basename "$1") $2" 8 70
  121. sudo -u "$name" git -C "$repodir" clone --depth 1 --single-branch \
  122. --no-tags -q "$1" "$dir" ||
  123. {
  124. cd "$dir" || return 1
  125. sudo -u "$name" git pull --force origin master
  126. }
  127. cd "$dir" || exit 1
  128. make >/dev/null 2>&1
  129. make install >/dev/null 2>&1
  130. cd /tmp || return 1
  131. }
  132. aurinstall() {
  133. whiptail --title "LARBS Installation" \
  134. --infobox "Installing \`$1\` ($n of $total) from the AUR. $1 $2" 9 70
  135. echo "$aurinstalled" | grep -q "^$1$" && return 1
  136. sudo -u "$name" $aurhelper -S --noconfirm "$1" >/dev/null 2>&1
  137. }
  138. pipinstall() {
  139. whiptail --title "LARBS Installation" \
  140. --infobox "Installing the Python package \`$1\` ($n of $total). $1 $2" 9 70
  141. [ -x "$(command -v "pip")" ] || installpkg python-pip >/dev/null 2>&1
  142. yes | pip install "$1"
  143. }
  144. installationloop() {
  145. ([ -f "$progsfile" ] && cp "$progsfile" /tmp/progs.csv) ||
  146. curl -Ls "$progsfile" | sed '/^#/d' >/tmp/progs.csv
  147. total=$(wc -l </tmp/progs.csv)
  148. aurinstalled=$(pacman -Qqm)
  149. while IFS=, read -r tag program comment; do
  150. n=$((n + 1))
  151. echo "$comment" | grep -q "^\".*\"$" &&
  152. comment="$(echo "$comment" | sed -E "s/(^\"|\"$)//g")"
  153. case "$tag" in
  154. "A") aurinstall "$program" "$comment" ;;
  155. "G") gitmakeinstall "$program" "$comment" ;;
  156. "P") pipinstall "$program" "$comment" ;;
  157. *) maininstall "$program" "$comment" ;;
  158. esac
  159. done </tmp/progs.csv
  160. }
  161. putgitrepo() {
  162. # Downloads a gitrepo $1 and places the files in $2 only overwriting conflicts
  163. whiptail --infobox "Downloading and installing config files..." 7 60
  164. [ -z "$3" ] && branch="master" || branch="$repobranch"
  165. dir=$(mktemp -d)
  166. [ ! -d "$2" ] && mkdir -p "$2"
  167. chown "$name":wheel "$dir" "$2"
  168. sudo -u "$name" git -C "$repodir" clone --depth 1 \
  169. --single-branch --no-tags -q --recursive -b "$branch" \
  170. --recurse-submodules "$1" "$dir"
  171. sudo -u "$name" cp -rfT "$dir" "$2"
  172. }
  173. vimplugininstall() {
  174. # Installs vim plugins.
  175. whiptail --infobox "Installing neovim plugins..." 7 60
  176. mkdir -p "/home/$name/.config/nvim/autoload"
  177. curl -Ls "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim" > "/home/$name/.config/nvim/autoload/plug.vim"
  178. chown -R "$name:wheel" "/home/$name/.config/nvim"
  179. sudo -u "$name" nvim -c "PlugInstall|q|q"
  180. }
  181. makeuserjs(){
  182. # Get the Arkenfox user.js and prepare it.
  183. arkenfox="$pdir/arkenfox.js"
  184. overrides="$pdir/user-overrides.js"
  185. userjs="$pdir/user.js"
  186. ln -fs "/home/$name/.config/firefox/larbs.js" "$overrides"
  187. [ ! -f "$arkenfox" ] && curl -sL "https://raw.githubusercontent.com/arkenfox/user.js/master/user.js" > "$arkenfox"
  188. cat "$arkenfox" "$overrides" > "$userjs"
  189. chown "$name:wheel" "$arkenfox" "$userjs"
  190. }
  191. finalize() {
  192. whiptail --title "All done!" \
  193. --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 Luke" 13 80
  194. }
  195. ### THE ACTUAL SCRIPT ###
  196. ### This is how everything happens in an intuitive format and order.
  197. # Check if user is root on Arch distro. Install whiptail.
  198. pacman --noconfirm --needed -Sy libnewt ||
  199. error "Are you sure you're running this as the root user, are on an Arch-based distribution and have an internet connection?"
  200. # Welcome user and pick dotfiles.
  201. welcomemsg || error "User exited."
  202. # Get and verify username and password.
  203. getuserandpass || error "User exited."
  204. # Give warning if user already exists.
  205. usercheck || error "User exited."
  206. # Last chance for user to back out before install.
  207. preinstallmsg || error "User exited."
  208. ### The rest of the script requires no user input.
  209. # Refresh Arch keyrings.
  210. refreshkeys ||
  211. error "Error automatically refreshing Arch keyring. Consider doing so manually."
  212. for x in curl ca-certificates base-devel git ntp zsh dash; do
  213. whiptail --title "LARBS Installation" \
  214. --infobox "Installing \`$x\` which is required to install and configure other programs." 8 70
  215. installpkg "$x"
  216. done
  217. whiptail --title "LARBS Installation" \
  218. --infobox "Synchronizing system time to ensure successful and secure installation of software..." 8 70
  219. ntpd -q -g >/dev/null 2>&1
  220. adduserandpass || error "Error adding username and/or password."
  221. [ -f /etc/sudoers.pacnew ] && cp /etc/sudoers.pacnew /etc/sudoers # Just in case
  222. # Allow user to run sudo without password. Since AUR programs must be installed
  223. # in a fakeroot environment, this is required for all builds with AUR.
  224. trap 'rm -f /etc/sudoers.d/larbs-temp' HUP INT QUIT TERM PWR EXIT
  225. echo "%wheel ALL=(ALL) NOPASSWD: ALL
  226. Defaults:%wheel,root runcwd=*" >/etc/sudoers.d/larbs-temp
  227. # Make pacman colorful, concurrent downloads and Pacman eye-candy.
  228. grep -q "ILoveCandy" /etc/pacman.conf || sed -i "/#VerbosePkgLists/a ILoveCandy" /etc/pacman.conf
  229. sed -Ei "s/^#(ParallelDownloads).*/\1 = 5/;/^#Color$/s/#//" /etc/pacman.conf
  230. # Use all cores for compilation.
  231. sed -i "s/-j2/-j$(nproc)/;/^#MAKEFLAGS/s/^#//" /etc/makepkg.conf
  232. manualinstall $aurhelper || error "Failed to install AUR helper."
  233. # Make sure .*-git AUR packages get updated automatically.
  234. $aurhelper -Y --save --devel
  235. # The command that does all the installing. Reads the progs.csv file and
  236. # installs each needed program the way required. Be sure to run this only after
  237. # the user has been created and has priviledges to run sudo without a password
  238. # and all build dependencies are installed.
  239. installationloop
  240. # Install the dotfiles in the user's home directory, but remove .git dir and
  241. # other unnecessary files.
  242. putgitrepo "$dotfilesrepo" "/home/$name" "$repobranch"
  243. rm -rf "/home/$name/.git/" "/home/$name/README.md" "/home/$name/LICENSE" "/home/$name/FUNDING.yml"
  244. # Write urls for newsboat if it doesn't already exist
  245. [ -s "/home/$name/.config/newsboat/urls" ] ||
  246. echo "$rssurls" | sudo -u "$name" tee "/home/$name/.config/newsboat/urls" >/dev/null
  247. # Install vim plugins if not alread present.
  248. [ ! -f "/home/$name/.config/nvim/autoload/plug.vim" ] && vimplugininstall
  249. # Most important command! Get rid of the beep!
  250. rmmod pcspkr
  251. echo "blacklist pcspkr" >/etc/modprobe.d/nobeep.conf
  252. # Make zsh the default shell for the user.
  253. chsh -s /bin/zsh "$name" >/dev/null 2>&1
  254. sudo -u "$name" mkdir -p "/home/$name/.cache/zsh/"
  255. sudo -u "$name" mkdir -p "/home/$name/.config/abook/"
  256. sudo -u "$name" mkdir -p "/home/$name/.config/mpd/playlists/"
  257. # Make dash the default #!/bin/sh symlink.
  258. ln -sfT /bin/dash /bin/sh >/dev/null 2>&1
  259. # dbus UUID must be generated for Artix runit.
  260. dbus-uuidgen >/var/lib/dbus/machine-id
  261. # Use system notifications for Brave on Artix
  262. # Only do it when systemd is not present
  263. [ "$(readlink -f /sbin/init)" != "/usr/lib/systemd/systemd" ] && echo "export \$(dbus-launch)" >/etc/profile.d/dbus.sh
  264. # Enable tap to click
  265. [ ! -f /etc/X11/xorg.conf.d/40-libinput.conf ] && printf 'Section "InputClass"
  266. Identifier "libinput touchpad catchall"
  267. MatchIsTouchpad "on"
  268. MatchDevicePath "/dev/input/event*"
  269. Driver "libinput"
  270. # Enable left mouse button by tapping
  271. Option "Tapping" "on"
  272. EndSection' >/etc/X11/xorg.conf.d/40-libinput.conf
  273. # All this below to get Librewolf installed with add-ons and non-bad settings.
  274. whiptail --infobox "Setting browser privacy settings and add-ons..." 7 60
  275. browserdir="/home/$name/.librewolf"
  276. profilesini="$browserdir/profiles.ini"
  277. # Start librewolf headless so it generates a profile. Then get that profile in a variable.
  278. sudo -u "$name" librewolf --headless >/dev/null 2>&1 &
  279. sleep 1
  280. profile="$(sed -n "/Default=.*.default-default/ s/.*=//p" "$profilesini")"
  281. pdir="$browserdir/$profile"
  282. [ -d "$pdir" ] && makeuserjs
  283. # Kill the now unnecessary librewolf instance.
  284. pkill -u "$name" librewolf
  285. # Allow wheel users to sudo with password and allow several system commands
  286. # (like `shutdown` to run without password).
  287. echo "%wheel ALL=(ALL:ALL) ALL" >/etc/sudoers.d/00-larbs-wheel-can-sudo
  288. echo "%wheel 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/pacman -Syu,/usr/bin/pacman -Syyu,/usr/bin/pacman -Syyu --noconfirm,/usr/bin/loadkeys,/usr/bin/pacman -Syyuw --noconfirm,/usr/bin/pacman -S -y --config /etc/pacman.conf --,/usr/bin/pacman -S -y -u --config /etc/pacman.conf --" >/etc/sudoers.d/01-larbs-cmds-without-password
  289. echo "Defaults editor=/usr/bin/nvim" >/etc/sudoers.d/02-larbs-visudo-editor
  290. mkdir -p /etc/sysctl.d
  291. echo "kernel.dmesg_restrict = 0" > /etc/sysctl.d/dmesg.conf
  292. # Cleanup
  293. rm -f /etc/sudoers.d/larbs-temp
  294. # Last message! Install complete!
  295. finalize