summaryrefslogtreecommitdiffstats
path: root/ci/treefmt.nix
blob: a0d4ffbc1ef283d74a1a439bef8cc8ca2bc5fd31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{
  lib,
  pkgs,
  ...
}:
{
  runtimeInputs = [
    # tree-root uses `git rev-parse --show-toplevel`
    pkgs.gitMinimal
  ];

  settings = {
    # numtide/treefmt-nix defaults
    excludes = [
      "*.lock"
      "*.patch"
      "*.diff"
      "package-lock.json"
      "go.mod"
      "go.sum"
      ".gitattributes"
      ".gitignore"
      ".gitmodules"
      "COPYING"
      "LICENSE"
    ];

    # Be a bit more verbose by default, so we can see progress happening
    verbose = 1;

    # By default it's info, which is too noisy since we have many unmatched files
    on-unmatched = "debug";

    formatter = {
      # keep-sorted start block=yes newline_separated=yes
      actionlint = {
        command = lib.getExe pkgs.actionlint;
        includes = [
          ".github/workflows/*.yml"
          ".github/workflows/*.yaml"
        ];
      };

      biome = {
        command = lib.getExe pkgs.biome;
        excludes = [
          "*.min.js"
          "pkgs/*"
        ];
        includes = [
          "*.js"
          "*.ts"
          "*.mjs"
          "*.mts"
          "*.cjs"
          "*.cts"
          "*.jsx"
          "*.tsx"
          "*.d.ts"
          "*.d.cts"
          "*.d.mts"
          "*.css"
        ];
        options = [
          "check"
          "--write"
          "--no-errors-on-unmatched"
          "--use-editorconfig=true"
          "--javascript-formatter-quote-style=single"
          "--semicolons=as-needed"
        ];
      };

      editorconfig-checker = {
        command = lib.getExe pkgs.editorconfig-checker;
        options = [
          "-disable-indent-size"
          # TODO: Remove this once this upstream issue is fixed:
          #   https://github.com/editorconfig-checker/editorconfig-checker/issues/505
          "-disable-charset"
        ];
        includes = [ "*" ];
        priority = 1;
      };

      keep-sorted = {
        command = lib.getExe pkgs.keep-sorted;
        includes = [ "*" ];
      };

      markdown-code-runner = {
        command = lib.getExe pkgs.markdown-code-runner;
        options =
          let
            config = pkgs.writers.writeTOML "markdown-code-runner-config" {
              presets.nixfmt = {
                language = "nix";
                command = [ (lib.getExe pkgs.nixfmt) ];
              };
            };
          in
          [ "--config=${config}" ];
        includes = [ "*.md" ];
      };

      nixf-diagnose = {
        command = lib.getExe pkgs.nixf-diagnose;
        excludes = [
          # Auto-generated; violates sema-extra-with
          # Can only sensibly be removed when --auto-fix supports multiple fixes at once:
          # https://github.com/inclyc/nixf-diagnose/issues/13
          "pkgs/servers/home-assistant/component-packages.nix"
          # https://github.com/nix-community/nixd/issues/708
          "nixos/maintainers/scripts/azure-new/examples/basic/system.nix"
        ];
        includes = [ "*.nix" ];
        options = [
          "--auto-fix"
          # Rule names can currently be looked up here:
          # https://github.com/nix-community/nixd/blob/main/libnixf/src/Basic/diagnostic.py
          # TODO: Remove the following and fix things.
          "--ignore=sema-unused-def-lambda-noarg-formal"
          "--ignore=sema-unused-def-lambda-witharg-arg"
          "--ignore=sema-unused-def-lambda-witharg-formal"
          "--ignore=sema-unused-def-let"
          # TODO: remove after outstanding prelude diagnostics issues are fixed:
          # https://github.com/nix-community/nixd/issues/761
          # https://github.com/nix-community/nixd/issues/762
          "--ignore=sema-primop-removed-prefix"
          "--ignore=sema-primop-overridden"
          "--ignore=sema-constant-overridden"
          "--ignore=sema-primop-unknown"
        ];
        # Ensure nixfmt cleans up after nixf-diagnose.
        priority = -1;
      };

      # This uses nixfmt underneath, the default formatter for Nix code.
      # See https://github.com/NixOS/nixfmt
      nixfmt = {
        command = lib.getExe pkgs.nixfmt;
        includes = [ "*.nix" ];
      };

      yamlfmt = {
        command = lib.getExe pkgs.yamlfmt;
        excludes = [
          # Aligns comments with whitespace
          "pkgs/development/haskell-modules/configuration-hackage2nix/main.yaml"
          # TODO: Fix formatting for auto-generated file
          "pkgs/development/haskell-modules/configuration-hackage2nix/transitive-broken.yaml"
        ];
        includes = [
          "*.yaml"
          "*.yml"
        ];
        options = [
          "-formatter"
          "retain_line_breaks=true"
        ];
      };

      zizmor = {
        command = lib.getExe pkgs.zizmor;
        includes = [
          ".github/workflows/*.yml"
          ".github/workflows/*.yaml"
          ".github/actions/**/*.yml"
          ".github/actions/**/*.yaml"
        ];
      };
      # keep-sorted end
    };
  };
}