Skip to content

feat(template): author repo configuration in Nix (ADR 0013 prototype) - #44

Draft
cooper (czxtm) wants to merge 1 commit into
mainfrom
cm/nix-generated-config
Draft

cooper (czxtm) wants to merge 1 commit into
mainfrom
cm/nix-generated-config

Conversation

@czxtm

Copy link
Copy Markdown
Member

Prototype for ADR 0013 (proposed): author configuration in Nix.

Every config file in the repo is written in Nix. The files tools actually read are rendered from that source and committed wherever each tool looks for them. JSON is only hand-written for data.

How it works

  • nix/files/ uses the files flake-parts module. Each source declares files.file."<path>".text.
  • x files (nix run .#write-files) writes the outputs.
  • nix flake check gets one files:<path> check per output and fails if the committed copy differs. I tested this: a hand edit to tsconfig.json fails the check.
  • nix/files/render.nix renders JSON, YAML and line-based files as pure Nix functions with no build step, so nix eval shows exactly what gets written.
    • YAML uses block scalars for multi-line run: steps.
    • Outputs that allow comments start with a banner naming their Nix source.
  • A rendered .gitattributes marks every output as linguist-generated. GitHub collapses them in diffs, and vite.config.ts reads that list so oxfmt doesn't reformat them.
  • Each source lives with the part of the repo that owns it, so ADR 0001 still holds:
    • ops/secrets/sops.nix → .sops.yaml
    • ops/policies/dependencies/renovate.nix → .github/renovate.json
    • nix/files/* → repo plumbing
Rendered file Source Compared with the original
tsconfig.json nix/files/tsconfig.nix identical (parsed)
.config/dependency-cruiser.json (was .cjs) nix/files/dependency-cruiser.nix identical to require() of the old .cjs
.github/workflows/ci.yaml nix/files/ci-workflow.nix identical (parsed)
.gitignore nix/files/gitignore.nix same git check-ignore result on 26 paths
.sops.yaml ops/secrets/sops.nix intended fix, see below
.github/renovate.json ops/policies/dependencies/renovate.nix keeps the old rules and adds the Nix routing
.gitattributes nix/files/default.nix new

Two fixes this makes

  • sops picked the wrong rules. sops looks for .sops.yaml starting from the directory it runs in and moving up, not from the file's location. So sops ops/secrets/x.sops.yaml run from the repo root used the root rules: the CI, keyservice and cooper keys, encrypting every field. It never used ops/secrets/.sops.yaml (the deploy key, encrypting only data/stringData). I reproduced this. The ops/secrets/ rule now lives in the root config, and I confirmed it applies whether sops runs from the repo root or from inside ops/secrets.
  • Renovate never read its config. ops/policies/dependencies/renovate.json isn't one of the paths Renovate checks. It's now rendered to .github/renovate.json.

What to watch when this merges

  • Renovate behavior changes. The repo config takes effect, so pinDigests for Dockerfiles and compose files starts applying and Renovate will open pin PRs.
  • Action updates move to the Nix source. Renovate is turned off for rendered files. A regex manager updates uses = "…"; lines in nix/files/*.nix instead. I confirmed with a local renovate --platform=local extract that it finds all four actions. Expect chore(deps): update actions/checkout action to v7 #11 (actions/checkout v7) to be recreated against the Nix source.
  • Template slices don't include nix/files/, so they ship the rendered files as ordinary files.

Left out on purpose (stacked follow-ups if you like this)

  • ops/compose and ops/config/*.conf: move to services-flake (Postgres/Redis/nginx/Prometheus/Grafana configured in Nix, no Docker needed locally).
  • ops/deploy/kubernetes and ops/environments: render with nixidy (CI generates plain manifests for the GitOps controller).
  • ops/deploy/terraform: these are only variables/outputs stubs, so delete them in favor of Alchemy (ADR 0004).
  • ops/policies/repo-root/allowed and the Prometheus/OTel/Loki YAML.

Verification

  • Ran locally: bun install --frozen-lockfile, bun run prepare, check, test (12/12), lint (exit 0; 3 new advisory Effect-style warnings in vite.config.ts), fmt:check, lint:root, lint:boundaries.
  • Built every flake check except smoke locally: all files:* checks, template-slices, treefmt, bun-version. CI runs smoke and the full nix flake check.
  • Checked sops end to end: encrypting with the new root config picks the right recipients from both the repo root and ops/secrets.
  • Ran renovate-config-validator and a local Renovate extract.
  • This repo has no preview deploy, and the web app's runtime code is unchanged.

The flake.lock entry for files was computed from a git fetch of the locked rev, because GitHub tarball downloads are blocked where this was built. CI fetches the tarball and checks the hash.


Generated by Claude Code

Prototype for ADR 0013 (proposed): configuration the repo controls is
written in Nix, and the files tools read are rendered outputs committed
where each tool looks for them. `x files` (nix run .#write-files) writes
them; `nix flake check` gets one check per file and fails on drift.

- Add the mightyiam/files flake-parts module and nix/files/, with pure
  JSON/YAML/lines renderers evaluated by Nix.
- Render tsconfig.json, .gitignore, .gitattributes, the CI workflow,
  .config/dependency-cruiser.json (was .cjs), .github/renovate.json, and
  .sops.yaml. Rendered outputs are semantically identical to the files
  they replace, except the two fixes below.
- Keep operational sources under ops/: ops/secrets/sops.nix renders the
  root .sops.yaml, ops/policies/dependencies/renovate.nix renders
  .github/renovate.json.
- Fix sops rule selection: sops reads the nearest .sops.yaml above the
  working directory, so ops/secrets/.sops.yaml was skipped when sops ran
  from the repo root. Its rule now lives in the root config.
- Fix Renovate discovery: it never read ops/policies/dependencies/.
  Rendered files are disabled for Renovate, and a regex manager updates
  action versions in the Nix source instead.
- oxfmt skips files marked linguist-generated in .gitattributes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants