1 { pkgs, lib, config, inputs, hosts, hostName, ... }:
3 inherit (config.users) users groups;
4 inherit (hosts.mermet.config.networking) domain;
7 # TODO: nsupdate in the initrd
8 systemd.services.nsupdate = {
9 wantedBy = [ "multi-user.target" ];
10 startAt = "*:0/5"; # every 5 min
13 LoadCredentialEncrypted = [
14 "${hostName}.${domain}.tsig:${./nsupdate +"/${domain}/tsig.cred"}"
16 ExecStart = pkgs.writeShellScript "nsupdate" ''
18 publicIPv4=$(${pkgs.curl}/bin/curl -s4 https://whoami.sourcephile.fr/addr ||
19 ${pkgs.curl}/bin/curl -s4L https://icanhazip.com || true)
20 publicIPv6=$(${pkgs.curl}/bin/curl -s6L https://icanhazip.com || true)
21 privateIPv4=$(${pkgs.miniupnpc}/bin/upnpc -s | sed -ne 's/^Local LAN ip address : //p')
22 ${pkgs.knot-dns}/bin/knsupdate -k $CREDENTIALS_DIRECTORY/${hostName}.${domain}.tsig <<EOF
26 update delete ${hostName} A
27 ''${publicIPv4:+update add ${hostName} 300 A $publicIPv4}
28 update delete ${hostName} AAAA
29 ''${publicIPv6:+update add ${hostName} 300 AAAA $publicIPv6}
30 update delete lan.${hostName} A
31 ''${privateIPv4:+update add lan.${hostName} 300 A $privateIPv4}
36 Restart = "on-failure";
39 User = users."nsupdate".name;
42 users.users."nsupdate" = {
44 group = groups."nsupdate".name;
46 users.groups."nsupdate" = { };
47 networking.nftables.ruleset = ''
54 udp dport @nsupdate-ssdp counter accept comment "SSDP answer"
57 skuid ${users.nsupdate.name} \
58 ip daddr ${hosts.mermet._module.args.ipv4} \
59 meta l4proto { udp, tcp } th dport domain \
60 counter accept comment "nsupdate: DNS"
61 skuid ${users.nsupdate.name} \
64 comment "SSDP automatic opening"
65 skuid ${users.nsupdate.name} \
66 ip daddr 239.255.255.250 udp dport ssdp \
67 set add udp sport @nsupdate-ssdp \
68 comment "SSDP automatic opening"
69 skuid ${users.nsupdate.name} \
70 ip daddr 239.255.255.250 udp dport ssdp \
71 counter accept comment "SSDP"
74 '' + lib.optionalString config.networking.enableIPv6 ''
77 skuid ${users.nsupdate.name} \
78 ip6 daddr { FF02::C, FF05::C, FF08::C, FF0E::C } \
80 set add udp sport @nsupdate-ssdp \
81 comment "SSDP automatic opening"
82 skuid ${users.nsupdate.name} \
83 ip6 daddr { FF02::C, FF05::C, FF08::C, FF0E::C } \
85 counter accept comment "SSDP"