summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/web-apps/szurubooru.nix
blob: f69b7e3fc1f73f5ef6df650b097d8aecbe532f31 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
{
  config,
  pkgs,
  lib,
  ...
}:

let
  cfg = config.services.szurubooru;
  inherit (lib)
    mkOption
    mkEnableOption
    mkIf
    mkPackageOption
    types
    ;
  format = pkgs.formats.yaml { };
  python = pkgs.python3;
in

{
  options = {
    services.szurubooru = {
      enable = mkEnableOption "Szurubooru, an image board engine dedicated for small and medium communities";

      user = mkOption {
        type = types.str;
        default = "szurubooru";
        description = ''
          User account under which Szurubooru runs.
        '';
      };

      group = mkOption {
        type = types.str;
        default = "szurubooru";
        description = ''
          Group under which Szurubooru runs.
        '';
      };

      dataDir = mkOption {
        type = types.path;
        default = "/var/lib/szurubooru";
        example = "/var/lib/szuru";
        description = ''
          The path to the data directory in which Szurubooru will store its data.
        '';
      };

      openFirewall = mkOption {
        type = types.bool;
        default = false;
        example = true;
        description = ''
          Whether to open the firewall for the port in {option}`services.szurubooru.server.port`.
        '';
      };

      server = {
        package = mkPackageOption pkgs [
          "szurubooru"
          "server"
        ] { };

        port = mkOption {
          type = types.port;
          default = 8080;
          example = 9000;
          description = ''
            Port to expose HTTP service.
          '';
        };

        host = lib.mkOption {
          type = types.str;
          default = "127.0.0.1";
          example = "0.0.0.0";
          description = "The host address for Szurubooru to bind to.";
        };

        threads = mkOption {
          type = types.int;
          default = 4;
          example = 6;
          description = "Number of waitress threads to start.";
        };

        settings = mkOption {
          type = types.submodule {
            freeformType = format.type;
            options = {
              name = mkOption {
                type = types.str;
                default = "szurubooru";
                example = "Szuru";
                description = "Name shown in the website title and on the front page.";
              };

              domain = mkOption {
                type = types.str;
                example = "http://example.com";
                description = "Full URL to the homepage of this szurubooru site (with no trailing slash).";
              };

              # NOTE: this is not a real upstream option
              secretFile = mkOption {
                type = types.path;
                example = "/run/secrets/szurubooru-server-secret";
                description = ''
                  File containing a secret used to salt the users' password hashes and generate filenames for static content.
                '';
              };

              delete_source_files = mkOption {
                type = types.enum [
                  "yes"
                  "no"
                ];
                default = "no";
                example = "yes";
                description = "Whether to delete thumbnails and source files on post delete.";
              };

              smtp = {
                host = mkOption {
                  type = types.nullOr types.str;
                  default = null;
                  example = "localhost";
                  description = "Host of the SMTP server used to send reset password.";
                };

                port = mkOption {
                  type = types.nullOr types.port;
                  default = null;
                  example = 25;
                  description = "Port of the SMTP server.";
                };

                user = mkOption {
                  type = types.nullOr types.str;
                  default = null;
                  example = "bot";
                  description = "User to connect to the SMTP server.";
                };

                # NOTE: this is not a real upstream option
                passFile = mkOption {
                  type = types.nullOr types.path;
                  default = null;
                  example = "/run/secrets/szurubooru-smtp-pass";
                  description = "File containing the password associated to the given user for the SMTP server.";
                };
              };

              data_url = mkOption {
                type = types.str;
                default = "${cfg.server.settings.domain}/data/";
                defaultText = lib.literalExpression ''"''${services.szurubooru.server.settings.domain}/data/"'';
                example = "http://example.com/content/";
                description = "Full URL to the data endpoint.";
              };

              data_dir = mkOption {
                type = types.path;
                default = "${cfg.dataDir}/data";
                defaultText = lib.literalExpression ''"''${services.szurubooru.dataDir}/data"'';
                example = "/srv/szurubooru/data";
                description = "Path to the static files.";
              };

              debug = mkOption {
                type = types.int;
                default = 0;
                example = 1;
                description = "Whether to generate server logs.";
              };

              show_sql = mkOption {
                type = types.int;
                default = 0;
                example = 1;
                description = "Whether to show SQL in server logs.";
              };
            };
          };
          description = ''
            Configuration to write to {file}`config.yaml`.
            See <https://github.com/rr-/szurubooru/blob/master/server/config.yaml.dist> for more information.
          '';
        };
      };

      client = {
        package = mkPackageOption pkgs [
          "szurubooru"
          "client"
        ] { };
      };

      database = {
        host = mkOption {
          type = types.str;
          default = "localhost";
          example = "192.168.1.2";
          description = "Host on which the PostgreSQL database runs.";
        };

        port = mkOption {
          type = types.port;
          default = 5432;
          description = "The port under which PostgreSQL listens to.";
        };

        name = mkOption {
          type = types.str;
          default = cfg.database.user;
          defaultText = lib.literalExpression "szurubooru.database.name";
          example = "szuru";
          description = "Name of the PostgreSQL database.";
        };

        user = mkOption {
          type = types.str;
          default = "szurubooru";
          example = "szuru";
          description = "PostgreSQL user.";
        };

        passwordFile = mkOption {
          type = types.path;
          example = "/run/secrets/szurubooru-db-password";
          description = "A file containing the password for the PostgreSQL user.";
        };
      };
    };
  };

  config = mkIf cfg.enable {

    networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.server.port ];

    users.groups = mkIf (cfg.group == "szurubooru") {
      szurubooru = { };
    };

    users.users = mkIf (cfg.user == "szurubooru") {
      szurubooru = {
        group = cfg.group;
        description = "Szurubooru Daemon user";
        isSystemUser = true;
      };
    };

    systemd.services.szurubooru =
      let
        configFile = format.generate "config.yaml" (
          lib.pipe cfg.server.settings [
            (
              settings:
              lib.recursiveUpdate settings {
                secretFile = null;
                secret = "$SZURUBOORU_SECRET";

                smtp.pass = if settings.smtp.passFile != null then "$SZURUBOORU_SMTP_PASS" else null;
                smtp.passFile = null;
                smtp.enable = null;

                database = "postgresql://${cfg.database.user}:$SZURUBOORU_DATABASE_PASSWORD@${cfg.database.host}:${toString cfg.database.port}/${cfg.database.name}";
              }
            )
            (lib.filterAttrsRecursive (_: x: x != null))
          ]
        );
      in
      {
        description = "Server of Szurubooru, an image board engine dedicated for small and medium communities";

        wantedBy = [
          "multi-user.target"
          "szurubooru-client.service"
        ];
        before = [ "szurubooru-client.service" ];
        after = [
          "network.target"
          "network-online.target"
        ];
        wants = [ "network-online.target" ];

        path = with pkgs; [
          ffmpeg-full
        ];

        script = ''
          export SZURUBOORU_SECRET="$(<$CREDENTIALS_DIRECTORY/secret)"
          export SZURUBOORU_DATABASE_PASSWORD="$(<$CREDENTIALS_DIRECTORY/database)"
          ${lib.optionalString (cfg.server.settings.smtp.passFile != null) ''
            export SZURUBOORU_SMTP_PASS=$(<$CREDENTIALS_DIRECTORY/smtp)
          ''}
          install -m0640 ${cfg.server.package.src}/config.yaml.dist ${cfg.dataDir}/config.yaml.dist
          touch ${cfg.dataDir}/config.yaml
          chmod 0640 ${cfg.dataDir}/config.yaml
          ${lib.getExe pkgs.envsubst} -i ${configFile} -o ${cfg.dataDir}/config.yaml
          sed 's|script_location = |script_location = ${cfg.server.package.src}/|' ${cfg.server.package.src}/alembic.ini > ${cfg.dataDir}/alembic.ini
          ${lib.getExe cfg.server.package.alembic} upgrade head
          ${lib.getExe cfg.server.package.waitress} --host ${cfg.server.host} --port ${toString cfg.server.port} --threads ${toString cfg.server.threads} szurubooru.facade:app
        '';

        serviceConfig = {
          LoadCredential = [
            "secret:${cfg.server.settings.secretFile}"
            "database:${cfg.database.passwordFile}"
          ]
          ++ (lib.optionals (cfg.server.settings.smtp.passFile != null) [
            "smtp:${cfg.server.settings.smtp.passFile}"
          ]);

          User = cfg.user;
          Group = cfg.group;

          Type = "simple";
          Restart = "on-failure";

          StateDirectory = mkIf (cfg.dataDir == "/var/lib/szurubooru") "szurubooru";
          WorkingDirectory = cfg.dataDir;
        };
      };
  };

  meta = {
    maintainers = with lib.maintainers; [ ratcornu ];
    doc = ./szurubooru.md;
  };
}