summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2020-09-16 20:05:07 +0200
committerSilvan Mosberger <contact@infinisil.com>2020-09-21 18:24:50 +0200
commit7c20e68f6be7b7421d8717cc8d0ad1dadef76c67 (patch)
tree557afb0ec110819bfa7150ad916ed9bee80c1406 /lib
parent2ff7c3e2e1982fd3db4f8f7725143f0645682a54 (diff)
downloadnixpkgs-7c20e68f6be7b7421d8717cc8d0ad1dadef76c67.tar
nixpkgs-7c20e68f6be7b7421d8717cc8d0ad1dadef76c67.tar.gz
nixpkgs-7c20e68f6be7b7421d8717cc8d0ad1dadef76c67.tar.bz2
nixpkgs-7c20e68f6be7b7421d8717cc8d0ad1dadef76c67.tar.lz
nixpkgs-7c20e68f6be7b7421d8717cc8d0ad1dadef76c67.tar.xz
nixpkgs-7c20e68f6be7b7421d8717cc8d0ad1dadef76c67.tar.zst
nixpkgs-7c20e68f6be7b7421d8717cc8d0ad1dadef76c67.zip
lib/options: Introduce showDefs
For pretty-printing definitions, including file and values
Diffstat (limited to 'lib')
-rw-r--r--lib/options.nix18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/options.nix b/lib/options.nix
index 0494a597ab8..73856573a5d 100644
--- a/lib/options.nix
+++ b/lib/options.nix
@@ -213,6 +213,24 @@ rec {
          else escaped;
     in (concatStringsSep ".") (map escapeOptionPart parts);
   showFiles = files: concatStringsSep " and " (map (f: "`${f}'") files);
+
+  showDefs = defs: concatMapStrings (def:
+    let
+      # Pretty print the value for display, if successful
+      prettyEval = builtins.tryEval (lib.generators.toPretty {} def.value);
+      # Split it into its lines
+      lines = filter (v: ! isList v) (builtins.split "\n" prettyEval.value);
+      # Only display the first 5 lines, and indent them for better visibility
+      value = concatStringsSep "\n    " (take 5 lines ++ optional (length lines > 5) "...");
+      result =
+        # Don't print any value if evaluating the value strictly fails
+        if ! prettyEval.success then ""
+        # Put it on a new line if it consists of multiple
+        else if length lines > 1 then ":\n    " + value
+        else ": " + value;
+    in "\n- In `${def.file}'${result}"
+  ) defs;
+
   unknownModule = "<unknown-file>";
 
 }