|
5 | 5 | ]; |
6 | 6 | perSystem = |
7 | 7 | { config, pkgs, ... }: |
| 8 | + let |
| 9 | + # treefmt.toml is generated from the treefmt settings below so that the |
| 10 | + # two can not drift apart (see checks.treefmt-toml). The only difference |
| 11 | + # is the `command` of each formatter: the Nix config pins absolute store |
| 12 | + # paths, while contributors without Nix need the bare binary name from |
| 13 | + # their PATH. |
| 14 | + treefmtTomlHeader = '' |
| 15 | + # One CLI to format the code tree - https://git.numtide.com/numtide/treefmt |
| 16 | + # |
| 17 | + # GENERATED FILE - DO NOT EDIT. |
| 18 | + # Source of truth: nix/flake-modules/formatter.nix |
| 19 | + # Regenerate with: nix run .#update-treefmt-toml |
| 20 | + # |
| 21 | + # This copy exists for contributors without Nix, who run a bare `treefmt`. |
| 22 | + # Nix users (`nix fmt`, `nix run .#lint`) and CI use the generated config |
| 23 | + # directly and never read this file. Note that it resolves formatters from |
| 24 | + # PATH, so their versions are not pinned the way the Nix config pins them. |
| 25 | + ''; |
| 26 | + |
| 27 | + generatedTreefmtToml = pkgs.runCommand "treefmt.toml" { } '' |
| 28 | + cat ${pkgs.writeText "treefmt-toml-header" treefmtTomlHeader} > $out |
| 29 | + echo >> $out |
| 30 | + # Reduce the pinned store paths to bare binary names, resolved from PATH. |
| 31 | + sed -E 's|"/nix/store/[^"]*/|"|' ${config.treefmt.build.configFile} >> $out |
| 32 | + ''; |
| 33 | + |
| 34 | + # treefmt.toml must stay byte-identical to what the treefmt settings |
| 35 | + # generate, otherwise contributors without Nix format against a stale |
| 36 | + # config and CI rejects their result. |
| 37 | + treefmtTomlInSync = pkgs.runCommand "treefmt-toml-in-sync" { } '' |
| 38 | + if ! diff -u ${../../treefmt.toml} ${generatedTreefmtToml}; then |
| 39 | + echo >&2 |
| 40 | + echo "treefmt.toml is out of sync with nix/flake-modules/formatter.nix." >&2 |
| 41 | + echo "Regenerate it with: nix run .#update-treefmt-toml" >&2 |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | + touch $out |
| 45 | + ''; |
| 46 | + in |
8 | 47 | { |
9 | 48 | # Auto formatters. This also adds a flake check to ensure that the |
10 | 49 | # source tree was auto formatted. |
|
13 | 52 | flakeCheck = false; # Add a flake check to run treefmt, disabled, as mix format does need the dependencies fetched beforehand |
14 | 53 | projectRootFile = "VERSION"; # File used to identity repo root |
15 | 54 |
|
16 | | - # we really need to mirror the treefmt.toml as we can't use it directly |
17 | 55 | settings.global.excludes = [ |
18 | 56 | "*.gitignore" |
19 | 57 | "*.dockerignore" |
|
62 | 100 |
|
63 | 101 | programs.prettier.enable = true; |
64 | 102 |
|
65 | | - programs.nixpkgs-fmt.enable = true; |
| 103 | + programs.nixfmt.enable = true; |
| 104 | + }; |
| 105 | + |
| 106 | + # Exposed twice on purpose: as a check so `nix flake check` covers it, and |
| 107 | + # as a package so CI can build it as `.#check-treefmt-toml`, which resolves |
| 108 | + # the current system instead of hardcoding one. |
| 109 | + checks.treefmt-toml = treefmtTomlInSync; |
| 110 | + packages.check-treefmt-toml = treefmtTomlInSync; |
| 111 | + |
| 112 | + # Writes next to the root marker treefmt itself anchors on, rather than |
| 113 | + # deriving a second notion of the project root. |
| 114 | + packages.update-treefmt-toml = pkgs.writeShellApplication { |
| 115 | + name = "update-treefmt-toml"; |
| 116 | + text = '' |
| 117 | + if [ ! -f ${config.treefmt.projectRootFile} ]; then |
| 118 | + echo "run this from the project root (no ${config.treefmt.projectRootFile} here)" >&2 |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | + install -m 644 ${generatedTreefmtToml} treefmt.toml |
| 122 | + echo "wrote treefmt.toml" |
| 123 | + ''; |
66 | 124 | }; |
67 | 125 |
|
68 | 126 | # Lean treefmt entrypoint for CI: `nix run .#lint -- --ci` |
|
0 commit comments