dotfiles/setup_system.sh

382 lines
10 KiB
Bash
Raw Normal View History

2022-05-27 21:07:07 +02:00
#!/bin/bash
#
# Default variables
#
2022-11-10 15:43:45 +01:00
2022-12-14 20:29:00 +01:00
upgrade_packages=false
2022-12-15 17:08:11 +01:00
os_id="$(grep ^ID= </etc/os-release | cut -d= -f2)"
os_id_like="$(grep ^ID_LIKE= </etc/os-release | cut -d= -f2)"
2022-12-14 20:29:00 +01:00
script_dir="$(
2022-05-27 21:07:07 +02:00
cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit 1
pwd -P
)"
opt_dir=/opt/dotfiles
python_venv=$opt_dir/pyenv
2022-05-27 21:07:07 +02:00
2024-02-12 08:23:28 +01:00
[ -z "$KREW_ROOT" ] && export KREW_ROOT="${XDG_DATA_HOME:-$HOME/.local/share}/krew"
2022-11-10 15:43:45 +01:00
#
# Argument parsing and help
#
2022-05-27 21:07:07 +02:00
function usage() {
2022-12-14 20:29:00 +01:00
echo "Usage: $(basename -- "$0") [OPTIONS]"
2022-05-27 21:07:07 +02:00
echo
echo "Options:"
echo " -h Display help."
echo " -u Upgrade existing packages."
exit 0
}
while getopts ":uh" arg; do
case $arg in
h) usage ;;
2022-12-14 20:29:00 +01:00
u) upgrade_packages=true ;;
2022-05-27 21:07:07 +02:00
:)
echo "$0: Must supply an argument to -$OPTARG." >&2
exit 1
;;
?)
echo "Invalid option: -${OPTARG}."
echo
usage
exit 2
;;
esac
done
2022-11-10 15:43:45 +01:00
#
# Distribution-agnostic functions
#
function _acpib() {
acpi -b | grep -Fv ' 0%'
}
2022-12-14 20:54:12 +01:00
function has_battery() {
2022-11-10 15:43:45 +01:00
[[ $(_acpib | wc -l) -gt 0 ]] && return 0 || return 1
}
2022-12-14 20:54:12 +01:00
# Returns a list of packages for one or more targets.
2022-05-27 21:07:07 +02:00
# The packages file may contain multiple rows with the same target name.
function get_packages() {
2022-12-14 20:29:00 +01:00
local pkg_file="$script_dir/.installer/packages"
2022-05-27 21:07:07 +02:00
for target in "$@"; do
while read -r row; do
2022-12-14 20:38:54 +01:00
for package in $row; do
2022-05-27 21:07:07 +02:00
echo "$package"
done
2022-12-14 20:25:03 +01:00
done <<<"$(grep "^${target:?}:" "$pkg_file" | cut -d: -f2 | cut -d# -f1)"
2022-05-27 21:07:07 +02:00
done
}
2022-12-14 20:54:12 +01:00
# Return a list of targets for a given package manager
function construct_target_list() {
local package_manager=${1:?package_manager not set} targets=()
targets+=("$package_manager-any")
targets+=("$package_manager-host-$(hostname | cut -d. -f1)")
has_battery && targets+=("$package_manager-type-hasbattery")
echo "${targets[@],,}" # Return lowercase
}
function setup_pip_packages() {
echo "Installing Python packages into virtualenv $python_venv"
! [ -d "$opt_dir" ] && sudo mkdir "$opt_dir"
2024-04-11 12:21:20 +02:00
sudo python3 -m venv "$python_venv"
# shellcheck disable=SC1091
source "$python_venv/bin/activate"
2022-12-14 20:54:12 +01:00
# shellcheck disable=SC2046
sudo pip3 install $(get_packages $(construct_target_list pip)) --no-input || return 1
deactivate
2022-12-14 20:54:12 +01:00
}
2022-06-07 12:11:19 +02:00
function install_sddm_aerial_theme() {
local theme_dir="/usr/share/sddm/themes/aerial"
[ -d "$theme_dir" ] && return
echo "Installing SDDM Aerial theme."
git clone https://github.com/3ximus/aerial-sddm-theme.git /tmp/aerial
sudo mv /tmp/aerial "$theme_dir"
sudo chown -R root:root "$theme_dir"
}
2022-06-05 00:19:56 +02:00
function configure_sddm() {
local sddm_config_dir="/etc/sddm.conf.d"
local theme_dir="/usr/share/sddm/themes/aerial"
local theme_user_config="$theme_dir/theme.conf.user"
echo "Setting up '$sddm_config_dir'."
if ! [ -d "$sddm_config_dir" ]; then
sudo mkdir "$sddm_config_dir"
sudo chown root:root -R "$sddm_config_dir"
fi
{
echo "[Theme]"
echo "Current=aerial"
} | sudo tee "$sddm_config_dir/theme" >/dev/null
{
echo "[X11]"
echo "DisplayCommand=/usr/share/sddm/scripts/Xsetup"
} | sudo tee "$sddm_config_dir/xsetup" >/dev/null
echo "Setting up '$theme_dir'."
[ -f "$theme_user_config" ] && sudo mv "$theme_user_config" "$theme_user_config.disabled"
2022-12-13 16:58:15 +01:00
return 0
2022-06-05 00:19:56 +02:00
}
function configure_ufw() {
echo "Setting up UFW."
sudo systemctl enable ufw.service || return 1
sudo systemctl start ufw.service || return 2
sudo ufw enable || return 3
sudo ufw allow ssh || return 4
}
2022-11-10 15:43:45 +01:00
function install_picom() {
local tmp=/tmp/picom
echo "Installing Picom."
rm -Rf "$tmp"
git clone https://github.com/yshui/picom.git "$tmp" || return 1
cd "$tmp" || return 2
git submodule update --init --recursive || return 3
meson --buildtype=release . build || return 4
ninja -C build || return 5
sudo ninja -C build install || return 6
cd || return 7
rm -Rf "$tmp"
}
2022-11-10 16:27:35 +01:00
function setup_homebrew() {
2024-04-11 12:49:48 +02:00
local dir="${XDG_DATA_HOME:-$HOME/.local/share}/homebrew"
2022-11-10 16:27:35 +01:00
echo "Setting up Homebrew."
if [ ! -d "$dir" ]; then
echo "Homebrew is already installed."
mkdir "$dir" || return 1
curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C "$dir" || return 2
fi
echo "Updating Homebrew."
eval "$("$dir/bin/brew" shellenv)"
brew update --force
chmod -R go-w "$(brew --prefix)/share/zsh"
}
function setup_brew_formulas() {
brew update || return 1
2022-12-14 20:54:12 +01:00
# shellcheck disable=SC2046
for package in $(get_packages $(construct_target_list brew)); do
2022-11-10 16:27:35 +01:00
if brew list --full-name | grep -Eq "(^| )$package($| )" >/dev/null; then
2022-12-14 20:29:00 +01:00
if $upgrade_packages; then
2022-11-10 16:27:35 +01:00
echo "Package '$package' will be upgraded."
brew upgrade "$package" || return 2
else
echo "Package '$package' already exists."
continue
fi
else
echo "Package '$package' will be installed."
brew install "$package" || return 2
fi
done
}
2022-12-14 09:41:02 +01:00
function setup_krew() {
2023-09-15 13:42:07 +02:00
set -e
cd "$(mktemp -d)"
OS="$(uname | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')"
KREW="krew-${OS}_${ARCH}"
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz"
tar zxvf "${KREW}.tar.gz"
./"${KREW}" install krew
2024-02-12 08:23:28 +01:00
export PATH="$KREW_ROOT/bin:$PATH"
2023-09-15 13:42:07 +02:00
set +e
2022-12-14 09:41:02 +01:00
}
2022-11-10 15:45:51 +01:00
function setup_krew_plugins() {
2022-12-14 20:28:08 +01:00
local targets=("krew-any")
2022-11-10 15:45:51 +01:00
if ! command -v kubectl-krew >/dev/null; then
echo "WARN: Krew not installed: kubectl-krew"
return 1
fi
2022-12-14 20:54:12 +01:00
# shellcheck disable=SC2046
for package in $(get_packages $(construct_target_list krew)); do
2022-11-10 15:45:51 +01:00
if kubectl-krew list | grep -q "^$package$" >/dev/null; then
2022-12-14 20:29:00 +01:00
if $upgrade_packages; then
2022-11-10 15:45:51 +01:00
echo "Package '$package' will be upgraded."
else
echo "Package '$package' already exists."
continue
fi
else
echo "Package '$package' will be installed."
fi
kubectl-krew install "$package" || return 2
done
}
2022-12-14 10:44:37 +01:00
function change_shell() {
sudo chsh -s "$(which zsh)" "$USER"
2022-12-14 10:18:11 +01:00
}
2022-11-10 15:43:45 +01:00
#
# Setup for Arch-like systems
#
function prepare_arch() {
which pamac >/dev/null && return 0
sudo pacman -S pamac --noconfirm || return 1
}
function setup_arch_with_pamac() {
2022-12-14 20:54:12 +01:00
# shellcheck disable=SC2046
sudo pamac install $(get_packages $(construct_target_list pacman)) --no-confirm || return 1
2022-11-10 15:43:45 +01:00
}
function setup_arch_with_yay() {
2022-12-14 20:54:12 +01:00
# shellcheck disable=SC2046
for package in $(get_packages $(construct_target_list aur)); do
2022-11-10 15:43:45 +01:00
if pacman -Qs "^$package$" >/dev/null; then
2022-12-14 20:29:00 +01:00
if $upgrade_packages; then
2022-11-10 15:43:45 +01:00
echo "Package '$package' will be upgraded."
else
echo "Package '$package' already exists."
continue
fi
else
echo "Package '$package' will be installed."
fi
sudo pamac build "$package" --no-confirm || return 1
done
}
2022-05-27 21:07:07 +02:00
function setup_arch() {
prepare_arch || return 1
setup_arch_with_pamac || return 2
setup_arch_with_yay || return 3
2022-12-14 20:54:12 +01:00
setup_pip_packages || return 4
2022-06-30 20:44:01 +02:00
configure_sddm || return 5
configure_ufw || return 6
2022-11-10 16:27:35 +01:00
setup_homebrew || return 7
setup_brew_formulas || return 8
2022-12-14 09:41:02 +01:00
setup_krew || return 9
setup_krew_plugins || return 10
2022-12-14 10:44:37 +01:00
change_shell || return 11
2022-05-27 21:07:07 +02:00
}
2022-11-10 15:43:45 +01:00
#
# Setup for Debian-like systems
#
2022-12-15 17:08:11 +01:00
function add_debian_keyring() {
local url="${1:?Missing key URL}" name="${2:?Missing key name}"
local file="/usr/share/keyrings/$name.gpg"
echo "Adding keyring for $name from $url."
wget -qO - "$url" | gpg --dearmor | sudo dd of="$file"
sudo chmod a+r "$file"
}
function setup_debian_repo_docker() {
echo "Setting up repository for Docker."
add_debian_keyring "https://download.docker.com/linux/$os_id/gpg" docker || return 1
echo "deb [ arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker.gpg ] https://download.docker.com/linux/$os_id $(lsb_release -cs) stable" |
sudo tee /etc/apt/sources.list.d/docker.list
}
2022-06-07 12:11:19 +02:00
function setup_debian_repo_vscodium() {
echo "Setting up repository for VSCodium."
2022-12-15 17:08:11 +01:00
add_debian_keyring https://gitlab.com/paulcarroty/vscodium-deb-rpm-repo/raw/master/pub.gpg vscodium || return 1
echo "deb [ signed-by=/usr/share/keyrings/vscodium.gpg ] https://download.vscodium.com/debs vscodium main" |
2022-06-07 12:11:19 +02:00
sudo tee /etc/apt/sources.list.d/vscodium.list
}
2024-04-11 13:16:57 +02:00
function setup_debian_repo_azure() {
echo "Setting up repository for Azure CLI."
add_debian_keyring "https://packages.microsoft.com/keys/microsoft.asc" "microsoft" || return 1
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/microsoft.gpg] https://packages.microsoft.com/repos/azure-cli/ $(lsb_release -cs) main" |
sudo tee /etc/apt/sources.list.d/azure-cli.list
}
2022-12-14 09:49:42 +01:00
function setup_debian_base_with_apt() {
2022-06-07 12:11:19 +02:00
sudo apt-get update
2022-12-14 20:54:12 +01:00
# shellcheck disable=SC2046
sudo apt-get install $(get_packages apt-base) -y || return 1
2022-12-14 09:49:42 +01:00
}
function setup_debian_with_apt() {
sudo apt-get update
2022-12-14 20:54:12 +01:00
# shellcheck disable=SC2046
sudo apt-get install $(get_packages $(construct_target_list apt)) -y || return 2
2022-06-07 12:11:19 +02:00
}
2022-06-08 10:18:53 +02:00
function setup_debian_with_git() {
2022-06-07 12:11:19 +02:00
install_picom || return 1
install_sddm_aerial_theme || return 2
}
function setup_debian() {
2022-12-14 09:49:42 +01:00
setup_debian_base_with_apt || return 1
setup_debian_repo_vscodium || return 2
2024-04-11 13:16:57 +02:00
setup_debian_repo_azure || return 3
setup_debian_repo_docker || return 4
setup_debian_with_apt || return 5
setup_pip_packages || return 6
setup_debian_with_git || return 7
configure_sddm || return 8
configure_ufw || return 9
setup_homebrew || return 10
setup_brew_formulas || return 11
setup_krew || return 12
setup_krew_plugins || return 13
change_shell || return 14
2022-06-07 12:11:19 +02:00
}
2022-12-15 17:08:11 +01:00
#
# Main
#
2022-12-14 11:48:21 +01:00
if [ "$EUID" -eq 0 ]; then
echo "Don't run this script as root." 1>&2
exit 2
fi
2022-12-15 17:08:11 +01:00
echo "Setting up ${os_id_like^:?}-like OS."
2022-12-15 17:08:11 +01:00
if [[ "$os_id_like" == "arch" ]]; then
2022-12-13 16:58:15 +01:00
setup_arch || echo "Setup failed: $?"
2022-12-15 17:08:11 +01:00
elif [[ "$os_id_like" == "debian" ]]; then
2022-12-13 16:58:15 +01:00
setup_debian || echo "Setup failed: $?"
2022-05-27 21:07:07 +02:00
else
2022-12-15 17:08:11 +01:00
echo "ERROR: Unsupported system: os_id_like=$os_id_like"
2022-12-14 11:48:21 +01:00
exit 3
2022-05-27 21:07:07 +02:00
fi
2022-12-15 17:08:11 +01:00
for group in video docker; do
echo "Adding $USER to '$group' group."
sudo usermod -aG "$group" "$USER"
done
2022-12-15 16:02:45 +01:00
2022-05-27 21:07:07 +02:00
echo "Setup finished."