From 7a4a4cea9562794a2cf2d114fdfed3f789a4c64d Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 22 Oct 2024 12:59:44 +0200 Subject: [PATCH] lib/jsonSchema: handle defaults for defaultText --- lib/jsonschema/default.nix | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/jsonschema/default.nix b/lib/jsonschema/default.nix index 8b9d03308..8d478e018 100644 --- a/lib/jsonschema/default.nix +++ b/lib/jsonschema/default.nix @@ -26,8 +26,7 @@ let # Exclude the option if its type is in the excludedTypes list # or if the option has a defaultText attribute - isExcludedOption = - option: ((lib.elem (option.type.name or null) excludedTypes) || (option ? defaultText)); + isExcludedOption = option: (lib.elem (option.type.name or null) excludedTypes); filterExcluded = lib.filter (opt: !isExcludedOption opt); @@ -58,6 +57,24 @@ rec { in parseOptions evaled.options { }; + # get default value from option + + # Returns '{ default = Value; }' + # - '{}' if no default is present. + # - Value is "" (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 = ""; + } + else + lib.optionalAttrs (opt ? default) { + default = opt.default; + }; + parseOptions' = lib.flip parseOptions { addHeader = false; }; # parses a set of evaluated nixos options to a jsonschema @@ -92,7 +109,7 @@ rec { parseOption = option: let - default = lib.optionalAttrs (option ? default && includeDefaults) { inherit (option) default; }; + default = getDefaultFrom option; example = lib.optionalAttrs (option ? example) { examples = if (builtins.typeOf option.example) == "list" then option.example else [ option.example ];