forked from numtide/treefmt-nix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
module-options.nix
206 lines (193 loc) · 6.37 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
{ config
, 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;
};
# The schema of the treefmt.toml data structure.
configSchema = {
global = {
excludes = mkOption {
description = "A global list of paths to exclude. Supports glob.";
type = types.listOf types.str;
default = [ ];
example = [ "./node_modules/**" ];
};
hidden = mkOption {
description = "Whether to include hidden files and directories when formatting. Do note that this will include .git, so it is recommended that you exclude that directory if enabling this.";
type = types.bool;
default = false;
example = false;
};
};
formatter = mkOption {
type = types.attrsOf (types.submodule [
{
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";
};
};
configFormat = pkgs.formats.toml { };
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";
};
# 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
x = pkgs.writeShellScriptBin "treefmt" ''
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} ${if config.settings.global.hidden then "--hidden" else ""} "$@" --tree-root "$tree_root"
'';
in
(x // { 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 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=C.UTF-8
export LC_ALL=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);
};
};
}