Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.he-dns;
cfgs = attrValues cfg;
perDomainConfig = {name, ...}: {
options = {
domain = mkOption {
type = types.str;
default = "";
description = ''
The Domain to configure HE DDNS for.
'';
};
keyfile = mkOption {
type = types.str;
description = ''
Path to a file containing the HE DDNS password.
'';
};
updateA = mkOption {
type = types.bool;
default = true;
description = ''
If to update the A record.
'';
};
updateAAAA = mkOption {
type = types.bool;
default = true;
description = ''
If to update the AAAA record.
'';
};
onCalendar = mkOption {
type = with types; nullOr str;
default = "*:00/10:00";
example = "*:00/5:00";
description = ''
OnCalendar value for the systemd-timer. See SYSTEMD.TIME(7).
'';
};
onBootSec = mkOption {
type = with types; nullOr str;
default = "1min";
example = "5min";
description = ''
OnBootSec value for the systemd-timer. See SYSTEMD.TIME(7).
'';
};
updateEndpoint = mkOption {
type = types.str;
default = "https://dyn.dns.he.net/nic/update";
description = ''
Update Url. Changes to this are very unlikely.
'';
};
};
config = {
domain = mkDefault name;
};
};
flattenList = l: builtins.foldl' (x: y: x//y) {} l;
ddnsV4Script = domainCfg: flags: ''
${pkgs.curl}/bin/curl --silent ${flags} "${domainCfg.updateEndpoint}" -d "hostname=${domainCfg.domain}" -d "password=$(cat ${domainCfg.keyfile})"
echo
'';
ddnsV6Script = domainCfg: flags: ''
# take the first global (should be routable) primary (to filter out privacy extension addresses) ipv6 address
myip="$(${pkgs.iproute2}/bin/ip -json -6 address show scope global primary | ${pkgs.jq}/bin/jq --raw-output '.[0].addr_info | map(.local | strings | select(startswith("fc") or startswith("fd") | not)) | .[0]')"
# ensure we have a valid v6 address
if ${pkgs.iproute2}/bin/ip route get "$myip" >/dev/null &>/dev/null
then
echo "Using IPv6 address $myip"
else
echo "No global primary ipv6 address available"
exit 1
fi
${pkgs.curl}/bin/curl --silent ${flags} "${domainCfg.updateEndpoint}" -d "hostname=${domainCfg.domain}" -d "password=$(cat ${domainCfg.keyfile})" -d "myip=$myip"
ddnsScript = domainCfg:
# ipv6 does ip detection which might fail, so run ipv4 first
(optionalString domainCfg.updateA (ddnsV4Script domainCfg "-4")) +
(optionalString domainCfg.updateAAAA (ddnsV6Script domainCfg "-6"));
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
ddnsService = domainCfg: mkIf (domainCfg.updateAAAA || domainCfg.updateA) {
"he-ddns-${domainCfg.domain}" = {
description = "Hurricane Electric DDNS Update Service";
requires = [ "network-online.target" ];
after = [ "network-online.target" ];
script = ddnsScript domainCfg;
};
};
ddnsTimer = domainCfg: mkIf (domainCfg.updateAAAA || domainCfg.updateA) {
"he-ddns-${domainCfg.domain}" = {
description = "Hurricane Electric DDNS Update Timer";
wantedBy = [ "multi-user.target" ];
requires = [ "network-online.target" ];
after = [ "network-online.target" ];
timerConfig = { Unit = "he-ddns-${domainCfg.domain}.service"; }
// (optionalAttrs (!isNull domainCfg.onBootSec) { OnBootSec = domainCfg.onBootSec; })
// (optionalAttrs (!isNull domainCfg.onCalendar) { OnCalendar = domainCfg.onCalendar; });
};
};
in {
options.services.he-dns = mkOption {
type = with types; loaOf (submodule perDomainConfig);
default = {};
description = ''
Timer for updating HE DDNS Records.
Documentation: https://dns.he.net/docs.html
'';
};
config = {
systemd.services = flattenList (map ddnsService cfgs);
systemd.timers = flattenList (map ddnsTimer cfgs);
};
}