1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.cloudflare-dyndns;
7in
8{
9 options = {
10 services.cloudflare-dyndns = {
11 enable = mkEnableOption (lib.mdDoc "Cloudflare Dynamic DNS Client");
12
13 apiTokenFile = mkOption {
14 type = types.nullOr types.str;
15 default = null;
16 description = lib.mdDoc ''
17 The path to a file containing the CloudFlare API token.
18
19 The file must have the form `CLOUDFLARE_API_TOKEN=...`
20 '';
21 };
22
23 domains = mkOption {
24 type = types.listOf types.str;
25 default = [ ];
26 description = lib.mdDoc ''
27 List of domain names to update records for.
28 '';
29 };
30
31 proxied = mkOption {
32 type = types.bool;
33 default = false;
34 description = lib.mdDoc ''
35 Whether this is a DNS-only record, or also being proxied through CloudFlare.
36 '';
37 };
38
39 ipv4 = mkOption {
40 type = types.bool;
41 default = true;
42 description = lib.mdDoc ''
43 Whether to enable setting IPv4 A records.
44 '';
45 };
46
47 ipv6 = mkOption {
48 type = types.bool;
49 default = false;
50 description = lib.mdDoc ''
51 Whether to enable setting IPv6 AAAA records.
52 '';
53 };
54
55 deleteMissing = mkOption {
56 type = types.bool;
57 default = false;
58 description = lib.mdDoc ''
59 Whether to delete the record when no IP address is found.
60 '';
61 };
62 };
63 };
64
65 config = mkIf cfg.enable {
66 systemd.services.cloudflare-dyndns = {
67 description = "CloudFlare Dynamic DNS Client";
68 after = [ "network.target" ];
69 wantedBy = [ "multi-user.target" ];
70 startAt = "*:0/5";
71
72 environment = {
73 CLOUDFLARE_DOMAINS = toString cfg.domains;
74 };
75
76 serviceConfig = {
77 Type = "simple";
78 DynamicUser = true;
79 StateDirectory = "cloudflare-dyndns";
80 EnvironmentFile = cfg.apiTokenFile;
81 ExecStart =
82 let
83 args = [ "--cache-file /var/lib/cloudflare-dyndns/ip.cache" ]
84 ++ (if cfg.ipv4 then [ "-4" ] else [ "-no-4" ])
85 ++ (if cfg.ipv6 then [ "-6" ] else [ "-no-6" ])
86 ++ optional cfg.deleteMissing "--delete-missing"
87 ++ optional cfg.proxied "--proxied";
88 in
89 "${pkgs.cloudflare-dyndns}/bin/cloudflare-dyndns ${toString args}";
90 };
91 };
92 };
93}