about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/networking/cloudflare-dyndns.nix
blob: 627fdb880a67f002c652cf78860856259e463533 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.services.cloudflare-dyndns;
in
{
  options = {
    services.cloudflare-dyndns = {
      enable = mkEnableOption (lib.mdDoc "Cloudflare Dynamic DNS Client");

      apiTokenFile = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = lib.mdDoc ''
          The path to a file containing the CloudFlare API token.

          The file must have the form `CLOUDFLARE_API_TOKEN=...`
        '';
      };

      domains = mkOption {
        type = types.listOf types.str;
        default = [ ];
        description = lib.mdDoc ''
          List of domain names to update records for.
        '';
      };

      proxied = mkOption {
        type = types.bool;
        default = false;
        description = lib.mdDoc ''
          Whether this is a DNS-only record, or also being proxied through CloudFlare.
        '';
      };

      ipv4 = mkOption {
        type = types.bool;
        default = true;
        description = lib.mdDoc ''
          Whether to enable setting IPv4 A records.
        '';
      };

      ipv6 = mkOption {
        type = types.bool;
        default = false;
        description = lib.mdDoc ''
          Whether to enable setting IPv6 AAAA records.
        '';
      };

      deleteMissing = mkOption {
        type = types.bool;
        default = false;
        description = lib.mdDoc ''
          Whether to delete the record when no IP address is found.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.cloudflare-dyndns = {
      description = "CloudFlare Dynamic DNS Client";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      startAt = "*:0/5";

      environment = {
        CLOUDFLARE_DOMAINS = toString cfg.domains;
      };

      serviceConfig = {
        Type = "simple";
        DynamicUser = true;
        StateDirectory = "cloudflare-dyndns";
        EnvironmentFile = cfg.apiTokenFile;
        ExecStart =
          let
            args = [ "--cache-file /var/lib/cloudflare-dyndns/ip.cache" ]
              ++ (if cfg.ipv4 then [ "-4" ] else [ "-no-4" ])
              ++ (if cfg.ipv6 then [ "-6" ] else [ "-no-6" ])
              ++ optional cfg.deleteMissing "--delete-missing"
              ++ optional cfg.proxied "--proxied";
          in
          "${pkgs.cloudflare-dyndns}/bin/cloudflare-dyndns ${toString args}";
      };
    };
  };
}