-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathmodule-options.nix
More file actions
360 lines (331 loc) · 11.2 KB
/
module-options.nix
File metadata and controls
360 lines (331 loc) · 11.2 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
{
config,
options,
lib,
pkgs,
...
}:
let
inherit (lib) mkOption mkPackageOption types;
# A new kind of option type that calls lib.getExe on derivations
exeType = lib.mkOptionType {
name = "exe";
description = "Path to executable";
check = x: lib.isString x || builtins.isPath x || lib.isDerivation x;
merge =
loc: defs:
let
res = lib.mergeOneOption loc defs;
in
if lib.isString res || builtins.isPath res then "${res}" else lib.getExe res;
};
configFormat = pkgs.formats.toml { };
# Remove keys in the setting that are "empty" to keep the config file lean
emptySettingsKeys =
lib.optional (config.settings.excludes == [ ]) "excludes"
++ lib.optional (config.settings.on-unmatched == null) "on-unmatched"
# Remove deprecated 'global' key (created by mkRenamedOptionModule for backwards compatibility)
++ [ "global" ];
settingsData = builtins.removeAttrs config.settings emptySettingsKeys;
configFile = configFormat.generate "treefmt.toml" settingsData;
# The schema of the treefmt.toml data structure.
configSchema = mkOption {
default = { };
description = "The contents of treefmt.toml";
type = types.submodule {
imports = [
(lib.mkRenamedOptionModule [ "global" "excludes" ] [ "excludes" ])
(lib.mkRenamedOptionModule [ "global" "on-unmatched" ] [ "on-unmatched" ])
];
freeformType = configFormat.type;
options = {
excludes = mkOption {
description = "A global list of paths to exclude. Supports glob.";
type = types.listOf types.str;
default = [ ];
example = [ "node_modules/*" ];
};
on-unmatched = mkOption {
description = "Log paths that did not match any formatters at the specified log level.";
type = types.nullOr (
types.enum [
"debug"
"info"
"warn"
"error"
"fatal"
]
);
default = null;
};
formatter = mkOption {
type = types.attrsOf (
types.submodule [
{
freeformType = configFormat.type;
options = {
command = mkOption {
description = "Executable obeying the treefmt formatter spec";
type = exeType;
};
options = mkOption {
description = "List of arguments to pass to the command";
type = types.listOf types.str;
default = [ ];
};
includes = mkOption {
description = "List of files to include for formatting. Supports globbing.";
type = types.listOf types.str;
};
excludes = mkOption {
description = "List of files to exclude for formatting. Supports globbing. Takes precedence over the includes.";
type = types.listOf types.str;
default = [ ];
};
};
}
]
);
default = { };
description = "Set of formatters to use";
};
};
config = {
excludes = lib.mkIf config.enableDefaultExcludes [
# 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
".gitattributes"
".gitignore"
".gitmodules"
".hgignore"
".svnignore"
# License
"LICENSE"
];
};
};
};
in
{
# Schema
options = {
# Represents the treefmt.toml config
settings = configSchema;
package = mkPackageOption pkgs "treefmt" { };
projectRootFile = mkOption {
description = ''
File to look for to determine the root of the project in the
build.wrapper.
Set to null to let treefmt use its native detection.
'';
default = ".git/config";
type = types.nullOr types.str;
};
enableDefaultExcludes = mkOption {
description = ''
Enable the default excludes in the treefmt configuration.
'';
type = types.bool;
default = true;
};
# Meta attributes
meta = {
maintainers = lib.mkOption {
type = lib.types.listOf lib.types.str;
internal = true;
default = [ ];
example = lib.literalExpression ''[ "zimbatm" ]'';
description = ''
List of github users responsible for a formatter.
This option should be defined at most once per module.
'';
};
broken = lib.mkOption {
type = lib.types.bool;
internal = true;
default = false;
description = ''
Whether this formatter module is broken.
'';
};
platforms = lib.mkOption {
type = lib.types.nullOr (lib.types.listOf lib.types.str);
internal = true;
default = null;
example = lib.literalExpression ''[ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]'';
description = ''
List of supported platforms for this formatter.
Null means all platforms are supported.
'';
};
skipExample = lib.mkOption {
type = lib.types.bool;
internal = true;
default = false;
description = ''
Whether to skip this formatter in example generation (e.g., due to store paths in output).
When true, the formatter will be excluded from example generation.
'';
};
brokenPlatforms = lib.mkOption {
type = lib.types.listOf lib.types.str;
internal = true;
default = [ ];
example = lib.literalExpression ''[ "x86_64-darwin" ]'';
description = ''
List of platforms where this formatter is broken.
These platforms will be excluded even if they appear in the platforms list.
'';
};
};
# Outputs
build = {
devShell = mkOption {
description = "The development shell with treefmt and its underlying programs";
type = types.package;
readOnly = true;
};
configFile = mkOption {
description = ''
Contains the generated config file derived from the settings.
'';
type = types.path;
};
wrapper = mkOption {
description = ''
The treefmt package, wrapped with the config file.
'';
type = types.package;
defaultText = lib.literalMD "wrapped `treefmt` command";
default =
let
code =
if config.projectRootFile == null then
''
set -euo pipefail
exec ${config.package}/bin/treefmt \
--config-file=${config.build.configFile} \
"$@"
''
else if builtins.compareVersions "2.0.0-rc4" config.package.version == 1 then
''
set -euo pipefail
find_up() {
ancestors=()
while true; do
if [[ -f $1 ]]; then
echo "$PWD"
exit 0
fi
ancestors+=("$PWD")
if [[ $PWD == / ]] || [[ $PWD == // ]]; then
echo "ERROR: Unable to locate the projectRootFile ($1) in any of: ''${ancestors[*]@Q}" >&2
exit 1
fi
cd ..
done
}
tree_root=$(find_up "${config.projectRootFile}")
exec ${config.package}/bin/treefmt --config-file ${config.build.configFile} "$@" --tree-root "$tree_root"
''
# treefmt-2.0.0-rc4 and later support the tree-root-file option
else
''
set -euo pipefail
unset PRJ_ROOT
exec ${config.package}/bin/treefmt \
--config-file=${config.build.configFile} \
--tree-root-file=${config.projectRootFile} \
"$@"
'';
x = pkgs.writeShellScriptBin "treefmt" code;
in
x.overrideAttrs (prev: {
meta = config.package.meta // prev.meta;
});
};
programs = mkOption {
type = types.attrsOf types.package;
description = ''
Attrset of formatter programs enabled in treefmt configuration.
The key of the attrset is the formatter name, with the value being the
package used to do the formatting.
'';
defaultText = lib.literalMD "Programs used in configuration";
default = pkgs.lib.concatMapAttrs (
k: v:
if (options.programs.${k}.enable.visible or true) && v.enable then
{
"${k}" = if options.programs.${k}.finalPackage.isDefined then v.finalPackage else v.package;
}
else
{ }
) config.programs;
};
check = mkOption {
description = ''
Create a flake check to test that the given project tree is already
formatted.
Input argument is the path to the project tree (usually 'self').
'';
type = types.functionTo types.package;
defaultText = lib.literalMD "Default check implementation";
default =
self:
pkgs.runCommandLocal "treefmt-check"
{
buildInputs = [
pkgs.git
pkgs.git-lfs
config.build.wrapper
];
meta.description = "Check that the project tree is formatted";
}
''
set -e
# `treefmt --fail-on-change` is broken for purs-tidy; So we must rely
# on git to detect changes. An unintended advantage of this approach
# is that when the check fails, it will print a helpful diff at the end.
PRJ=$TMP/project
cp -r ${self} $PRJ
chmod -R a+w $PRJ
cd $PRJ
export HOME=$TMPDIR
cat > $HOME/.gitconfig <<EOF
[user]
name = Nix
email = nix@localhost
[init]
defaultBranch = main
EOF
git init --quiet
git add .
git commit -m init --quiet
export LANG=${if pkgs.stdenv.isDarwin then "en_US.UTF-8" else "C.UTF-8"}
export LC_ALL=${if pkgs.stdenv.isDarwin then "en_US.UTF-8" else "C.UTF-8"}
treefmt --version
treefmt --no-cache
git status --short
git --no-pager diff --exit-code
touch $out
'';
};
};
};
# Config
config.build = {
inherit configFile;
devShell = pkgs.mkShell {
nativeBuildInputs = [ config.build.wrapper ] ++ (lib.attrValues config.build.programs);
};
};
}