diff --git a/README.md b/README.md index e50ddd5..3c71442 100644 --- a/README.md +++ b/README.md @@ -208,9 +208,11 @@ For a fresh installation, set `DOTFILES_PROFILE=full-work`. `headless` remains t | Command | What | |---|---| +| `dot sync` | `git pull --ff-only` then switch — apply small dotfiles edits without upgrading anything (see below) | | `dot update` | `git pull`, switch, `flatpak update`, `mise upgrade`, `claude update` | | `dot update --inputs` | same, plus bump `flake.lock` (nixpkgs, home-manager, plasma-manager, konsole_daynight) | -| `dot switch` | apply after editing Nix files | +| `dot update --switch-only` | alias for `dot sync` | +| `dot switch` | apply after editing Nix files, without pulling first | | `dot profile headless` / `full` / `full-work` / `auto` | switch layers; then `dot switch` | | `dot secrets push` | upload this machine's secrets.env, SSH keys and WireGuard configs to Bitwarden | | `dot defguard` | preconfigure the Defguard VPN app for the home lab (see below) | @@ -218,6 +220,38 @@ For a fresh installation, set `DOTFILES_PROFILE=full-work`. `headless` remains t | `dot sounds` | build the Portal 2 sound theme from your Steam install | | `dot doctor` | what's installed or missing | +### `dot update` versus `dot sync` + +`dot update` is a maintenance run: process upgrades for Flatpak, mise and Claude +Code, a missing-app check (`dot apps`) and, with `--inputs`, a `flake.lock` bump. +That is slow and unnecessary when you only changed a dotfiles file — a secretctl +edit or a new package does not touch `flake.lock`. + +`dot sync` does the small-change path and nothing else: + +```bash +dot sync # git -C ~/.dotfiles pull --ff-only, then home-manager switch +dot sync headless # same, switching to another profile +dot update --switch-only # alias for dot sync +``` + +It never runs `flatpak update`, `mise upgrade`, `claude update`, `dot apps` or +`nix flake update`; `dot sync --inputs` is refused rather than ignored. Two +exceptions worth knowing: + +- **A newly added package is still built once.** `secretctl` comes from `pkgs/` + and is compiled from source, so the first build may fetch a Go toolchain and + build dependencies far bigger than the tool. Later syncs reuse the store path. + If that build prints `git-minimal`/store-lock timeouts, another + Home Manager run is in progress — see the next point. +- **Switches are serialised.** `dot switch`, `dot sync` and `dot update` take + `$HOME/.config/dotfiles/switch.lock`; a second run prints the running pid and + waits instead of competing for the Nix store. Nothing needs to be started in a + second terminal — start one and let it finish. If you did start two, cancel the + newer one in its own terminal. + +Run `bash tests/sync.sh` to check that the sync path stays upgrade-free. + `dot doctor` checks Nix startup, store/database access, broken profile links, daemon versus single-user setup, `nix-command` and flakes, and the checkout's flake files. This catches interrupted installers and feature overrides before diff --git a/bin/dot b/bin/dot index 9f9fd63..10e0018 100755 --- a/bin/dot +++ b/bin/dot @@ -149,8 +149,22 @@ cmd_nix() { } # ---------------------------------------------------------------- home-manager +# Two home-manager runs in one home fight over the Nix store (git-minimal lock) +# and over ~/.local/state/nix/profiles. Serialise them instead of racing. +switch_lock() { + local lock="$STATE_DIR/switch.lock" + have flock || return 0 + exec 9>>"$lock" || return 0 + if ! flock -n 9; then + warn "another dot switch/update is running (pid $(cat "$lock" 2>/dev/null || echo '?')); waiting for it to finish" + flock 9 || die "cannot lock $lock" + fi + printf '%s\n' "$$" >"$lock" +} + cmd_switch() { [ -n "${1:-}" ] && cmd_profile "$1" + switch_lock source_nix || die "nix missing: run dot nix" ensure_nix_features local attr backup_ext; attr="$(flake_attr)" @@ -771,7 +785,32 @@ cmd_sounds() { } # ---------------------------------------------------------------- update / doctor +# Pull the checkout and apply it: no package/tool/app upgrades, no flake input bump. +# A newly added package still gets built once here, which can take a while. +cmd_sync() { + case " ${*:-} " in + *" --inputs "*) die "dot sync never bumps flake inputs; use: dot update --inputs" ;; + esac + step "pull dotfiles" + git -C "$DOTFILES_DIR" pull --ff-only || warn "git pull failed; continuing with local tree" + cmd_switch "$@" + ok "dotfiles synced (no app, tool or flake-input upgrades)" +} + cmd_update() { + local args=() a sync_only=0 + for a in "$@"; do + case "$a" in + --switch-only) sync_only=1 ;; + *) args+=("$a") ;; + esac + done + if [ "$sync_only" = 1 ]; then + step "dot update --switch-only → dot sync" + cmd_sync ${args[@]+"${args[@]}"} + return + fi + set -- ${args[@]+"${args[@]}"} step "pull dotfiles" git -C "$DOTFILES_DIR" pull --rebase --autostash || warn "git pull failed; continuing with local tree" if [ "${1:-}" = --inputs ]; then @@ -867,6 +906,7 @@ dot — portable dev environment dot bootstrap first-time setup (nix → system → switch → apps) dot update [--inputs] pull repo, (bump flake inputs), switch, update flatpaks/mise/claude + dot sync [profile] pull repo (--ff-only) and switch only — no upgrades, no flake input bump dot switch [profile] apply the home-manager config dot profile [full|full-work|headless|auto] dot system [--netbird] [--defguard] sudo layer: distro packages, browser PWA policies, netbird, Defguard VPN service, login shell @@ -885,7 +925,7 @@ EOF main() { local cmd="${1:-help}"; shift || true case "$cmd" in - bootstrap|update|switch|profile|system|nix|apps|secrets|defguard|wallpapers|sounds|boxes|doctor) "cmd_$cmd" "$@" ;; + bootstrap|update|sync|switch|profile|system|nix|apps|secrets|defguard|wallpapers|sounds|boxes|doctor) "cmd_$cmd" "$@" ;; help|-h|--help) usage ;; *) usage; exit 1 ;; esac diff --git a/tests/sync.sh b/tests/sync.sh new file mode 100755 index 0000000..4f00aa5 --- /dev/null +++ b/tests/sync.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# dot sync: pull --ff-only + switch only. No upgrades, no flake input bump. +set -euo pipefail +cd "$(dirname "$0")/.." + +fixture="$(mktemp -d)" +trap 'rm -rf "$fixture"' EXIT +export HOME="$fixture/home" +mkdir -p "$HOME/.config/dotfiles" "$fixture/repo" +DOTFILES_DIR="$fixture/repo" +export DOTFILES_DIR + +# Load the CLI without running main(). +source <(sed '$d' bin/dot) +STATE_DIR="$HOME/.config/dotfiles" + +log="$fixture/calls.log" +: >"$log" +git() { printf 'git %s\n' "$*" >>"$log"; } +cmd_switch() { printf 'switch %s\n' "${*:-}" >>"$log"; } +flatpak() { printf 'flatpak %s\n' "$*" >>"$log"; } +mise() { printf 'mise %s\n' "$*" >>"$log"; } +claude() { printf 'claude %s\n' "$*" >>"$log"; } +cmd_apps() { printf 'apps\n' >>"$log"; } +have() { case "$1" in git|flatpak|mise|claude) return 0;; *) return 1;; esac; } + +cmd_sync + +grep -q '^git -C .* pull --ff-only$' "$log" || { echo "FAIL: no ff-only pull"; cat "$log"; exit 1; } +grep -q '^switch' "$log" || { echo "FAIL: no switch"; cat "$log"; exit 1; } +for unwanted in flatpak mise claude apps; do + if grep -qi "^$unwanted" "$log"; then echo "FAIL: dot sync ran $unwanted"; cat "$log"; exit 1; fi +done + +# --inputs is refused rather than silently ignored. +: >"$log" +if ( cmd_sync --inputs ) 2>/dev/null; then echo "FAIL: dot sync accepted --inputs"; exit 1; fi +[ -s "$log" ] && { echo "FAIL: dot sync --inputs still pulled"; cat "$log"; exit 1; } + +# `dot sync ` passes the profile through to the switch. +: >"$log" +cmd_sync headless +grep -q '^switch headless$' "$log" || { echo "FAIL: profile not forwarded"; cat "$log"; exit 1; } + +# The concurrency guard serialises a second run instead of racing it. +exec 8>>"$STATE_DIR/switch.lock" +flock 8 +switch_lock_ok=0 +if flock -n "$STATE_DIR/switch.lock" 2>/dev/null; then switch_lock_ok=1; fi +[ "$switch_lock_ok" = 0 ] || { echo "FAIL: lock not held by a concurrent run"; exit 1; } + +echo "ok dot sync"