···
{ configuration ? import ../lib/from-env.nix "NIXOS_CONFIG" <nixos-config>
+
# provide an option name, as a string literal.
+
# provide a list of option names, as string literals.
+
# This file is made to be used as follow:
+
# $ nix-instantiate ./option-usage.nix --argstr testOption service.xserver.enable -A txtContent --eval
+
# $ nix-build ./option-usage.nix --argstr testOption service.xserver.enable -A txt -o service.xserver.enable._txt
+
# otther target exists such as, `dotContent`, `dot`, and `pdf`. If you are
+
# looking for the option usage of multiple options, you can provide a list
+
# $ nix-build ./option-usage.nix --arg testOptions \
+
# '["boot.loader.gummiboot.enable" "boot.loader.gummiboot.timeout"]' \
+
# -A txt -o gummiboot.list
+
# Note, this script is slow as it has to evaluate all options of the system
+
# once per queried option.
+
# This nix expression works by doing a first evaluation, which evaluates the
+
# result of every option.
+
# Then, for each queried option, we evaluate the NixOS modules a second
+
# time, except that we replace the `config` argument of all the modules with
+
# the result of the original evaluation, except for the tested option which
+
# value is replaced by a `throw` statement which is caught by the `tryEval`
+
# evaluation of each option value.
+
# We then compare the result of the evluation of the original module, with
+
# the result of the second evaluation, and consider that the new failures are
+
# caused by our mutation of the `config` argument.
+
# Doing so returns all option results which are directly using the
+
# tested option result.
}: import ../lib/eval-config.nix {
modules = [ configuration ];
+
excludedTestOptions = [
+
# We cannot evluate _module.args, as it is used during the computation
+
# For some reasons which we yet have to investigate, some options cannot
+
# be replaced by a throw without cuasing a non-catchable failure.
+
"networking.interfaces"
+
"services.openssh.startWhenNeeded"
+
# for some reasons which we yet have to investigate, some options are
+
# time-consuming to compute, thus we filter them out at the moment.
+
"boot.systemd.services"
+
"environment.gnome3.packageSet"
+
filter (opt: !(elem (showOption opt.loc) excludedOptions)) list;
+
reportNewFailures = old: new:
+
!(fst.success -> snd.success)
+
/* assert fst.name == snd.name; */ snd.name
+
# Use tryEval (strict ...) to know if there is any failure while
+
# evaluating the option value.
+
# Note, the `strict` function is not strict enough, but using toXML
+
# builtins multiply by 4 the memory usage and the time used to compute
+
tryCollectOptions = moduleResult:
+
flip map (excludeOptions (collect isOption moduleResult)) (opt:
+
{ name = showOption opt.loc; } // builtins.tryEval (strict opt.value));
+
zipLists (tryCollectOptions old) (tryCollectOptions new)
# Create a list of modules where each module contains only one failling
setIntrospection = opt: rec {
+
name = showOption opt.loc;
config = setAttrByPath path
(throw "Usage introspection of '${name}' by forced failure.");
···
map setIntrospection (collect isOption eval.options);
overrideConfig = thrower:
+
recursiveUpdateUntil (path: old: new:
) eval.config thrower.config;
+
usedBy = assert __trace "Investigate ${thrower.name}" true;
+
reportNewFailures eval.options (evalFun {
+
config = overrideConfig thrower;
+
if !(isNull testOption) then [ testOption ]
+
checkAll = checkList == [];
+
flip filter graph ({option, usedBy}:
+
(checkAll || elem option checkList)
+
&& !(elem option excludedTestOptions)
digraph "Option Usages" {
${concatMapStrings ({option, usedBy}:
+
concatMapStrings (user: ''
+
"${option}" -> "${user}"''
+
concatMapStrings ({option, usedBy}:
+
concatMapStrings (user: ''
+
dotContent = graphToDot graph;
+
dot = pkgs.writeTextFile {
name = "option_usages.dot";
+
pdf = pkgs.texFunctions.dot2pdf {
+
txtContent = graphToText graph;
+
txt = pkgs.writeTextFile {
+
name = "option_usages.txt";