persiskv deployment
[myNix.git] / lbhost / modules / services / duckdns.nix
blobdf9e3b7962b5d25bb99930b1c546d523e2449b04
1 { config
2 , lib
3 , pkgs
4 , ...
5 }:
7 let
9   inherit (lib)
10     mkOption
11     mkIf
12     mdDoc
13     optionalString
14     types
15     ;
16   cfg = config.services.duckdns;
21   options.services.duckdns = {
23     enable = mkOption {
24       default = false;
25       type = types.bool;
26       description = mdDoc ''
27         Enable duckdns service to update dynamic dns of duckdns.
28       '';
29     };
31     interval = mkOption {
32       default = "10min";
33       type = types.str;
34       description = mdDoc ''
35         The interval at which to run the check and update.
36         See {command}`main 7 systemd.time` for the format.
37       '';
38     };
40     ipv4 = mkOption {
41       default = true;
42       type = types.bool;
43       description = mdDoc ''
44         Whether to use IPv4.
45       '';
46     };
48     ipv6 = mkOption {
49       default = false;
50       type = types.bool;
51       description = mdDoc ''
52         Whether to use IPv6.
53       '';
54     };
56     domain = mkOption {
57       default = "";
58       type = types.str;
59       description = mdDoc ''
60         The sub domain name to synchronize. See duckdns url format.
61       '';
62     };
64     token = mkOption {
65       default = "";
66       type = types.str;
67       description = mdDoc ''
68         The duckdns generated token for account identifying.
69       '';
70     };
72   };
74   config = mkIf cfg.enable {
76     assertions = [
77       {
78         assertion = cfg.domain != "";
79         message = "services.duckdns.domain shall not be empty!";
80       }
81       {
82         assertion = cfg.token != "";
83         message = "services.duckdns.token missing value!";
84       }
85     ];
87     systemd.services.duckdns-updater = {
88       script =
89         let
90           curl = "${pkgs.curl}/bin/curl";
91           get-ipv4-addr = optionalString cfg.ipv4
92             "$(${curl} -s \"https://api.ipify.org\")";
93           get-ipv6-addr = optionalString cfg.ipv6
94             "$(${curl} -s \"https://api6.ipify.org\")";
95         in
96         ''
97           set -eu
98           ipv4_addr=${get-ipv4-addr}
99           ipv6_addr=${get-ipv6-addr}
100           ${curl} -s "https://www.duckdns.org/update?domains=${cfg.domain}&token=${cfg.token}&ip=$ipv4_addr&ipv6=$ipv6_addr"
101         '';
102       serviceConfig = {
103         DynamicUser = true;
104         Type = "oneshot";
105       };
106     };
108     systemd.timers.duckdns-updater = {
109       description = "Update duckdns";
110       wantedBy = [ "timers.target" ];
111       timerConfig = {
112         OnBootSec = cfg.interval;
113         OnUnitInactiveSec = cfg.interval;
114         Unit = "duckdns-updater.service";
115       };
116     };
118   };