From 82774bc9788cbb06fd4fd001b26378fdd28ed8e0 Mon Sep 17 00:00:00 2001 From: magic_rb Date: Thu, 18 Jan 2024 13:15:30 +0100 Subject: [PATCH] Add start stop hooks to notnftables Signed-off-by: magic_rb --- nixos/modules/notnft.nix | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/nixos/modules/notnft.nix b/nixos/modules/notnft.nix index 06dd260..b3295bb 100644 --- a/nixos/modules/notnft.nix +++ b/nixos/modules/notnft.nix @@ -11,14 +11,34 @@ let listToAttrs optional filter - optionalString; + optionalString + ; cfg = config.networking.notnft; - jsonFormat = (pkgs.formats.json {}); -in -{ + jsonFormat = pkgs.formats.json {}; +in { options.networking.notnft = { enable = mkEnableOption "notnft"; + preStart = mkOption { + type = types.lines; + default = ""; + }; + + postStart = mkOption { + type = types.lines; + default = ""; + }; + + preStop = mkOption { + type = types.lines; + default = ""; + }; + + postStop = mkOption { + type = types.lines; + default = ""; + }; + preRules = mkOption { type = types.listOf jsonFormat.type; default = []; @@ -91,7 +111,15 @@ in reloadIfChanged = true; serviceConfig = let startScript = pkgs.writeShellScript "start-nft.sh" '' + ${cfg.preStart} ${pkgs.buildPackages.nftables}/bin/nft -j -f ${cfg.jsonFile} + ${cfg.postStart} + ''; + + stopScript = pkgs.writeShellScript "stop-nft.sh" '' + ${cfg.preStop} + ${optionalString cfg.flush "${pkgs.nftables}/bin/nft flush ruleset"} + ${cfg.postStop} ''; # rulesScript = pkgs.writeTextFile { # name = "nftables-rules"; @@ -116,7 +144,7 @@ in RemainAfterExit = true; ExecStart = startScript; ExecReload = startScript; - ExecStop = optionalString cfg.flush "${pkgs.nftables}/bin/nft flush ruleset"; + ExecStop = stopScript; }; }; };