|
9 | 9 | inputs.nixpkgs.follows = "nixpkgs"; |
10 | 10 | }; |
11 | 11 | pslua.url = "github:purescript-lua/purescript-lua"; |
| 12 | + treefmt-nix = { |
| 13 | + url = "github:numtide/treefmt-nix"; |
| 14 | + inputs.nixpkgs.follows = "nixpkgs"; |
| 15 | + }; |
12 | 16 | }; |
13 | 17 |
|
14 | | - outputs = { self, nixpkgs, flake-utils, purescript-overlay, pslua }: |
15 | | - flake-utils.lib.eachDefaultSystem (system: |
| 18 | + outputs = |
| 19 | + { |
| 20 | + self, |
| 21 | + nixpkgs, |
| 22 | + flake-utils, |
| 23 | + purescript-overlay, |
| 24 | + pslua, |
| 25 | + treefmt-nix, |
| 26 | + }: |
| 27 | + flake-utils.lib.eachDefaultSystem ( |
| 28 | + system: |
16 | 29 | let |
17 | 30 | pkgs = import nixpkgs { |
18 | 31 | inherit system; |
19 | 32 | overlays = [ purescript-overlay.overlays.default ]; |
20 | 33 | }; |
21 | | - in { |
| 34 | + treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix; |
| 35 | + in |
| 36 | + { |
| 37 | + formatter = treefmtEval.config.build.wrapper; |
| 38 | + checks.formatting = treefmtEval.config.build.check self; |
22 | 39 | devShell = pkgs.mkShell { |
23 | 40 | buildInputs = with pkgs; [ |
24 | 41 | dhall |
|
31 | 48 | spago-bin.spago-0_21_0 |
32 | 49 | treefmt |
33 | 50 | ]; |
| 51 | + # Install a content-based pre-commit hook. It compares the working |
| 52 | + # tree diff before and after `nix fmt`, so it only objects to changes |
| 53 | + # the formatter itself introduces (not the developer's existing |
| 54 | + # unstaged work) and is not fooled by formatters that only bump mtime. |
| 55 | + # Rewritten each shell entry to stay in sync with this flake. |
| 56 | + shellHook = '' |
| 57 | + hook=.git/hooks/pre-commit |
| 58 | + if [ -d .git ]; then |
| 59 | + printf '%s\n' \ |
| 60 | + '#!/usr/bin/env bash' \ |
| 61 | + 'before=$(git diff)' \ |
| 62 | + 'nix fmt >/dev/null 2>&1 || exit 0' \ |
| 63 | + '[ "$before" = "$(git diff)" ] || { echo "nix fmt changed files; re-stage them, then commit." >&2; exit 1; }' \ |
| 64 | + > "$hook" |
| 65 | + chmod +x "$hook" |
| 66 | + fi |
| 67 | + ''; |
34 | 68 | }; |
35 | | - }); |
| 69 | + } |
| 70 | + ); |
36 | 71 |
|
37 | 72 | # --- Flake Local Nix Configuration ---------------------------- |
38 | 73 | nixConfig = { |
|
0 commit comments