From 59f56c94364b0f89a037fdeae65631aa82498c25 Mon Sep 17 00:00:00 2001 From: Kiara Grouwstra Date: Wed, 27 Aug 2025 12:18:47 +0200 Subject: [PATCH] WIP: add data model test using selfhostblocks status: properly test that this works --- .forgejo/workflows/ci.yaml | 6 ++ deployment/shb-test.nix | 177 +++++++++++++++++++++++++++++++++++++ npins/sources.json | 16 ++++ 3 files changed, 199 insertions(+) create mode 100644 deployment/shb-test.nix diff --git a/.forgejo/workflows/ci.yaml b/.forgejo/workflows/ci.yaml index 5015d407..77bded68 100644 --- a/.forgejo/workflows/ci.yaml +++ b/.forgejo/workflows/ci.yaml @@ -21,6 +21,12 @@ jobs: - uses: actions/checkout@v4 - run: nix-shell --run 'nix-unit ./deployment/data-model-test.nix' + check-shb: + runs-on: native + steps: + - uses: actions/checkout@v4 + - run: nix-shell --run 'nix-unit ./deployment/shb-test.nix' + check-mastodon: runs-on: native steps: diff --git a/deployment/shb-test.nix b/deployment/shb-test.nix new file mode 100644 index 00000000..340ab113 --- /dev/null +++ b/deployment/shb-test.nix @@ -0,0 +1,177 @@ +let + sources = import ../npins; + inherit (import ../default.nix { }) pkgs inputs; + inherit (pkgs) lib; + inherit (lib) mkOption types; + eval = + module: + (lib.evalModules { + specialArgs = { + inherit inputs; + }; + modules = [ + module + ./data-model.nix + ]; + }).config; + nixops4Deployment = inputs.nixops4.modules.nixops4Deployment.default; + contracts = pkgs.callPackage "${sources.selfhostblocks}/modules/contracts" { }; + inherit (inputs.nixops4.lib) mkDeployment; +in +{ + _class = "nix-unit"; + + test-eval = { + expr = + let + fediversity = eval ( + { config, ... }: + { + config = { + resources.backup = { + description = "handle SelfHostBlocks backup requests"; + request = + { ... }: + { + _class = "fediversity-resource-request"; + options = { + req = mkOption { + type = contracts.backup.contract.request; + }; + }; + }; + policy = + { config, ... }: + { + _class = "fediversity-resource-policy"; + options = { + block = mkOption { + description = '' + the environment's SHB implementation handling back-up requests, + using any of: https://shb.skarabox.com/contracts-backup.html#contract-backup-providers. + takes the form of a function from a request to NixOS configuration handling the back-up request. + ''; + type = types.functionTo types.raw; + }; + }; + config = { + resource-type = types.raw; # TODO: splice out the user type from NixOS + apply = + requests: + lib.mkMerge ( + lib.mapAttrsToList (name: request: config.block { inherit name request; }) requests.resources + ); + }; + }; + }; + applications.bar = + { ... }: + { + description = "application to test SHB backups"; + module = + { ... }: + { + options = { + enable = lib.mkEnableOption "shb test"; + backup = mkOption { + default = { }; + type = lib.types.submodule { + options = contracts.backup.mkRequester { + sourceDirectories = [ + "/var/lock" # some small dir + ]; + }; + }; + }; + # }; + }; + }; + implementation = cfg: { + input = cfg; + output = lib.optionalAttrs cfg.enable { + resources.hello.req = { + inherit (cfg) backup; + }; + }; + }; + }; + environments.single-nixos-vm = + { config, ... }: + { + resources.operator-environment.backup.block = + { name, request }: + { + imports = [ + "${sources.selfhostblocks}/modules/blocks/restic.nix" + ]; + shb.restic.instances.${name} = { + inherit request; + settings = { + enable = true; + repository.path = "/srv/backup/restic/fediversity-test/${name}"; + }; + }; + }; + implementation = requests: { + input = requests; + output = + { providers, ... }: + { + providers = { + inherit (inputs.nixops4.modules.nixops4Provider) local; + }; + resources.the-machine = { + type = providers.local.exec; + imports = [ + inputs.nixops4-nixos.modules.nixops4Resource.nixos + ]; + nixos.module = { ... }: config.resources.baz.backup.apply requests; + }; + }; + }; + }; + }; + options = { + example-configuration = mkOption { + type = config.configuration; + readOnly = true; + default = { + enable = true; + applications = { + bar = { + enable = true; + }; + }; + }; + }; + example-deployment = mkOption { + type = types.submodule nixops4Deployment; + readOnly = true; + default = config.environments.single-nixos-vm.deployment config.example-configuration; + }; + }; + } + ); + result = mkDeployment { + modules = [ + (fediversity.environments.single-nixos-vm.deployment fediversity.example-configuration) + ]; + }; + + in + { + deployment = { + inherit (result) _type; + deploymentFunction = lib.isFunction result.deploymentFunction; + getProviders = lib.isFunction result.getProviders; + }; + }; + expected = { + deployment = { + _type = "nixops4Deployment"; + deploymentFunction = true; + getProviders = true; + }; + }; + }; +} diff --git a/npins/sources.json b/npins/sources.json index ea9606bd..60564bac 100644 --- a/npins/sources.json +++ b/npins/sources.json @@ -150,6 +150,22 @@ "revision": "f33a4d26226c05d501b9d4d3e5e60a3a59991921", "url": "https://github.com/nixos/nixpkgs/archive/f33a4d26226c05d501b9d4d3e5e60a3a59991921.tar.gz", "hash": "1b6dm1sn0bdpcsmxna0zzspjaixa2dald08005fry5jrbjvwafdj" + }, + "selfhostblocks": { + "type": "GitRelease", + "repository": { + "type": "GitHub", + "owner": "ibizaman", + "repo": "selfhostblocks" + }, + "pre_releases": false, + "version_upper_bound": null, + "release_prefix": null, + "submodules": false, + "version": "v0.4.0", + "revision": "00fc98da3528ab270e987feb0f0579da89efd6f0", + "url": "https://api.github.com/repos/ibizaman/selfhostblocks/tarball/v0.4.0", + "hash": "1jp2qsib3xmlcbzz4d11xj5lbyn6a6zyp5n1ag5w3fabrichzvv7" } }, "version": 5