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

@ -54,9 +54,18 @@ if [[ ! -f "$HOSTS_FILE" ]]; then
exit 1
fi
# If our block already exists, remove it first (so re-runs don't duplicate)
# If our block already exists with all entries, skip. Otherwise rewrite.
# This avoids a no-op sudo prompt on boxes that already have the block.
if grep -q "$LAN_BLOCK_BEGIN" "$HOSTS_FILE" 2>/dev/null \
&& grep -q "miche.local" "$HOSTS_FILE" 2>/dev/null \
&& grep -q "bit.local" "$HOSTS_FILE" 2>/dev/null; then
log "LAN block already present in $HOSTS_FILE; skipping"
exit 0
fi
# If our block exists but is stale (missing some entries), remove it first
if grep -q "$LAN_BLOCK_BEGIN" "$HOSTS_FILE"; then
log "removing old LAN block"
log "stale LAN block detected; removing before re-adding"
sudo cp "$HOSTS_FILE" "${HOSTS_FILE}.bak.$(date +%s)"
sudo sed -i "/$LAN_BLOCK_BEGIN/,/$LAN_BLOCK_END/d" "$HOSTS_FILE"
fi