~aasg/nixexprs

ref: b731c5cd290b73154a55dac3dbab485164a985ff nixexprs/modules/services/networking/dyndnsc.nix -rw-r--r-- 3.9 KiB
b731c5cd — Aluísio Augusto Silva Gonçalves dyndnsc: 0.5.1 -> 0.6.0 10 months ago
                                                                                
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{ config, lib, pkgs, ... }:

with lib;
let
  stateDir = "/run/dyndnsc";

  runtimeConfigFile = "${stateDir}/config.ini";

  cfg = config.services.dyndnsc;

  profileConfigs =
    mapAttrsToList
      (name: profile: ''
        [${name}]
        ${optionalString (profile.preset != "") "use_preset = ${profile.preset}"}
        updater-hostname = ${profile.hostname}
        updater-userid = ${profile.username}
        updater-password = %(password_${name})s
        ${profile.extraConfig}
      '')
      cfg.profiles;

  configFile = pkgs.writeText "dyndnsc.ini" ''
    [dyndnsc]
    configs = ${concatStringsSep ", " (attrNames cfg.profiles)}

    ${concatStrings profileConfigs}
  '';

in
{
  options = {
    services.dyndnsc = {
      enable = mkEnableOption "dyndnsc Dynamic DNS client";

      package = mkOption {
        default = pkgs.dyndnsc;
        defaultText = "pkgs.dyndnsc";
        type = types.package;
        description = "dyndnsc package to use.";
      };

      profiles = mkOption {
        type = types.attrsOf (types.submodule {
          options = {
            preset = mkOption {
              type = types.nullOr (types.enum [
                # Keep in sync with dyndnsc/resources/presets.ini in the source.
                "no-ip.com"
                "freedns.afraid.com"
                "nsupdate.info:ipv4"
                "nsupdate.info:ipv6"
                "dns.he.net"
                "dnsimple.com"
                "dnsdynamic.org"
                "hopper.pw:ipv4"
                "hopper.pw:ipv6"
                "dyn.com"
                "duckdns.org"
                "strato.com:ipv4"
                "strato.com:ipv6"
              ]);
              default = null;
              description = ''
                Preset profile to inherit.
              '';
              example = "dns.he.net";
            };
            hostname = mkOption {
              type = types.str;
              description = "Hostname or domain to be updated.";
              example = "dynamic.example.com";
            };
            username = mkOption {
              type = types.str;
              description = "Username to login with in the dynamic DNS service.";
            };
            passwordFile = mkOption {
              type = types.path;
              description = "Path to a file containing the service's password.";
              example = "/run/keys/dyndnsc-myprofile";
            };
            extraConfig = mkOption {
              type = types.lines;
              default = "";
              description = "Additional profile parameters.";
            };
          };
        });
        example = literalExample ''
          {
            "myhost_ip4" = {
              preset = "nsupdate.info:ipv4";
              hostname = "dynamic.example.com";
              username = "me";
              passwordFile = "/run/keys/dyndnsc-myhost_ip4";
            };
          };
        '';
        description = "Declarative profile config";
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.dyndnsc = {
      description = "Dynamic DNS client";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      stopIfChanged = true;
      serviceConfig = {
        ExecStart = ''${cfg.package}/bin/dyndnsc --verbose --loop --config "${runtimeConfigFile}"'';
        Restart = "on-failure";
        RestartIntervalSec = 30;
      };
      preStart =
        let
          profilePasswords = concatStrings (
            mapAttrsToList
              (name: profile: ''
                password_${name} = $(cat "${profile.passwordFile}")
              '')
              cfg.profiles
          );
        in
        ''
          mkdir -p "${stateDir}"
          install -m0600 /dev/null "${runtimeConfigFile}"
          cat "${configFile}" - >"${runtimeConfigFile}" <<EOF
          [DEFAULT]
          ${profilePasswords}
          EOF
        '';
      postStop = ''
        rm -f "${runtimeConfigFile}"
      '';
    };
  };
}