From d59c1ed209ed22385df287323cb6140675ea7256 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Thu, 10 Sep 2026 05:33:45 +0000 Subject: [PATCH] flake: format the tree with one treefmt The pre-commit hooks kept their own list of formatters, so nixpkgs-fmt in a hook and nixfmt in the check disagreed the moment flake-checks moved. `nix fmt` is now built from the same treefmt module the formatting check is, and the hook and the Makefile both call it. --- .pre-commit-config.yaml | 20 +++++++------------- .prettierignore | 3 +++ AGENTS.md | 3 ++- Makefile | 10 +++++----- flake.nix | 11 +++++++++++ 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1b96f1ff..4823ae12 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,21 +32,15 @@ repos: # Local hooks for project-specific tooling - repo: local hooks: - # nixfmt for Nix files; the same formatter the flake's treefmt check - # runs, so the hook and the check cannot disagree. - - id: nixfmt - name: nixfmt - entry: nixfmt - language: system - files: \.nix$ - - # Prettier for formatting - - id: prettier - name: prettier - entry: prettier --write --list-different + # One formatter for Nix and markup, built from the same treefmt module as + # the flake's formatting check, so the hook and the check cannot disagree. + # Go is absent on purpose: golangci-lint owns it, below. + - id: treefmt + name: treefmt + entry: nix fmt -- language: system exclude: ^docs/ - types_or: [javascript, jsx, ts, tsx, yaml, json, html, css, scss, sass, markdown] + types_or: [javascript, jsx, ts, tsx, yaml, json, html, css, scss, sass, markdown, nix] # mdformat for docs - id: mdformat diff --git a/.prettierignore b/.prettierignore index 9d8aa3bc..5b01a873 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,5 @@ .github/workflows/test-integration-v2* docs/ + +# Golden fixtures are byte-compared; a formatter must not rewrite them. +hscontrol/testdata/ diff --git a/AGENTS.md b/AGENTS.md index 80062f04..07db0190 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -109,7 +109,8 @@ prek run --all-files # run hooks on the full tree Hooks cover: file hygiene (trailing whitespace, line endings, BOM), syntax validation (JSON/YAML/TOML/XML), merge-conflict markers, private -key detection, nixpkgs-fmt, prettier, and `golangci-lint` via +key detection, `treefmt` (the same formatter `nix fmt` and the flake's +formatting check run), `mdformat` for `docs/`, and `golangci-lint` via `--new-from-rev=HEAD~1` (see the golangci-lint hook in .pre-commit-config.yaml). A manual invocation with an `upstream/main` remote is equivalent: diff --git a/Makefile b/Makefile index 1887e92e..47f09c34 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ test: check-deps $(GO_SOURCES) go.mod go.sum # Formatting targets .PHONY: fmt -fmt: fmt-go fmt-mdformat fmt-prettier +fmt: fmt-go fmt-mdformat fmt-tree .PHONY: fmt-go fmt-go: check-deps $(GO_SOURCES) @@ -63,10 +63,10 @@ fmt-mdformat: check-deps @echo "Formatting documentation..." mdformat docs/ -.PHONY: fmt-prettier -fmt-prettier: check-deps $(PRETTIER_SOURCES) - @echo "Formatting markup and config files..." - prettier --write '**/*.{ts,js,md,yaml,yml,sass,css,scss,html}' +.PHONY: fmt-tree +fmt-tree: check-deps $(PRETTIER_SOURCES) + @echo "Formatting Nix, markup and config files..." + nix fmt # Linting targets .PHONY: lint diff --git a/flake.nix b/flake.nix index ffa8db53..f712dab0 100644 --- a/flake.nix +++ b/flake.nix @@ -278,6 +278,17 @@ drv = pkgs.headscale; }; + # `nix fmt` is built from the same treefmt module as the formatting + # check, so the two cannot disagree. Every formatter the tree is judged + # by lives here, not in a hand-kept list of pre-commit hooks. + formatter = fc.formatter ( + common + // { + goFmt = "off"; + prettier = true; + } + ); + checks = { headscale = pkgs.testers.nixosTest (import ./nix/tests/headscale.nix); }