-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasserts.nix
60 lines (51 loc) · 1.86 KB
/
asserts.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
{ config, lib, modules, extendModules, ... }@ args:
with lib;
let
mkRedundantOptionWarning = module: paths: path:
let
filterByPaths = paths: set:
foldl' (res: p:
recursiveUpdate res (setAttrByPath p (getAttrFromPath p set))
) {} paths;
systemWithoutOption = extendModules {
modules = singleton (module // {
config = filterByPaths (remove path paths) config;
});
};
defaultValue = attrByPath path id systemWithoutOption.config;
actualValue = getAttrFromPath path config;
isRedundant =
(builtins.tryEval defaultValue).success && actualValue == defaultValue;
in
builtins.traceVerbose
"Checking `${showAttrPath path}' in `${module._file}'…"
optional isRedundant
"The option `${
showAttrPath path
}' is set in `${
module._file
}' to the redundant value `${
generators.toPretty { multiline = false; } actualValue
}'.";
mkRedundantOptionsWarnings = module:
let
getOptionsPaths = val:
if (builtins.tryEval (isAttrs val)).value -> val ? outPath then [[]]
else if !(val ? _type) then concatLists
(mapAttrsToList (k: v: map (p: [ k ] ++ p) (getOptionsPaths v)) val)
else concatMap getOptionsPaths val.contents or
(optional val.condition or true val.content);
optionsPaths = remove [ "system" "stateVersion" ] #176295
(getOptionsPaths module.config);
in
concatMap (mkRedundantOptionWarning module optionsPaths) optionsPaths;
userModules =
filter (m: elem m.key (map (m: toString m._file or null) modules))
(lib.modules.collectModules "" modules (args // {
pkgs = throw "Unhandled access to `pkgs' input in `${__curPos.file}'";
}));
in
{
assertions = map (message: { assertion = false; inherit message; })
(config.warnings ++ concatMap mkRedundantOptionsWarnings userModules);
}