Add interface and credentials path options for DDNS client

This commit is contained in:
Tim Schubert 2023-06-21 14:27:49 +02:00
parent 68702a27da
commit eed2c88bcc
Signed by: dadada
GPG key ID: EEB8D1CE62C4DFEA

View file

@ -5,17 +5,17 @@
}: }:
with lib; let with lib; let
cfg = config.dadada.ddns; cfg = config.dadada.ddns;
ddnsConfig = hostNames: { ddnsConfig = { domains, credentialsPath, interface }: {
systemd.timers = listToAttrs (forEach hostNames (hostname: systemd.timers = listToAttrs (forEach domains (domain:
nameValuePair "ddns-${hostname}" nameValuePair "ddns-${domain}"
{ {
wantedBy = [ "timers.target" ]; wantedBy = [ "timers.target" ];
partOf = [ "ddns-${hostname}.service" ]; partOf = [ "ddns-${domain}.service" ];
timerConfig.OnCalendar = "hourly"; timerConfig.OnCalendar = "hourly";
})); }));
systemd.services = listToAttrs (forEach hostNames (hostname: systemd.services = listToAttrs (forEach domains (domain:
nameValuePair "ddns-${hostname}" nameValuePair "ddns-${domain}"
{ {
serviceConfig.Type = "oneshot"; serviceConfig.Type = "oneshot";
script = '' script = ''
@ -24,13 +24,13 @@ with lib; let
} }
IFS=':' IFS=':'
read -r user password < /var/lib/ddns/credentials read -r user password < ${credentialsPath}
unset IFS unset IFS
curl_url=$(url "$user" "$password" ${hostname}) curl_url=$(url "$user" "$password" ${domain})
${pkgs.curl}/bin/curl -4 "$curl_url" ${pkgs.curl}/bin/curl -4 "$curl_url" ${if interface == null then "" else "--interface ${interface}"}
${pkgs.curl}/bin/curl -6 "$curl_url" ${pkgs.curl}/bin/curl -6 "$curl_url" ${if interface == null then "" else "--interface ${interface}"}
''; '';
})); }));
}; };
@ -47,7 +47,17 @@ in
''; '';
default = [ ]; default = [ ];
}; };
dadada.ddns.credentialsPath = mkOption {
type = types.path;
description = "Credentials file";
default = "/var/lib/ddns/credentials";
};
dadada.ddns.interface = mkOption {
type = types.nullOr types.str;
description = "Source interface to use";
default = null;
};
}; };
config = ddnsConfig cfg.domains; config = with cfg; ddnsConfig { inherit domains interface credentialsPath; };
} }