diff --git a/devenv/devenv.nix b/devenv/devenv.nix index 092e819..a4d8783 100644 --- a/devenv/devenv.nix +++ b/devenv/devenv.nix @@ -4,87 +4,111 @@ # processes on top. Plain devenv module: never reference flake outputs. { config, + lib, pkgs, ... }: { - # Editor tooling for .vscode/settings.json. languages.nix brings nixd - # (the language server; nixf diagnostics, the same rules CI runs), - # statix and deadnix; alejandra is the formatter nixd calls. - languages.nix.enable = true; + options.org.check.extraCommands = lib.mkOption { + type = with lib.types; listOf str; + default = []; + example = ["clan vars check"]; + description = '' + Shell commands `check` runs after the flake checks, in order; the + first failure fails `check`. For gates that cannot run inside + `nix flake check` (they need the network, the repo's CLI or a + secret), such as `clan vars check` in a clan repo. + ''; + }; - # Stop gh (and other termenv tools) asking the terminal for its - # background colour. devenv 2.3+ runs the shell inside its own PTY - # (status line, auto-reload): it answers the cursor-position query - # (CSI 6n) itself but forwards the OSC 11 colour query to the real - # terminal, so the reply comes back second. termenv reads the cursor - # reply first, decides OSC 11 is unsupported and leaves the late - # `ESC ] 11;rgb:…` reply in the input: it prints as garbage, breaks - # the next gh prompt ("unexpected escape sequence") and lands at the - # shell prompt. gh skips the query when GLAMOUR_STYLE is set to a - # real style; "dark" matches every org terminal. - env.GLAMOUR_STYLE = "dark"; + config = { + # Editor tooling for .vscode/settings.json. languages.nix brings nixd + # (the language server; nixf diagnostics, the same rules CI runs), + # statix and deadnix; alejandra is the formatter nixd calls. + languages.nix.enable = true; - packages = with pkgs; [ - alejandra - git - jq - ]; + # Stop gh (and other termenv tools) asking the terminal for its + # background colour. devenv 2.3+ runs the shell inside its own PTY + # (status line, auto-reload): it answers the cursor-position query + # (CSI 6n) itself but forwards the OSC 11 colour query to the real + # terminal, so the reply comes back second. termenv reads the cursor + # reply first, decides OSC 11 is unsupported and leaves the late + # `ESC ] 11;rgb:…` reply in the input: it prints as garbage, breaks + # the next gh prompt ("unexpected escape sequence") and lands at the + # shell prompt. gh skips the query when GLAMOUR_STYLE is set to a + # real style; "dark" matches every org terminal. + env.GLAMOUR_STYLE = "dark"; - scripts = { - fmt = { - exec = ''nix fmt "$@"''; - description = "Format and lint the repo (treefmt, org linters)"; - }; - check = { - # The repo's flake-check app (org-pkgs flakeModules.default) fails - # on dangerous evaluation warnings; plain nix flake check where a - # repo does not have it yet. - exec = '' - if nix eval --raw ".#apps.$(nix eval --raw --impure --expr builtins.currentSystem).flake-check.program" >/dev/null 2>&1; then - nix run .#flake-check -- "$@" - else - nix flake check "$@" - fi - ''; - description = "Run every flake check (lint, lock sync, docs, tests); fail on dangerous eval warnings"; - }; - sync-locks = { - exec = ''nix run .#sync-locks''; - description = "Pin devenv.lock to the flake.lock nixpkgs"; - }; - # Static build: no server, open result-docs/index.html straight - # from disk (file://) in any browser. - docs-build = { - exec = ''nix build .#documentation -o result-docs "$@" && echo "$PWD/result-docs/index.html"''; - description = "Build the docs site (slides embedded at slides/) to ./result-docs"; - }; - # Live previews. Ports 8981/8982 (docs.port, docs.slides.port in - # flakeModules.docs) stay clear of the 8000/8001 that app dev servers - # usually take. - docs = { - exec = ''nix run .#watch-documentation''; - description = "Live docs preview on 127.0.0.1:8981"; - }; - slides = { - exec = ''nix run .#serve-slides''; - description = "Live slides preview on 127.0.0.1:8982"; + packages = with pkgs; [ + alejandra + gh + git + jq + ]; + + scripts = { + fmt = { + exec = ''nix fmt "$@"''; + description = "Format and lint the repo (treefmt, org linters)"; + }; + check = { + # The repo's flake-check app (org-pkgs flakeModules.default) fails + # on dangerous evaluation warnings; plain nix flake check where a + # repo does not have it yet. + exec = + '' + set -euo pipefail + if nix eval --raw ".#apps.$(nix eval --raw --impure --expr builtins.currentSystem).flake-check.program" >/dev/null 2>&1; then + nix run .#flake-check -- "$@" + else + nix flake check "$@" + fi + '' + + lib.concatMapStrings (command: "${command}\n") config.org.check.extraCommands; + description = "Run every flake check (lint, lock sync, docs, tests) and the repo's org.check.extraCommands; fail on dangerous eval warnings"; + }; + sync-locks = { + exec = ''nix run .#sync-locks''; + description = "Pin devenv.lock to the flake.lock nixpkgs"; + }; + # Static build: no server, open result-docs/index.html straight + # from disk (file://) in any browser. + docs-build = { + exec = ''nix build .#documentation -o result-docs "$@" && echo "$PWD/result-docs/index.html"''; + description = "Build the docs site (slides embedded at slides/) to ./result-docs"; + }; + # Live previews. Ports 8981/8982 (docs.port, docs.slides.port in + # flakeModules.docs) stay clear of the 8000/8001 that app dev servers + # usually take. + docs = { + exec = ''nix run .#watch-documentation''; + description = "Live docs preview on 127.0.0.1:8981"; + }; + slides = { + exec = ''nix run .#serve-slides''; + description = "Live slides preview on 127.0.0.1:8982"; + }; + # flakeModules.vmTests. Manual only: VM tests never run in CI. + vm-test = { + exec = ''nix run .#vm-test -- "$@"''; + description = "Run NixOS VM tests (vm-test --help)"; + }; + # The clone stays on main; each PR branch gets its own worktree. + worktree = { + exec = builtins.readFile ./worktree.sh; + description = "One worktree per PR branch: worktree new BRANCH | list | prune"; + }; }; - # flakeModules.vmTests. Manual only: VM tests never run in CI. - vm-test = { - exec = ''nix run .#vm-test -- "$@"''; - description = "Run NixOS VM tests (vm-test --help)"; + + # `devenv up` serves docs and slides side by side. + processes = { + docs.exec = config.scripts.docs.exec; + slides.exec = config.scripts.slides.exec; }; - }; - # `devenv up` serves docs and slides side by side. - processes = { - docs.exec = config.scripts.docs.exec; - slides.exec = config.scripts.slides.exec; + # `devenv test`: the cheap static gates, never VMs. + enterTest = '' + nix fmt -- --ci + nix build --no-link ".#checks.${pkgs.stdenv.hostPlatform.system}.lock-sync" + ''; }; - - # `devenv test`: the cheap static gates, never VMs. - enterTest = '' - nix fmt -- --ci - nix build --no-link ".#checks.${pkgs.stdenv.hostPlatform.system}.lock-sync" - ''; } diff --git a/devenv/worktree.sh b/devenv/worktree.sh new file mode 100644 index 0000000..15f6b61 --- /dev/null +++ b/devenv/worktree.sh @@ -0,0 +1,79 @@ +# shellcheck shell=bash +# worktree: one git worktree per PR branch, so several sessions (people +# or agents) can work in one repo at once while the clone stays on main. +# Worktrees live in /.worktrees//, next to the +# clones (~/external-systems/.worktrees/... for the org), outside every +# repo tree. Sourced by the org devenv shell's `worktree` script. +set -euo pipefail + +usage() { + cat <<'USAGE' +usage: worktree new create from origin/main in its own worktree; print the path + worktree list list this repo's worktrees and their PR state + worktree prune remove worktrees whose PR merged or closed, then update main +USAGE +} + +# The main clone, also when called from inside a worktree. +common=$(git rev-parse --path-format=absolute --git-common-dir) +clone=$(dirname "$common") +repo=$(basename "$clone") +root="${WORKTREE_ROOT:-$(dirname "$clone")/.worktrees}/$repo" + +pr_state() { + (cd "$clone" && gh pr view "$1" --json state --jq .state 2>/dev/null) || echo NONE +} + +# Branch and path of every worktree except the main clone. +worktrees() { + git -C "$clone" worktree list --porcelain | awk -v clone="$clone" ' + /^worktree / { path = substr($0, 10) } + /^branch / { branch = substr($0, 19); if (path != clone) print branch "\t" path } + ' +} + +update_main() { + if [ "$(git -C "$clone" branch --show-current)" = main ] && [ -z "$(git -C "$clone" status --porcelain)" ]; then + git -C "$clone" pull --ff-only --quiet && echo "main: $(git -C "$clone" log --oneline -1)" + else + echo "main clone is not a clean checkout of main; left as is: $clone" >&2 + fi +} + +case "${1:-}" in + new) + branch=${2:?$(usage)} + path="$root/$branch" + git -C "$clone" fetch --quiet origin + git -C "$clone" worktree add --quiet "$path" -b "$branch" origin/main + echo "$path" + ;; + list) + worktrees | while IFS=$'\t' read -r branch path; do + printf '%-40s %-8s %s\n' "$branch" "$(pr_state "$branch")" "$path" + done + ;; + prune) + git -C "$clone" fetch --quiet --prune origin + worktrees | while IFS=$'\t' read -r branch path; do + state=$(pr_state "$branch") + case "$state" in + MERGED | CLOSED) + if git -C "$clone" worktree remove "$path"; then + git -C "$clone" branch -D "$branch" >/dev/null + echo "removed $branch ($state)" + else + echo "kept $branch: its worktree has uncommitted changes ($path)" >&2 + fi + ;; + *) echo "kept $branch ($state)" ;; + esac + done + git -C "$clone" worktree prune + update_main + ;; + *) + usage + exit 2 + ;; +esac