about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/web-apps/changedetection-io.nix
blob: bbf4c2aed1861175b7bbb5a6c06c9414d538ba07 (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
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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.changedetection-io;
in
{
  options.services.changedetection-io = {
    enable = mkEnableOption (lib.mdDoc "changedetection-io");

    user = mkOption {
      default = "changedetection-io";
      type = types.str;
      description = lib.mdDoc ''
        User account under which changedetection-io runs.
      '';
    };

    group = mkOption {
      default = "changedetection-io";
      type = types.str;
      description = lib.mdDoc ''
        Group account under which changedetection-io runs.
      '';
    };

    listenAddress = mkOption {
      type = types.str;
      default = "localhost";
      description = lib.mdDoc "Address the server will listen on.";
    };

    port = mkOption {
      type = types.port;
      default = 5000;
      description = lib.mdDoc "Port the server will listen on.";
    };

    datastorePath = mkOption {
      type = types.str;
      default = "/var/lib/changedetection-io";
      description = lib.mdDoc ''
        The directory used to store all data for changedetection-io.
      '';
    };

    baseURL = mkOption {
      type = types.nullOr types.str;
      default = null;
      example = "https://changedetection-io.example";
      description = lib.mdDoc ''
        The base url used in notifications and `{base_url}` token.
      '';
    };

    behindProxy = mkOption {
      type = types.bool;
      default = false;
      description = lib.mdDoc ''
        Enable this option when changedetection-io runs behind a reverse proxy, so that it trusts X-* headers.
        It is recommend to run changedetection-io behind a TLS reverse proxy.
      '';
    };

    environmentFile = mkOption {
      type = types.nullOr types.path;
      default = null;
      example = "/run/secrets/changedetection-io.env";
      description = lib.mdDoc ''
        Securely pass environment variabels to changedetection-io.

        This can be used to set for example a frontend password reproducible via `SALTED_PASS`
        which convinetly also deactivates nags about the hosted version.
        `SALTED_PASS` should be 64 characters long while the first 32 are the salt and the second the frontend password.
        It can easily be retrieved from the settings file when first set via the frontend with the following command:
        ``jq -r .settings.application.password /var/lib/changedetection-io/url-watches.json``
      '';
    };

    webDriverSupport = mkOption {
      type = types.bool;
      default = false;
      description = lib.mdDoc ''
        Enable support for fetching web pages using WebDriver and Chromium.
        This starts a headless chromium controlled by puppeteer in an oci container.

        ::: {.note}
        Playwright can currently leak memory.
        See https://github.com/dgtlmoon/changedetection.io/wiki/Playwright-content-fetcher#playwright-memory-leak
        :::
      '';
    };

    playwrightSupport = mkOption {
      type = types.bool;
      default = false;
      description = lib.mdDoc ''
        Enable support for fetching web pages using playwright and Chromium.
        This starts a headless Chromium controlled by puppeteer in an oci container.

        ::: {.note}
        Playwright can currently leak memory.
        See https://github.com/dgtlmoon/changedetection.io/wiki/Playwright-content-fetcher#playwright-memory-leak
        :::
      '';
    };

    chromePort = mkOption {
      type = types.port;
      default = 4444;
      description = lib.mdDoc ''
        A free port on which webDriverSupport or playwrightSupport listen on localhost.
      '';
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = !((cfg.webDriverSupport == true) && (cfg.playwrightSupport == true));
        message = "'services.changedetection-io.webDriverSupport' and 'services.changedetection-io.playwrightSupport' cannot be used together.";
      }
    ];

    systemd = let
      defaultStateDir = cfg.datastorePath == "/var/lib/changedetection-io";
    in {
      services.changedetection-io = {
        wantedBy = [ "multi-user.target" ];
        after = [ "network.target" ];
        preStart = ''
          mkdir -p ${cfg.datastorePath}
        '';
        serviceConfig = {
          User = cfg.user;
          Group = cfg.group;
          StateDirectory = mkIf defaultStateDir "changedetection-io";
          StateDirectoryMode = mkIf defaultStateDir "0750";
          WorkingDirectory = cfg.datastorePath;
          Environment = [ "HIDE_REFERER=true" ]
            ++ lib.optional (cfg.baseURL != null) "BASE_URL=${cfg.baseURL}"
            ++ lib.optional cfg.behindProxy "USE_X_SETTINGS=1"
            ++ lib.optional cfg.webDriverSupport "WEBDRIVER_URL=http://127.0.0.1:${toString cfg.chromePort}/wd/hub"
            ++ lib.optional cfg.playwrightSupport "PLAYWRIGHT_DRIVER_URL=ws://127.0.0.1:${toString cfg.chromePort}/?stealth=1&--disable-web-security=true";
          EnvironmentFile = mkIf (cfg.environmentFile != null) cfg.environmentFile;
          ExecStart = ''
            ${pkgs.changedetection-io}/bin/changedetection.py \
              -h ${cfg.listenAddress} -p ${toString cfg.port} -d ${cfg.datastorePath}
          '';
          ProtectHome = true;
          ProtectSystem = true;
          Restart = "on-failure";
        };
      };
      tmpfiles.rules = mkIf defaultStateDir [
        "d ${cfg.datastorePath} 0750 ${cfg.user} ${cfg.group} - -"
      ];
    };

    users = {
      users = optionalAttrs (cfg.user == "changedetection-io") {
        "changedetection-io" = {
          isSystemUser = true;
          group = "changedetection-io";
        };
      };

      groups = optionalAttrs (cfg.group == "changedetection-io") {
        "changedetection-io" = { };
      };
    };

    virtualisation = {
      oci-containers.containers = lib.mkMerge [
        (mkIf cfg.webDriverSupport {
          changedetection-io-webdriver = {
            image = "selenium/standalone-chrome";
            environment = {
              VNC_NO_PASSWORD = "1";
              SCREEN_WIDTH = "1920";
              SCREEN_HEIGHT = "1080";
              SCREEN_DEPTH = "24";
            };
            ports = [
              "127.0.0.1:${toString cfg.chromePort}:4444"
            ];
            volumes = [
              "/dev/shm:/dev/shm"
            ];
            extraOptions = [ "--network=bridge" ];
          };
        })

        (mkIf cfg.playwrightSupport {
          changedetection-io-playwright = {
            image = "browserless/chrome";
            environment = {
              SCREEN_WIDTH = "1920";
              SCREEN_HEIGHT = "1024";
              SCREEN_DEPTH = "16";
              ENABLE_DEBUGGER = "false";
              PREBOOT_CHROME = "true";
              CONNECTION_TIMEOUT = "300000";
              MAX_CONCURRENT_SESSIONS = "10";
              CHROME_REFRESH_TIME = "600000";
              DEFAULT_BLOCK_ADS = "true";
              DEFAULT_STEALTH = "true";
            };
            ports = [
              "127.0.0.1:${toString cfg.chromePort}:3000"
            ];
            extraOptions = [ "--network=bridge" ];
          };
        })
      ];
      podman.defaultNetwork.settings.dns_enabled = true;
    };
  };
}