Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions devenv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,6 +74,7 @@
"root": {
"inputs": {
"devenv": "devenv",
"git-hooks": "git-hooks",
"nixpkgs": "nixpkgs"
}
}
Expand Down
12 changes: 9 additions & 3 deletions devenv.yaml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 27 additions & 1 deletion devenv/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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.<name>, 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
Expand Down
7 changes: 7 additions & 0 deletions devenv/worktree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 54 additions & 4 deletions documentation/docs/flake-standard.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 `<major>.<minor>.<commits>`, 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 |

Expand All @@ -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`, `<name>`, `--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
Expand Down Expand Up @@ -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.
22 changes: 22 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading