feat(dgn-web): Add a way to detect internal port clashes

This commit is contained in:
Tom Hubrecht 2024-10-12 18:19:43 +02:00 committed by thubrecht
parent f819acf9bc
commit 9ea6bada0a

View file

@ -1,16 +1,61 @@
{ config, lib, ... }:
let
inherit (lib) mkEnableOption mkIf;
inherit (lib)
attrsToList
concatStringsSep
filterAttrs
mkEnableOption
mkIf
mkOption
;
inherit (lib.types) attrsOf port;
cfg = config.dgn-web;
in
{
options.dgn-web = {
enable = mkEnableOption "sane defaults for web services.";
internalPorts = mkOption {
type = attrsOf port;
default = { };
description = ''
Map from the web services to their internal ports, it should avoid port clashes.
'';
};
};
config = mkIf cfg.enable {
assertions = [
(
let
duplicates = builtins.attrValues (
builtins.mapAttrs (p: serv: "${p}: ${concatStringsSep ", " serv}") (
filterAttrs (_: ls: builtins.length ls != 1) (
builtins.foldl' (
rev:
{ name, value }:
let
str = builtins.toString value;
in
rev // { ${str} = (rev.${str} or [ ]) ++ [ name ]; }
) { } (attrsToList cfg.internalPorts)
)
)
);
in
{
assertion = duplicates == [ ];
message = ''
Internal ports cannot be used for multiple services, the clashes are:
${concatStringsSep "\n " duplicates}
'';
}
)
];
services.nginx = {
enable = true;