summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/urlwatch.nix
blob: 1a88de1a41705d2c6987f1e67d41ff1e3ab32e9d (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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
{ config, lib, pkgs, ... }:

# TODO multiple users
# TODO inform about unused caches
# cache = url: "${cfg.dataDir}/.urlwatch/cache/${hashString "sha1" url}"

with lib;
let
  cfg = config.krebs.urlwatch;

  # TODO assert sendmail's existence
  out = {
    options.krebs.urlwatch = api;
    config = lib.mkIf cfg.enable imp;
  };

  api = {
    enable = mkEnableOption "krebs.urlwatch";
    dataDir = mkOption {
      type = types.str;
      default = "/var/lib/urlwatch";
      description = ''
        Directory where the urlwatch service should store its state.
      '';
    };
    from = mkOption {
      type = types.str;
      default = "${user.name}@${config.networking.hostName}.r";
      description = ''
        Content of the From: header of the generated mails.
      '';
    };
    # TODO hooks :: attrsOf hook
    hooksFile = mkOption {
      type = with types; nullOr path;
      default = null;
      description = ''
        File to use as hooks.py module.
      '';
    };
    mailto = mkOption {
      type = types.str;
      default = config.krebs.build.user.mail;
      description = ''
        Content of the To: header of the generated mails. [AKA recipient :)]
      '';
    };
    onCalendar = mkOption {
      type = types.str;
      default = "04:23";
      description = ''
        Run urlwatch at this interval.
        The format is described in systemd.time(7), CALENDAR EVENTS.
      '';
    };
    sendmail.enable = mkEnableOption "krebs.urlwatch.sendmail" // {
      default = true;
    };
    telegram = {
      enable = mkEnableOption "krebs.urlwatch.telegram";
      botToken = mkOption {
        type = types.str;
      };
      chatId = mkOption {
        type = types.listOf types.str;
      };
    };
    urls = mkOption {
      type = with types; listOf (either str subtypes.job);
      default = [];
      description = "URL to watch.";
      example = [
        https://nixos.org/channels/nixos-unstable/git-revision
        { url = http://localhost ; filter = [ (grep "important.*stuff") ]; }
      ];
      apply = map (x: getAttr (typeOf x) {
        set = x;
        string.url = x;
      });
    };
    verbose = mkOption {
      type = types.bool;
      default = false;
      description = ''
        verbose output of urlwatch
      '';
    };
  };

  urlsFile = pkgs.writeText "urls"
    (concatMapStringsSep "\n---\n"
      (x: toJSON (filterAttrs (n: v: n != "_module") x)) cfg.urls);

  hooksFile = cfg.hooksFile;

  configFile = pkgs.writeJSON "urlwatch.yaml" {
    display = {
      error = true;
      new = true;
      unchanged = false;
    };
    report = {
      email = {
        enabled = false;
        from = "";
        html = false;
        smtp = {
          host = "localhost";
          keyring = true;
          port = 25;
          starttls = true;
        };
        subject = "{count} changes: {jobs}";
        to = "";
      };
      html.diff = "unified";
      stdout = {
        color = true;
        enabled = true;
      };
      ${if cfg.telegram.enable then "telegram" else null} = {
        enabled = cfg.telegram.enable;
        bot_token = cfg.telegram.botToken;
        chat_id = cfg.telegram.chatId;
      };
      text = {
        details = true;
        footer = true;
        line_length = 75;
      };
    };
  };

  imp = {
    systemd.timers.urlwatch = {
      wantedBy = [ "timers.target" ];
      timerConfig = {
        OnCalendar = cfg.onCalendar;
        Persistent = "true";
      };
    };
    systemd.services.urlwatch = {
      path = with pkgs; [
        coreutils
        gnused
        urlwatch
      ];
      environment = {
        HOME = cfg.dataDir;
        LC_ALL = "en_US.UTF-8";
        LOCALE_ARCHIVE = "${pkgs.glibcLocales}/lib/locale/locale-archive";
        SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
      };
      serviceConfig = {
        User = user.name;
        PermissionsStartOnly = "true";
        PrivateTmp = "true";
        SyslogIdentifier = "urlwatch";
        Type = "oneshot";
        ExecStart = pkgs.writeDash "urlwatch" ''
          set -euf

          cd /tmp

          urlwatch \
              ${optionalString cfg.verbose "-v"} \
              --config=${shell.escape configFile} \
              ${optionalString (hooksFile != null)
                "--hooks=${shell.escape hooksFile}"
              } \
              --urls=${shell.escape urlsFile} \
            > changes || :

          ${optionalString cfg.sendmail.enable /* sh */ ''
            if test -s changes; then
              {
                echo Date: $(date -R)
                echo From: ${shell.escape cfg.from}
                echo Subject: $(
                  sed -nr 's/^(CHANGED|ERROR|NEW): //p' changes |
                  sed '1!s/^  //'
                )
                echo To: ${shell.escape cfg.mailto}
                echo Mime-Version: 1.0
                echo Content-Type: text/plain\; charset=UTF-8
                echo Content-Transfer-Encoding: base64
                echo
                base64 changes
              } | /run/wrappers/bin/sendmail -t
            fi
          ''}
        '';
      };
    };
    users.users.${user.name} = {
      inherit (user) uid;
      home = cfg.dataDir;
      createHome = true;
      isSystemUser = true;
      group = user.name;
    };
    users.groups.${user.name} = {};
  };

  user = rec {
    name = "urlwatch";
    uid = genid_uint31 name;
  };

  subtypes.job = types.submodule {
    options = {
      url = mkOption {
        type = types.str;
      };
      filter = mkOption {
        default = null;
        type =
          with types;
          nullOr (either str (listOf (pkgs.formats.json {}).type));
      };
      ignore_cached = mkOption {
        default = null;
        type = with types; nullOr bool;
      };
    };
  };
in out