lib/jsonSchema: handle defaults for defaultText

This commit is contained in:
Johannes Kirschbauer
2024-10-22 12:59:44 +02:00
parent 78bf5b9147
commit 7a4a4cea95

View File

@@ -26,8 +26,7 @@ let
# Exclude the option if its type is in the excludedTypes list # Exclude the option if its type is in the excludedTypes list
# or if the option has a defaultText attribute # or if the option has a defaultText attribute
isExcludedOption = isExcludedOption = option: (lib.elem (option.type.name or null) excludedTypes);
option: ((lib.elem (option.type.name or null) excludedTypes) || (option ? defaultText));
filterExcluded = lib.filter (opt: !isExcludedOption opt); filterExcluded = lib.filter (opt: !isExcludedOption opt);
@@ -58,6 +57,24 @@ rec {
in in
parseOptions evaled.options { }; parseOptions evaled.options { };
# get default value from option
# Returns '{ default = Value; }'
# - '{}' if no default is present.
# - Value is "<thunk>" (string literal) if the option has a defaultText attribute. This means we cannot evaluate default safely
getDefaultFrom =
opt:
if !includeDefaults then
{ }
else if opt ? defaultText then
{
default = "<thunk>";
}
else
lib.optionalAttrs (opt ? default) {
default = opt.default;
};
parseOptions' = lib.flip parseOptions { addHeader = false; }; parseOptions' = lib.flip parseOptions { addHeader = false; };
# parses a set of evaluated nixos options to a jsonschema # parses a set of evaluated nixos options to a jsonschema
@@ -92,7 +109,7 @@ rec {
parseOption = parseOption =
option: option:
let let
default = lib.optionalAttrs (option ? default && includeDefaults) { inherit (option) default; }; default = getDefaultFrom option;
example = lib.optionalAttrs (option ? example) { example = lib.optionalAttrs (option ? example) {
examples = examples =
if (builtins.typeOf option.example) == "list" then option.example else [ option.example ]; if (builtins.typeOf option.example) == "list" then option.example else [ option.example ];