Add ionos-dyndns module for the NixOS config

This commit is contained in:
Paul Brinkmeier 2023-11-19 03:05:31 +01:00
parent ea38d94178
commit e61a07f8d3

View File

@ -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;
};
};
};
};
}