33 lines
1.3 KiB
Nix
33 lines
1.3 KiB
Nix
{ lib, ... }:
|
|
|
|
let
|
|
filterPort = pm: port: (
|
|
lib.attrsets.catAttrs port (
|
|
lib.attrsets.attrValues (
|
|
lib.attrsets.filterAttrs (n: v: v ? "${port}") pm
|
|
)
|
|
)
|
|
);
|
|
onlyUniqueItemsInList = (x: lib.lists.length x == lib.lists.length (lib.lists.unique x));
|
|
protocols = (x: lib.lists.unique (lib.flatten (map builtins.attrNames (lib.attrValues x))));
|
|
mkRange = (x: lib.lists.range (builtins.elemAt x 0) (builtins.elemAt x 1));
|
|
validateList = allowed: builtins.all (x: builtins.elem x allowed);
|
|
in
|
|
{
|
|
validatePortAttrset = portmap:
|
|
if ! onlyUniqueItemsInList (lib.flatten (map
|
|
(x:
|
|
if lib.isInt x then x
|
|
else if lib.isList x then x
|
|
else if lib.isAttrs x then
|
|
(
|
|
if ! validateList [ "range" ] (builtins.attrNames x) then builtins.abort "found invalid attribute name"
|
|
else if x ? "range" then if lib.lists.length x.range == 2 then mkRange x.range else builtins.abort "range needs a list with exactly two items"
|
|
else builtins.abort "found invalid attrset"
|
|
)
|
|
else builtins.abort "found invalid entry in portmap"
|
|
)
|
|
(filterPort portmap "udp"))) then builtins.abort "Found duplicate ports."
|
|
else if ! validateList [ "tcp" "udp" ] (protocols portmap) then builtins.abort "Found invalid protocol."
|
|
else portmap;
|
|
}
|