diff --git a/.gitignore b/.gitignore index fd49205..0fe5981 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ result devenv.local.nix # vm-test logs and driver output. .vm-tests/ +# Generated by the devenv shell (git-hooks); points into the nix store. +.pre-commit-config.yaml diff --git a/README.md b/README.md index 37f8a94..d966878 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,11 @@ docs-build # static docs to ./result-docs (nix build devenv up # live docs :8981 and slides :8982 ``` +Entering the shell installs the org git hooks (`fmt` on commit). pkgs pins +git-hooks.nix in `flake.lock`; other repos follow it from `devenv.yaml` +(`git-hooks.follows: org-pkgs/git-hooks`). See +`documentation/docs/flake-standard.md` (Git hooks), including worktrees. + `git add` new files before evaluating — Nix reads the git tree. This repo is the organization's nixpkgs authority: downstream flakes follow `pkgs/nixpkgs` so routine artifact and nixpkgs updates remain independent of consumer update diff --git a/devenv.lock b/devenv.lock index dbbfabb..dfd33ef 100644 --- a/devenv.lock +++ b/devenv.lock @@ -17,6 +17,44 @@ "type": "github" } }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1767039857, + "narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=", + "owner": "NixOS", + "repo": "flake-compat", + "rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "flake-compat", + "type": "github" + } + }, + "git-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1795608474, + "narHash": "sha256-CwC7f1DripMBQjB7BozobprSYiA8Cn9rz8vsBthWJwk=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "5c6bf7c418b4abf003e6d00d231e87f5e6ad5230", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "5c6bf7c418b4abf003e6d00d231e87f5e6ad5230", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1793110635, @@ -36,6 +74,7 @@ "root": { "inputs": { "devenv": "devenv", + "git-hooks": "git-hooks", "nixpkgs": "nixpkgs" } } diff --git a/devenv.yaml b/devenv.yaml index b9ace91..f17bb4f 100644 --- a/devenv.yaml +++ b/devenv.yaml @@ -1,9 +1,15 @@ # devenv CLI entry for pkgs (devenv shell, auto-activation, devenv up, -# devenv mcp). pkgs owns nixpkgs, so the pin is the flake.lock rev -# itself; checks.lock-sync enforces it and `sync-locks` rewrites it. -# Other repos take nixpkgs from pkgs instead (templates/default). +# devenv mcp). pkgs owns nixpkgs and git-hooks, so the pins are the +# flake.lock revs themselves; checks.lock-sync enforces them and +# `sync-locks` rewrites them. Other repos follow pkgs instead +# (templates/default). inputs: nixpkgs: url: github:NixOS/nixpkgs/31ecbaca1bdefa45dffc409f0250bd8039ea0010 + git-hooks: + url: github:cachix/git-hooks.nix/5c6bf7c418b4abf003e6d00d231e87f5e6ad5230 + inputs: + nixpkgs: + follows: nixpkgs imports: - ./devenv diff --git a/devenv/devenv.nix b/devenv/devenv.nix index a4d8783..fe477c7 100644 --- a/devenv/devenv.nix +++ b/devenv/devenv.nix @@ -4,10 +4,16 @@ # processes on top. Plain devenv module: never reference flake outputs. { config, + inputs, lib, pkgs, ... -}: { +}: let + # devenv reads inputs from the repo's own devenv.yaml only, so a repo + # opts in with `git-hooks.follows: org-pkgs/git-hooks`. Without it the + # hooks stay off (devenv would fail on a hook with no input). + hasGitHooks = inputs ? git-hooks; +in { options.org.check.extraCommands = lib.mkOption { type = with lib.types; listOf str; default = []; @@ -99,12 +105,32 @@ }; }; + # Git hooks (cachix/git-hooks.nix, run by prek). Entering the shell + # installs them into the clone's hooks directory, which all its + # worktrees share; each worktree runs its own .pre-commit-config.yaml. + # A repo adds its own next to these (git-hooks.hooks., guarded + # the same way). Local feedback only: CI stays the gate. + git-hooks.hooks.fmt = { + enable = hasGitHooks; + name = "fmt"; + # The flake's own treefmt, as CI runs it: fail on any change. + entry = "nix fmt -- --ci"; + pass_filenames = false; + stages = ["pre-commit"]; + }; + # `devenv up` serves docs and slides side by side. processes = { docs.exec = config.scripts.docs.exec; slides.exec = config.scripts.slides.exec; }; + # devenv (2.4) does not print module warnings on shell entry: say it + # here, on stderr, until the repo opts in. + enterShell = lib.mkIf (!hasGitHooks) '' + echo "org shell: git hooks are off; add 'git-hooks: {follows: org-pkgs/git-hooks}' to devenv.yaml inputs" >&2 + ''; + # `devenv test`: the cheap static gates, never VMs. enterTest = '' nix fmt -- --ci diff --git a/devenv/worktree.sh b/devenv/worktree.sh index 15f6b61..33da54d 100644 --- a/devenv/worktree.sh +++ b/devenv/worktree.sh @@ -46,6 +46,13 @@ case "${1:-}" in path="$root/$branch" git -C "$clone" fetch --quiet origin git -C "$clone" worktree add --quiet "$path" -b "$branch" origin/main + # Git hooks live in the clone and all worktrees share them, but each + # worktree reads its own .pre-commit-config.yaml, which the devenv + # shell writes. Enter it once so the first commit finds the config. + if [ -f "$path/devenv.yaml" ]; then + (cd "$path" && devenv shell -- true >/dev/null 2>&1) || + echo "worktree: devenv shell failed in $path; enter it before committing" >&2 + fi echo "$path" ;; list) diff --git a/documentation/docs/flake-standard.md b/documentation/docs/flake-standard.md index 737f01f..043fb3e 100644 --- a/documentation/docs/flake-standard.md +++ b/documentation/docs/flake-standard.md @@ -10,8 +10,9 @@ flake.nix / flake.lock one org-pkgs input (nixpkgs, flake-parts, import-tree follow it); imports inputs.org-pkgs.flakeModules.default flake-parts/ the repo's own modules (import-tree) -devenv.yaml / devenv.lock org-pkgs input (this repo), nixpkgs follows - org-pkgs/nixpkgs, imports org-pkgs/devenv +devenv.yaml / devenv.lock org-pkgs input (this repo), nixpkgs and + git-hooks follow org-pkgs, imports + org-pkgs/devenv devenv.nix this repo's languages, services, processes documentation/ docs/*.md, slides/slides.md; optional flake-module.nix (docs.* overrides) @@ -37,7 +38,7 @@ nix flake init -t github:external-systems/pkgs | `flakeModules.vmTests` | NixOS VM tests from `vm-tests/*.nix`: `legacyPackages.vmTests`, `apps.vm-test`, eval-only checks. See [VM tests](vm-tests.md) | | `flakeModules.python` | uv2nix for repos with `pyproject.toml` + `uv.lock`: packages versioned `..`, apps, checks (pytest, ty, uv-lock, version), ruff in treefmt, the `python-cli` VM test. See [Python](python.md) | | `flakeModules.default` | treefmt, docs, vmTests, python, `checks.lock-sync`, `apps.sync-locks`, `apps.flake-check` (the evaluation-warning gate, below), and the docs and slides builds as checks | -| `devenv/` (imported by `devenv.yaml`) | the org shell: `fmt`, `check`, `sync-locks`, `docs`, `slides`, `vm-test` scripts, `devenv up` docs + slides processes, `devenv test` gates | +| `devenv/` (imported by `devenv.yaml`) | the org shell: `fmt`, `check`, `sync-locks`, `docs`, `slides`, `vm-test`, `worktree` scripts, the `fmt` git hook (below), `devenv up` docs + slides processes, `devenv test` gates | | `devenv/python/` (`org-pkgs/devenv/python`) | Python repos: the editable uv2nix venv, `uv`, `ruff`, `ty`, the uv settings. See [Python](python.md) | | `templates.default` | the whole layout for a new repo | @@ -57,6 +58,54 @@ inputs, so a repo declares neither. They add two small nodes to every | `sync-locks` | after `nix flake update org-pkgs`: re-pin `devenv.lock` | | `vm-test` | NixOS VM tests: `--list`, ``, `--all [--serial]` (side by side within `vmTests.parallel`), `--remote`, `--rebuild`, `--resume`, `--check`, `--driver`. Never in CI | +## Git hooks + +The org shell installs a `pre-commit` hook that runs `nix fmt -- --ci`: +the commit stops when anything is unformatted (treefmt fixes the files; +`git add` them and commit again). The hooks come from +[cachix/git-hooks.nix](https://github.com/cachix/git-hooks.nix) through +devenv and run under prek. They are local feedback: anyone can skip them, +so CI stays the gate. Never commit with `--no-verify`; fix what the hook +reports. + +A repo opts in with two lines. devenv reads inputs from the repo's own +`devenv.yaml` only (not from the imported `org-pkgs/devenv`), so each repo +follows the pkgs pin, and `sync-locks` keeps it in step: + +```yaml +# devenv.yaml, under inputs: + git-hooks: + follows: org-pkgs/git-hooks +``` + +```gitignore +# .gitignore: the shell writes this link into the nix store +.pre-commit-config.yaml +``` + +Without the input the hooks stay off, and entering the shell prints a +one-line note. A repo adds its own hooks in `devenv.nix`: + +```nix +git-hooks.hooks.clan-vars-check = { + enable = true; + entry = "clan vars check"; + pass_filenames = false; + stages = ["pre-push"]; +}; +``` + +Keep hooks fast (seconds): a full `check` belongs before the push, not in +a hook. + +**Worktrees.** Git keeps hooks in the clone's `.git/hooks`, so every +worktree runs the same hooks, but each reads its own +`.pre-commit-config.yaml`, which the devenv shell writes. A worktree that +never entered the shell cannot commit (`config file not found`): enter it +once (`devenv shell -- true`, or any `fmt`). `worktree new` does that for +you. A branch from before its repo opted in has no config at all: rebase +it onto `main`, or commit with `PREK_ALLOW_NO_CONFIG=1` once. + ## Evaluation warnings `check` and CI run `nix run .#flake-check`: `nix flake check`, then the @@ -112,6 +161,7 @@ replaces nixpkgs' `pkgs` and fails with "attribute 'bash' missing". ## Two lockfiles, one nixpkgs -`flake.lock` and `devenv.lock` must resolve nixpkgs to the same rev. `checks.lock-sync` fails when they +`flake.lock` and `devenv.lock` must resolve nixpkgs to the same rev, and +git-hooks too once `devenv.yaml` has it. `checks.lock-sync` fails when they drift and names the fix. The bump flow is always `nix flake update org-pkgs`, then `sync-locks`, in one commit. diff --git a/flake.lock b/flake.lock index c5dea14..af78a37 100644 --- a/flake.lock +++ b/flake.lock @@ -54,6 +54,27 @@ "type": "github" } }, + "git-hooks": { + "inputs": { + "flake-compat": [], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1795608474, + "narHash": "sha256-CwC7f1DripMBQjB7BozobprSYiA8Cn9rz8vsBthWJwk=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "5c6bf7c418b4abf003e6d00d231e87f5e6ad5230", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, "import-tree": { "locked": { "lastModified": 1788467110, @@ -233,6 +254,7 @@ "root": { "inputs": { "flake-parts": "flake-parts", + "git-hooks": "git-hooks", "import-tree": "import-tree", "llm-agents": "llm-agents", "moscripts": "moscripts", diff --git a/flake.nix b/flake.nix index 7060f7f..d3613e4 100644 --- a/flake.nix +++ b/flake.nix @@ -28,6 +28,20 @@ flake = false; }; + # ------ Git hooks (the org devenv shell) ------ # + # cachix/git-hooks.nix, read by devenv's git-hooks module. devenv + # takes inputs from the repo's own devenv.yaml only, so each repo + # follows this pin (`git-hooks.follows: org-pkgs/git-hooks`) and + # sync-locks keeps it in step with org-pkgs. Lazy: never fetched by + # a flake evaluation. + git-hooks = { + url = "github:cachix/git-hooks.nix"; + inputs = { + nixpkgs.follows = "nixpkgs"; + flake-compat.follows = ""; + }; + }; + # ------ Third-party flakes (overlays.third-party) ------ # # Packages that only ship as flakes. Lazy: a consumer that never reads # pkgs.zen-browser / pkgs.moscripts never fetches them. diff --git a/flakeModules/dev.nix b/flakeModules/dev.nix index 5273d8d..b511ec7 100644 --- a/flakeModules/dev.nix +++ b/flakeModules/dev.nix @@ -7,9 +7,11 @@ # The lockfiles must agree on nixpkgs: flake.lock, devenv.lock (the # devenv CLI shell, nixpkgs following org-pkgs/nixpkgs) and, in a # repo with a dev partition (none today), dev/flake.lock's dev-nixpkgs. -# checks.lock-sync fails when they drift; `sync-locks` (devenv script, or -# nix run .#sync-locks) re-pins devenv.yaml (and dev/flake.lock) to -# flake.lock. +# A devenv.lock with git-hooks (the org shell's hooks) must also match +# the git-hooks that flake.lock resolves through org-pkgs (in pkgs, its +# own input). checks.lock-sync fails when they drift; `sync-locks` +# (devenv script, or nix run .#sync-locks) re-pins devenv.yaml (and +# dev/flake.lock) to flake.lock. {orgInputs}: { lib, self, @@ -17,19 +19,28 @@ }: let lockOf = file: builtins.fromJSON (builtins.readFile file); - # Node a root input resolves to, following `follows` paths (lists, - # always relative to the root node). + # Node an input path resolves to, following `follows` paths (lists, + # always relative to the root node); null when an input is missing. resolve = lock: path: lib.foldl' (node: name: let - ref = lock.nodes.${node}.inputs.${name}; + ref = lock.nodes.${node}.inputs.${name} or null; in - if builtins.isList ref + if node == null + then null + else if builtins.isList ref then resolve lock ref else ref) lock.root path; - revOf = lock: name: lock.nodes.${resolve lock [name]}.locked.rev or "missing"; + revAt = lock: path: let + node = resolve lock path; + in + if node == null + then "missing" + else lock.nodes.${node}.locked.rev or "missing"; + + revOf = lock: name: revAt lock [name]; rootLock = lockOf "${self}/flake.lock"; hasDevenv = builtins.pathExists "${self}/devenv.lock"; @@ -58,6 +69,13 @@ }; rootRev = revs."flake.lock nixpkgs"; drifted = lib.filterAttrs (_: rev: rev != rootRev) revs; + + # git-hooks: checked only once devenv.yaml has opted in. + devenvLock = lockOf "${self}/devenv.lock"; + hasGitHooks = hasDevenv && devenvLock.nodes.${devenvLock.root}.inputs ? git-hooks; + gitHooksRev = revAt rootLock (lib.optional (pkgsInput != null) pkgsInput ++ ["git-hooks"]); + devenvGitHooksRev = revOf devenvLock "git-hooks"; + gitHooksDrifted = hasGitHooks && devenvGitHooksRev != gitHooksRev; in { key = "external-systems/pkgs#flakeModules.dev"; imports = [ @@ -97,10 +115,15 @@ in { sed -i -E 's#(url: github:external-systems/pkgs)[^[:space:]]*#\1/${pkgsRev}#' devenv.yaml devenv update org-pkgs '' - else '' - sed -i -E 's#(url: github:NixOS/nixpkgs)[^[:space:]]*#\1/${rootRev}#' devenv.yaml - devenv update nixpkgs - '' + else + '' + sed -i -E 's#(url: github:NixOS/nixpkgs)[^[:space:]]*#\1/${rootRev}#' devenv.yaml + devenv update nixpkgs + '' + + lib.optionalString hasGitHooks '' + sed -i -E 's#(url: github:cachix/git-hooks.nix)[^[:space:]]*#\1/${gitHooksRev}#' devenv.yaml + devenv update git-hooks + '' ) + '' fi @@ -117,14 +140,21 @@ in { checks = { lock-sync = pkgs.runCommand "lock-sync" {} ( - if drifted == {} + if drifted == {} && !gitHooksDrifted then "touch $out" - else '' - echo "nixpkgs drifted from flake.lock (${rootRev}):" >&2 - ${lib.concatLines (lib.mapAttrsToList (name: rev: "echo ' ${name} = ${rev}' >&2") drifted)} - echo "Fix: sync-locks (devenv shell) or nix run .#sync-locks" >&2 - exit 1 - '' + else + lib.optionalString (drifted != {}) '' + echo "nixpkgs drifted from flake.lock (${rootRev}):" >&2 + ${lib.concatLines (lib.mapAttrsToList (name: rev: "echo ' ${name} = ${rev}' >&2") drifted)} + '' + + lib.optionalString gitHooksDrifted '' + echo "git-hooks drifted from flake.lock (${gitHooksRev}):" >&2 + echo " devenv.lock git-hooks = ${devenvGitHooksRev}" >&2 + '' + + '' + echo "Fix: sync-locks (devenv shell) or nix run .#sync-locks" >&2 + exit 1 + '' ); } # Docs and slides build on every flake check (mkdocs strict mode diff --git a/templates/default/.gitignore b/templates/default/.gitignore index 65f24c2..1b44785 100644 --- a/templates/default/.gitignore +++ b/templates/default/.gitignore @@ -8,3 +8,5 @@ _site/ site/ # vm-test logs and driver output. .vm-tests/ +# Generated by the devenv shell (git-hooks); points into the nix store. +.pre-commit-config.yaml diff --git a/templates/default/devenv.yaml b/templates/default/devenv.yaml index 7906ae6..8da477c 100644 --- a/templates/default/devenv.yaml +++ b/templates/default/devenv.yaml @@ -9,6 +9,10 @@ inputs: url: github:external-systems/pkgs nixpkgs: follows: org-pkgs/nixpkgs + # The org shell's git hooks (fmt on commit). devenv reads inputs from + # this file only, so the repo follows the pkgs pin. + git-hooks: + follows: org-pkgs/git-hooks imports: # The org shell: fmt, check, sync-locks, docs, slides; devenv up serves # docs and slides.