#!/usr/bin/env bash # Moves pkgs/t3code/lock.nix to upstream's latest stable release tag. # # Runs from apps/daily-update/daily-update.sh, after `nix flake update` and # before the system and home switches: # 1. resolve the stable tag through the GitHub releases API, then the tag to # a commit # 2. rewrite the lock with the new tag and rev and a placeholder hash for # each of the three fixed outputs # 3. build the package through the deploy wrapper's nixpkgs pins, and each # time nix reports a fixed-output hash mismatch, write the hash it reports # and build again # 4. commit the lock # # The releases/latest endpoint only ever serves a stable release: GitHub # excludes drafts and prereleases from it by definition. The tag is still # checked against the stable vX.Y.Z shape before use, so a nightly, preview, # or mobile build tag can never move the pin even if the endpoint ever served # one. A tag older than the locked one is refused too: the endpoint orders by # publish date, so a re-published older patch would otherwise move the pin # backwards. Either refusal keeps the old pin and exits 0, same as a tag that # does not build. # # The rev is reconciled from the tag on every run, not just when the tag # moves: a kill between the tag and rev writes used to leave tag=new with # rev=old, and the next run then built the old commit while committing it as # the new tag. A rev that disagrees with the tag is rewritten with placeholder # hashes and relearned, same as a fresh tag. # # Every run builds, including one where the tag has not moved. A cached pin # costs a couple of seconds and proves the lock still builds: a hash that # nixpkgs invalidated under a fixed rev shows up here as a mismatch instead of # failing the switch later. That is also why a tag bump relearns every hash # rather than keeping the old pnpm and cargo ones as guesses. A guess that # still resolves to a store path nix already has is reused without a mismatch, # and the build then dies inside t3code-unwrapped with # ERR_PNPM_NO_OFFLINE_TARBALL, which reports no hash to learn from. # # Every way out other than a finished build puts the previous lock back, so the # switch never sees a half-written pin and an interrupted run cannot wedge it. # A build failure (nixpkgs changed the recipe under us, or the tag is broken) # restores the previous lock and exits 0, so the old T3 keeps working and the # rest of the update still runs. Run it by hand to skip the wait for 08:30. set -euo pipefail DOTFILES=${DOTFILES:-$(cd -- "$(dirname -- "$(readlink -f -- "$0")")/../.." && pwd)} DEPLOY=${DEPLOY:-$DOTFILES/deploy-system} REMOTE=https://github.com/pingdotgg/t3code RELEASE_API=https://api.github.com/repos/pingdotgg/t3code/releases/latest LOCK=$DOTFILES/pkgs/t3code/lock.nix PKG=$DOTFILES/pkgs/t3code/default.nix FAKE=sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= HASHES=(srcHash pnpmDepsHash cargoHash) notify() { command -v notify-send >/dev/null && notify-send "$@" || true; } lock_value() { if [ -f "$LOCK" ]; then sed -n "s/^ $1 = \"\(.*\)\";$/\1/p" "$LOCK"; fi } # Writes through a sibling file and renames, so a kill can never leave a # truncated lock for the next run to trust. write_lock() { cat > "$LOCK.new" mv -f "$LOCK.new" "$LOCK" } set_lock_value() { sed -i "s|^ $1 = \".*\";\$| $1 = \"$2\";|" "$LOCK"; } # Builds the package exactly the way the home switch will: the package set comes # from lib/pkgs.nix, read through the deploy wrapper's locked inputs, which is # the set home-manager evaluates (the wrapper overrides my-dotfiles with the # live tree, so the module and this expression resolve the same inputs). Config # and overlays therefore live in one place instead of here. # # The recipe comes from the unstable-small pin: that tree carries a recipe new # enough for a 0.1.x tag. The codex provider is off, so nothing else is taken # from it. # Building against the stable set is what keeps this cheap: its channel cache # carries electron, and unstable-small's does not, so a build from that set # would compile Chromium. The wrapper's lock is the one source of truth; a hand # `nix flake update` on the checkout alone is the one way these can drift. build_pkg() { nix build --no-link --impure --cores "${CORES:-4}" --expr " let deploy = builtins.getFlake \"path:$DEPLOY\"; # The wrapper's pins, which my-dotfiles follows, so this is the same set # home-manager evaluates. Reading deploy.inputs.my-dotfiles.inputs instead # would re-resolve the whole dotfiles graph, submodules included, for a # minute of nothing. dotfiles = import $DOTFILES/lib/pkgs.nix { inputs = deploy.inputs; }; in import $PKG { pkgs = dotfiles.stable; recipeSrc = dotfiles.unstable-small.path; } " } # Which of the three hashes a failed fixed-output derivation belongs to. classify() { case "$1" in *-source.drv) echo srcHash ;; *-pnpm-deps.drv) echo pnpmDepsHash ;; *vendor*.drv) echo cargoHash ;; *) echo "" ;; esac } # Every fixed-output hash mismatch in a build log, as "drv hash" lines. The # apostrophes around the derivation path are dropped first so the pattern stays # readable. nix prints one block per mismatched output, sometimes several in one # log: # error: hash mismatch in fixed-output derivation '/nix/store/x-source.drv': # specified: sha256-old # got: sha256-new # `got:` is anchored to the start of the line so nixpkgs' own advice text # ("3. Copy the 'got: sha256-' value back...") is not read as a hash. mismatches() { tr -d "'" | awk ' /in fixed-output derivation / { drv = $0 sub(/.*in fixed-output derivation /, "", drv) sub(/:.*/, "", drv) next } /^[[:space:]]*got:[[:space:]]*sha256-/ { got = $0 sub(/^[[:space:]]*got:[[:space:]]*/, "", got) sub(/[^A-Za-z0-9+\/=-].*/, "", got) if (drv != "") print drv, got drv = "" } ' } # curl is bare on purpose: the timer inherits the user manager PATH, whose # trailing /run/current-system/sw/bin entry links the system curl, git, and # nix, the same PATH the daily switch already relies on for bare nix and git. release_json=$(curl -fsSL "$RELEASE_API") || { echo "t3code: could not fetch the latest stable release" exit 1 } # Parse from the variable, not the network. A reader that quits on the match # would close the pipe while printf is still writing the multi-line release # JSON, and pipefail turns that SIGPIPE into exit 141: read to the end and keep # the first match instead. The release carries one tag_name, and the first line # is the answer even if a body mentions the word later. tag=$(printf '%s\n' "$release_json" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') tag=${tag%%$'\n'*} if [ -z "${tag:-}" ]; then echo "t3code: could not resolve the latest stable release" exit 1 fi old_tag=$(lock_value tag) old_rev=$(lock_value rev) # Structural guard on top of the endpoint: only a plain vX.Y.Z tag moves the # pin, so nightly, preview, and mobile build tags are never candidates no # matter how new they are. if [[ ! $tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo "t3code: refusing non-stable tag $tag, keeping ${old_tag:-none}" notify -u normal "T3 Code update skipped" "refusing non-stable tag $tag; still on ${old_tag:-none}" exit 0 fi # A tag object and a commit share the ref name, so ask for both and prefer the # peeled commit. A lightweight tag answers with one line and that line is the # commit already. ls_remote=$(git ls-remote "$REMOTE" "refs/tags/$tag" "refs/tags/$tag^{}") rev=$(printf '%s\n' "$ls_remote" | awk '$2 ~ /\^\{\}$/ { peeled = $1 } $2 !~ /\^\{\}$/ { plain = $1 } END { if (peeled != "") print peeled; else print plain }') if [ -z "${rev:-}" ]; then echo "t3code: could not resolve tag $tag" exit 1 fi # The endpoint orders by publish date, so a re-published older patch would # move the pin backwards. Refuse a tag that sorts older than the locked one. # sort -C checks the order without printing, so no reader can close a pipe. if [ -n "${old_tag:-}" ] && [ "$tag" != "$old_tag" ] \ && ! printf '%s\n%s\n' "$old_tag" "$tag" | sort -V -C 2>/dev/null; then echo "t3code: stable $tag is older than $old_tag, keeping the pin" notify -u normal "T3 Code update skipped" "stable $tag is older than $old_tag; still on $old_tag" exit 0 fi built=0 backup=$(mktemp) if [ -f "$LOCK" ]; then cp "$LOCK" "$backup"; fi # Any exit that is not a finished build puts the lock back, so an interrupted # run cannot leave a placeholder behind for the switch to trip over. restore() { if [ "$built" -ne 1 ]; then if [ -s "$backup" ]; then write_lock < "$backup" else rm -f "$LOCK" "$LOCK.new" git -C "$DOTFILES" checkout -- pkgs/t3code/lock.nix 2>/dev/null || true fi fi rm -f "$backup" } trap restore EXIT trap 'exit 130' INT TERM # No previous lock at all: start from the header plus placeholders so the first # build can tell us every hash. if [ ! -s "$backup" ]; then write_lock </dev/null || true echo "t3code: stable $tag built" else echo "t3code: stable $tag is current and builds" fi