···
{ configuration ? import ../lib/from-env.nix "NIXOS_CONFIG" <nixos-config>
3
-
# []: display all options
4
-
# [<option names>]: display the selected options
6
-
"hardware.pcmcia.enable"
7
-
"environment.systemPackages"
9
-
"services.udev.packages"
12
-
"system.activationScripts"
3
+
# provide an option name, as a string literal.
6
+
# provide a list of option names, as string literals.
16
-
# This file is used to generate a dot graph which contains all options and
17
-
# there dependencies to track problems and their sources.
10
+
# This file is made to be used as follow:
12
+
# $ nix-instantiate ./option-usage.nix --argstr testOption service.xserver.enable -A txtContent --eval
16
+
# $ nix-build ./option-usage.nix --argstr testOption service.xserver.enable -A txt -o service.xserver.enable._txt
18
+
# otther target exists such as, `dotContent`, `dot`, and `pdf`. If you are
19
+
# looking for the option usage of multiple options, you can provide a list
22
+
# $ nix-build ./option-usage.nix --arg testOptions \
23
+
# '["boot.loader.gummiboot.enable" "boot.loader.gummiboot.timeout"]' \
24
+
# -A txt -o gummiboot.list
26
+
# Note, this script is slow as it has to evaluate all options of the system
27
+
# once per queried option.
29
+
# This nix expression works by doing a first evaluation, which evaluates the
30
+
# result of every option.
32
+
# Then, for each queried option, we evaluate the NixOS modules a second
33
+
# time, except that we replace the `config` argument of all the modules with
34
+
# the result of the original evaluation, except for the tested option which
35
+
# value is replaced by a `throw` statement which is caught by the `tryEval`
36
+
# evaluation of each option value.
38
+
# We then compare the result of the evluation of the original module, with
39
+
# the result of the second evaluation, and consider that the new failures are
40
+
# caused by our mutation of the `config` argument.
42
+
# Doing so returns all option results which are directly using the
43
+
# tested option result.
45
+
with import ../../lib;
}: import ../lib/eval-config.nix {
modules = [ configuration ];
53
+
inherit specialArgs;
31
-
reportNewFailures = old: new: with pkgs.lib;
59
+
excludedTestOptions = [
60
+
# We cannot evluate _module.args, as it is used during the computation
61
+
# of the modules list.
64
+
# For some reasons which we yet have to investigate, some options cannot
65
+
# be replaced by a throw without cuasing a non-catchable failure.
67
+
"networking.bridges"
68
+
"networking.interfaces"
69
+
"networking.macvlans"
72
+
"services.openssh.startWhenNeeded"
75
+
# for some reasons which we yet have to investigate, some options are
76
+
# time-consuming to compute, thus we filter them out at the moment.
78
+
"boot.systemd.services"
80
+
"environment.gnome3.packageSet"
83
+
excludeOptions = list:
84
+
filter (opt: !(elem (showOption opt.loc) excludedOptions)) list;
87
+
reportNewFailures = old: new:
35
-
!(fst.config.success -> snd.config.success)
91
+
!(fst.success -> snd.success)
40
-
assert fst.name == snd.name; snd.name
96
+
/* assert fst.name == snd.name; */ snd.name
99
+
# Use tryEval (strict ...) to know if there is any failure while
100
+
# evaluating the option value.
102
+
# Note, the `strict` function is not strict enough, but using toXML
103
+
# builtins multiply by 4 the memory usage and the time used to compute
105
+
tryCollectOptions = moduleResult:
106
+
flip map (excludeOptions (collect isOption moduleResult)) (opt:
107
+
{ name = showOption opt.loc; } // builtins.tryEval (strict opt.value));
45
-
zipLists (collect isOption old) (collect isOption new)
111
+
zipLists (tryCollectOptions old) (tryCollectOptions new)
# Create a list of modules where each module contains only one failling
52
-
introspectionModules = with pkgs.lib;
118
+
introspectionModules =
setIntrospection = opt: rec {
56
-
path = splitString "." opt.name;
121
+
name = showOption opt.loc;
config = setAttrByPath path
(throw "Usage introspection of '${name}' by forced failure.");
···
map setIntrospection (collect isOption eval.options);
overrideConfig = thrower:
64
-
pkgs.lib.recursiveUpdateUntil (path: old: new:
130
+
recursiveUpdateUntil (path: old: new:
) eval.config thrower.config;
69
-
graph = with pkgs.lib;
72
-
usedBy = reportNewFailures eval.options (evalFun {
74
-
config = overrideConfig thrower;
138
+
usedBy = assert __trace "Investigate ${thrower.name}" true;
139
+
reportNewFailures eval.options (evalFun {
141
+
config = overrideConfig thrower;
79
-
graphToDot = graph: with pkgs.lib; ''
146
+
displayOptionsGraph =
149
+
if !(isNull testOption) then [ testOption ]
151
+
checkAll = checkList == [];
153
+
flip filter graph ({option, usedBy}:
154
+
(checkAll || elem option checkList)
155
+
&& !(elem option excludedTestOptions)
158
+
graphToDot = graph: ''
digraph "Option Usages" {
${concatMapStrings ({option, usedBy}:
82
-
assert __trace option true;
83
-
if displayOptions == [] || elem option displayOptions then
84
-
concatMapStrings (user: ''
85
-
"${option}" -> "${user}"''
161
+
concatMapStrings (user: ''
162
+
"${option}" -> "${user}"''
164
+
) displayOptionsGraph}
168
+
graphToText = graph:
169
+
concatMapStrings ({option, usedBy}:
170
+
concatMapStrings (user: ''
173
+
) displayOptionsGraph;
94
-
pkgs.texFunctions.dot2pdf {
95
-
dotGraph = pkgs.writeTextFile {
178
+
dotContent = graphToDot graph;
179
+
dot = pkgs.writeTextFile {
name = "option_usages.dot";
97
-
text = graphToDot graph;
184
+
pdf = pkgs.texFunctions.dot2pdf {
188
+
txtContent = graphToText graph;
189
+
txt = pkgs.writeTextFile {
190
+
name = "option_usages.txt";