@@ -0,0 +1,70 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.miniupnpd;
7+ configFile = pkgs.writeText "miniupnpd.conf" ''
8+ ext_ifname=${cfg.externalInterface}
9+ enable_natpmp=${if cfg.natpmp then "yes" else "no"}
10+ enable_upnp=${if cfg.upnp then "yes" else "no"}
11+
12+ ${concatMapStrings (range: ''
13+ listening_ip=${range}
14+ '') cfg.internalIPs}
15+
16+ ${cfg.appendConfig}
17+ '';
18+in
19+{
20+ options = {
21+ services.miniupnpd = {
22+ enable = mkEnableOption "MiniUPnP daemon";
23+
24+ externalInterface = mkOption {
25+ type = types.str;
26+ description = ''
27+ Name of the external interface.
28+ '';
29+ };
30+
31+ internalIPs = mkOption {
32+ type = types.listOf types.str;
33+ example = [ "192.168.1.0/24" ];
34+ description = ''
35+ The IP address ranges to listen on.
36+ '';
37+ };
38+
39+ natpmp = mkEnableOption "NAT-PMP support";
40+
41+ upnp = mkOption {
42+ default = true;
43+ type = types.bool;
44+ description = ''
45+ Whether to enable UPNP support.
46+ '';
47+ };
48+
49+ appendConfig = mkOption {
50+ type = types.lines;
51+ default = "";
52+ description = ''
53+ Configuration lines appended to the MiniUPnP config.
54+ '';
55+ };
56+ };
57+ };
58+
59+ config = mkIf cfg.enable {
60+ systemd.services.miniupnpd = {
61+ description = "MiniUPnP daemon";
62+ after = [ "network.target" ];
63+ wantedBy = [ "multi-user.target" ];
64+ path = [ pkgs.miniupnpd ];
65+ serviceConfig = {
66+ ExecStart = "${pkgs.miniupnpd}/bin/miniupnpd -d -f ${configFile}";
67+ };
68+ };
69+ };
70+}