-
Notifications
You must be signed in to change notification settings - Fork 78
/
module-options.nix
283 lines (263 loc) · 8.68 KB
/
module-options.nix
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
{ 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 { };
# The schema of the treefmt.toml data structure.
configSchema = mkOption {
default = { };
description = "The contents of treefmt.toml";
type = types.submodule {
freeformType = configFormat.type;
options = {
global = {
excludes = mkOption {
description = "A global list of paths to exclude. Supports glob.";
type = types.listOf types.str;
default = [ ];
example = [ "./node_modules/**" ];
};
};
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 = {
global.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
".gitignore"
".gitmodules"
".hgignore"
".svnignore"
];
};
};
};
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.
'';
example = "flake.nix";
};
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.
'';
};
};
# 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 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;
# used by tooling to detect if treefmt was wrapped or not
y = pkgs.writeShellScriptBin "treefmt-nix" code;
in
(pkgs.symlinkJoin
{
name = "treefmt-nix";
paths = [
x
y
];
} // { meta = config.package.meta // x.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}" = 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
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
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
git --no-pager diff --exit-code
touch $out
'';
};
};
};
# Config
config.build = {
configFile = configFormat.generate "treefmt.toml" config.settings;
devShell = pkgs.mkShell {
nativeBuildInputs = [ config.build.wrapper ] ++ (lib.attrValues config.build.programs);
};
};
}