2023-06-18 18:40:16 +02:00
|
|
|
{
|
|
|
|
nftables
|
|
|
|
, writeScript
|
|
|
|
, lib
|
|
|
|
} :
|
|
|
|
name : ruleset :
|
|
|
|
let
|
|
|
|
inherit (lib.strings) concatStringsSep splitString hasInfix substring;
|
|
|
|
inherit (lib.lists) groupBy;
|
|
|
|
inherit (lib.attrsets) mapAttrsToList nameValuePair;
|
|
|
|
inherit (builtins) map listToAttrs replaceStrings head tail;
|
|
|
|
|
|
|
|
indentLines = offset : lines :
|
|
|
|
if lines == []
|
|
|
|
then ""
|
|
|
|
else
|
|
|
|
let
|
|
|
|
line = head lines;
|
|
|
|
isOpen = hasInfix "{" line;
|
|
|
|
isClose = hasInfix "}" line;
|
|
|
|
offset' = offset +
|
|
|
|
(if isOpen then 4 else 0) +
|
|
|
|
(if isClose then -4 else 0);
|
|
|
|
padding = offset: substring 0 offset " ";
|
|
|
|
in
|
|
|
|
if (isClose && !isOpen)
|
|
|
|
then
|
|
|
|
(padding offset') + line + "\n" + indentLines offset' (tail lines)
|
|
|
|
else
|
|
|
|
(padding offset) + line + "\n" + indentLines offset' (tail lines);
|
|
|
|
|
|
|
|
indent = text : indentLines 0 (splitString "\n" text);
|
|
|
|
|
2023-06-18 23:18:44 +02:00
|
|
|
dochain = { name, type, family, rules,
|
|
|
|
policy ? null,
|
|
|
|
priority ? "filter",
|
|
|
|
hook ? null } : ''
|
2023-06-18 18:40:16 +02:00
|
|
|
chain ${name} {
|
|
|
|
${if hook != null
|
2023-06-18 23:18:44 +02:00
|
|
|
then "type ${type} hook ${hook} priority ${priority}; policy ${policy};"
|
2023-06-18 18:40:16 +02:00
|
|
|
else ""
|
|
|
|
}
|
|
|
|
${concatStringsSep "\n" rules}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
dotable = family : chains : ''
|
2023-06-18 23:18:44 +02:00
|
|
|
table ${family} table-${family} {
|
2023-06-18 18:40:16 +02:00
|
|
|
${concatStringsSep "\n" (map dochain chains)}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
categorise = chains :
|
|
|
|
groupBy
|
|
|
|
({ family, ... } : family)
|
|
|
|
(mapAttrsToList (n : v : v // { name = n; }) chains);
|
|
|
|
in writeScript name ''
|
|
|
|
#!${nftables}/sbin/nft -cf
|
|
|
|
|
|
|
|
flush ruleset
|
|
|
|
|
|
|
|
${indent (concatStringsSep "\n" (mapAttrsToList dotable (categorise ruleset)))}
|
|
|
|
''
|