diff --git a/lib/options.nix b/lib/options.nix index c348491f0457..0cda30fc3544 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -589,11 +589,16 @@ rec { renderOptionValue opt.example ); } - // optionalAttrs (opt ? defaultText || opt ? default) { - default = builtins.addErrorContext "while evaluating the ${ - if opt ? defaultText then "defaultText" else "default value" - } of option `${name}`" (renderOptionValue (opt.defaultText or opt.default)); - } + // + optionalAttrs (opt ? defaultText || opt ? default || ((opt.type or { }).emptyValue or { }) ? value) + { + default = + builtins.addErrorContext + "while evaluating the ${ + if opt ? defaultText then "defaultText" else "default value" + } of option `${name}`" + (renderOptionValue (opt.defaultText or opt.default or opt.type.emptyValue.value)); + } // optionalAttrs (opt ? relatedPackages && opt.relatedPackages != null) { inherit (opt) relatedPackages; }; diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index d4bdc344deb9..f1a11cd7e6fb 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -3393,6 +3393,44 @@ runTests { ]; }; + testEmptyValueOption = { + expr = + let + module = + { lib, ... }: + { + options = { + "empty-value" = lib.mkOption { + type = lib.mkOptionType { + name = "propagate-empty-value-to-default"; + emptyValue.value = 2; + }; + }; + }; + }; + eval = evalModules { + modules = [ module ]; + }; + in + filter (o: o.name == "empty-value") (optionAttrSetToDocList eval.options); + expected = [ + { + declarations = [ ]; + default = { + _type = "literalExpression"; + text = "2"; + }; + description = null; + internal = false; + loc = [ "empty-value" ]; + name = "empty-value"; + readOnly = false; + type = "propagate-empty-value-to-default"; + visible = true; + } + ]; + }; + testDocOptionVisiblity = { expr = let