Compare commits

..

1 commit

Author SHA1 Message Date
e5710979ba
fix typo 2025-07-17 19:00:41 +02:00
6 changed files with 49 additions and 335 deletions

View file

@ -13,7 +13,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
- name: Update pins
run: nix-shell --run "npins --verbose update"
run: nix-shell --run "npins update"
- name: Create PR
uses: https://github.com/KiaraGrouwstra/gitea-create-pull-request@f9f80aa5134bc5c03c38f5aaa95053492885b397
with:

View file

@ -11,8 +11,7 @@ let
;
inherit (pkgs) lib;
inherit (import sources.flake-inputs) import-flake;
inputs = (import-flake { src = ./.; }).inputs;
inherit (inputs) nixops4;
inherit ((import-flake { src = ./.; }).inputs) nixops4;
panel = import ./panel { inherit sources system; };
pre-commit-check =
(import "${git-hooks}/nix" {
@ -79,7 +78,6 @@ in
# re-export inputs so they can be overridden granularly
# (they can't be accessed from the outside any other way)
inherit
inputs
sources
system
pkgs

View file

@ -1,7 +1,7 @@
let
inherit (import ../default.nix { }) pkgs inputs;
inherit (pkgs) lib;
inherit (lib) mkOption types;
inherit (lib) mkOption;
eval =
module:
(lib.evalModules {
@ -13,82 +13,17 @@ let
./data-model.nix
];
}).config;
inherit (inputs.nixops4.lib) mkDeployment;
in
{
_class = "nix-unit";
test-eval = {
/**
This tests a very simple arrangement that features all ingredients of the Fediversity business logic:
application, resource, environment, deployment; and wires it all up in one end-to-end exercise.
- The dummy resource is a login shell made available for some user.
- The dummy application is `hello` that requires a shell to be deployed.
- The dummy environment is a single NixOS VM that hosts one login shell, for the operator.
- The dummy configuration enables the `hello` application.
This will produce a NixOps4 deployment for a NixOS VM with a login shell for the operator and `hello` available.
*/
expr =
let
fediversity = eval (
{ config, ... }:
{
config = {
resources.login-shell = {
description = "The operator needs to be able to log into the shell";
request =
{ ... }:
{
_class = "fediversity-resource-request";
options = {
wheel = mkOption {
description = "Whether the login user needs root permissions";
type = types.bool;
default = false;
};
packages = mkOption {
description = "Packages that need to be available in the user environment";
type = with types; attrsOf package;
};
};
};
policy =
{ config, ... }:
{
_class = "fediversity-resource-policy";
options = {
username = mkOption {
description = "Username for the operator";
type = types.str; # TODO: use the proper constraints from NixOS
};
wheel = mkOption {
description = "Whether to allow login with root permissions";
type = types.bool;
default = false;
};
};
config = {
resource-type = types.raw; # TODO: splice out the user type from NixOS
apply =
requests:
let
# Filter out requests that need wheel if policy doesn't allow it
validRequests = lib.filterAttrs (
_name: req: !req.login-shell.wheel || config.wheel
) requests.resources;
in
lib.optionalAttrs (validRequests != { }) {
${config.username} = {
isNormalUser = true;
packages =
with lib;
attrValues (concatMapAttrs (_name: request: request.login-shell.packages) validRequests);
extraGroups = lib.optional config.wheel "wheel";
};
};
};
};
};
applications.hello =
{ ... }:
{
@ -96,54 +31,16 @@ in
module =
{ ... }:
{
options.enable = lib.mkEnableOption "Hello in the shell";
options = {
enable = lib.mkEnableOption "Hello in the shell";
};
};
implementation =
cfg:
lib.optionalAttrs cfg.enable {
dummy.login-shell.packages.hello = pkgs.hello;
};
implementation = input: fn: {
inherit input;
output =
let
cfg = fn.config.input;
in
lib.optionalAttrs cfg.enable {
resources.hello.login-shell.packages.hello = pkgs.hello;
};
};
};
environments.single-nixos-vm =
{ config, ... }:
{
resources.operator-environment.login-shell.username = "operator";
implementation = input: fn: {
inherit input;
output =
let
requests = fn.config.input;
in
{ providers, ... }:
{
providers = {
inherit (inputs.nixops4.modules.nixops4Provider) local;
};
resources.the-machine = {
type = providers.local.exec;
imports = [
inputs.nixops4-nixos.modules.nixops4Resource.nixos
];
nixos.module =
{ ... }:
{
users.users = config.resources.shell.login-shell.apply (
lib.filterAttrs (_name: value: value ? login-shell) requests
);
};
};
};
};
};
deployments.example = {
environment = config.environments.single-nixos-vm;
configuration = config.example-configuration;
};
};
options = {
example-configuration = mkOption {
@ -157,63 +54,17 @@ in
};
}
);
resources = fediversity.example-configuration.required-resources.hello;
hello-shell = resources.resources.hello.login-shell;
environment = fediversity.environments.single-nixos-vm.resources.operator-environment.login-shell;
result = mkDeployment {
modules = [
fediversity.deployments.example.deployment
];
};
in
{
number-of-resources = with lib; length (attrNames fediversity.resources);
# XXX: nix-unit unlike nix-build seems to hit infinite recursions on
# evaluation of package paths, so we avoid evaluating
# fediversity.example-configuration.required-resources.hello.resources.hello.login-shell.packages.hello
example-configuration = {
inherit (fediversity.example-configuration) enable applications;
};
hello-package-exists = hello-shell.packages ? hello;
wheel-required = hello-shell.wheel;
wheel-allowed = environment.wheel;
operator-shell =
let
operator = (environment.apply resources).operator;
in
{
inherit (operator) isNormalUser;
packages = map (p: "${p.pname}") operator.packages;
extraGroups = operator.extraGroups;
};
deployment = {
inherit (result) _type;
isModule = lib.isFunction fediversity.deployments.example.deployment;
deploymentFunction = lib.isFunction result.deploymentFunction;
getProviders = lib.isFunction result.getProviders;
};
inherit (fediversity)
example-configuration
;
};
expected = {
number-of-resources = 1;
example-configuration = {
enable = true;
applications.hello.enable = true;
};
hello-package-exists = true;
wheel-required = false;
wheel-allowed = false;
operator-shell = {
isNormalUser = true;
packages = [ "hello" ];
extraGroups = [ ];
};
deployment = {
_type = "nixops4Deployment";
deploymentFunction = true;
getProviders = true;
isModule = false;
};
};
};
}

View file

@ -1,8 +1,6 @@
{
lib,
config,
options,
inputs,
...
}:
let
@ -16,74 +14,20 @@ let
functionTo
;
functionType = import ./function.nix { inherit lib; };
application-resources = submodule {
functionType = import ./function.nix;
application-resources = {
options.resources = mkOption {
# TODO: maybe transpose, and group the resources by type instead
type = attrsOf (
attrTag (
lib.mapAttrs (_name: resource: mkOption { type = submodule resource.request; }) config.resources
)
attrTag (lib.mapAttrs (_name: resource: mkOption { type = resource.request; }) config.resources)
);
};
};
nixops4Deployment = types.deferredModuleWith {
staticModules = [
inputs.nixops4.modules.nixops4Deployment.default
{
_class = "nixops4Deployment";
_module.args = {
resourceProviderSystem = builtins.currentSystem;
resources = { };
};
}
];
};
in
{
_class = "nixops4Deployment";
options = {
resources = mkOption {
description = "Collection of deployment resources that can be required by applications and policed by hosting providers";
type = attrsOf (
submodule (
{ ... }:
{
_class = "fediversity-resource";
options = {
description = mkOption {
description = "Description of the resource to help application module authors and hosting providers to work with it";
type = types.str;
};
request = mkOption {
description = "Options for declaring resource requirements by an application, a description of how the resource is consumed or accessed";
type = deferredModuleWith { staticModules = [ { _class = "fediversity-resource-request"; } ]; };
};
policy = mkOption {
description = "Options for configuring the resource policy for the hosting provider, a description of how the resource is made available";
type = deferredModuleWith {
staticModules = [
(policy: {
_class = "fediversity-resource-policy";
options.resource-type = mkOption {
description = "The type of resource this policy configures";
type = types.optionType;
};
# TODO(@fricklerhandwerk): we may want to make the function type explict here: `request -> resource-type`
# and then also rename this to be consistent with the application's resource mapping
options.apply = mkOption {
description = "Apply the policy to a request";
type = functionTo policy.config.resource-type;
};
})
];
};
};
};
}
)
);
};
applications = mkOption {
description = "Collection of Fediversity applications";
type = attrsOf (
@ -102,13 +46,18 @@ in
description = "Mapping of application configuration to deployment resources, a description of what an application needs to run";
type = application.config.config-mapping.function-type;
};
# TODO(@fricklerhandwerk): this needs a better name, it's just the type
resources = mkOption {
description = "Compute resources required by an application";
type = functionTo application.config.config-mapping.output-type;
readOnly = true;
default = input: (application.config.implementation input).output;
};
config-mapping = mkOption {
description = "Function type for the mapping from application configuration to required resources";
type = functionType;
type = submodule functionType;
readOnly = true;
default = {
input-type = submodule application.config.module;
input-type = application.config.module;
output-type = application-resources;
};
};
@ -116,47 +65,11 @@ in
})
);
};
environments = mkOption {
description = "Run-time environments for Fediversity applications to be deployed to";
type = attrsOf (
submodule (environment: {
_class = "fediversity-environment";
options = {
resources = mkOption {
description = ''
Resources made available by the hosting provider, and their policies.
Setting this is optional, but provides a place to declare that information for programmatic use in the resource mapping.
'';
# TODO: maybe transpose, and group the resources by type instead
type = attrsOf (
attrTag (
lib.mapAttrs (_name: resource: mkOption { type = submodule resource.policy; }) config.resources
)
);
};
implementation = mkOption {
description = "Mapping of resources required by applications to available resources; the result can be deployed";
type = environment.config.resource-mapping.function-type;
};
resource-mapping = mkOption {
description = "Function type for the mapping from resources to a (NixOps4) deployment";
type = functionType;
# readOnly = true;
default = {
input-type = application-resources;
output-type = nixops4Deployment;
};
};
};
})
);
};
configuration = mkOption {
description = "Configuration type declaring options to be set by operators";
type = optionType;
readOnly = true;
default = submodule (configuration: {
default = submodule {
options = {
enable = lib.mkEnableOption {
description = "your Fediversity configuration";
@ -169,55 +82,8 @@ in
default = { };
}
) config.applications;
required-resources = mkOption {
description = "The deployment's required resources by application";
type = submodule {
options = lib.mapAttrs (
name: _application:
mkOption {
description = "Resources required for ${name}";
type = config.applications.${name}.config-mapping.output-type;
}
) config.applications;
};
readOnly = true;
default = lib.mapAttrs (
# FIXME: in nix-unit invoking the implementation somehow triggers an infinite recursion,
# tho my trace statements so far got triggered just once
name: application-settings: (config.applications.${name}.implementation application-settings).output
) configuration.config.applications;
};
};
});
};
deployments = mkOption {
description = "Deployment of a configuration of applications to a run-time environment.";
type = attrsOf (
submodule (deployment: {
options = {
configuration = mkOption {
description = "Configuration to be deployed";
type = config.configuration;
};
environment = mkOption {
description = "The run-time environment to deploy to";
type = options.environments.type.nestedTypes.elemType;
};
# 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 = deployment.config.environment.resource-mapping.output-type;
readOnly = true;
# TODO: check cfg.enable.true
default =
(deployment.config.environment.implementation deployment.config.configuration.required-resources)
.output;
};
};
})
);
};
};
};
}

View file

@ -1,36 +1,37 @@
/**
Modular function type
*/
{ lib, ... }:
{ config, lib, ... }:
let
inherit (lib) mkOption types;
inherit (types)
deferredModule
submodule
functionTo
optionType
;
in
submodule (function: {
{
options = {
input-type = mkOption {
type = optionType;
type = deferredModule;
};
output-type = mkOption {
type = optionType;
type = deferredModule;
};
function-type = mkOption {
type = optionType;
# readOnly = true;
readOnly = true;
default = functionTo (submodule {
options = {
input = mkOption {
type = function.config.input-type;
type = submodule config.input-type;
};
output = mkOption {
type = function.config.output-type;
type = submodule config.output-type;
};
};
});
};
};
})
}

View file

@ -23,17 +23,21 @@ let
makeResourceModule =
{ vmName, isTestVm }:
{
# TODO(@fricklerhandwerk): this is terrible but IMO we should just ditch flake-parts and have our own data model for how the project is organised internally
_module.args = {
inherit
inputs
keys
secrets
;
};
nixos.module.imports = [
./common/proxmox-qemu-vm.nix
];
nixos.specialArgs = {
inherit
sources
inputs
keys
secrets
;
inherit sources;
};
imports =
@ -75,13 +79,7 @@ let
# TODO(@fricklerhandwerk): we may want to pass through all of `specialArgs`
# once we're sure it's sane. leaving it here for better control during refactoring.
specialArgs = {
inherit
sources
inputs
keys
secrets
;
inherit sources;
};
});
};