burnham: Add domeneshop-dyndns, make it a module

This commit is contained in:
2024-09-08 01:22:36 +02:00
parent 162134d951
commit 7cd7596d66
7 changed files with 108 additions and 26 deletions

View File

@@ -0,0 +1,45 @@
{ config, pkgs, lib, ... }:
let
cfg = config.services.domeneshop-dyndns;
in {
options.services.domeneshop-dyndns = {
enable = lib.mkEnableOption "Domeneshop DynDNS";
domain = lib.mkOption {
type = lib.types.str;
description = "Domain name to configure";
};
environmentFile = lib.mkOption {
type = lib.types.path;
description = "Path to the file that sets DDNS_TOKEN and DDNS_SERET from https://www.domeneshop.no/admin?view=api";
};
startAt = lib.mkOption {
type = lib.types.str;
default = "*/10 * * * *";
description = "Systemd onCalendar expression for when to run the timer";
};
};
config = lib.mkIf cfg.enable {
systemd.services.domeneshop-dyndns = {
serviceConfig.EnvironmentFile = cfg.environmentFile;
startAt = cfg.startAt;
script = ''
DNSNAME="${cfg.domain}"
NEW_IP="$(${lib.getExe pkgs.curl} --silent https://ipinfo.io/ip)"
OLD_IP="$(${lib.getExe pkgs.getent} hosts "$DNSNAME" | ${lib.getExe pkgs.gawk} '{ print $1 }')"
if [[ "$NEW_IP" != "$OLD_IP" ]]; then
echo "Old IP ($OLD_IP) does not match new IP ($NEW_IP), updating..."
${lib.getExe pkgs.curl} --silent "https://$DDNS_TOKEN:$DDNS_SECRET@api.domeneshop.no/v0/dyndns/update?hostname=$DNSNAME&myip=$NEW_IP"
else
echo "Old IP ($OLD_IP) matches new IP ($NEW_IP), exiting..."
fi
'';
};
};
}