summaryrefslogtreecommitdiffstats
path: root/lib/generators.nix
diff options
context:
space:
mode:
authorNaïm Favier <n@monade.li>2022-11-03 15:56:27 +0100
committerpennae <82953136+pennae@users.noreply.github.com>2022-12-08 17:52:52 +0100
commit6a117e2759b84b9508f1d69cb5be54ca331bff98 (patch)
tree5679f8c723a9c84da0fc40ba03bdfef6bb60e27c /lib/generators.nix
parent0b661ce32af9baa07da56b3f48fcd3ef5611b66c (diff)
nixos/doc: render option values using `lib.generators.toPretty`
Render un`_type`d defaults and examples as `literalExpression`s using `lib.generators.toPretty` so that consumers don't have to reinvent Nix pretty-printing. `renderOptionValue` is kept internal for now intentionally. Make `toPretty` print floats as valid Nix values (without a tilde). Get rid of the now-obsolete `substSpecial` function. Move towards disallowing evaluation of packages in the manual by raising a warning on `pkgs.foo.{outPath,drvPath}`; later, this should throw an error. Instead, module authors should use `literalExpression` and `mkPackageOption`.
Diffstat (limited to 'lib/generators.nix')
-rw-r--r--lib/generators.nix14
1 files changed, 10 insertions, 4 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index 06823d215e36..c0fe69389e00 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -278,8 +278,11 @@ rec {
mapAny 0;
/* Pretty print a value, akin to `builtins.trace`.
- * Should probably be a builtin as well.
- */
+ * Should probably be a builtin as well.
+ * The pretty-printed string should be suitable for rendering default values
+ * in the NixOS manual. In particular, it should be as close to a valid Nix expression
+ * as possible.
+ */
toPretty = {
/* If this option is true, attrsets like { __pretty = fn; val = …; }
will use fn to convert val to a pretty printed representation.
@@ -294,7 +297,10 @@ rec {
introSpace = if multiline then "\n${indent} " else " ";
outroSpace = if multiline then "\n${indent}" else " ";
in if isInt v then toString v
- else if isFloat v then "~${toString v}"
+ # toString loses precision on floats, so we use toJSON instead. This isn't perfect
+ # as the resulting string may not parse back as a float (e.g. 42, 1e-06), but for
+ # pretty-printing purposes this is acceptable.
+ else if isFloat v then builtins.toJSON v
else if isString v then
let
lines = filter (v: ! isList v) (builtins.split "\n" v);
@@ -328,7 +334,7 @@ rec {
else "<function, args: {${showFnas}}>"
else if isAttrs v then
# apply pretty values if allowed
- if attrNames v == [ "__pretty" "val" ] && allowPrettyValues
+ if allowPrettyValues && v ? __pretty && v ? val
then v.__pretty v.val
else if v == {} then "{ }"
else if v ? type && v.type == "derivation" then