Compare commits

...

6 commits

Author SHA1 Message Date
bf488f89e1 readability 2025-07-22 12:43:32 +02:00
7066b2cb69 use mapAttrs right, again 2025-07-22 12:42:08 +02:00
243ef4425b WIP: more type-safe policy application 2025-07-22 12:38:50 +02:00
0f7da57392 use submodule to turn module into type for functionTo 2025-07-22 10:57:32 +02:00
bb93d2d0de use mapAttrs right
`mapAttrs'` takes two args rather than a set, whereas if only the val
changes `mapAttrs (_: v: ...)` should do
2025-07-22 10:54:43 +02:00
b25ddac298 fix typos, lint, format 2025-07-22 10:54:27 +02:00
2 changed files with 44 additions and 40 deletions

View file

@ -40,7 +40,7 @@ in
_class = "fediversity-resource-policy"; _class = "fediversity-resource-policy";
options = { options = {
extra-config = mkOptions { extra-config = mkOption {
description = "Any options from NixOS"; description = "Any options from NixOS";
}; };
apply = mkOption { apply = mkOption {
@ -82,25 +82,23 @@ in
type = types.bool; type = types.bool;
default = false; default = false;
}; };
apply = mkOption { };
type = with types; functionTo raw; # TODO: splice out the user type from NixOS config.resource-type = types.any; # TODO: splice out the user type from NixOS
default = config.apply =
requests: requests:
let let
# Filter out requests that need wheel if policy doesn't allow it # Filter out requests that need wheel if policy doesn't allow it
validRequests = lib.filterAttrs (name: req: !req.wheel || config.wheel) requests; validRequests = lib.filterAttrs (_name: req: !req.wheel || config.wheel) requests;
in in
lib.optionalAttrs (validRequests != { }) { lib.optionalAttrs (validRequests != { }) {
${config.username} = { ${config.username} = {
isNormalUser = true; isNormalUser = true;
packages = with lib; concatMapAttrs (name: request: attrValues request.packages) validRequests; packages = with lib; concatMapAttrs (_name: request: attrValues request.packages) validRequests;
extraGroups = lib.optional config.wheel "wheel"; extraGroups = lib.optional config.wheel "wheel";
}; };
}; };
}; };
}; };
};
};
applications.hello = applications.hello =
{ ... }: { ... }:
{ {
@ -113,7 +111,9 @@ in
implementation = implementation =
cfg: cfg:
lib.optionalAttrs cfg.enable { lib.optionalAttrs cfg.enable {
dummy.login-shell.packages.hello = pkgs.hello; hello.login-shell.packages = {
inherit (pkgs) hello;
};
}; };
}; };
environments.single-nixos-vm = environments.single-nixos-vm =
@ -133,10 +133,10 @@ in
inputs.nixops4-nixos.modules.nixops4Resource.nixos inputs.nixops4-nixos.modules.nixops4Resource.nixos
]; ];
nixos.module = nixos.module =
{ pkgs, ... }: { ... }:
{ {
users.users = config.resources.shell.login-shell.apply ( users.users = config.resources.shell.login-shell.apply (
lib.filterAttrs (name: value: value ? login-shell) requests lib.filterAttrs (_name: value: value ? login-shell) requests
); );
}; };
}; };
@ -164,8 +164,7 @@ in
{ {
inherit (fediversity) example-deployment; inherit (fediversity) example-deployment;
}; };
expected = expected = {
{
}; };
}; };
} }

View file

@ -21,11 +21,7 @@ let
options.resources = mkOption { options.resources = mkOption {
# TODO: maybe transpose, and group the resources by type instead # TODO: maybe transpose, and group the resources by type instead
type = attrsOf ( type = attrsOf (
attrTag ( attrTag (lib.mapAttrs (_name: resource: mkOption { type = resource.request; }) config.resources)
lib.mapAttrs' (name: resource: {
${name} = mkOption { type = resource.request; };
}) config.resources
)
); );
}; };
}; };
@ -37,7 +33,7 @@ in
description = "Collection of deployment resources that can be required by applications and policed by hosting providers"; description = "Collection of deployment resources that can be required by applications and policed by hosting providers";
type = attrsOf ( type = attrsOf (
submodule ( submodule (
{ config, ... }: { ... }:
{ {
_class = "fediversity-resource"; _class = "fediversity-resource";
options = { options = {
@ -53,12 +49,23 @@ in
description = "Options for configuring the resource policy for the hosting provider, a description of how the resource is made available"; description = "Options for configuring the resource policy for the hosting provider, a description of how the resource is made available";
type = deferredModuleWith { type = deferredModuleWith {
staticModules = [ staticModules = [
{ (policy: {
_class = "fediversity-resource-policy"; _class = "fediversity-resource-policy";
options.apply = mkOption { # TODO(@fricklerhandwerk): not sure it can be made
desciption = "Apply the policy to a request"; # sensible syntactically, but essentially we want to
# ensure that `apply` is defined, but since its output
# depends on the specific policy we also need to
# determine that somehow.
# hopefully this also helps with correct composition down the line.
options.resource-type = mkOption {
description = "The type of resource this policy configures";
type = types.optionType;
}; };
} options.apply = mkOption {
description = "Apply the policy to a request";
type = with types; functionTo policy.config.resource-type;
};
})
]; ];
}; };
}; };
@ -119,9 +126,7 @@ in
# TODO: maybe transpose, and group the resources by type instead # TODO: maybe transpose, and group the resources by type instead
type = attrsOf ( type = attrsOf (
attrTag ( attrTag (
lib.mapAttrs' (name: resource: { lib.mapAttrs (_name: resource: mkOption { type = submodule resource.policy; }) config.resources
${name} = mkOption { type = resource.policy; };
}) config.resources
) )
); );
}; };
@ -140,7 +145,7 @@ in
}; };
deployment = mkOption { deployment = mkOption {
description = "Generate a deployment from a configuration"; description = "Generate a deployment from a configuration";
type = functionTo environment.config.resource-mapping.output-type; type = functionTo (submodule environment.config.resource-mapping.output-type);
readOnly = true; readOnly = true;
default = default =
cfg: cfg:
@ -161,13 +166,13 @@ in
description = "Configuration type declaring options to be set by operators"; description = "Configuration type declaring options to be set by operators";
type = optionType; type = optionType;
readOnly = true; readOnly = true;
default = submodule (configuration: { default = submodule {
options = { options = {
enable = lib.mkEnableOption { enable = lib.mkEnableOption {
description = "your Fediversity configuration"; description = "your Fediversity configuration";
}; };
applications = lib.mapAttrs ( applications = lib.mapAttrs (
name: application: _name: application:
mkOption { mkOption {
description = application.description; description = application.description;
type = submodule application.module; type = submodule application.module;
@ -175,7 +180,7 @@ in
} }
) config.applications; ) config.applications;
}; };
}); };
}; };
}; };
} }