-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.nix
358 lines (330 loc) · 15.4 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
let
f = { pkgs, skipNixpkgsVersionCheck ? false }:
assert pkgs.lib.assertMsg
(!skipNixpkgsVersionCheck && pkgs.lib.versionAtLeast (builtins.replaceStrings [ "pre-git" ] [ "" ] (pkgs.lib.version or "0.0.0")) "22.05")
"Nedryland supports nixpkgs versions >= 22.05, you have ${pkgs.lib.version or "unknown"}}";
let
pkgs' = pkgs;
version = "10.0.0";
versionAtLeast = pkgs'.lib.versionAtLeast version;
in
{
inherit version versionAtLeast;
pkgs = pkgs';
docs = pkgs'.stdenv.mkDerivation rec {
name = "nedryland-docs";
src = builtins.path { inherit name; path = ./docs; };
changelog = ./CHANGELOG.md;
postUnpack = "cp $changelog CHANGELOG.md";
buildInputs = [ pkgs'.mdbook ];
buildPhase = "mdbook build --dest-dir book";
installPhase = ''
mkdir -p $out/share/doc/nedryland/manual
cp -r book/. $out/share/doc/nedryland/manual
'';
};
checks = pkgs'.callPackage ./ci { };
mkTheme = import ./mktheme.nix pkgs';
mkProject =
attrs@{ name
, ...
}:
let
configContentFromEnv = builtins.getEnv "${pkgs'.lib.toUpper appliedAttrs.name}_config";
configContent =
if configContentFromEnv != "" then configContentFromEnv else
(
if appliedAttrs ? configFile
&& builtins.pathExists appliedAttrs.configFile then
builtins.readFile appliedAttrs.configFile
else ""
);
configRoot = if appliedAttrs ? configFile then builtins.dirOf appliedAttrs.configFile else null;
parseConfig = import ./config.nix pkgs' configContent configRoot (pkgs'.lib.toUpper name);
deployment = import ./deployment.nix pkgs';
componentFns = import ./component.nix pkgs' deployment.mkCombinedDeployment parseConfig;
# create the non-extended base
minimalBase =
let
minimalBase = {
inherit
version
versionAtLeast
callFile
callFunction
parseConfig;
mkShellCommands = pkgs'.callPackage ./shell-commands.nix { };
inCI = builtins.stringLength (builtins.getEnv "CI") > 0;
resolveInputs = name: typeName: targets: builtins.map
(input:
if input ? isNedrylandComponent then
input."${(pkgs'.lib.findFirst
(target: builtins.hasAttr target input)
(abort "${name}.${typeName} did not contain any of the targets ${builtins.toString targets}. Please specify a valid target.")
targets)}"
else
input
);
mkDerivation = attrs@{ stdenv ? pkgs'.stdenv, ... }:
assert pkgs'.lib.assertMsg
(!(attrs ? name) -> attrs ? pname && attrs ? version)
"mkDerivation missing required argument name, alternatively supply pname and version.";
let
gitignore = (import (builtins.fetchTarball {
url = "https://github.com/hercules-ci/gitignore.nix/archive/a20de23b925fd8264fd7fad6454652e142fd7f73.tar.gz";
sha256 = "sha256:07vg2i9va38zbld9abs9lzqblz193vc5wvqd6h7amkmwf66ljcgh";
})) {
lib = pkgs'.lib // (pkgs'.lib.optionalAttrs (! pkgs'.lib ? inPureEvalMode) {
inPureEvalMode = ! builtins ? currentSystem;
});
};
customerFilter = src:
let
# IMPORTANT: use a let binding like this to memoize info about the git directories.
srcIgnored = gitignore.gitignoreFilter src;
in
filter:
path: type:
(srcIgnored path type) && (filter path type);
filteredSrc =
if attrs ? srcFilter && attrs ? src then
pkgs'.lib.cleanSourceWith
{
inherit (attrs) src;
filter = customerFilter attrs.src attrs.srcFilter;
name = "${attrs.name or attrs.pname}-source";
} else gitignore.gitignoreSource attrs.src;
in
stdenv.mkDerivation ((builtins.removeAttrs attrs [ "stdenv" "srcFilter" "shellCommands" ]) //
{
isNedrylandDerivation = true;
passthru = (attrs.passthru or { }) // {
isNedrylandDerivation = true;
shellCommands = attrs.shellCommands or { };
};
shellInputs = attrs.shellInputs or [ ] ++ pkgs'.lib.optional (attrs ? targetSetup) attrs.targetSetup;
}
// pkgs'.lib.optionalAttrs (attrs.doCheck or true)
(
let
checkAttrs = {
# LintPhase for checks that does not require to run the built program
lintPhase = attrs.lintPhase or ''echo "No lintPhase defined, doing nothing"'';
preInstallPhases = attrs.preInstallPhases or [ ] ++ [ "lintPhase" ];
nativeBuildInputs = attrs.nativeBuildInputs or [ ] ++ attrs.lintInputs or [ ];
};
in
checkAttrs // pkgs'.lib.optionalAttrs (stdenv.hostPlatform != stdenv.buildPlatform && attrs.doCrossCheck or false) {
crossCheckPhase = attrs.crossCheckPhase or attrs.checkPhase or ''echo "No checkPhase or crossCheckPhase defined (but doCrossCheck is true), doing nothing"'';
preInstallPhases = checkAttrs.preInstallPhases ++ [ "crossCheckPhase" ];
nativeBuildInputs = checkAttrs.nativeBuildInputs ++ attrs.checkInputs or [ ];
}
)
// (pkgs'.lib.optionalAttrs (attrs ? src) {
src = if pkgs'.lib.isStorePath attrs.src then attrs.src else filteredSrc;
}));
inherit (componentFns) mapComponentsRecursive collectComponentsRecursive mkComponentSet;
inherit deployment;
mkTargetSetup = import ./target-setup pkgs' parseConfig;
documentation = import ./documentation pkgs' minimalBase;
setComponentPath = path:
let
overriddenMkComponent = componentFns.mkComponent path;
in
minimalBase // {
mkComponentSet = componentFns.mkComponentSet overriddenMkComponent;
mkComponent = overriddenMkComponent;
mkClient = targets: overriddenMkComponent (targets // { nedrylandType = "client"; });
mkService = targets: overriddenMkComponent (targets // { nedrylandType = "service"; });
mkLibrary = targets: overriddenMkComponent (targets // { nedrylandType = "library"; });
};
};
in
minimalBase.setComponentPath ./.;
evalBaseExtensionsWith = baseExtensions: initialBase: components:
(builtins.foldl'
(
combinedBaseExtensions: currentBaseExtension:
let
extFn = if builtins.isPath currentBaseExtension then import currentBaseExtension else currentBaseExtension;
args = builtins.functionArgs extFn;
in
pkgs'.lib.recursiveUpdate combinedBaseExtensions (if builtins.isAttrs currentBaseExtension then currentBaseExtension else
(
extFn (
builtins.intersectAttrs args (components // { inherit components; })
// builtins.intersectAttrs args pkgs'
// builtins.intersectAttrs args (let base = pkgs'.lib.recursiveUpdate initialBase combinedBaseExtensions; in base // { inherit base; })
)
))
)
{ }
baseExtensions
);
# extend base with base extensions from this and dependent projects
extendBase = minimalBase:
let
originalSetComponentPath = minimalBase.setComponentPath;
inner = minimalBase:
let
evalDependenciesBaseExtensions = dependencies: initialBase:
builtins.foldl' pkgs'.lib.recursiveUpdate initialBase (builtins.map
(pd:
evalBaseExtensionsWith
pd.baseExtensions
(evalDependenciesBaseExtensions pd.dependencies initialBase)
pd.components.nedrylandComponents
)
dependencies);
# evaluate all base extensions from dependent projects recursively
dependenciesBase = evalDependenciesBaseExtensions (appliedAttrs.dependencies or [ ]) minimalBase;
in
# evaluate base extensions for current project
pkgs'.lib.recursiveUpdate dependenciesBase (evalBaseExtensionsWith
(appliedAttrs.baseExtensions or [ ])
dependenciesBase
resolvedComponents);
in
(inner minimalBase) // {
setComponentPath = path: inner (originalSetComponentPath path);
};
extendedBase = extendBase minimalBase;
# callFile and callFunction will auto-populate dependencies
# on nixpkgs, base members and project components
callFile = path: callFunction (import path) path;
callFunction = function: path:
let
args = builtins.functionArgs function;
# Burn the path into newBase
newBase = extendedBase.setComponentPath path;
in
pkgs'.lib.makeOverridable
(
overrides:
function
(
(builtins.intersectAttrs args pkgs')
// (builtins.intersectAttrs args (resolvedComponents // { components = resolvedComponents; }))
// (builtins.intersectAttrs args (newBase // { base = newBase; }))
// overrides
)
);
# we support most arguments to mkProject being functions
# that accept a minimal base
appliedAttrs =
builtins.mapAttrs
(_: v:
# intersect with minimal base (without extensions) to avoid cyclic deps
if builtins.isFunction v then
v (builtins.intersectAttrs (builtins.functionArgs v) minimalBase)
else
v
)
attrs;
resolvedComponents = appliedAttrs.components;
resolvedNedrylandComponents = componentFns.collectComponentsRecursive resolvedComponents;
# create a set of all available targets
# for use as one axis in the matrix
allTargets =
builtins.mapAttrs
(target: drvs:
# create a link farm where each link has the name of the access path to
# the component. I.e. the target `tgt` in the component `myComponent`
# has the link name `myComponent` so that if you `nix build
# .#targets.tgt` followed by `ls -l result/` you will see `myComponent
# -> /nix/store/something`.
extendedBase.mkComponentSet
target
(builtins.listToAttrs (builtins.map
(value: {
inherit value;
name = builtins.concatStringsSep "." (value.accessPath ++ [ target ]);
})
drvs)))
(pkgs'.lib.zipAttrs
(
builtins.map
(comp:
# add the accessPath of the component to the individual
# drvs to be able to use in linkfarm above.
# looks like `[ "grandParentComponent" "parentComponent" "component" ]`
builtins.mapAttrs
(_: v: v // { inherit (comp) accessPath; })
(pkgs'.lib.filterAttrs
(
name: value:
name != "_default" &&
(pkgs'.lib.isDerivation value) &&
!(value.isNedrylandComponent or false)
)
comp.componentAttrs)
)
resolvedNedrylandComponents
));
# any extra attributes are assumed to be targets in the matrix
extraTargets = builtins.mapAttrs
(
_: value: if builtins.isFunction value then value resolvedComponents else value
)
(builtins.removeAttrs appliedAttrs [
"components"
"extraShells"
"lib"
"baseExtensions"
"configFile"
"name"
"dependencies"
"themes"
"version"
]);
componentSet = builtins.mapAttrs
(name: value:
if pkgs.lib.isDerivation value then
value
else
if builtins.isAttrs value then
extendedBase.mkComponentSet
name
value
else
builtins.throw "components can only be attrsets or derivations."
)
(resolvedComponents // extraTargets);
in
rec {
inherit (appliedAttrs) name;
pkgs = pkgs';
base = extendedBase;
lib = appliedAttrs.lib or { };
baseExtensions = appliedAttrs.baseExtensions or [ ];
dependencies = appliedAttrs.dependencies or [ ];
# x-axis
components = extendedBase.mkComponentSet
"components"
componentSet;
# y-axis
targets = extendedBase.mkComponentSet
"targets"
allTargets;
# matrix
matrix = componentSet // { inherit targets; };
# do not fall for the temptation to use callPackage here. callPackage blindly
# adds "override" and "overrideDerivation" functions which will break flake
# checks since functions are not derivations.
shells =
let
f = import ./shells.nix;
in
f ((builtins.intersectAttrs (builtins.functionArgs f) pkgs') // {
components = componentSet;
inherit (minimalBase) mkShellCommands mapComponentsRecursive parseConfig collectComponentsRecursive;
extraShells = appliedAttrs.extraShells or { };
});
} // (pkgs'.lib.optionalAttrs
(appliedAttrs ? version)
{ inherit (appliedAttrs) version; });
override =
f;
};
in
f