1
0
Fork 0

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).
This commit is contained in:
Rain 2026-06-22 15:10:49 -04:00
parent a2cc669b22
commit b40d724f6c
5 changed files with 109 additions and 33 deletions

View file

@ -14,23 +14,45 @@ die() { printf '\033[1;31m[bootstrap ERROR]\033[0m %s\n' "$*" >&2; exit 1; }
# will be prompted once per sudo invocation.
{{ if eq .os_family "arch" -}}
log "pacman-sync"
sudo pacman -Sy --noconfirm
# Only sync the package DB if anything is missing. Avoids a no-op sudo
# (which would still prompt for a password even when there's nothing to
# install) on boxes where all the bootstrap tools are already present.
MISSING_PKGS=()
for p in age curl ca-certificates git base-devel wget; do
if ! command -v "$p" >/dev/null 2>&1 && ! pacman -Qi "$p" >/dev/null 2>&1; then
MISSING_PKGS+=("$p")
fi
done
log "install base tools (arch)"
PACMAN_PKGS=(age curl ca-certificates git base-devel wget)
sudo pacman -S --needed --noconfirm "${PACMAN_PKGS[@]}"
if (( ${#MISSING_PKGS[@]} > 0 )); then
log "pacman-sync (missing: ${MISSING_PKGS[*]})"
sudo pacman -Sy --noconfirm
log "install base tools (arch)"
sudo pacman -S --needed --noconfirm "${MISSING_PKGS[@]}"
else
log "all base tools already installed; skipping pacman"
fi
{{ else if eq .os_family "debian" -}}
export DEBIAN_FRONTEND=noninteractive
log "apt-update"
sudo apt-get update -y
log "apt-upgrade"
sudo apt-get upgrade -y
# Only run apt if anything is missing, so a no-op sudo isn't required.
MISSING_PKGS=()
for p in age curl ca-certificates git wget gnupg libssl-dev pkg-config; do
if ! command -v "$p" >/dev/null 2>&1; then
MISSING_PKGS+=("$p")
fi
done
log "install base tools (debian)"
APT_PKGS=(age curl ca-certificates git wget gnupg libssl-dev pkg-config)
sudo apt-get install -y --no-install-recommends "${APT_PKGS[@]}"
if (( ${#MISSING_PKGS[@]} > 0 )); then
log "apt-update (missing: ${MISSING_PKGS[*]})"
sudo apt-get update -y
log "apt-upgrade"
sudo apt-get upgrade -y
log "install base tools (debian)"
sudo apt-get install -y --no-install-recommends "${MISSING_PKGS[@]}"
else
log "all base tools already installed; skipping apt"
fi
{{ else -}}
die "unsupported os_family: {{ .os_family }} (this script supports arch or debian)"