forked from ngi-nix/ngipkgs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.nix
More file actions
276 lines (253 loc) · 8.12 KB
/
Copy pathdefault.nix
File metadata and controls
276 lines (253 loc) · 8.12 KB
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
{
sources ? import (fetchTarball "https://github.com/fricklerhandwerk/flake-inputs/tarball/main") {
root = ./.;
},
system ? builtins.currentSystem,
pkgs ? import sources.nixpkgs {
config = { };
overlays = [ ];
inherit system;
},
lib ? import "${sources.nixpkgs}/lib",
}:
let
dream2nix = (import sources.dream2nix).overrideInputs { inherit (sources) nixpkgs; };
sops-nix = import "${sources.sops-nix}/modules/sops";
in
rec {
inherit
lib
pkgs
system
sources
;
optionsDoc = pkgs.nixosOptionsDoc {
inherit (evaluated-modules) options;
};
# TODO: we should be exporting our custom functions as `lib`, but refactoring
# this to use `pkgs.lib` everywhere is a lot of movement
lib' = {
# Take an attrset of arbitrary nesting and make it flat
# by concatenating the nested names with the given separator.
flattenAttrs =
separator:
let
f = path: lib.concatMapAttrs (flatten path);
flatten =
path: name: value:
if lib.isAttrs value then f (path + name + separator) value else { ${path + name} = value; };
in
f "";
# Recursively evaluate attributes for an attribute set.
# Coupled with an evaluated nixos configuration, this presents an efficient
# way for checking module types.
forceEvalRecursive =
attrs:
lib.mapAttrsRecursive (
n: v:
if lib.isList v then
map (
i:
# if eval fails
if !(builtins.tryEval i).success then
# recursively recurse into attrsets
if lib.isAttrs i then lib'.forceEvalRecursive i else (builtins.tryEval i).success
else
(builtins.tryEval i).success
) v
else
(builtins.tryEval v).success
) attrs;
# get the path of NixOS module from string
# example:
# lib'.moduleLocFromOptionString "services.ntpd-rs"
# => "/nix/store/...-source/nixos/modules/services/networking/ntp/ntpd-rs.nix"
moduleLocFromOptionString =
let
inherit
(lib.evalModules {
class = "nixos";
specialArgs.modulesPath = "${sources.nixpkgs}/nixos/modules";
modules = [
({
config = {
_module.check = false;
nixpkgs.hostPlatform = if builtins.isNull system then builtins.currentSystem else system;
};
})
] ++ import "${sources.nixpkgs}/nixos/modules/module-list.nix";
})
options
;
in
opt:
let
locList = lib.splitString "." opt;
optAttrs = lib.getAttrFromPath locList options;
# collect all file paths from all options
collectFiles =
attrs:
let
# get value of `files` attr or empty list
getFiles =
attr: if attr.value ? files && builtins.isList attr.value.files then attr.value.files else [ ];
in
lib.concatMap getFiles (lib.attrsToList attrs);
in
lib.head (collectFiles optAttrs);
};
overlays.default =
final: prev:
import ./pkgs/by-name {
pkgs = prev;
inherit lib dream2nix;
};
examples =
with lib;
mapAttrs (_: project: mapAttrs (_: example: example.module) project.nixos.examples) projects;
nixos-modules =
with lib;
# TODO: this is a weird shape for what we need: ngipkgs, services, modules?
{
# Allow using packages from `ngipkgs` to be used alongside regular `pkgs`
ngipkgs =
{ ... }:
{
nixpkgs.overlays = [ overlays.default ];
};
}
// foldl recursiveUpdate { } (map (project: project.nixos.modules) (attrValues projects));
ngipkgsModules = lib.filter (m: m != null) (
lib.mapAttrsToList (name: value: value) nixos-modules.services
++ lib.mapAttrsToList (name: value: value) nixos-modules.programs
);
nixosModules = import "${sources.nixpkgs}/nixos/modules/module-list.nix";
extendedNixosModules =
[
# Allow using packages from `ngipkgs` to be used alongside regular `pkgs`
{
nixpkgs.overlays = [ overlays.default ];
}
# TODO: needed for examples that use sops (like Pretalx)
sops-nix
]
++ ngipkgsModules
++ nixosModules;
evaluated-modules = lib.evalModules {
class = "nixos";
modules = [
{
nixpkgs.hostPlatform = { inherit system; };
networking = {
domain = "invalid";
hostName = "options";
};
# faster eval time
documentation.nixos.enable = false;
documentation.man.generateCaches = false;
system.stateVersion = "23.05";
}
raw-projects # for checks
] ++ extendedNixosModules;
specialArgs = {
modulesPath = "${sources.nixpkgs}/nixos/modules";
};
};
# recursively evaluates each attribute for all projects
check-projects = lib'.forceEvalRecursive evaluated-modules.config.projects;
ngipkgs = import ./pkgs/by-name { inherit pkgs lib dream2nix; };
raw-projects = import ./projects {
inherit lib;
pkgs = pkgs // ngipkgs;
sources = {
inputs = sources;
modules = nixos-modules;
inherit examples;
};
};
projects = make-projects raw-projects.config.projects;
# TODO: find a better place for this
make-projects =
projects:
with lib;
let
nixosTest =
test:
let
# Amenities for interactive tests
tools =
{ pkgs, ... }:
{
environment.systemPackages = with pkgs; [
vim
tmux
jq
];
# Use kmscon <https://www.freedesktop.org/wiki/Software/kmscon/>
# to provide a slightly nicer console.
# kmscon allows zooming with [Ctrl] + [+] and [Ctrl] + [-]
services.kmscon = {
enable = true;
autologinUser = "root";
};
};
debugging.interactive.nodes = mapAttrs (_: _: tools) test.nodes;
in
pkgs.nixosTest (debugging // test);
empty-if-null = x: if x != null then x else { };
filter-map =
attrs: input:
lib.pipe attrs [
(lib.concatMapAttrs (_: value: value."${input}" or { }))
(lib.filterAttrs (_: v: v != null))
];
hydrate =
# we use fields to track state of completion.
# - `null` means "expected but missing"
# - not set means "not applicable"
# TODO: encode this in types, either yants or the module system
project: rec {
metadata = empty-if-null (filterAttrs (_: m: m != null) (project.metadata or { }));
nixos.modules.services = filterAttrs (_: m: m != null) (
lib.mapAttrs (name: value: value.module or null) project.nixos.modules.services or { }
);
nixos.modules.programs = filterAttrs (_: m: m != null) (
lib.mapAttrs (name: value: value.module or null) project.nixos.modules.programs or { }
);
# TODO: access examples for services and programs separately?
nixos.examples =
(empty-if-null (project.nixos.examples or { }))
// (filter-map (project.nixos.modules.programs or { }) "examples")
// (filter-map (project.nixos.modules.services or { }) "examples");
nixos.tests = mapAttrs (
_: test:
if lib.isString test then
(import test {
inherit pkgs;
inherit (pkgs) system;
})
else if lib.isDerivation test then
test
else
nixosTest test
) ((empty-if-null project.nixos.tests or { }) // (filter-map (nixos.examples or { }) "tests"));
};
in
mapAttrs (name: project: hydrate project) projects;
shell = pkgs.mkShellNoCC {
packages = [
# live overview watcher
(pkgs.devmode.override {
buildArgs = "${toString ./overview/devmode.nix} --show-trace";
})
];
};
demo = import ./overview/demo {
inherit
lib
pkgs
sources
extendedNixosModules
;
};
}