From e61a07f8d3757631d4cc00e0e5c868f9150cf252 Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Sun, 19 Nov 2023 03:05:31 +0100 Subject: [PATCH] Add ionos-dyndns module for the NixOS config --- nix/modules/ionos-dyndns.nix | 118 +++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 nix/modules/ionos-dyndns.nix diff --git a/nix/modules/ionos-dyndns.nix b/nix/modules/ionos-dyndns.nix new file mode 100644 index 0000000..5994c16 --- /dev/null +++ b/nix/modules/ionos-dyndns.nix @@ -0,0 +1,118 @@ +{ config, lib, pkgs, ... }: +with lib; +let + cfg = config.services.ionos-dyndns; + ionos-dyndns = pkgs.callPackage ../packages/ionos-dyndns.nix {}; + + command = lib.concatStringsSep " " ( + [ + "${ionos-dyndns}/bin/ionos-dyndns" + "--api-prefix" + "$(cat ${cfg.apiPrefixPath})" + "--api-secret" + "$(cat ${cfg.apiSecretPath})" + "--fqdn" + cfg.fqdn + "--interface" + cfg.interface + ] + ++ lib.optionals cfg.a [ "--A" ] + ++ lib.optionals cfg.aaaa [ "--AAAA" ] + ); +in { + options = { + services.ionos-dyndns = { + enable = mkOption { + type = types.bool; + default = false; + description = '' + Whether to turn on the IONOS DynDNS timer. + ''; + }; + user = mkOption { + type = types.str; + default = "ionos-dyndns"; + }; + apiPrefixPath = mkOption { + type = types.path; + description = '' + Path of a file holding the API prefix. + ''; + }; + apiSecretPath = mkOption { + type = types.path; + description = '' + Path of a file holding the API secret. + ''; + }; + a = mkOption { + type = types.bool; + default = false; + description = '' + Whether to set the A record (IPv4). + ''; + }; + aaaa = mkOption { + type = types.bool; + default = false; + description = '' + Whether to set the AAAA record (IPv6). + ''; + }; + fqdn = mkOption { + type = types.str; + description = '' + Fully qualified domain name for this host. + ''; + }; + interface = mkOption { + type = types.str; + description = '' + Interface to get the IP address from. + ''; + }; + interval = mkOption { + type = types.str; + default = "14m"; + description = "How often to run the update script in systemd.timers notation."; + }; + serviceName = mkOption { + type = types.str; + default = "ionos-dyndns"; + }; + }; + }; + config = mkIf cfg.enable { + users = { + users = { + ${cfg.user} = { + isSystemUser = true; + group = cfg.user; + description = "IONOS DynDNS user."; + }; + }; + groups = { + ${cfg.user} = { + }; + }; + }; + systemd = { + services.${cfg.serviceName} = { + serviceConfig = { + Type = "oneshot"; + User = cfg.user; + # We assume that command doesn't contain any single quotes + ExecStart = "${pkgs.bash}/bin/bash -c '${command}'"; + }; + }; + timers.${cfg.serviceName} = { + wantedBy = [ "timers.target" ]; + timerConfig = { + Unit = "${cfg.serviceName}.service"; + OnBootSec = "30s"; + OnActiveSec = cfg.interval; + }; + }; + }; + }; +}