fediversity/deployment/function.nix
Kiara Grouwstra 3fd61aea5b
All checks were successful
deploy-infra / deploy (push) Successful in 2m11s
/ check-data-model (push) Successful in 31s
Nix flake checks / _complete (push) Successful in 30s
Nix flake checks / deployment-basic (push) Successful in 33s
Nix flake checks / deployment-cli (push) Successful in 44s
Nix flake checks / deployment-model-nixops4 (push) Successful in 34s
Nix flake checks / deployment-model-ssh (push) Successful in 24s
Nix flake checks / deployment-model-tf (push) Successful in 24s
Nix flake checks / deployment-panel (push) Successful in 1m52s
Nix flake checks / nixops-deployment-providers-default (push) Successful in 12s
Nix flake checks / nixops-deployment-providers-fedi200 (push) Successful in 11s
Nix flake checks / nixops-deployment-providers-fedi201 (push) Successful in 12s
Nix flake checks / nixops-deployment-providers-forgejo-ci (push) Successful in 12s
Nix flake checks / nixops-deployment-providers-test (push) Successful in 11s
Nix flake checks / nixops-deployment-providers-vm02116 (push) Successful in 11s
Nix flake checks / nixops-deployment-providers-vm02187 (push) Successful in 11s
Nix flake checks / nixosConfigurations-fedi200 (push) Successful in 17s
Nix flake checks / nixosConfigurations-fedi201 (push) Successful in 33s
Nix flake checks / nixosConfigurations-forgejo-ci (push) Successful in 19s
Nix flake checks / nixosConfigurations-test01 (push) Successful in 17s
Nix flake checks / nixosConfigurations-test02 (push) Successful in 17s
Nix flake checks / nixosConfigurations-test03 (push) Successful in 18s
Nix flake checks / nixosConfigurations-test04 (push) Successful in 17s
Nix flake checks / nixosConfigurations-test05 (push) Successful in 17s
Nix flake checks / nixosConfigurations-test06 (push) Successful in 17s
Nix flake checks / nixosConfigurations-test11 (push) Successful in 18s
Nix flake checks / nixosConfigurations-test12 (push) Successful in 17s
Nix flake checks / nixosConfigurations-test13 (push) Successful in 17s
Nix flake checks / nixosConfigurations-test14 (push) Successful in 17s
Nix flake checks / nixosConfigurations-vm02116 (push) Successful in 18s
Nix flake checks / nixosConfigurations-vm02187 (push) Successful in 18s
Nix flake checks / panel (push) Successful in 29s
Nix flake checks / pre-commit (push) Successful in 12s
Nix flake checks / proxmox-basic (push) Successful in 35s
Nix flake checks / test-mastodon-service (push) Successful in 23s
Nix flake checks / test-peertube-service (push) Successful in 22s
Nix flake checks / vmOptions-fedi200 (push) Successful in 5s
Nix flake checks / vmOptions-fedi201 (push) Successful in 4s
Nix flake checks / vmOptions-test01 (push) Successful in 5s
Nix flake checks / vmOptions-test02 (push) Successful in 5s
Nix flake checks / vmOptions-test03 (push) Successful in 5s
Nix flake checks / vmOptions-test04 (push) Successful in 5s
Nix flake checks / vmOptions-test05 (push) Successful in 5s
Nix flake checks / vmOptions-test06 (push) Successful in 4s
Nix flake checks / vmOptions-test11 (push) Successful in 5s
Nix flake checks / vmOptions-test12 (push) Successful in 4s
Nix flake checks / vmOptions-test13 (push) Successful in 5s
Nix flake checks / vmOptions-test14 (push) Successful in 4s
Nix flake checks / _checks (push) Successful in 0s
better document function.nix (#524)
Reviewed-on: Fediversity/Fediversity#524
2025-09-18 18:22:09 +02:00

91 lines
1.9 KiB
Nix

/**
Modular function type.
Compared to plain nix functions, adds input type-checks
at the cost of longer stack traces.
Usage:
```nix
{ lib, ... }:
{
options = {
my-function = lib.mkOption {
description = "My type-safe function invocation.";
type = lib.types.submodule PATH/TO/function.nix;
readOnly = true;
default = {
input-type = lib.types.int;
output-type = lib.types.int;
implementation = x: x + x;
};
};
};
config = {
my-function.apply "1"
};
}
```
A sample stack trace using this ends up like:
- `INVOKER.apply.<function body>``
- `function.nix`
- `INVOKER.wrapper.<function body>.output`
- `INVOKER.implementation.<function body>`
*/
{ config, lib, ... }:
let
inherit (lib) mkOption types;
inherit (types)
submodule
functionTo
optionType
;
in
{
options = {
input-type = mkOption {
type = optionType;
};
output-type = mkOption {
type = optionType;
};
function-type = mkOption {
type = optionType;
readOnly = true;
default = functionTo config.output-type;
};
wrapper-type = mkOption {
type = optionType;
readOnly = true;
default = functionTo (submodule {
options = {
input = mkOption {
type = config.input-type;
};
output = mkOption {
type = config.output-type;
};
};
});
};
implementation = 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;
};
};
}