factor out function wrapper to module function #9

Closed
kiara wants to merge 4 commits from data-model-factor-out-wrapper into deployment-data-model-with-tests
3 changed files with 71 additions and 41 deletions

View file

@ -99,20 +99,18 @@ in
{ {
options.enable = lib.mkEnableOption "Hello in the shell"; options.enable = lib.mkEnableOption "Hello in the shell";
}; };
implementation = cfg: { implementation =
input = cfg; cfg:
output = lib.optionalAttrs cfg.enable { lib.optionalAttrs cfg.enable {
resources.hello.login-shell.packages.hello = pkgs.hello; resources.hello.login-shell.packages.hello = pkgs.hello;
}; };
}; };
};
environments.single-nixos-vm = environments.single-nixos-vm =
{ config, ... }: { config, ... }:
{ {
resources.operator-environment.login-shell.username = "operator"; resources.operator-environment.login-shell.username = "operator";
implementation = requests: { implementation =
input = requests; requests:
output =
{ providers, ... }: { providers, ... }:
{ {
providers = { providers = {
@ -134,7 +132,6 @@ in
}; };
}; };
}; };
};
options = { options = {
example-configuration = mkOption { example-configuration = mkOption {
type = config.configuration; type = config.configuration;

View file

@ -103,18 +103,19 @@ in
}; };
resources = mkOption { resources = mkOption {
description = "Compute resources required by an application"; description = "Compute resources required by an application";
type = functionTo application.config.config-mapping.output-type; type = application.config.config-mapping.function-type;
readOnly = true; readOnly = true;
default = input: (application.config.implementation input).output; default = application.config.config-mapping.apply;
}; };
# TODO(@fricklerhandwerk): this needs a better name, it's just the type # TODO(@fricklerhandwerk): this needs a better name
config-mapping = mkOption { config-mapping = mkOption {
description = "Function type for the mapping from application configuration to required resources"; description = "Compute resources required by an application";
type = submodule functionType; type = submodule functionType;
readOnly = true; readOnly = true;
default = { default = {
input-type = submodule application.config.module; input-type = submodule application.config.module;
output-type = application-resources; output-type = application-resources;
declare = application.config.implementation;
}; };
}; };
}; };
@ -151,16 +152,17 @@ in
default = { default = {
input-type = application-resources; input-type = application-resources;
output-type = nixops4Deployment; output-type = nixops4Deployment;
declare = environment.config.implementation;
}; };
}; };
# TODO(@fricklerhandwerk): maybe this should be a separate thing such as `fediversity-setup`, config-mapping = mkOption {
# which makes explicit which applications and environments are available. description = "Mapping from a configuration to a deployment";
# then the deployments can simply be the result of the function application baked into this module. type = submodule functionType;
deployment = mkOption {
description = "Generate a deployment from a configuration, by applying an environment's resource policies to the applications' resource mappings";
type = functionTo (environment.config.resource-mapping.output-type);
readOnly = true; readOnly = true;
default = default = {
input-type = config.configuration;
output-type = nixops4Deployment;
declare =
cfg: cfg:
# TODO: check cfg.enable.true # TODO: check cfg.enable.true
let let
@ -168,8 +170,17 @@ in
name: application-settings: config.applications.${name}.resources application-settings name: application-settings: config.applications.${name}.resources application-settings
) cfg.applications; ) cfg.applications;
in in
(environment.config.implementation required-resources).output; environment.config.resource-mapping.apply required-resources;
};
};
# TODO(@fricklerhandwerk): maybe this should be a separate thing such as `fediversity-setup`,
# which makes explicit which applications and environments are available.
# then the deployments can simply be the result of the function application baked into this module.
deployment = mkOption {
description = "Generate a deployment from a configuration, by applying an environment's resource policies to the applications' resource mappings";
type = environment.config.config-mapping.function-type;
readOnly = true;
default = environment.config.config-mapping.apply;
}; };
}; };
}) })

View file

@ -19,6 +19,11 @@ in
type = optionType; type = optionType;
}; };
function-type = mkOption { function-type = mkOption {
type = optionType;
readOnly = true;
default = functionTo config.output-type;
};
wrapper-type = mkOption {
type = optionType; type = optionType;
readOnly = true; readOnly = true;
default = functionTo (submodule { default = functionTo (submodule {
@ -32,5 +37,22 @@ in
}; };
}); });
}; };
declare = mkOption {
type = config.function-type;
default = _: { };
};
wrapper = mkOption {
type = config.wrapper-type;
readOnly = true;
default = input: fn: {
inherit input;
output = config.implementation fn.config.input;
};
};
apply = mkOption {
type = config.function-type;
readOnly = true;
default = input: (config.wrapper input).output;
};
}; };
} }