2020-04-24 23:36:52 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.ddclient;
|
|
|
|
boolToStr = bool: if bool then "yes" else "no";
|
|
|
|
dataDir = "/var/lib/ddclient";
|
2021-12-06 16:07:01 +00:00
|
|
|
StateDirectory = builtins.baseNameOf dataDir;
|
|
|
|
RuntimeDirectory = StateDirectory;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
configFile' = pkgs.writeText "ddclient.conf" ''
|
2020-04-24 23:36:52 +00:00
|
|
|
# This file can be used as a template for configFile or is automatically generated by Nix options.
|
|
|
|
cache=${dataDir}/ddclient.cache
|
|
|
|
foreground=YES
|
|
|
|
use=${cfg.use}
|
|
|
|
login=${cfg.username}
|
2022-07-14 12:49:19 +00:00
|
|
|
password=${if cfg.protocol == "nsupdate" then "/run/${RuntimeDirectory}/ddclient.key" else "@password_placeholder@"}
|
2020-04-24 23:36:52 +00:00
|
|
|
protocol=${cfg.protocol}
|
|
|
|
${lib.optionalString (cfg.script != "") "script=${cfg.script}"}
|
|
|
|
${lib.optionalString (cfg.server != "") "server=${cfg.server}"}
|
|
|
|
${lib.optionalString (cfg.zone != "") "zone=${cfg.zone}"}
|
|
|
|
ssl=${boolToStr cfg.ssl}
|
|
|
|
wildcard=YES
|
2021-07-14 22:03:04 +00:00
|
|
|
ipv6=${boolToStr cfg.ipv6}
|
2020-04-24 23:36:52 +00:00
|
|
|
quiet=${boolToStr cfg.quiet}
|
|
|
|
verbose=${boolToStr cfg.verbose}
|
|
|
|
${cfg.extraConfig}
|
|
|
|
${lib.concatStringsSep "," cfg.domains}
|
|
|
|
'';
|
2021-12-06 16:07:01 +00:00
|
|
|
configFile = if (cfg.configFile != null) then cfg.configFile else configFile';
|
|
|
|
|
|
|
|
preStart = ''
|
2023-04-12 12:48:02 +00:00
|
|
|
install --mode=600 --owner=$USER ${configFile} /run/${RuntimeDirectory}/ddclient.conf
|
2022-01-13 20:06:32 +00:00
|
|
|
${lib.optionalString (cfg.configFile == null) (if (cfg.protocol == "nsupdate") then ''
|
2023-04-12 12:48:02 +00:00
|
|
|
install --mode=600 --owner=$USER ${cfg.passwordFile} /run/${RuntimeDirectory}/ddclient.key
|
2022-01-13 20:06:32 +00:00
|
|
|
'' else if (cfg.passwordFile != null) then ''
|
2022-07-14 12:49:19 +00:00
|
|
|
"${pkgs.replace-secret}/bin/replace-secret" "@password_placeholder@" "${cfg.passwordFile}" "/run/${RuntimeDirectory}/ddclient.conf"
|
2021-12-06 16:07:01 +00:00
|
|
|
'' else ''
|
2022-07-14 12:49:19 +00:00
|
|
|
sed -i '/^password=@password_placeholder@$/d' /run/${RuntimeDirectory}/ddclient.conf
|
2021-12-06 16:07:01 +00:00
|
|
|
'')}
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
imports = [
|
|
|
|
(mkChangedOptionModule [ "services" "ddclient" "domain" ] [ "services" "ddclient" "domains" ]
|
|
|
|
(config:
|
|
|
|
let value = getAttrFromPath [ "services" "ddclient" "domain" ] config;
|
|
|
|
in if value != "" then [ value ] else []))
|
|
|
|
(mkRemovedOptionModule [ "services" "ddclient" "homeDir" ] "")
|
2021-12-06 16:07:01 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "ddclient" "password" ] "Use services.ddclient.passwordFile instead.")
|
2020-04-24 23:36:52 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.ddclient = with lib.types; {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Whether to synchronise your machine's IP address with a dynamic DNS provider (e.g. dyndns.org).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = package;
|
|
|
|
default = pkgs.ddclient;
|
2022-12-17 10:02:37 +00:00
|
|
|
defaultText = lib.literalExpression "pkgs.ddclient";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-12-06 16:07:01 +00:00
|
|
|
The ddclient executable package run by the service.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
domains = mkOption {
|
|
|
|
default = [ "" ];
|
|
|
|
type = listOf str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Domain name(s) to synchronize.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
username = mkOption {
|
2022-01-13 20:06:32 +00:00
|
|
|
# For `nsupdate` username contains the path to the nsupdate executable
|
|
|
|
default = lib.optionalString (config.services.ddclient.protocol == "nsupdate") "${pkgs.bind.dnsutils}/bin/nsupdate";
|
|
|
|
defaultText = "";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
User name.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
passwordFile = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = nullOr str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-01-13 20:06:32 +00:00
|
|
|
A file containing the password or a TSIG key in named format when using the nsupdate protocol.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
interval = mkOption {
|
|
|
|
default = "10min";
|
|
|
|
type = str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The interval at which to run the check and update.
|
2022-08-12 12:06:08 +00:00
|
|
|
See {command}`man 7 systemd.time` for the format.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
configFile = mkOption {
|
2021-12-06 16:07:01 +00:00
|
|
|
default = null;
|
|
|
|
type = nullOr path;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Path to configuration file.
|
2021-12-06 16:07:01 +00:00
|
|
|
When set this overrides the generated configuration from module options.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
example = "/root/nixos/secrets/ddclient.conf";
|
|
|
|
};
|
|
|
|
|
|
|
|
protocol = mkOption {
|
|
|
|
default = "dyndns2";
|
|
|
|
type = str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Protocol to use with dynamic DNS provider (see https://sourceforge.net/p/ddclient/wiki/protocols).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
server = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Server address.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
ssl = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-14 22:03:04 +00:00
|
|
|
Whether to use SSL/TLS to connect to dynamic DNS provider.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
ipv6 = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-14 22:03:04 +00:00
|
|
|
Whether to use IPv6.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
quiet = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Print no messages for unnecessary updates.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
script = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
script as required by some providers.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
use = mkOption {
|
|
|
|
default = "web, web=checkip.dyndns.com/, web-skip='Current IP Address: '";
|
|
|
|
type = str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Method to determine the IP address to send to the dynamic DNS provider.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
verbose = mkOption {
|
2022-05-18 14:49:53 +00:00
|
|
|
default = false;
|
2020-04-24 23:36:52 +00:00
|
|
|
type = bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Print verbose information.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
zone = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
zone as required by some providers.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = lines;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Extra configuration. Contents will be added verbatim to the configuration file.
|
2022-10-21 18:38:19 +00:00
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
`daemon` should not be added here because it does not work great with the systemd-timer approach the service uses.
|
|
|
|
:::
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf config.services.ddclient.enable {
|
|
|
|
systemd.services.ddclient = {
|
|
|
|
description = "Dynamic DNS Client";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
2021-12-06 16:07:01 +00:00
|
|
|
restartTriggers = optional (cfg.configFile != null) cfg.configFile;
|
2023-04-12 12:48:02 +00:00
|
|
|
path = lib.optional (lib.hasPrefix "if," cfg.use) pkgs.iproute2;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
serviceConfig = {
|
2020-04-24 23:36:52 +00:00
|
|
|
DynamicUser = true;
|
2021-12-06 16:07:01 +00:00
|
|
|
RuntimeDirectoryMode = "0700";
|
|
|
|
inherit RuntimeDirectory;
|
|
|
|
inherit StateDirectory;
|
2020-04-24 23:36:52 +00:00
|
|
|
Type = "oneshot";
|
2021-12-06 16:07:01 +00:00
|
|
|
ExecStartPre = "!${pkgs.writeShellScript "ddclient-prestart" preStart}";
|
|
|
|
ExecStart = "${lib.getBin cfg.package}/bin/ddclient -file /run/${RuntimeDirectory}/ddclient.conf";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.timers.ddclient = {
|
|
|
|
description = "Run ddclient";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnBootSec = cfg.interval;
|
|
|
|
OnUnitInactiveSec = cfg.interval;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|