From c523ebe44b405b6a09ee0e49aaaf419462542fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikodem=20Rabuli=C5=84ski?= Date: Sun, 23 Mar 2025 22:02:59 +0100 Subject: [PATCH] services: migrate to nilla --- modules/nilla/default.nix | 5 +- modules/nilla/services.nix | 95 ++++++++++++++++++++++++++++++++++++++ nilla.nix | 4 ++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 modules/nilla/services.nix diff --git a/modules/nilla/default.nix b/modules/nilla/default.nix index f9e4319..6f39646 100644 --- a/modules/nilla/default.nix +++ b/modules/nilla/default.nix @@ -1,3 +1,6 @@ { - includes = [ ./builders ]; + includes = [ + ./builders + ./services.nix + ]; } diff --git a/modules/nilla/services.nix b/modules/nilla/services.nix new file mode 100644 index 0000000..1b72eaf --- /dev/null +++ b/modules/nilla/services.nix @@ -0,0 +1,95 @@ +{ lib, config }: +let + inherit (builtins) + attrNames + attrValues + concatStringsSep + mapAttrs + foldl' + groupBy + length + ; + serviceModule = + { config }: + { + options = { + host = lib.options.create { + type = lib.types.str; + }; + ports = lib.options.create { + type = lib.types.list.of lib.types.port; + default.value = [ ]; + }; + hosts = lib.options.create { + type = lib.types.list.of lib.types.str; + default.value = [ config.host ]; + }; + module = lib.options.create { + type = lib.types.raw; + default.value = { }; + }; + hostModule = lib.options.create { + type = lib.types.attrs.of lib.types.raw; + default.value = { }; + }; + }; + }; + + moduleToHostConfigs = + cfg: + lib.attrs.generate cfg.hosts (host: { + imports = [ + cfg.module + (cfg.hostModule.${host} or { }) + ]; + }); + + maybeGetPreviousConfigs = acc: host: (acc.${host} or { imports = [ ]; }).imports; + + # Copied from nixpkgs/lib/lists.nix + groupBy' = + op: nul: pred: lst: + mapAttrs (_name: foldl' op nul) (groupBy pred lst); + duplicatePorts = lib.fp.pipe [ + attrValues + (map (cfg: cfg.ports)) + lib.lists.flatten + (groupBy' (cnt: _: cnt + 1) 0 toString) + (lib.attrs.filter (_: cnt: cnt > 1)) + attrNames + ] config.services; +in +{ + options.services = lib.options.create { + type = lib.types.attrs.of (lib.types.submodule serviceModule); + default.value = { }; + }; + + options.extraHostConfigs = lib.options.create { + type = lib.types.attrs.of lib.types.raw; + writable = false; + default.value = lib.fp.pipe [ + attrValues + (foldl' ( + acc: cfg: + acc + // mapAttrs (host: c: { + imports = c.imports ++ (maybeGetPreviousConfigs acc host); + }) (moduleToHostConfigs cfg) + ) { }) + ] config.services; + }; + + config.assertions = [ + { + assertion = duplicatePorts == [ ]; + message = + let + plural = length duplicatePorts > 1; + in + "\nBad service config:\nThe following port${if plural then "s" else ""} ${ + if plural then "were" else "was" + } declared multiple times: ${concatStringsSep ", " duplicatePorts}"; + } + ]; +} diff --git a/nilla.nix b/nilla.nix index af9de9a..0aea0c5 100644 --- a/nilla.nix +++ b/nilla.nix @@ -8,6 +8,10 @@ ./modules/nilla ./pkgs ./wrappers + + ./services/attic.nix + ./services/forgejo-runner.nix + ./services/forgejo.nix ]; config.inputs = builtins.mapAttrs (_: src: {