-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
234 lines (206 loc) · 7.78 KB
/
flake.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
{
description = "@stepbrobd: flake parts with autoloading";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable-small";
parts.url = "github:hercules-ci/flake-parts";
systems.url = "github:nix-systems/default";
};
outputs = { nixpkgs, parts, systems, ... } @ inputs: {
inherit (parts.lib.mkFlake { inherit inputs; } {
systems = import systems;
perSystem = { pkgs, ... }: { formatter = pkgs.nixpkgs-fmt; };
flake.lib =
let
lib = nixpkgs.lib // parts.lib;
inherit (builtins)
attrNames
elem
filter
listToAttrs
map
readDir
removeAttrs
substring
;
inherit (lib)
attrByPath
concatMapStrings
evalFlakeModule
makeExtensible
mergeAttrsList
mkIf
optionals
recursiveUpdate
removeSuffix
splitString
toLower
toUpper
;
in
makeExtensible (_: rec {
/**
Generates a list of files in a directory, excluding the ones specified in `excludes`.
# Type: filesList :: Path -> [String] -> [String]
# Example:
filesList ./. [ "default.nix" ]
=> [ "file1.nix" "file2.nix" ... ]
*/
filesList = dir: excludes: map (f: dir + "/${f}") (filter
(f: !(elem f excludes))
(attrNames (readDir dir)));
/**
Converts a kebab-case string to a camelCase string.
https://discourse.nixos.org/t/implementing-kebab-case-to-camelcase-in-nix/47313/3
# Type: kebabToCamel :: String -> String
# Example:
kebabToCamel "abc-def-g"
=> "abcDefG"
*/
kebabToCamel = s:
mutFirstChar toLower (concatMapStrings
(mutFirstChar toUpper) # eta reduction
(splitString "-" s)
);
/**
Imports all `.nix` files in a directory with optional arguments.
This is meant to be used to load functions from a directory, and use the file name as the function name.
# Type: loadAll :: { dir :: Path; importer :: (Path -> ?); transformer :: (a -> String); excludes :: [String]; args :: AttrSet } -> AttrSet
# Example:
loadAll { dir = ./.
; transformer = kebabToCamel
; excludes = [ "default.nix" ]
; args = { lib = final; }
}
=> { mkModuleArgs = <function>; ... }
*/
loadAll =
{ dir ? ./.
, importer ? import
, transformer ? _: _
, excludes ? [ ]
, args ? { }
}: listToAttrs (map
# file name
(fn: {
# name transformation (e.g. "mk-module-args" -> "mkModuleArgs")
name = transformer (removeSuffix ".nix" fn);
# function import
value = importer (dir + "/${fn}") args;
})
(filter (n: !(elem n excludes)) (attrNames (readDir dir))));
/**
Eval Autopilot before invoking flake-parts' `evalFlakeModule`.
# Type: mkFlake :: (args :: AttrSet) -> (module :: AttrSet) -> AttrSet
# Example:
mkFlake { inherit inputs; } { system = [ "x86_64-linux" ]; }
=> { ... }
*/
mkFlake = args: module:
let
cfg = recursiveUpdate
# default config
{
lib = {
enable = if (attrByPath [ "autopilot" "lib" "path" ] null args) == null then false else true;
path = null; # a sensible default without infinite recursion?
excludes = [ ];
extender = args.inputs.nixpkgs.lib;
extensions = [ ];
};
nixpkgs = {
enable = true;
config = { };
overlays = [ ];
instances = [{ name = "pkgs"; value = args.inputs.nixpkgs; }];
};
parts = {
enable = if (attrByPath [ "autopilot" "parts" "path" ] null args) == null then false else true;
path = null; # a sensible default without infinite recursion?
excludes = [ ];
};
}
# user config
args.autopilot;
# load `lib` first
# autopilot.lib = {
# path = ./lib;
# excludes = [ ... ];
# extender = args.inputs.nixpkgs.lib;
# extensions = [ ... ];
# };
finalLib =
if cfg.lib.enable then
cfg.lib.extender.extend
(final: prev: mergeAttrsList (
cfg.lib.extensions ++ [
(loadAll {
dir = cfg.lib.path;
transformer = kebabToCamel;
excludes = cfg.lib.excludes;
args = { lib = final; };
})
]
))
else { };
userLib =
if cfg.lib.enable then
removeAttrs
finalLib
((attrNames (mergeAttrsList cfg.lib.extensions))
++
(attrNames cfg.lib.extender))
else { };
# inject `lib` to flake-parts `evalModules`'s `specialArgs`
finalArgs = removeAttrs (recursiveUpdate args (if cfg.lib.enable then { specialArgs.lib = finalLib; } else { })) [ "autopilot" ];
finalModule = {
flake.lib = makeExtensible (_: userLib);
# customize flake-parts per-system nixpkgs instances
# autopilot.nixpkgs = {
# config = { ... }; # nixpkgs config
# overlays = [ ... ]; # nixpkgs overlays
# instances = [
# { name = "pkgs"; value = args.inputs.nixpkgs; };
# { name = "unstable"; value = args.inputs.unstable; };
# ];
# };
perSystem = { system, ... }: mkIf cfg.nixpkgs.enable {
_module.args = listToAttrs (
map
(attr: {
inherit (attr) name;
value = import attr.value { inherit system; inherit (cfg.nixpkgs) config overlays; };
})
cfg.nixpkgs.instances
);
};
# user defined flake-part module
imports = [ module ]
# load user flake-parts
# autopilot.parts = {
# path = ./parts;
# excludes = [ ... ];
# };
++ optionals cfg.parts.enable (filesList cfg.parts.path cfg.parts.excludes);
};
in
(evalFlakeModule finalArgs finalModule).config.flake;
/**
Mutates the first character of a string using a function, the rest of the string is left untouched.
# Type: mutFirstChar :: (a -> String) -> String -> String
# Example:
mutFirstChar toUpper "abcd"
=> "Abcd"
*/
mutFirstChar = f: s:
let
first = f (substring 0 1 s);
rest = substring 1 (-1) s;
in
first + rest;
});
})
formatter
lib
;
};
}