Repository navigation
Expand file tree
/
Copy pathformatter.nix
More file actions
260 lines (247 loc) · 8.87 KB
/
Copy pathformatter.nix
File metadata and controls
260 lines (247 loc) · 8.87 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
pkgs:
# user-provided args
{ nix ? true
, rust ? true
, python ? true
, shell ? true
, protobuf ? true
, toml ? true
, yaml ? true
, json ? true
, javascript ? true
, typescript ? true
, css ? true
, scss ? true
, html ? true
, graphql ? true
, markdown ? true
, mermaid ? true
, caddyfile ? true
# `just --fmt` is marked unstable upstream (requires `--unstable`); opt-in.
, justfile ? false
, copyright ? true
, config ? { }
}:
let
lib = pkgs.lib;
args = {
inherit nix rust python shell protobuf toml yaml json javascript typescript css scss html graphql markdown mermaid caddyfile justfile copyright;
config = ({ withOpenDir ? false, exclude ? [ ] }: { inherit withOpenDir exclude; }) config;
};
# stolen from https://github.com/numtide/treefmt-nix/blob/29a3d7b768c70addce17af0869f6e2bd8f5be4b7/module-options.nix#L90
defaultExcludes = [
# generated lock files i.e. yarn, cargo, nix flakes
"*.lock"
# Files generated by patch
"*.patch"
# NPM
"package-lock.json"
# Go
# In theory go mod tidy could format this, but it has other side-effects beyond formatting.
"go.mod"
"go.sum"
# VCS
".gitignore"
".gitmodules"
".hgignore"
".svnignore"
];
mermaidFormatter = pkgs.stdenv.mkDerivation {
pname = "mermaid-formatter";
version = "0.3.0";
src = pkgs.fetchurl {
url = "https://registry.npmjs.org/mermaid-formatter/-/mermaid-formatter-0.3.0.tgz";
hash = "sha256-PKv0cFTu7lr8U5raoGhF/xbjEx1cQEvZ9xhSY51jFHQ=";
};
dontBuild = true;
installPhase = ''
mkdir -p $out/lib/mermaid-formatter $out/bin
cp -r . $out/lib/mermaid-formatter
cat > $out/bin/mermaidfmt <<WRAPPER
#!/bin/sh
exec ${lib.getExe pkgs.nodejs} $out/lib/mermaid-formatter/dist/cli.js "\$@"
WRAPPER
chmod +x $out/bin/mermaidfmt
'';
};
copyrightFiles = [ "*.rs" ];
treefmtConfig = pkgs.treefmt.buildConfig {
on-unmatched = "debug";
excludes = args.config.exclude ++ defaultExcludes;
formatter = with lib.modules; mkMerge [
{
nix = mkIf args.nix {
command = lib.getExe pkgs.nixpkgs-fmt;
includes = [ "*.nix" ];
};
rust = mkIf args.rust {
command = lib.getExe pkgs.rustfmt;
options = [ "--edition" "2024" ];
includes = [ "*.rs" ];
};
python = mkIf args.python {
command = lib.getExe pkgs.ruff;
options = [ "format" "--no-cache" "--config" ./fmt-assets/ruff.toml ];
includes = [ "*.py" "*.pyi" ];
};
shell = mkIf args.shell {
command = lib.getExe pkgs.shfmt;
options = [ "--simplify" "--write" "--binary-next-line" "--indent" "4" ];
includes = [ "*.sh" "*.bash" "*.envrc" "*.envrc.*" ];
};
protobuf = mkIf args.protobuf {
command = pkgs.writeShellScript "buf-format" "exec ${pkgs.buf}/bin/buf format --write";
includes = [ "*.proto" ];
};
toml = mkIf args.toml {
command = lib.getExe pkgs.taplo;
options = [ "format" "--config" ./fmt-assets/taplo.toml ];
includes = [ "*.toml" ];
};
yaml = mkIf args.yaml {
command = lib.getExe pkgs.yamlfmt;
options = [ "-conf" ./fmt-assets/yamlfmt.yml ];
includes = [ "*.yaml" "*.yml" ];
};
json = mkIf args.json {
command = lib.getExe pkgs.biome;
options = [ "format" "--write" "--config-path" ./fmt-assets/biome.json ];
includes = [ "*.json" "*.jsonc" ];
};
javascript = mkIf args.javascript {
command = lib.getExe pkgs.biome;
options = [ "format" "--write" "--config-path" ./fmt-assets/biome.json ];
includes = [ "*.js" "*.mjs" "*.cjs" "*.jsx" ];
};
typescript = mkIf args.typescript {
command = lib.getExe pkgs.biome;
options = [ "format" "--write" "--config-path" ./fmt-assets/biome.json ];
includes = [ "*.ts" "*.mts" "*.cts" "*.tsx" ];
};
css = mkIf args.css {
command = lib.getExe pkgs.biome;
options = [ "format" "--write" "--config-path" ./fmt-assets/biome.json ];
includes = [ "*.css" ];
};
scss = mkIf args.scss {
command = lib.getExe pkgs.prettier;
options = [ "--write" ];
includes = [ "*.scss" "*.sass" ];
};
html = mkIf args.html {
command = lib.getExe pkgs.biome;
options = [ "format" "--write" "--config-path" ./fmt-assets/biome.json ];
includes = [ "*.html" "*.htm" ];
};
graphql = mkIf args.graphql {
command = lib.getExe pkgs.biome;
options = [ "format" "--write" "--config-path" ./fmt-assets/biome.json ];
includes = [ "*.graphql" "*.gql" ];
};
markdown = mkIf args.markdown {
command = lib.getExe (pkgs.mdformat.withPlugins (p: [
p.mdformat-gfm
p.mdformat-frontmatter
p.mdformat-simple-breaks
]));
options = [ "--number" "--wrap=120" ];
includes = [ "*.md" "*.markdown" ];
};
# mermaidfmt only accepts one file at a time, so we loop.
# We compare formatted output before writing to avoid unnecessary
# mtime updates (same false-positive issue as caddyfile, see above).
mermaid = mkIf args.mermaid {
command = pkgs.writeShellScript "mermaid-format" ''
mermaidfmt=${mermaidFormatter}/bin/mermaidfmt
for file in "$@"; do
if ! "$mermaidfmt" "$file" | diff -q - "$file" >/dev/null 2>&1; then
"$mermaidfmt" -w "$file"
fi
done
'';
includes = [ "*.mermaid" "*.mmd" ];
};
# `just --fmt` only accepts one file at a time, so we loop.
# `just --fmt` is idempotent: it leaves the file (and mtime) untouched
# when no reformatting is needed, so no diff-check is required here.
# `--fmt` is flagged unstable upstream, hence `--unstable`.
justfile = mkIf args.justfile {
command = pkgs.writeShellScript "just-format" ''
just=${lib.getExe pkgs.just}
for file in "$@"; do
"$just" --unstable --fmt --justfile "$file"
done
'';
includes = [ "justfile" "**/justfile" "Justfile" "**/Justfile" "*.just" "*.justfile" ];
};
# caddy fmt only accepts one file at a time, so we loop.
# We check with --diff first (exits 1 when changes needed) and only
# overwrite if formatting is required. This prevents unnecessary mtime
# updates which cause treefmt to detect false positives in CI where
# files from Nix store have epoch timestamps (1970-01-01).
caddyfile = mkIf args.caddyfile {
command = pkgs.writeShellScript "caddy-format" ''
caddy=${lib.getExe pkgs.caddy}
for file in "$@"; do
if ! "$caddy" fmt --diff "$file" >/dev/null 2>&1; then
"$caddy" fmt --overwrite "$file"
fi
done
'';
includes = [ "Caddyfile" "**/Caddyfile" "*.caddyfile" ];
};
copyright = mkIf (args.copyright && !args.config.withOpenDir) {
command = lib.getExe pkgs.addlicense;
options = [ "-f" ./fmt-assets/copyright-closed.tpl ];
includes = copyrightFiles;
priority = 1;
};
}
# two mutually exclusive instances of addlicense
(mkIf (args.copyright && args.config.withOpenDir) {
copyright = {
command = lib.getExe pkgs.addlicense;
options = [ "-f" ./fmt-assets/copyright-closed.tpl ];
includes = copyrightFiles;
excludes = [ "open/*" ];
priority = 1;
};
copyright-open = {
command = lib.getExe pkgs.addlicense;
options = [ "-f" ./fmt-assets/copyright-open.tpl ];
includes = map (pattern: "open/${pattern}") copyrightFiles;
priority = 2;
};
})
];
};
in
pkgs.writeShellApplication {
name = "braiinsfmt";
runtimeInputs = with pkgs; [ gitMinimal diffutils ]; # `diffutils` is required by `buf`
runtimeEnv.PATH = null;
excludeShellChecks = [ "SC2123" ];
text = ''
args=("$@")
${lib.optionalString (lib.versionOlder builtins.nixVersion "2.25") ''
# remove `.` added automatically by `nix fmt` because it breaks `treefmt` when executed from a subdirectory
for i in "''${!args[@]}"; do
if [[ ''${args[i]} == "." ]]; then
unset 'args[i]'
fi
done
''}
if tree_root=$(git rev-parse --show-toplevel 2>/dev/null); then
walk="git"
else
walk="filesystem"
tree_root="."
fi
echo "formatter root: $tree_root ($walk)"
exec ${pkgs.treefmt}/bin/treefmt \
--config-file ${treefmtConfig} \
--tree-root "$tree_root" \
--walk "$walk" \
"''${args[@]}"
'';
}