1
0
Fork 0
gnu-plus-dotfiles/run_once_40-install-sway.sh.tmpl
rain b40d724f6c Make run_once scripts sudo-prompt-free when packages already present
Several run_once scripts unconditionally called sudo pacman/apt to
install packages — even on boxes where every package was already
present. That triggered a sudo password prompt on every fresh
chezmoi apply for nothing.

Two changes:

1. .chezmoi.yaml.tmpl: fall back to ~/.local/bin/age if /usr/bin/age
   isn't installed (matters during initial bootstrap before age is
   installed system-wide).

2. run_once_*.sh.tmpl: detect missing packages first; only call sudo
   if there's actually something to install. For the LAN hosts script,
   detect the existing block and skip if it's already correct.

These changes are transparent on boxes that already had everything
installed (the existing 5): no behavior change. They reduce sudo
prompts on bit (the new box, where most packages are pre-installed)
from ~5 prompts to 1 (just for /etc/hosts).
2026-06-22 15:10:49 -04:00

72 lines
No EOL
2.4 KiB
Bash

#!/usr/bin/env bash
# =============================================================================
# run_once_40-install-sway.sh.tmpl
# Install the sway + wofi + foot + supporting tooling desktop stack.
#
# Gated on .sway (set in .chezmoi.yaml.tmpl). When false (Pis, headless
# boxes, etc.), this script exits 0 without doing anything.
#
# To opt a new box in: `touch ~/.config/chezmoi/features/sway` then
# `chezmoi apply`. To opt out: `touch ~/.config/chezmoi/features/no-sway`.
#
# Idempotent: skips if sway is already installed.
# =============================================================================
set -euo pipefail
log() { printf '\033[1;34m[sway]\033[0m %s\n' "$*"; }
# --- 0. Gate on the .sway flag ---
{{ if not .sway_setup -}}
log "sway_setup not enabled for this host (.sway_setup=false). Skipping."
log "To enable: touch ~/.config/chezmoi/features/sway && chezmoi apply"
exit 0
{{ end -}}
# --- 1. Install packages ---
SWAY_PKGS=(sway wofi foot swaybg swaylock swayidle grim slurp waybar wl-clipboard)
{{ if eq .os_family "arch" -}}
# mako is in arch [extra]
SWAY_PKGS+=(mako)
{{ else if eq .os_family "debian" -}}
# mako isn't packaged on debian. Use dunst (lightweight notification daemon,
# widely compatible with sway/waybar setups).
SWAY_PKGS+=(dunst)
{{ else -}}
log "WARNING: sway packages not configured for os_family={{ .os_family }}"
{{ end -}}
log "checking sway stack: ${SWAY_PKGS[*]}"
# Only invoke sudo if any of the packages are missing. Bit has most
# already; byte/kaiser are full. This avoids a no-op sudo prompt.
MISSING_PKGS=()
for p in "${SWAY_PKGS[@]}"; do
if ! command -v "$p" >/dev/null 2>&1; then
MISSING_PKGS+=("$p")
fi
done
if (( ${#MISSING_PKGS[@]} > 0 )); then
log "missing: ${MISSING_PKGS[*]}"
{{ if eq .os_family "arch" -}}
sudo pacman -S --needed --noconfirm "${MISSING_PKGS[@]}"
{{ else if eq .os_family "debian" -}}
export DEBIAN_FRONTEND=noninteractive
sudo apt-get install -y --no-install-recommends "${MISSING_PKGS[@]}"
{{ end -}}
else
log "all sway packages already installed; skipping"
fi
log "sway stack installed"
sway --version 2>&1 | head -1
foot --version 2>&1 | head -1
wofi --version 2>&1 | head -1
# --- 2. Mark the box so subsequent chezmoi applies know sway is enabled ---
mkdir -p ~/.config/chezmoi/features
touch ~/.config/chezmoi/features/sway
log "marked box: ~/.config/chezmoi/features/sway"
log "sway stack install complete"