Files
dotfiles/rebuild.sh
T
Lap Tran aae4dfd6d0 feat(bird): read X cookies from Brave only
Bird 0.8.0 asks Keychain for Chrome Safe Storage, but Brave stores its key
under Brave Safe Storage, so the reader fails before it can use Brave.

Manage the Bird config in Nix and add a wrapper at ~/.pi/agent/bin/bird.
The wrapper decrypts auth_token and ct0 from Brave cookie database with the
Brave Safe Storage key and passes them to bird as env vars. It has no
browser fallback, writes no Keychain item, and never opens the credentials
file.

Add an end-to-end shell check with a fake Brave cookie source and keychain.
The check fails before the wrapper exists and passes after implementation.
2026-11-28 15:46:40 -05:00

243 lines
8.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Rebuild nix-darwin + home-manager from this checkout.
#
# Before the switch, update the non-declarative tools listed below:
# npm globals (through Homebrew's node, never Nix's npm), no-mistakes,
# Hermes Agent, and the flake inputs. After the switch,
# update the installed agent skills and Pi extensions against the upgraded
# binaries.
#
# Rotates stale home-manager *.backup files before the switch so a second switch
# never fails with "would be clobbered".
# Optional updater failures warn and allow the rebuild to continue.
# A failed switch stops the script before post-switch updates.
set -euo pipefail
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
ln -sfn "$DIR" ~/.dotfiles
NPM=/opt/homebrew/bin/npm
# The npm globals listed below have no declarative updater.
# Use /opt/homebrew/bin/npm explicitly: the activation PATH can resolve
# Nix's npm, which cannot write the Homebrew prefix, and these packages belong to
# Homebrew's node. Each package is compared with the registry first, so a current
# copy is left alone and a failed lookup or install only warns.
echo "==> Updating npm global tools..."
if [ ! -x "$NPM" ]; then
echo "==> WARNING: $NPM not found (declare node in homebrew.brews). Skipping npm global updates."
else
npm_node=/opt/homebrew/bin/node
update_npm_global() {
pkg="$1"
installed="$("$NPM" ls -g --depth=0 --json 2>/dev/null \
| "$npm_node" -e '
let raw = "";
process.stdin.on("data", (chunk) => { raw += chunk; });
process.stdin.on("end", () => {
try {
const tree = JSON.parse(raw);
const entry = (tree.dependencies || {})[process.argv[1]] || {};
process.stdout.write(entry.version || "");
} catch {}
});
' "$pkg" || true)"
if [ -z "$installed" ]; then
return 0
fi
latest="$("$NPM" view "$pkg" version --fetch-timeout=15000 --fetch-retries=1 2>/dev/null \
| tr -d '[:space:]' || true)"
if [ -z "$latest" ]; then
echo "==> WARNING: cannot read the latest $pkg version from the registry. Keeping the installed copy."
return 0
fi
if [ "$installed" = "$latest" ]; then
echo "==> $pkg already at $latest"
return 0
fi
echo "==> $pkg $installed -> $latest"
if "$NPM" install -g "$pkg@$latest" >/dev/null 2>&1; then
echo "==> $pkg installed at $latest"
else
echo "==> WARNING: $pkg install of $latest failed. Keeping the installed copy."
fi
return 0
}
# home.activation.axiTools enforces the tasks-axi minimum without downgrades,
# so a newer release installed here survives the switch.
npm_globals=(
"@steipete/bird"
chrome-devtools-axi
defuddle
forgejo-axi
gh-axi
lavish-axi
quota-axi
tasks-axi
)
for pkg in "${npm_globals[@]}"; do
update_npm_global "$pkg"
done
echo "==> npm global tools update complete"
fi
# Remove retired npm OpenCode installs if an older rebuild installed them:
# the `@opencode-ai/cli` beta and the older `opencode-ai` CLI. The Homebrew
# `opencode` formula declared in configuration.nix is now the only OpenCode
# install, unpinned so it tracks the latest release. The guard is a local check
# for npm, so a rebuild without npm skips this instead of failing.
if [ -x "$NPM" ] && npm_root="$("$NPM" root -g 2>/dev/null)"; then
for pkg in @opencode-ai/cli opencode-ai; do
if [ -d "$npm_root/$pkg" ]; then
if "$NPM" uninstall -g "$pkg" >/dev/null 2>&1; then
echo "==> Removed retired npm package $pkg"
else
echo "==> WARNING: $pkg removal failed. Remove it manually: $NPM uninstall -g $pkg"
fi
fi
done
fi
# `opencode` links without --overwrite, so any npm shim left in the Homebrew
# prefix aborts the switch's brew bundle link step. Drop the retired packages'
# shims directly, which also covers a failed uninstall and a missing npm.
for link in /opt/homebrew/bin/*; do
case "$(readlink "$link" 2>/dev/null || true)" in
*/node_modules/@opencode-ai/cli/* | */node_modules/opencode-ai/*)
rm -f "$link"
echo "==> Removed stale OpenCode npm shim $link"
;;
esac
done
echo "==> Updating no-mistakes..."
NO_MISTAKES_BIN="$HOME/.no-mistakes/bin/no-mistakes"
if [ -x "$NO_MISTAKES_BIN" ]; then
if "$NO_MISTAKES_BIN" update 2>&1; then
echo "==> no-mistakes update complete"
else
echo "==> WARNING: no-mistakes update failed. Skipping."
fi
elif command -v no-mistakes >/dev/null 2>&1; then
if no-mistakes update 2>&1; then
echo "==> no-mistakes update complete"
else
echo "==> WARNING: no-mistakes update failed. Skipping."
fi
else
echo "==> WARNING: no-mistakes not found. Skipping update."
fi
# Hermes Agent is a mutable git clone at ~/.hermes/hermes-agent, so Nix and
# Homebrew never move it. Its own updater pulls the configured channel and
# restarts the services that run its new code; --yes keeps the rebuild
# non-interactive.
echo "==> Updating Hermes Agent..."
if ! command -v hermes >/dev/null 2>&1; then
echo "==> WARNING: hermes not found. Skipping update."
elif hermes update --yes 2>&1; then
echo "==> hermes update complete"
else
echo "==> WARNING: hermes update failed. Keeping the installed version."
fi
stamp="$(date +%Y%m%d-%H%M%S)"
# Paths managed by home.file, including config links and Nix package binaries.
managed=(
"$HOME/.grok/config.toml"
"$HOME/.grok/AGENTS.md"
"$HOME/.claude/settings.json"
"$HOME/.claude/CLAUDE.md"
"$HOME/.codex/AGENTS.md"
"$HOME/.codex/luna.config.toml"
"$HOME/.config/herdr/config.toml"
"$HOME/.config/bird/config.json5"
"$HOME/.config/opencode/AGENTS.md"
"$HOME/.config/opencode/opencode.json"
"$HOME/.config/opencode2/opencode.json"
"$HOME/.config/opencode2/AGENTS.md"
"$HOME/.local/bin/actionlint"
"$HOME/.local/bin/shellcheck"
"$HOME/.local/bin/oc2"
"$HOME/.local/bin/opencode-go-token"
"$HOME/.local/bin/opencode-go-key-sync"
"$HOME/.pi/agent/settings.json"
"$HOME/.pi/agent/models.json"
"$HOME/.pi/agent/AGENTS.md"
"$HOME/.pi/agent/bin/bird"
"$HOME/.pi/agent/extensions/terminal-status-title.js"
)
for path in "${managed[@]}"; do
bak="${path}.backup"
if [ -e "$bak" ] || [ -L "$bak" ]; then
mv "$bak" "${bak}.${stamp}"
echo "rotated stale backup: ${bak} -> ${bak}.${stamp}"
fi
done
# Directory targets use the same backup suffix on collision.
for path in \
"$HOME/.config/wezterm" \
"$HOME/.config/nvim" \
"$HOME/.config/opencode/skills" \
"$HOME/.pi/agent/themes" \
"$HOME/.pi/agent/extensions/calm"
do
bak="${path}.backup"
if [ -e "$bak" ] || [ -L "$bak" ]; then
mv "$bak" "${bak}.${stamp}"
echo "rotated stale backup: ${bak} -> ${bak}.${stamp}"
fi
done
echo "==> Updating flake inputs..."
if nix flake update --flake ~/.dotfiles 2>&1; then
echo "==> flake update complete"
else
echo "==> WARNING: flake update failed. Continuing with locked inputs."
fi
sudo darwin-rebuild switch --flake ~/.dotfiles#lt-mbp
# Update tracked skills after activation. Exclude security-audit to preserve
# home.activation.securityAuditSkill's freeze policy. Untracked skills,
# including the pinned pstack copies, are outside this updater's scope.
echo "==> Updating agent skills..."
NPX=/opt/homebrew/bin/npx
SKILL_LOCK="$HOME/.agents/.skill-lock.json"
if [ ! -x "$NPX" ]; then
echo "==> WARNING: $NPX not found. Skipping agent skill update."
elif [ ! -f "$SKILL_LOCK" ]; then
echo "==> WARNING: $SKILL_LOCK not found. Skipping agent skill update."
else
skill_names="$(/usr/bin/python3 - "$SKILL_LOCK" 2>/dev/null <<'PY' || true
import json
import sys
with open(sys.argv[1]) as lock_file:
skills = json.load(lock_file).get("skills", {})
for name in sorted(skills):
if name != "security-audit":
print(name)
PY
)"
if [ -z "$skill_names" ]; then
echo "==> WARNING: no tracked skills found in $SKILL_LOCK. Skipping agent skill update."
else
# Unquoted on purpose: each tracked name is a separate CLI argument.
# shellcheck disable=SC2086
if "$NPX" --yes skills@latest update -g -y $skill_names 2>&1; then
echo "==> agent skill update complete"
else
echo "==> WARNING: agent skill update failed (network or Skills CLI issue). Skipping."
fi
fi
fi
# Update installed pi packages after the switch, because the Homebrew bundle
# may have upgraded the pi binary.
echo "==> Updating pi extensions..."
if ! command -v pi >/dev/null 2>&1; then
echo "==> WARNING: pi not found. Skipping extension update."
elif pi update --extensions --no-approve 2>&1; then
echo "==> pi extension update complete"
else
echo "==> WARNING: pi extension update failed. Skipping."
fi