kubenix/modules/submodules.nix

417 lines
12 KiB
Nix
Raw Normal View History

2022-04-02 12:40:35 -07:00
{
config,
options,
kubenix,
pkgs,
lib,
...
}:
with lib; let
cfg = config.submodules;
parentConfig = config;
matchesVersion = requiredVersion: version:
2022-04-02 12:40:35 -07:00
if requiredVersion != null
then
if hasPrefix "~" requiredVersion
then (builtins.match (removePrefix "~" requiredVersion) version) != null
else requiredVersion == version
else true;
2022-04-02 12:40:35 -07:00
getDefaults = {
name,
version,
tags,
features,
}:
2021-05-13 17:27:08 -04:00
catAttrs "default" (filter
2022-04-02 12:40:35 -07:00
(
submoduleDefault:
(submoduleDefault.name == null || submoduleDefault.name == name)
&& (matchesVersion submoduleDefault.version version)
&& (
(length submoduleDefault.tags == 0)
|| (length (intersectLists submoduleDefault.tags tags)) > 0
)
&& (
(length submoduleDefault.features == 0)
|| (length (intersectLists submoduleDefault.features features)) > 0
)
2019-02-26 21:23:14 +01:00
)
2021-05-13 17:27:08 -04:00
config.submodules.defaults);
2019-02-26 21:23:14 +01:00
2022-04-02 12:40:35 -07:00
specialArgs =
cfg.specialArgs
// {
parentConfig = config;
};
2022-04-02 12:40:35 -07:00
findSubmodule = {
name,
version ? null,
latest ? true,
}: let
matchingSubmodules =
filter
(
el:
el.definition.name
== name
&& (matchesVersion version el.definition.version)
)
cfg.imports;
2021-05-13 17:27:08 -04:00
2022-04-02 12:40:35 -07:00
versionSortedSubmodules =
sort
(
s1: s2:
2021-05-13 17:27:08 -04:00
if builtins.compareVersions s1.definition.version s2.definition.version > 0
2022-04-02 12:40:35 -07:00
then true
else false
)
matchingSubmodules;
matchingModule =
if length versionSortedSubmodules == 0
then
throw "No module found ${name}/${
if version == null
then "latest"
else version
}"
else head versionSortedSubmodules;
in
2021-05-13 17:27:08 -04:00
matchingModule;
2022-04-02 12:40:35 -07:00
passthruConfig =
mapAttrsToList
2021-05-13 17:27:08 -04:00
(name: opt: {
${name} = mkMerge (mapAttrsToList
2022-04-02 12:40:35 -07:00
(
_: inst:
if inst.passthru.enable
then inst.config.submodule.passthru.${name} or {}
else {}
2021-05-13 17:27:08 -04:00
)
config.submodules.instances);
_module.args = mkMerge (mapAttrsToList
2022-04-02 12:40:35 -07:00
(
_: inst:
if inst.passthru.enable
then inst.config.submodule.passthru._module.args or {}
else {}
2021-05-13 17:27:08 -04:00
)
config.submodules.instances);
})
2022-04-02 12:40:35 -07:00
(removeAttrs options ["_definedNames" "_module" "_m" "submodules"]);
2021-05-31 17:24:59 -05:00
2022-04-02 12:40:35 -07:00
submoduleWithSpecialArgs = opts: specialArgs: let
opts' = toList opts;
inherit (lib.modules) evalModules;
in
2021-05-31 17:24:59 -05:00
mkOptionType rec {
name = "submodule";
check = x: isAttrs x || isFunction x;
2022-04-02 12:40:35 -07:00
merge = loc: defs: let
coerce = def:
if isFunction def
then def
else {config = def;};
modules =
opts'
++ map (def: {
_file = def.file;
imports = [(coerce def.value)];
})
defs;
in
2021-05-31 17:24:59 -05:00
(evalModules {
inherit modules specialArgs;
prefix = loc;
2022-04-02 12:40:35 -07:00
})
.config;
getSubOptions = prefix:
(evalModules
{
modules = opts';
inherit prefix specialArgs;
# This is a work-around due to the fact that some sub-modules,
# such as the one included in an attribute set, expects a "args"
# attribute to be given to the sub-module. As the option
# evaluation does not have any specific attribute name, we
# provide a default one for the documentation.
#
# This is mandatory as some option declaration might use the
# "name" attribute given as argument of the submodule and use it
# as the default of option declarations.
#
# Using lookalike unicode single angle quotation marks because
# of the docbook transformation the options receive. In all uses
# > and < wouldn't be encoded correctly so the encoded values
# would be used, and use of `<` and `>` would break the XML document.
# It shouldn't cause an issue since this is cosmetic for the manual.
args.name = "name";
})
.options;
2021-05-31 17:24:59 -05:00
getSubModules = opts';
substSubModules = m: submoduleWithSpecialArgs m specialArgs;
2022-04-02 12:40:35 -07:00
functor =
(defaultFunctor name)
// {
# Merging of submodules is done as part of mergeOptionDecls, as we have to annotate
# each submodule with its location.
payload = [];
binOp = lhs: rhs: [];
};
2021-05-31 17:24:59 -05:00
};
2022-04-02 12:40:35 -07:00
in {
imports = [./base.nix];
options = {
submodules.specialArgs = mkOption {
description = "Special args to pass to submodules. These arguments can be used for imports";
type = types.attrs;
2022-04-02 12:40:35 -07:00
default = {};
};
submodules.defaults = mkOption {
2019-02-26 21:23:14 +01:00
description = "List of defaults to apply to submodule instances";
2022-04-02 12:40:35 -07:00
type = types.listOf (types.submodule ({config, ...}: {
2019-02-26 21:23:14 +01:00
options = {
name = mkOption {
description = "Name of the submodule to apply defaults for";
type = types.nullOr types.str;
default = null;
};
version = mkOption {
description = ''
Version of submodule to apply defaults for. If version starts with
"~" it is threated as regex pattern for example "~1.0.*
'';
2019-02-26 21:23:14 +01:00
type = types.nullOr types.str;
default = null;
};
2019-02-26 21:23:14 +01:00
tags = mkOption {
description = "List of tags to apply defaults for";
2019-02-26 21:23:14 +01:00
type = types.listOf types.str;
2022-04-02 12:40:35 -07:00
default = [];
2019-02-26 21:23:14 +01:00
};
features = mkOption {
description = "List of features that submodule has to have to apply defaults";
type = types.listOf types.str;
2022-04-02 12:40:35 -07:00
default = [];
};
2019-02-26 21:23:14 +01:00
default = mkOption {
description = "Default to apply to submodule instance";
type = types.unspecified;
2022-04-02 12:40:35 -07:00
default = {};
2019-02-26 21:23:14 +01:00
};
};
}));
2022-04-02 12:40:35 -07:00
default = [];
};
submodules.propagate.enable = mkOption {
description = "Whether to propagate defaults and imports from parent to child";
type = types.bool;
default = true;
};
submodules.imports = mkOption {
description = "List of submodule imports";
type = types.listOf (
types.coercedTo
2022-04-02 12:40:35 -07:00
types.path
(module: {inherit module;})
(
types.submodule ({
name,
config,
...
}: let
evaledSubmodule' = evalModules {
inherit specialArgs;
modules = config.modules ++ [./base.nix];
check = false;
};
evaledSubmodule =
if (!(elem "submodule" evaledSubmodule'.config._m.features))
then throw "no submodule defined"
else evaledSubmodule';
in {
options = {
module = mkOption {
description = "Module defining submodule";
type = types.unspecified;
};
2022-04-02 12:40:35 -07:00
modules = mkOption {
description = "List of modules defining submodule";
type = types.listOf types.unspecified;
default = [config.module];
};
2022-04-02 12:40:35 -07:00
features = mkOption {
description = "List of features exposed by submodule";
type = types.listOf types.str;
};
2022-04-02 12:40:35 -07:00
definition = mkOption {
description = "Submodule definition";
type = types.attrs;
};
2022-04-02 12:40:35 -07:00
exportAs = mkOption {
description = "Name under which to register exports";
type = types.nullOr types.str;
default = null;
};
};
config = {
definition = {
inherit (evaledSubmodule.config.submodule) name description version tags exports;
};
features = evaledSubmodule.config._m.features;
};
})
)
);
2022-04-02 12:40:35 -07:00
default = [];
};
submodules.instances = mkOption {
description = "Attribute set of submodule instances";
2022-04-02 12:40:35 -07:00
default = {};
type = types.attrsOf (types.submodule ({
name,
config,
options,
...
}: let
# submodule associated with
submodule = findSubmodule {
name = config.submodule;
version = config.version;
};
2022-04-02 12:40:35 -07:00
# definition of a submodule
submoduleDefinition = submodule.definition;
2019-02-26 21:23:14 +01:00
2022-04-02 12:40:35 -07:00
# submodule defaults
defaults = getDefaults {
name = submoduleDefinition.name;
version = submoduleDefinition.version;
tags = submoduleDefinition.tags;
features = submodule.features;
};
in {
options = {
name = mkOption {
description = "Submodule instance name";
type = types.str;
default = name;
};
2022-04-02 12:40:35 -07:00
submodule = mkOption {
description = "Name of the submodule to use";
type = types.str;
default = name;
};
2022-04-02 12:40:35 -07:00
version = mkOption {
description = ''
Version of submodule to use, if version starts with "~" it is
threated as regex pattern for example "~1.0.*"
'';
type = types.nullOr types.str;
default = null;
};
2022-04-02 12:40:35 -07:00
passthru.enable = mkOption {
description = "Whether to passthru submodule resources";
type = types.bool;
default = true;
};
2022-04-02 12:40:35 -07:00
config = mkOption {
description = "Submodule instance ${config.name} for ${submoduleDefinition.name}:${submoduleDefinition.version} config";
type =
submoduleWithSpecialArgs
({...}: {
imports = submodule.modules ++ defaults ++ [./base.nix];
_module.args.pkgs = pkgs;
_module.args.name = config.name;
_module.args.submodule = config;
submodule.args = mkAliasDefinitions options.args;
})
specialArgs;
default = {};
};
2022-04-02 12:40:35 -07:00
args = mkOption {
description = "Submodule arguments (alias of config.submodule.args)";
};
2022-04-02 12:40:35 -07:00
};
}));
};
2022-04-02 12:40:35 -07:00
default = {};
};
config = mkMerge ([
2022-04-02 12:40:35 -07:00
{
# register exported functions as args
_module.args = mkMerge (map
(submodule: {
${submodule.exportAs} = submodule.definition.exports;
})
(filter (submodule: submodule.exportAs != null) cfg.imports));
2022-04-02 12:40:35 -07:00
_m.features = ["submodules"];
2022-04-02 12:40:35 -07:00
submodules.specialArgs.kubenix = kubenix;
2022-04-02 12:40:35 -07:00
# passthru kubenix.project to submodules
submodules.defaults = mkMerge [
[
{
default = {
kubenix.project = parentConfig.kubenix.project;
};
}
]
2022-04-02 12:40:35 -07:00
(map
(propagate: {
features = propagate.features;
default = propagate.module;
})
config._m.propagate)
];
}
(mkIf cfg.propagate.enable {
# if propagate is enabled and submodule has submodules included propagage defaults and imports
submodules.defaults = [
{
features = ["submodules"];
default = {
submodules = {
defaults = cfg.defaults;
imports = cfg.imports;
};
};
}
];
})
]
++ passthruConfig);
}