Files
nixos-core/nixos/modules/dynu.nix
T
kiskaadee 1a50c8a668 feat(nixos): scaffold declarative NixOS appliance for homelab server
Phase 1 & 2 of the homelab-core NixOS migration plan.

Adds a fully standalone NixOS flake configuration targeting the
roadtotech.me homelab server. Goal: wipe & restore from any bare
NixOS install via `nixos-install --flake ~/Core#server`.

Changes:
- flake.nix: 2-input flake (nixpkgs channel tarball + sops-nix)
- flake.lock: generated lockfile pinning nixos-26.11pre1070770.8ce4ef6
- .sops.yaml: SOPS rules scoped to nixos/secrets.yaml (server + laptop keys)
- nixos/configuration.nix: clean standalone config; no HM, no desktop
  inheritance, no lib.mkForce; headless-optimised; SOPS set exactly once
- nixos/hardware-configuration.nix: copied from Config/hosts/server/
- nixos/secrets.yaml: copied SOPS-encrypted secrets from Config/hosts/server/
- nixos/modules/homeserver.nix: migrated; sops.defaultSopsFile removed
- nixos/modules/traefik-deployments.nix: migrated; sops.defaultSopsFile removed
- nixos/modules/dynu.nix: migrated; paths updated to ../scripts/
- nixos/scripts/monitor.py: dynu IP monitor Python script
- nixos/scripts/ddclient.conf: ddclient config template
- AGENTS.md: fix stale reference to deprecated desktop secrets path

Validated:
  nix flake check -> all checks passed
  nix build .#nixosConfigurations.server.config.system.build.toplevel \
    --no-link -> success (nixos-system-server-26.11.20260910.8ce4ef6)
2026-09-11 17:29:22 -05:00

89 lines
2.9 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 ../scripts/monitor.py);
in
{
# Define the keys to decrypt
sops.secrets."dynu/user" = { };
sops.secrets."dynu/domain" = { };
sops.secrets."dynu/password" = { };
sops.secrets."system/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 ../scripts/ddclient.conf);
};
# 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}" ];
};
};
# Disable ddclient's automatic timer.
# This stops ddclient from polling every 10 minutes on its own.
systemd.timers.ddclient.enable = false;
# 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";
ExecStart = "${ipMonitorScript}/bin/dynu-ip-monitor";
TimeoutSec = 30;
# Hardening
NoNewPrivileges = true;
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
ReadWritePaths = "/var/lib/dynu";
};
};
# Run the smart IP change monitor periodically (every 30 minutes)
systemd.timers.dynu-monitor = {
description = "Run Dynu IP monitor periodically";
timerConfig = {
OnBootSec = "5min";
OnUnitActiveSec = "30min";
AccuracySec = "1min";
Persistent = true;
};
wantedBy = [ "timers.target" ];
};
}