init natpmp module thing

This commit is contained in:
xunuwu 2025-10-22 09:01:46 +02:00
parent aec7b8bdde
commit 6c46e5d5a7
Signed by: xun
SSH key fingerprint: SHA256:V3Mr/7NoQDU5RcVlyzUQTuAo3EiCWdg498yk6bfVf7E
3 changed files with 63 additions and 0 deletions

View file

@ -0,0 +1,57 @@
{
lib,
pkgs,
config,
...
}: let
cfg = config.own.natpmp-portforward;
in {
options.own.natpmp-portforward = {
enable = lib.mkEnableOption "enable natpmp port forwarding service";
mappings = lib.types.listOf (lib.types.submodule {
options = {
public = lib.mkOption {
type = lib.types.port;
};
private = lib.mkOption {
type = lib.types.port;
};
protocol = lib.mkOption {
default = "tcp";
type = lib.types.enum [
"tcp"
"udp"
];
};
};
});
};
config = lib.mkIf cfg.enable {
systemd.services.natpmp-portforward = {
requisite = ["network-online.target"];
serviceConfig = {
Restart = "on-failure";
ExecStart = pkgs.writeScript "natpmp-portforward" ''
#!${pkgs.bash}/bin/bash
"${lib.concatMapStrings (x: ''
${pkgs.libnatpmp}/bin/natpmpc -a ${x.public} ${x.private} ${x.protocol} 60
'')
cfg.mappings}"
'';
};
};
systemd.timers.natpmp-portforward = {
requires = ["network-online.target"];
wantedBy = ["timers.target"];
timerConfig = {
OnBootSec = "1m";
OnUnitActiveSec = "1m";
AccuracySec = "5s";
Unit = "natpmp-portforward.service";
};
};
};
}