diff --git a/profiles/domeneshop-dyndns.nix b/profiles/domeneshop-dyndns.nix index 7440f02..84a4322 100644 --- a/profiles/domeneshop-dyndns.nix +++ b/profiles/domeneshop-dyndns.nix @@ -5,10 +5,12 @@ in { # auto domain update + # TODO: ensure dns64 does not interfere with this + options = with lib; { services.domeneshop-updater.targets = mkOption { type = with types; listOf str; - #default = [ config.networking.fqdn ]; + example = [ config.networking.fqdn ]; }; }; @@ -33,7 +35,7 @@ in serviceConfig = let prog = pkgs.writeShellApplication { name = "domeneshop-dyndns-updater.sh"; - runtimeInputs = with pkgs; [ curl yq ]; + runtimeInputs = with pkgs; [ curl iproute2 jq ]; text = '' test -s /run/secrets/domeneshop/token || { >&2 echo "ERROR: /run/secrets/domeneshop/token not found!" @@ -45,8 +47,29 @@ in } DOMENESHOP_TOKEN="$( cat /run/secrets/domeneshop/token)" DOMENESHOP_SECRET="$(cat /run/secrets/domeneshop/secret)" + + # get stable ipv6 addr, fallback to ipv4, fallback to curl default + IF=$( + ip -6 -json addr show scope global -temporary \ + | jq '.[]| select(.ifname|contains("docker")|not) | .addr_info[].local | select(.==null|not)' -r \ + | head -n1 + ) + if [[ -z "$IF" ]]; then + IF=$( + ip -4 -json addr show scope global -temporary \ + | jq '.[]| select(.ifname|contains("docker")|not) | .addr_info[].local | select(.==null|not)' -r \ + | head -n1 + ) + fi + if [[ -n "$IF" ]]; then + IF="--interface $IF" + else + IF="" + fi + ${lib.concatMapStringsSep "\n" (target: '' - curl https://"$DOMENESHOP_TOKEN":"$DOMENESHOP_SECRET"@api.domeneshop.no/v0/dyndns/update?hostname="${target}" + # shellcheck disable=SC2086 + curl $IF https://"$DOMENESHOP_TOKEN":"$DOMENESHOP_SECRET"@api.domeneshop.no/v0/dyndns/update?hostname="${target}" '') cfg.targets} ''; };