diff --git a/README.md b/README.md index d966878..96c38c8 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,8 @@ 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 +Entering the shell installs the org git hooks (`fmt` on commit); every +org repo has them, with no opt-out (`checks.git-hooks`). 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. diff --git a/devenv/devenv.nix b/devenv/devenv.nix index fe477c7..cd343e4 100644 --- a/devenv/devenv.nix +++ b/devenv/devenv.nix @@ -4,16 +4,10 @@ # 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 = []; @@ -105,13 +99,15 @@ in { }; }; - # 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 (cachix/git-hooks.nix, run by prek), part of every org + # shell. devenv reads inputs from the repo's own devenv.yaml only, so + # every repo has `git-hooks.follows: org-pkgs/git-hooks` there + # (checks.git-hooks enforces it). Entering the shell installs the + # hooks 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.). git-hooks.hooks.fmt = { - enable = hasGitHooks; + enable = true; name = "fmt"; # The flake's own treefmt, as CI runs it: fail on any change. entry = "nix fmt -- --ci"; @@ -125,12 +121,6 @@ in { 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/documentation/docs/flake-standard.md b/documentation/docs/flake-standard.md index 043fb3e..9a87b4b 100644 --- a/documentation/docs/flake-standard.md +++ b/documentation/docs/flake-standard.md @@ -37,7 +37,7 @@ nix flake init -t github:external-systems/pkgs | `flakeModules.docs` | MkDocs site + reveal.js deck: `packages.documentation`, `packages.slides`, `apps.watch-documentation`, `apps.serve-slides`. See [Docs and slides](docs-and-slides.md) | | `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 | +| `flakeModules.default` | treefmt, docs, vmTests, python, `checks.lock-sync`, `checks.git-hooks`, `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`, `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 | @@ -60,17 +60,22 @@ inputs, so a repo declares neither. They add two small nodes to every ## Git hooks -The org shell installs a `pre-commit` hook that runs `nix fmt -- --ci`: +Every org repo has git hooks, the same way it has the devenv shell: there +is no opt-out. 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. +reports. Agents are held to that by the ground rules and, for claude, by +a hook that blocks the skip (external-systems/modules, +`homeSpec.agents.gitHookGuard`). -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: +Every repo carries two lines for them. 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. +`checks.git-hooks` (in `flakeModules.default`) fails CI when either line +is missing; without the input the shell would not start at all: ```yaml # devenv.yaml, under inputs: @@ -83,8 +88,11 @@ follows the pkgs pin, and `sync-locks` keeps it in step: .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`: +When a repo moves to a pkgs with mandatory hooks: `nix flake update +org-pkgs`, `nix run .#sync-locks` (outside the shell, which cannot start +before the input is there), then the two lines and `devenv shell -- true`. + +A repo adds its own hooks in `devenv.nix`: ```nix git-hooks.hooks.clan-vars-check = { @@ -103,8 +111,8 @@ 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. +you. A branch from before its repo had git hooks has no config at all: +rebase it onto `main`. ## Evaluation warnings @@ -162,6 +170,6 @@ 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, and -git-hooks too once `devenv.yaml` has it. `checks.lock-sync` fails when they +git-hooks too. `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/flakeModules/dev.nix b/flakeModules/dev.nix index b511ec7..7b6e81a 100644 --- a/flakeModules/dev.nix +++ b/flakeModules/dev.nix @@ -7,11 +7,16 @@ # 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. -# 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 +# devenv.lock's 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. +# +# checks.git-hooks: a repo with a devenv shell always has the org git +# hooks: devenv.yaml follows org-pkgs/git-hooks (devenv reads inputs from +# the repo's own devenv.yaml only) and .gitignore lists the generated +# .pre-commit-config.yaml. {orgInputs}: { lib, self, @@ -70,12 +75,24 @@ rootRev = revs."flake.lock nixpkgs"; drifted = lib.filterAttrs (_: rev: rev != rootRev) revs; - # git-hooks: checked only once devenv.yaml has opted in. + # git-hooks: every repo with a devenv shell has it. devenvLock = lockOf "${self}/devenv.lock"; hasGitHooks = hasDevenv && devenvLock.nodes.${devenvLock.root}.inputs ? git-hooks; - gitHooksRev = revAt rootLock (lib.optional (pkgsInput != null) pkgsInput ++ ["git-hooks"]); + gitHooksPath = lib.optional (pkgsInput != null) pkgsInput ++ ["git-hooks"]; + gitHooksRev = revAt rootLock gitHooksPath; devenvGitHooksRev = revOf devenvLock "git-hooks"; gitHooksDrifted = hasGitHooks && devenvGitHooksRev != gitHooksRev; + + gitignoreFile = "${self}/.gitignore"; + gitignored = name: + builtins.pathExists gitignoreFile + && lib.elem name (map lib.trim (lib.splitString "\n" (builtins.readFile gitignoreFile))); + gitHooksProblems = lib.optionals hasDevenv ( + lib.optional (!hasGitHooks) "devenv.yaml has no git-hooks input" + ++ lib.optional (hasGitHooks && pkgsInput != null && devenvLock.nodes.${devenvLock.root}.inputs.git-hooks != gitHooksPath) + "devenv.yaml git-hooks does not follow ${pkgsInput}/git-hooks" + ++ lib.optional (!gitignored ".pre-commit-config.yaml") ".gitignore does not list .pre-commit-config.yaml" + ); in { key = "external-systems/pkgs#flakeModules.dev"; imports = [ @@ -156,6 +173,20 @@ in { exit 1 '' ); + + git-hooks = pkgs.runCommand "git-hooks" {} ( + if gitHooksProblems == [] + then "touch $out" + else '' + echo "The org git hooks are not set up:" >&2 + ${lib.concatMapStrings (p: "echo ${lib.escapeShellArg " - ${p}"} >&2\n") gitHooksProblems} + echo "Fix: in devenv.yaml under inputs add" >&2 + echo " git-hooks:" >&2 + echo " follows: org-pkgs/git-hooks" >&2 + echo "and add .pre-commit-config.yaml to .gitignore; then devenv shell -- true" >&2 + exit 1 + '' + ); } # Docs and slides build on every flake check (mkdocs strict mode # fails on broken links). diff --git a/templates/default/devenv.yaml b/templates/default/devenv.yaml index 8da477c..575ec9f 100644 --- a/templates/default/devenv.yaml +++ b/templates/default/devenv.yaml @@ -9,8 +9,9 @@ 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. + # The org shell's git hooks (fmt on commit), required in every repo + # (checks.git-hooks). devenv reads inputs from this file only, so the + # repo follows the pkgs pin. git-hooks: follows: org-pkgs/git-hooks imports: