#!/usr/bin/env bash # Bootstrap / sync an Ubuntu machine from this repo. Idempotent: re-run it # whenever apt-packages.txt, ubuntu/etc/**, or the Home Manager config change. set -euo pipefail REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" HERE="$REPO/ubuntu" HM_TARGET="${HM_TARGET:-ubuntu}" ALL_STEPS=(nix repos apt etc services shell tools home) # Tools installed with their upstream install script instead of Nix/apt. TOOLS=( "mise|https://mise.run" "herdr|https://herdr.dev/install.sh" "tuicr|https://tuicr.dev/install.sh" ) log() { printf '\n\033[1;34m==> %s\033[0m\n' "$*"; } info() { printf ' %s\n' "$*"; } # Strip comments/blank lines from a list file. read_list() { sed -e 's/#.*//' -e 's/[[:space:]]*$//' -e '/^$/d' "$1"; } step_nix() { log "nix" if command -v nix >/dev/null 2>&1; then info "already installed: $(nix --version)" return fi # Determinate's installer, but upstream Nix (makes it easier to uninstall if needed). curl -fsSL https://install.determinate.systems/nix \ | sh -s -- install --no-confirm --prefer-upstream-nix # shellcheck disable=SC1091 . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh } step_repos() { log "third-party apt repos" local codename changed=0 # shellcheck disable=SC1091 codename="$(. /etc/os-release && echo "$VERSION_CODENAME")" # Tailscale: https://tailscale.com/kb/1031/install-linux if [ ! -f /etc/apt/sources.list.d/tailscale.list ]; then curl -fsSL "https://pkgs.tailscale.com/stable/ubuntu/${codename}.noarmor.gpg" \ | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null curl -fsSL "https://pkgs.tailscale.com/stable/ubuntu/${codename}.tailscale-keyring.list" \ | sudo tee /etc/apt/sources.list.d/tailscale.list >/dev/null changed=1 fi info "tailscale repo present" # Dux Labs PPA, for niri: https://duxlabs.dev/docs/repo # The pin has to be in place before the first apt-get update. local pin=apt/preferences.d/duxlabs-dux-repo if ! sudo cmp -s "$HERE/etc/$pin" "/etc/$pin"; then sudo install -D -m 0644 -o root -g root "$HERE/etc/$pin" "/etc/$pin" changed=1 fi if ! compgen -G "/etc/apt/sources.list.d/duxlabs-ubuntu-dux-repo-*" >/dev/null; then sudo add-apt-repository -y -n ppa:duxlabs/dux-repo changed=1 fi info "dux labs ppa present (pinned to niri, xwayland-satellite)" # Noctalia: https://docs.noctalia.dev/noctalia/getting-started/installation/#debian # The keyring is a package in the repo, so apt keeps it current. if [ ! -f /usr/share/keyrings/nickh-archive-keyring.gpg ]; then local tmp tmp="$(mktemp -d)" curl -fsSL -o "$tmp/nickh-archive-keyring.deb" https://pkg.noctalia.dev/deb/nickh-archive-keyring.deb sudo dpkg -i "$tmp/nickh-archive-keyring.deb" rm -rf "$tmp" fi if [ ! -f "/etc/apt/sources.list.d/noctalia-${codename}.sources" ]; then curl -fsSL "https://pkg.noctalia.dev/deb/noctalia-${codename}.sources" \ | sudo tee "/etc/apt/sources.list.d/noctalia-${codename}.sources" >/dev/null changed=1 fi info "noctalia repo present" if [ "$changed" = 1 ]; then sudo apt-get update; fi } step_apt() { log "apt packages" local pkgs=() missing=() p mapfile -t pkgs < <(read_list "$HERE/apt-packages.txt") pkgs+=(tailscale) # From step_repos. for p in "${pkgs[@]}"; do dpkg-query -W -f='${Status}' "$p" 2>/dev/null | grep -q 'install ok installed' \ || missing+=("$p") done if [ ${#missing[@]} -eq 0 ]; then info "all ${#pkgs[@]} packages installed" return fi info "installing: ${missing[*]}" sudo apt-get update sudo apt-get install -y "${missing[@]}" } # Copy ubuntu/etc/** to /etc. Only touches files that differ, and reloads the # daemons whose config changed. Removed files are orphaned. step_etc() { log "/etc files" local src rel dst changed=() while IFS= read -r -d '' src; do rel="${src#"$HERE/etc/"}" dst="/etc/$rel" if sudo cmp -s "$src" "$dst"; then info "unchanged $dst" continue fi if sudo test -e "$dst"; then sudo diff -u "$dst" "$src" || true fi sudo install -D -m 0644 -o root -g root "$src" "$dst" info "updated $dst" changed+=("$rel") done < <(find "$HERE/etc" -type f -print0 | sort -z) local rel_changed for rel_changed in "${changed[@]}"; do case "$rel_changed" in keyd/*) # (Ubuntu ships the CLI as keyd.rvaiya) if systemctl is-active -q keyd.service; then sudo systemctl restart keyd.service && info "restarted keyd"; fi ;; logid.cfg) if systemctl is-active -q logid.service; then sudo systemctl restart logid.service && info "restarted logid"; fi ;; systemd/logind.conf.d/*) sudo systemctl reload systemd-logind.service && info "reloaded logind" ;; udev/rules.d/*) sudo udevadm control --reload-rules && sudo udevadm trigger --action=change && info "reloaded udev rules" ;; esac done } step_services() { log "system services" local unit for unit in keyd.service logid.service docker.service tailscaled.service; do if ! systemctl cat "$unit" >/dev/null 2>&1; then info "skip $unit (not installed)" continue fi if ! systemctl is-enabled -q "$unit" 2>/dev/null || ! systemctl is-active -q "$unit"; then sudo systemctl enable --now "$unit" fi info "running $unit" done # dialout: serial ports (ttyUSB/ttyACM). local group for group in docker dialout; do if getent group "$group" >/dev/null && ! id -nG "$USER" | grep -qw "$group"; then sudo usermod -aG "$group" "$USER" info "added $USER to $group group (log out/in to apply)" fi done } step_shell() { log "login shell" local want=/usr/bin/zsh current current="$(getent passwd "$USER" | cut -d: -f7)" if [ "$current" = "$want" ]; then info "already $want" return fi sudo chsh -s "$want" "$USER" info "changed $current -> $want (log out/in to apply)" } step_tools() { log "self-installing tools (~/.local/bin)" local entry name url for entry in "${TOOLS[@]}"; do IFS='|' read -r name url <<<"$entry" # Also check ~/.local/bin directly: on a fresh machine this script may run from a shell that doesn't have it on PATH yet. if command -v "$name" >/dev/null 2>&1 || [ -x "$HOME/.local/bin/$name" ]; then info "already installed: $name" continue fi info "installing $name from $url" # TUICR_INSTALL_YES skips tuicr's confirmation prompt. curl -fsSL "$url" | TUICR_INSTALL_YES=1 sh done } step_home() { log "home-manager ($HM_TARGET)" local out out="$(nix build --no-link --print-out-paths "$REPO#homeConfigurations.$HM_TARGET.activationPackage")" HOME_MANAGER_BACKUP_EXT=hm-backup "$out/activate" if flatpak info --user app.zen_browser.zen >/dev/null 2>&1 \ && flatpak info --system app.zen_browser.zen >/dev/null 2>&1; then sudo flatpak uninstall --system -y --noninteractive app.zen_browser.zen info "removed system-wide Zen (user install takes over)" fi } main() { local steps=("$@") s [ ${#steps[@]} -eq 0 ] && steps=("${ALL_STEPS[@]}") for s in "${steps[@]}"; do if ! declare -F "step_$s" >/dev/null; then echo "unknown step: $s (valid: ${ALL_STEPS[*]})" >&2 exit 1 fi done for s in "${steps[@]}"; do "step_$s"; done log "done" cat <<'EOF' First run only: - sudo tailscale up - log out and back in (zsh login shell, docker group, keyd is live already) EOF } main "$@"