-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractConfig.nix
82 lines (72 loc) · 2.58 KB
/
extractConfig.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
{ lib, config }:
with builtins;
with lib;
let
utilities = rec {
# Extract the text from sets of the form `{ _type = "literalExpression"; text = <something>; }`
extractLiteralExpression = expr:
if ((expr._type or null) == "literalExpression") then expr.text else expr;
# Visibility related functions
isVisibleNameValue = name: v:
if name == "_module" then
false
else if isOption v then
isVisibleOption v
else
true;
isVisibleOption = { visible ? true, internal ? false, ... }@opt:
(if isString visible then visible == "shallow" else visible) && !internal;
# Guard methods, they take a function for easy composability
guardTryEval = f: v:
let try = tryEval v;
in if try.success then f try.value else { _error = true; };
guardDerivation = f: v:
if (isDerivation v) then {
_derivation = true;
# TODO: protect against errors here too?
inherit (v) name meta;
# Include `v.meta`, `v.drvPath` and `v.outPath` as well?
} else
f v;
guardFunction = f: v: if (isFunction v) then { _function = true; } else f v;
guardOptionType = f: v: if (isOptionType v) then { _type = true; } else f v;
# Recursively call on elements of product types with given function
recurseWith = f: val:
if (isAttrs val) then
# Module is enabled or doesn't have an enable switch
let
try = tryEval (val.enable or true);
isEnabled = if try.success then try.value else true;
in if isEnabled then
mapAttrs (k: v: f v) val
else
# Module was disabled, don't pollute with all of its values
{
enable = false;
}
else
(if (isList val) then map f val else val);
# Process config with recursive tryEval to root out `throw` errors
# catchErrors = guardTryEval (guardDerivation
catchErrors = guardTryEval (guardDerivation
(guardFunction (guardOptionType (recurseWith catchErrors))));
# Guard against values that don't make sense in JSON
catchJson =
guardDerivation (guardFunction (guardOptionType (recurseWith catchJson)));
catchJsonLeaveOption =
guardDerivation (guardFunction (recurseWith catchJson));
};
prunedConfig = lib.recursiveUpdate (config // {
assertions = null;
home-manager = null;
virtualisation = null;
boot = null;
}) {
hardware.nvidia = null;
nixpkgs.pkgs = null;
system.build.manual = null;
# These bug out
services.redis = null;
};
inherit (utilities) catchErrors;
in catchErrors prunedConfig