-
Notifications
You must be signed in to change notification settings - Fork 89
/
default.nix
159 lines (147 loc) · 3.9 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
# 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:
nixpkgs.lib.evalModules {
modules = all-modules nixpkgs ++ [ configuration ];
specialArgs = {
inherit mkFormatterModule;
};
};
# 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
mkConfigFile
mkWrapper
;
}