summaryrefslogtreecommitdiff
path: root/module.nix
blob: b227085ed294ea43b266ebd569c776dd91294e8a (plain)
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
94
95
96
97
{
  config,
  lib,
  pkgs,
  ...
}:

let
  inherit (lib)
    getExe
    literalExpression
    mkEnableOption
    mkIf
    mkOption
    types
    ;

  cfg = config.services.ddnnss;

  ddnnss = pkgs.callPackage ./package.nix { };

  configFile = pkgs.writeText "ddnnss.json" (builtins.toJSON cfg.settings);
in
{
  options.services.ddnnss = {
    enable = mkEnableOption "ddnnss";

    interval = mkOption {
      type = types.str;
      default = "5min";
      description = ''
        How often to run an update cycle.
      '';
    };

    settings = mkOption {
      type = types.attrs;
      description = ''
        The ddnnss configuration.
        Any key values expand environment variables.
      '';
      example = literalExpression ''
        {
          records = [{
            zone = "rnrd.eu";
            name = "silly";
            type = "AAAA";
            provider = "bunny-rnrd";
            source = "icanhazip";
          }];
          providers.bunny-rnrd = {
            kind = "bunny";
            keyFile = "$CREDENTIALS_DIRECTORY/bunny-dns";
          };
          sources.icanhazip = { kind = "icanhazip"; };
        }
      '';
    };

    credentials = mkOption {
      type = types.attrsOf types.path;
      default = { };
      description = ''
        Secrets exposed to ddnnss during the cycle, as systemd credentials.
      '';
      example = literalExpression ''
        { bunny-dns = config.age.secrets.bunny-dns.path; }
      '';
    };
  };

  config = mkIf cfg.enable {
    systemd.services.ddnnss = {
      description = "ddnnss dynamic dns update cycle";
      after = [ "network-online.target" ];
      wants = [ "network-online.target" ];

      serviceConfig = {
        Type = "oneshot";
        DynamicUser = true;
        LoadCredential = lib.mapAttrsToList
          (name: path: "${name}:${path}") cfg.credentials;
        ExecStart = "${getExe ddnnss} -config ${configFile}";
      };
    };

    systemd.timers.ddnnss = {
      wantedBy = [ "timers.target" ];
      timerConfig = {
        # wait 2 minutes from the start, with 30 second jitter.
        OnBootSec = "2min";
        RandomizedDelaySec = "30s";
        OnUnitActiveSec = cfg.interval;
      };
    };
  };
}