forked from DGNum/infrastructure
feat(dgn-web): Add a way to detect internal port clashes
This commit is contained in:
parent
f819acf9bc
commit
9ea6bada0a
1 changed files with 46 additions and 1 deletions
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue