- Add `__pycache__` to `.gitignore`. - Refactor `monitor.py` to maintain a stateful round-robin resolver pool (`/var/lib/dynu/state.json`) across 5 external IP reflection endpoints. - Increase systemd timer frequency to 30 seconds (`OnUnitActiveSec = "30s"`) while maintaining zero DNS API overhead when IPs remain unchanged. - Enhance IP parsing with robust regex extraction and range filtering. - Update documentation in `docs/dynu-ip-monitor.md`. Rationale: Improves WAN IP rotation detection latency and resilience against provider downtime or rate limiting.
96 lines
3.0 KiB
Nix
96 lines
3.0 KiB
Nix
# 🌐 Dynu DDNS IP Monitor & Updater Service
|
|
# This NixOS module configures a smart, local-first IP change detector.
|
|
# It queries public IP providers, maintains a history log of WAN IP rotations,
|
|
# and triggers ddclient only when an actual change is detected to prevent API rate limits.
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
# Declaratively read the external Python script into the Nix store at evaluation time
|
|
ipMonitorScript = pkgs.writers.writePython3Bin "dynu-ip-monitor" { } (builtins.readFile ./monitor.py);
|
|
in
|
|
{
|
|
# 1. Load the encrypted secrets file
|
|
sops.defaultSopsFile = ./secrets.yaml;
|
|
sops.defaultSopsFormat = "yaml";
|
|
|
|
# Define the keys to decrypt
|
|
sops.secrets.dynu_user = { };
|
|
sops.secrets.dynu_domain = { };
|
|
sops.secrets.dynu_password = { };
|
|
sops.secrets.pdf_decrypt_password = {
|
|
owner = "kiskaadee";
|
|
};
|
|
|
|
# Assemble the ddclient config file securely at boot time in /run/secrets/
|
|
# Defaults to owner root:root (which avoids dynamic user evaluation issues)
|
|
sops.templates."ddclient.conf" = {
|
|
content = builtins.replaceStrings
|
|
[ "@dynu_user@" "@dynu_password@" "@dynu_domain@" ]
|
|
[
|
|
config.sops.placeholder.dynu_user
|
|
config.sops.placeholder.dynu_password
|
|
config.sops.placeholder.dynu_domain
|
|
]
|
|
(builtins.readFile ./ddclient.conf);
|
|
};
|
|
|
|
# 2. Enable ddclient and feed it the decrypted configuration template via systemd credentials
|
|
services.ddclient = {
|
|
enable = true;
|
|
configFile = "/run/credentials/ddclient.service/ddclient.conf";
|
|
};
|
|
|
|
systemd.services.ddclient = {
|
|
serviceConfig = {
|
|
# Securely load the root-owned secret file into the dynamic ddclient service
|
|
LoadCredential = [ "ddclient.conf:${config.sops.templates."ddclient.conf".path}" ];
|
|
};
|
|
};
|
|
|
|
# 3. Disable ddclient's automatic timer.
|
|
# This stops ddclient from polling every 10 minutes on its own.
|
|
systemd.timers.ddclient.enable = false;
|
|
|
|
# 4. Define the smart IP change detector service
|
|
systemd.services.dynu-monitor = {
|
|
description = "Dynu DDNS Smart IP Change Monitor";
|
|
after = [ "network-online.target" ];
|
|
wants = [ "network-online.target" ];
|
|
path = with pkgs; [
|
|
systemd # Required for systemctl
|
|
];
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
User = "root";
|
|
Group = "root";
|
|
StateDirectory = "dynu";
|
|
Environment = [
|
|
"HISTORY_FILE=/var/lib/dynu/ip_history.jsonl"
|
|
"STATE_FILE=/var/lib/dynu/state.json"
|
|
];
|
|
ExecStart = "${ipMonitorScript}/bin/dynu-ip-monitor";
|
|
TimeoutSec = 30;
|
|
|
|
# Hardening
|
|
NoNewPrivileges = true;
|
|
PrivateTmp = true;
|
|
ProtectSystem = "strict";
|
|
ProtectHome = true;
|
|
ReadWritePaths = "/var/lib/dynu";
|
|
};
|
|
};
|
|
|
|
# 5. Run the smart IP change monitor periodically (every 30 seconds)
|
|
systemd.timers.dynu-monitor = {
|
|
description = "Run Dynu IP monitor periodically";
|
|
timerConfig = {
|
|
OnBootSec = "10s";
|
|
OnUnitActiveSec = "30s";
|
|
AccuracySec = "1s";
|
|
Persistent = true;
|
|
};
|
|
wantedBy = [ "timers.target" ];
|
|
};
|
|
}
|