summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/restya-board.nix
blob: 0bfa2368787ffc5ed83d9b7092928527d89456c0 (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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
{ config, lib, pkgs, ... }:

with lib;

# TODO: are these php-packages needed?
#imagick
#php-geoip -> php.ini: extension = geoip.so
#expat

let
  cfg = config.services.restya-board;
  fpm = config.services.phpfpm.pools.${poolName};

  runDir = "/run/restya-board";

  poolName = "restya-board";

in

{

  ###### interface

  options = {

    services.restya-board = {

      enable = mkEnableOption "restya-board";

      dataDir = mkOption {
        type = types.path;
        default = "/var/lib/restya-board";
        description = ''
          Data of the application.
        '';
      };

      user = mkOption {
        type = types.str;
        default = "restya-board";
        description = ''
          User account under which the web-application runs.
        '';
      };

      group = mkOption {
        type = types.str;
        default = "nginx";
        description = ''
          Group account under which the web-application runs.
        '';
      };

      virtualHost = {
        serverName = mkOption {
          type = types.str;
          default = "restya.board";
          description = ''
            Name of the nginx virtualhost to use.
          '';
        };

        listenHost = mkOption {
          type = types.str;
          default = "localhost";
          description = ''
            Listen address for the virtualhost to use.
          '';
        };

        listenPort = mkOption {
          type = types.int;
          default = 3000;
          description = ''
            Listen port for the virtualhost to use.
          '';
        };
      };

      database = {
        host = mkOption {
          type = types.nullOr types.str;
          default = null;
          description = ''
            Host of the database. Leave 'null' to use a local PostgreSQL database.
            A local PostgreSQL database is initialized automatically.
          '';
        };

        port = mkOption {
          type = types.nullOr types.int;
          default = 5432;
          description = ''
            The database's port.
          '';
        };

        name = mkOption {
          type = types.str;
          default = "restya_board";
          description = ''
            Name of the database. The database must exist.
          '';
        };

        user = mkOption {
          type = types.str;
          default = "restya_board";
          description = ''
            The database user. The user must exist and have access to
            the specified database.
          '';
        };

        passwordFile = mkOption {
          type = types.nullOr types.path;
          default = null;
          description = ''
            The database user's password. 'null' if no password is set.
          '';
        };
      };

      email = {
        server = mkOption {
          type = types.nullOr types.str;
          default = null;
          example = "localhost";
          description = ''
            Hostname to send outgoing mail. Null to use the system MTA.
          '';
        };

        port = mkOption {
          type = types.int;
          default = 25;
          description = ''
            Port used to connect to SMTP server.
          '';
        };

        login = mkOption {
          type = types.str;
          default = "";
          description = ''
            SMTP authentication login used when sending outgoing mail.
          '';
        };

        password = mkOption {
          type = types.str;
          default = "";
          description = ''
            SMTP authentication password used when sending outgoing mail.

            ATTENTION: The password is stored world-readable in the nix-store!
          '';
        };
      };

      timezone = mkOption {
        type = types.lines;
        default = "GMT";
        description = ''
          Timezone the web-app runs in.
        '';
      };

    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    services.phpfpm.pools = {
      ${poolName} = {
        inherit (cfg) user group;

        phpOptions = ''
          date.timezone = "CET"

          ${optionalString (cfg.email.server != null) ''
            SMTP = ${cfg.email.server}
            smtp_port = ${toString cfg.email.port}
            auth_username = ${cfg.email.login}
            auth_password = ${cfg.email.password}
          ''}
        '';
        settings = mapAttrs (name: mkDefault) {
          "listen.owner" = "nginx";
          "listen.group" = "nginx";
          "listen.mode" = "0600";
          "pm" = "dynamic";
          "pm.max_children" = 75;
          "pm.start_servers" = 10;
          "pm.min_spare_servers" = 5;
          "pm.max_spare_servers" = 20;
          "pm.max_requests" = 500;
          "catch_workers_output" = 1;
        };
      };
    };

    services.nginx.enable = true;
    services.nginx.virtualHosts.${cfg.virtualHost.serverName} = {
      listen = [ { addr = cfg.virtualHost.listenHost; port = cfg.virtualHost.listenPort; } ];
      serverName = cfg.virtualHost.serverName;
      root = runDir;
      extraConfig = ''
        index index.html index.php;

        gzip on;

        gzip_comp_level 6;
        gzip_min_length  1100;
        gzip_buffers 16 8k;
        gzip_proxied any;
        gzip_types text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss;

        client_max_body_size 300M;

        rewrite ^/oauth/authorize$ /server/php/authorize.php last;
        rewrite ^/oauth_callback/([a-zA-Z0-9_\.]*)/([a-zA-Z0-9_\.]*)$ /server/php/oauth_callback.php?plugin=$1&code=$2 last;
        rewrite ^/download/([0-9]*)/([a-zA-Z0-9_\.]*)$ /server/php/download.php?id=$1&hash=$2 last;
        rewrite ^/ical/([0-9]*)/([0-9]*)/([a-z0-9]*).ics$ /server/php/ical.php?board_id=$1&user_id=$2&hash=$3 last;
        rewrite ^/api/(.*)$ /server/php/R/r.php?_url=$1&$args last;
        rewrite ^/api_explorer/api-docs/$ /client/api_explorer/api-docs/index.php last;
      '';

      locations."/".root = "${runDir}/client";

      locations."~ \\.php$" = {
        tryFiles = "$uri =404";
        extraConfig = ''
          include ${config.services.nginx.package}/conf/fastcgi_params;
          fastcgi_pass    unix:${fpm.socket};
          fastcgi_index   index.php;
          fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param   PHP_VALUE "upload_max_filesize=9G \n post_max_size=9G \n max_execution_time=200 \n max_input_time=200 \n memory_limit=256M";
        '';
      };

      locations."~* \\.(css|js|less|html|ttf|woff|jpg|jpeg|gif|png|bmp|ico)" = {
        root = "${runDir}/client";
        extraConfig = ''
          if (-f $request_filename) {
                  break;
          }
          rewrite ^/img/([a-zA-Z_]*)/([a-zA-Z_]*)/([a-zA-Z0-9_\.]*)$ /server/php/image.php?size=$1&model=$2&filename=$3 last;
          add_header        Cache-Control public;
          add_header        Cache-Control must-revalidate;
          expires           7d;
        '';
      };
    };

    systemd.services.restya-board-init = {
      description = "Restya board initialization";
      serviceConfig.Type = "oneshot";
      serviceConfig.RemainAfterExit = true;

      wantedBy = [ "multi-user.target" ];
      requires = [ "postgresql.service" ];
      after = [ "network.target" "postgresql.service" ];

      script = ''
        rm -rf "${runDir}"
        mkdir -m 750 -p "${runDir}"
        cp -r "${pkgs.restya-board}/"* "${runDir}"
        sed -i "s/@restya.com/@${cfg.virtualHost.serverName}/g" "${runDir}/sql/restyaboard_with_empty_data.sql"
        rm -rf "${runDir}/media"
        rm -rf "${runDir}/client/img"
        chmod -R 0750 "${runDir}"

        sed -i "s@^php@${config.services.phpfpm.phpPackage}/bin/php@" "${runDir}/server/php/shell/"*.sh

        ${if (cfg.database.host == null) then ''
          sed -i "s/^.*'R_DB_HOST'.*$/define('R_DB_HOST', 'localhost');/g" "${runDir}/server/php/config.inc.php"
          sed -i "s/^.*'R_DB_PASSWORD'.*$/define('R_DB_PASSWORD', 'restya');/g" "${runDir}/server/php/config.inc.php"
        '' else ''
          sed -i "s/^.*'R_DB_HOST'.*$/define('R_DB_HOST', '${cfg.database.host}');/g" "${runDir}/server/php/config.inc.php"
          sed -i "s/^.*'R_DB_PASSWORD'.*$/define('R_DB_PASSWORD', ${if cfg.database.passwordFile == null then "''" else "'file_get_contents(${cfg.database.passwordFile})'"});/g" "${runDir}/server/php/config.inc.php
        ''}
        sed -i "s/^.*'R_DB_PORT'.*$/define('R_DB_PORT', '${toString cfg.database.port}');/g" "${runDir}/server/php/config.inc.php"
        sed -i "s/^.*'R_DB_NAME'.*$/define('R_DB_NAME', '${cfg.database.name}');/g" "${runDir}/server/php/config.inc.php"
        sed -i "s/^.*'R_DB_USER'.*$/define('R_DB_USER', '${cfg.database.user}');/g" "${runDir}/server/php/config.inc.php"

        chmod 0400 "${runDir}/server/php/config.inc.php"

        ln -sf "${cfg.dataDir}/media" "${runDir}/media"
        ln -sf "${cfg.dataDir}/client/img" "${runDir}/client/img"

        chmod g+w "${runDir}/tmp/cache"
        chown -R "${cfg.user}":"${cfg.group}" "${runDir}"


        mkdir -m 0750 -p "${cfg.dataDir}"
        mkdir -m 0750 -p "${cfg.dataDir}/media"
        mkdir -m 0750 -p "${cfg.dataDir}/client/img"
        cp -r "${pkgs.restya-board}/media/"* "${cfg.dataDir}/media"
        cp -r "${pkgs.restya-board}/client/img/"* "${cfg.dataDir}/client/img"
        chown "${cfg.user}":"${cfg.group}" "${cfg.dataDir}"
        chown -R "${cfg.user}":"${cfg.group}" "${cfg.dataDir}/media"
        chown -R "${cfg.user}":"${cfg.group}" "${cfg.dataDir}/client/img"

        ${optionalString (cfg.database.host == null) ''
          if ! [ -e "${cfg.dataDir}/.db-initialized" ]; then
            ${pkgs.sudo}/bin/sudo -u ${config.services.postgresql.superUser} \
              ${config.services.postgresql.package}/bin/psql -U ${config.services.postgresql.superUser} \
              -c "CREATE USER ${cfg.database.user} WITH ENCRYPTED PASSWORD 'restya'"

            ${pkgs.sudo}/bin/sudo -u ${config.services.postgresql.superUser} \
              ${config.services.postgresql.package}/bin/psql -U ${config.services.postgresql.superUser} \
              -c "CREATE DATABASE ${cfg.database.name} OWNER ${cfg.database.user} ENCODING 'UTF8' TEMPLATE template0"

            ${pkgs.sudo}/bin/sudo -u ${cfg.user} \
              ${config.services.postgresql.package}/bin/psql -U ${cfg.database.user} \
              -d ${cfg.database.name} -f "${runDir}/sql/restyaboard_with_empty_data.sql"

            touch "${cfg.dataDir}/.db-initialized"
          fi
        ''}
      '';
    };

    systemd.timers.restya-board = {
      description = "restya-board scripts for e.g. email notification";
      wantedBy = [ "timers.target" ];
      after = [ "restya-board-init.service" ];
      requires = [ "restya-board-init.service" ];
      timerConfig = {
        OnUnitInactiveSec = "60s";
        Unit = "restya-board-timers.service";
      };
    };

    systemd.services.restya-board-timers = {
      description = "restya-board scripts for e.g. email notification";
      serviceConfig.Type = "oneshot";
      serviceConfig.User = cfg.user;

      after = [ "restya-board-init.service" ];
      requires = [ "restya-board-init.service" ];

      script = ''
        /bin/sh ${runDir}/server/php/shell/instant_email_notification.sh 2> /dev/null || true
        /bin/sh ${runDir}/server/php/shell/periodic_email_notification.sh 2> /dev/null || true
        /bin/sh ${runDir}/server/php/shell/imap.sh 2> /dev/null || true
        /bin/sh ${runDir}/server/php/shell/webhook.sh 2> /dev/null || true
        /bin/sh ${runDir}/server/php/shell/card_due_notification.sh 2> /dev/null || true
      '';
    };

    users.users.restya-board = {
      isSystemUser = true;
      createHome = false;
      home = runDir;
      group  = "restya-board";
    };
    users.groups.restya-board = {};

    services.postgresql.enable = mkIf (cfg.database.host == null) true;

    services.postgresql.identMap = optionalString (cfg.database.host == null)
      ''
        restya-board-users restya-board restya_board
      '';

    services.postgresql.authentication = optionalString (cfg.database.host == null)
      ''
        local restya_board all ident map=restya-board-users
      '';

  };

}