jsonschema: Add exportfield for module internals

This commit is contained in:
Johannes Kirschbauer
2024-11-12 12:48:39 +01:00
parent 90a2faf323
commit 499bc4743b

View File

@@ -38,6 +38,7 @@ let
# Filter out options where the visible attribute is set to false # Filter out options where the visible attribute is set to false
filterInvisibleOpts = lib.filterAttrs (_name: opt: opt.visible or true); filterInvisibleOpts = lib.filterAttrs (_name: opt: opt.visible or true);
# Constant: Used for the 'any' type
allBasicTypes = [ allBasicTypes = [
"boolean" "boolean"
"integer" "integer"
@@ -78,7 +79,26 @@ rec {
default = opt.default; default = opt.default;
}; };
parseOptions' = lib.flip parseOptions { addHeader = false; }; parseSubOptions =
{
option,
prefix ? [ ],
}:
let
subOptions = option.type.getSubOptions option.loc;
in
parseOptions subOptions {
addHeader = false;
path = option.loc ++ prefix;
};
makeModuleInfo =
{ path }:
{
"$exportedModuleInfo" = {
inherit path;
};
};
# parses a set of evaluated nixos options to a jsonschema # parses a set of evaluated nixos options to a jsonschema
parseOptions = parseOptions =
@@ -88,11 +108,12 @@ rec {
# Can be customized if needed # Can be customized if needed
# By default the header is not added to the schema # By default the header is not added to the schema
addHeader ? true, addHeader ? true,
path ? [ "<root>" ],
}: }:
let let
options' = filterInvisibleOpts (filterExcludedAttrs (clean options)); options' = filterInvisibleOpts (filterExcludedAttrs (clean options));
# parse options to jsonschema properties # parse options to jsonschema properties
properties = lib.mapAttrs (_name: option: parseOption option) options'; properties = lib.mapAttrs (_name: option: (parseOption' (path ++ [ _name ]) option)) options';
# TODO: figure out how to handle if prop.anyOf is used # TODO: figure out how to handle if prop.anyOf is used
isRequired = prop: !(prop ? default || prop.type or null == "object"); isRequired = prop: !(prop ? default || prop.type or null == "object");
requiredProps = lib.filterAttrs (_: prop: isRequired prop) properties; requiredProps = lib.filterAttrs (_: prop: isRequired prop) properties;
@@ -100,7 +121,7 @@ rec {
header' = if addHeader then header else { }; header' = if addHeader then header else { };
# freeformType is a special type # freeformType is a special type
freeformDefs = (options._module.freeformType.definitions or [ ]); freeformDefs = options._module.freeformType.definitions or [ ];
checkFreeformDefs = checkFreeformDefs =
defs: defs:
if (builtins.length defs) != 1 then if (builtins.length defs) != 1 then
@@ -113,15 +134,21 @@ rec {
# freeformType has only one definition # freeformType has only one definition
parseOption { parseOption {
# options._module.freeformType.definitions # options._module.freeformType.definitions
type = (builtins.head (checkFreeformDefs freeformDefs)); type = builtins.head (checkFreeformDefs freeformDefs);
_type = "option"; _type = "option";
loc = options._module.freeformType.loc; loc = path;
} }
else else
{ }; { };
# Metadata about the module that is made available to the schema via '$propagatedModuleInfo'
exportedModuleInfo = lib.optionalAttrs true (makeModuleInfo {
inherit path;
});
in in
# return jsonschema # return jsonschema
header' header'
// exportedModuleInfo
// required // required
// { // {
type = "object"; type = "object";
@@ -131,8 +158,9 @@ rec {
// freeformProperties; // freeformProperties;
# parses and evaluated nixos option to a jsonschema property definition # parses and evaluated nixos option to a jsonschema property definition
parseOption = parseOption = parseOption' [ ];
option: parseOption' =
currentPath: option:
let let
default = getDefaultFrom option; default = getDefaultFrom option;
example = lib.optionalAttrs (option ? example) { example = lib.optionalAttrs (option ? example) {
@@ -142,6 +170,9 @@ rec {
description = lib.optionalAttrs (option ? description) { description = lib.optionalAttrs (option ? description) {
description = option.description.text or option.description; description = option.description.text or option.description;
}; };
exposedModuleInfo = makeModuleInfo {
path = option.loc;
};
in in
# either type # either type
# TODO: if all nested options are excluded, the parent should be excluded too # TODO: if all nested options are excluded, the parent should be excluded too
@@ -164,10 +195,14 @@ rec {
]; ];
optionsList = filterExcluded optionsList'; optionsList = filterExcluded optionsList';
in in
default // example // description // { oneOf = map parseOption optionsList; } exposedModuleInfo // default // example // description // { oneOf = map parseOption optionsList; }
# handle nested options (not a submodule) # handle nested options (not a submodule)
# foo.bar = mkOption { type = str; };
else if !option ? _type then else if !option ? _type then
parseOptions' option (parseOptions option {
addHeader = false;
path = currentPath;
})
# throw if not an option # throw if not an option
else if option._type != "option" && option._type != "option-type" then else if option._type != "option" && option._type != "option-type" then
throw "parseOption: not an option" throw "parseOption: not an option"
@@ -184,6 +219,7 @@ rec {
}; };
in in
default default
// exposedModuleInfo
// example // example
// description // description
// { // {
@@ -196,19 +232,19 @@ rec {
option.type.name == "bool" option.type.name == "bool"
# return jsonschema property definition for bool # return jsonschema property definition for bool
then then
default // example // description // { type = "boolean"; } exposedModuleInfo // default // example // description // { type = "boolean"; }
# parse float # parse float
else if else if
option.type.name == "float" option.type.name == "float"
# return jsonschema property definition for float # return jsonschema property definition for float
then then
default // example // description // { type = "number"; } exposedModuleInfo // default // example // description // { type = "number"; }
# parse int # parse int
else if else if
(option.type.name == "int" || option.type.name == "positiveInt") (option.type.name == "int" || option.type.name == "positiveInt")
# return jsonschema property definition for int # return jsonschema property definition for int
then then
default // example // description // { type = "integer"; } exposedModuleInfo // default // example // description // { type = "integer"; }
# TODO: Add support for intMatching in jsonschema # TODO: Add support for intMatching in jsonschema
# parse port type aka. "unsignedInt16" # parse port type aka. "unsignedInt16"
else if else if
@@ -217,7 +253,7 @@ rec {
|| option.type.name == "pkcs11" || option.type.name == "pkcs11"
|| option.type.name == "intBetween" || option.type.name == "intBetween"
then then
default // example // description // { type = "integer"; } exposedModuleInfo // default // example // description // { type = "integer"; }
# parse string # parse string
# TODO: parse more precise string types # TODO: parse more precise string types
else if else if
@@ -227,55 +263,56 @@ rec {
|| option.type.name == "passwdEntry path" || option.type.name == "passwdEntry path"
# return jsonschema property definition for string # return jsonschema property definition for string
then then
default // example // description // { type = "string"; } exposedModuleInfo // default // example // description // { type = "string"; }
# TODO: Add support for stringMatching in jsonschema # TODO: Add support for stringMatching in jsonschema
# parse stringMatching # parse stringMatching
else if lib.strings.hasPrefix "strMatching" option.type.name then else if lib.strings.hasPrefix "strMatching" option.type.name then
default // example // description // { type = "string"; } exposedModuleInfo // default // example // description // { type = "string"; }
# TODO: Add support for separatedString in jsonschema # TODO: Add support for separatedString in jsonschema
else if lib.strings.hasPrefix "separatedString" option.type.name then else if lib.strings.hasPrefix "separatedString" option.type.name then
default // example // description // { type = "string"; } exposedModuleInfo // default // example // description // { type = "string"; }
# parse string # parse string
else if else if
option.type.name == "path" option.type.name == "path"
# return jsonschema property definition for path # return jsonschema property definition for path
then then
default // example // description // { type = "string"; } exposedModuleInfo // default // example // description // { type = "string"; }
# parse anything # parse anything
else if else if
option.type.name == "anything" option.type.name == "anything"
# return jsonschema property definition for anything # return jsonschema property definition for anything
then then
default // example // description // { type = allBasicTypes; } exposedModuleInfo // default // example // description // { type = allBasicTypes; }
# parse unspecified # parse unspecified
else if else if
option.type.name == "unspecified" option.type.name == "unspecified"
# return jsonschema property definition for unspecified # return jsonschema property definition for unspecified
then then
default // example // description // { type = allBasicTypes; } exposedModuleInfo // default // example // description // { type = allBasicTypes; }
# parse raw # parse raw
else if else if
option.type.name == "raw" option.type.name == "raw"
# return jsonschema property definition for raw # return jsonschema property definition for raw
then then
default // example // description // { type = allBasicTypes; } exposedModuleInfo // default // example // description // { type = allBasicTypes; }
# parse enum # parse enum
else if else if
option.type.name == "enum" option.type.name == "enum"
# return jsonschema property definition for enum # return jsonschema property definition for enum
then then
default // example // description // { enum = option.type.functor.payload; } exposedModuleInfo // default // example // description // { enum = option.type.functor.payload; }
# parse listOf submodule # parse listOf submodule
else if else if
option.type.name == "listOf" && option.type.functor.wrapped.name == "submodule" option.type.name == "listOf" && option.type.nestedTypes.elemType.name == "submodule"
# return jsonschema property definition for listOf submodule # return jsonschema property definition for listOf submodule
then then
default default
// exposedModuleInfo
// example // example
// description // description
// { // {
type = "array"; type = "array";
items = parseOptions' (option.type.functor.wrapped.getSubOptions option.loc); items = parseSubOptions { inherit option; };
} }
# parse list # parse list
else if else if
@@ -284,12 +321,13 @@ rec {
then then
let let
nestedOption = { nestedOption = {
type = option.type.functor.wrapped; type = option.type.nestedTypes.elemType;
_type = "option"; _type = "option";
loc = option.loc; loc = option.loc;
}; };
in in
default default
// exposedModuleInfo
// example // example
// description // description
// { // {
@@ -298,21 +336,25 @@ rec {
// (lib.optionalAttrs (!isExcludedOption nestedOption) { items = parseOption nestedOption; }) // (lib.optionalAttrs (!isExcludedOption nestedOption) { items = parseOption nestedOption; })
# parse list of unspecified # parse list of unspecified
else if else if
(option.type.name == "listOf") && (option.type.functor.wrapped.name == "unspecified") (option.type.name == "listOf") && (option.type.nestedTypes.elemType.name == "unspecified")
# return jsonschema property definition for list # return jsonschema property definition for list
then then
default // example // description // { type = "array"; } exposedModuleInfo // default // example // description // { type = "array"; }
# parse attrsOf submodule # parse attrsOf submodule
else if else if
option.type.name == "attrsOf" && option.type.nestedTypes.elemType.name == "submodule" option.type.name == "attrsOf" && option.type.nestedTypes.elemType.name == "submodule"
# return jsonschema property definition for attrsOf submodule # return jsonschema property definition for attrsOf submodule
then then
default default
// exposedModuleInfo
// example // example
// description // description
// { // {
type = "object"; type = "object";
additionalProperties = parseOptions' (option.type.nestedTypes.elemType.getSubOptions option.loc); additionalProperties = parseSubOptions {
inherit option;
prefix = [ "<name>" ];
};
} }
# parse attrs # parse attrs
else if else if
@@ -320,6 +362,7 @@ rec {
# return jsonschema property definition for attrs # return jsonschema property definition for attrs
then then
default default
// exposedModuleInfo
// example // example
// description // description
// { // {
@@ -340,6 +383,7 @@ rec {
}; };
in in
default default
// exposedModuleInfo
// example // example
// description // description
// { // {
@@ -360,7 +404,7 @@ rec {
# return jsonschema property definition for submodule # return jsonschema property definition for submodule
# then (lib.attrNames (option.type.getSubOptions option.loc).opt) # then (lib.attrNames (option.type.getSubOptions option.loc).opt)
then then
example // description // parseOptions' (option.type.getSubOptions option.loc) exposedModuleInfo // example // description // parseSubOptions { inherit option; }
# throw error if option type is not supported # throw error if option type is not supported
else else
notSupported option; notSupported option;