-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathdefault.nix
196 lines (176 loc) · 5.14 KB
/
default.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
# A pure Nix library that handles the treefmt configuration.
let
# The base module configuration that generates and wraps the treefmt config
# with Nix.
module-options = ./module-options.nix;
# Program to formatter mapping
programs = import ./programs.nix;
mkFormatterModule =
{
name,
package ? name,
mainProgram ? null,
args ? [ ],
includes ? [ ],
excludes ? [ ],
}:
{
pkgs,
lib,
config,
...
}:
let
cfg = config.programs.${name};
in
{
options.programs.${name} = {
enable = lib.mkEnableOption name;
package = lib.mkPackageOption pkgs package { };
includes = lib.mkOption {
description = "Path / file patterns to include";
type = lib.types.listOf lib.types.str;
default = includes;
};
excludes = lib.mkOption {
description = "Path / file patterns to exclude";
type = lib.types.listOf lib.types.str;
default = excludes;
};
priority = lib.mkOption {
description = "Priority";
type = lib.types.nullOr lib.types.int;
default = null;
};
};
config = lib.mkIf cfg.enable {
settings.formatter.${name} =
{
command = lib.mkDefault (
if mainProgram == null then cfg.package else "${cfg.package}/bin/${mainProgram}"
);
}
// (lib.optionalAttrs (args != [ ]) {
options = if args._type or null == "order" then args else lib.mkBefore args;
})
// (lib.optionalAttrs (cfg.includes != [ ]) {
inherit (cfg) includes;
})
// (lib.optionalAttrs (cfg.excludes != [ ]) {
inherit (cfg) excludes;
})
// (lib.optionalAttrs (cfg.priority != null) {
inherit (cfg) priority;
});
};
};
all-modules =
nixpkgs:
[
{
_module.args = {
pkgs = nixpkgs;
lib = nixpkgs.lib;
};
}
module-options
]
++ programs.modules;
# treefmt-nix can be loaded into a submodule. In this case we get our `pkgs` from
# our own standard option `pkgs`; not externally.
submodule-modules = [
(
{ config, lib, ... }:
let
inherit (lib)
mkOption
types
;
in
{
options.pkgs = mkOption {
type = types.uniq (types.lazyAttrsOf (types.raw or types.unspecified));
description = ''
Nixpkgs to use in `treefmt`.
'';
};
config._module.args = {
pkgs = config.pkgs;
};
}
)
module-options
] ++ programs.modules;
# Use the Nix module system to validate the treefmt config file format.
#
# nixpkgs is an instance of <nixpkgs> that contains treefmt.
# configuration is an attrset used to configure the nix module
evalModule =
nixpkgs: configuration:
# NOTE: keep in sync with submoduleWith
nixpkgs.lib.evalModules {
modules = all-modules nixpkgs ++ [ configuration ];
specialArgs = defaultSpecialArgs;
};
/**
The built-in specialArgs for treefmt-nix.
These are module arguments that are passed to all treefmt-nix modules.
*/
defaultSpecialArgs = {
inherit mkFormatterModule;
};
/**
Invoke treefmt-nix as a submodule, integrating this into a larger configuration management system.
Unlike in `evalModule`, the caller is responsible for setting `_module.args.pkgs` inside the submodule.
# Inputs
- `lib`: the Nixpkgs `lib`
- attribute set
- `modules`: additional modules to include. Unlike modules in `config`, these will be rendered in the documentation.
- `specialArgs`: additional arguments to pass to all modules in the submodule. See [`evalModules`' `specialArgs`](https://nixos.org/manual/nixpkgs/stable/#module-system-lib-evalModules-param-specialArgs).
# Output
A module system type that can be passed to [`mkOption`](https://nixos.org/manual/nixos/stable/#sec-option-declarations)'s `type`.
*/
submoduleWith =
lib:
{
modules ? [ ],
specialArgs ? { },
}:
# NOTE: keep in sync with evalModule
lib.types.submoduleWith {
modules = submodule-modules ++ modules;
specialArgs = defaultSpecialArgs // specialArgs;
};
# Returns a treefmt.toml generated from the passed configuration.
#
# nixpkgs is an instance of <nixpkgs> that contains treefmt.
# configuration is an attrset used to configure the nix module
mkConfigFile =
nixpkgs: configuration:
let
mod = evalModule nixpkgs configuration;
in
mod.config.build.configFile;
# Returns an instance of treefmt, wrapped with some configuration.
#
# nixpkgs is an instance of <nixpkgs> that contains treefmt.
# configuration is an attrset used to configure the nix module
mkWrapper =
nixpkgs: configuration:
let
mod = evalModule nixpkgs configuration;
in
mod.config.build.wrapper;
in
{
inherit
module-options
programs
all-modules
submodule-modules
evalModule
submoduleWith
mkConfigFile
mkWrapper
;
}