summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/web-apps/romm.nix
blob: bdb7bee8cad589ca516ca6217f6497f738cdf918 (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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.services.romm;

  authSecretFile = "${cfg.dataDir}/.auth-secret.env";

  isIPv6 = addr: lib.hasInfix ":" addr;
  bracketed = addr: if isIPv6 addr then "[${addr}]" else addr;

  # Wildcard binds are proxied via loopback; specific addresses as-is.
  proxyHost =
    if
      lib.elem cfg.listenAddress [
        "0.0.0.0"
        "::"
      ]
    then
      "127.0.0.1"
    else
      cfg.listenAddress;
  proxyTarget = "http://${bracketed proxyHost}:${toString cfg.port}";

  redisHost = if cfg.redis.createLocally then "127.0.0.1" else cfg.redis.host;

  commonEnv = {
    ROMM_BASE_PATH = cfg.dataDir;
    ROMM_HOST = bracketed cfg.listenAddress;
    ROMM_PORT = toString cfg.port;
    # gunicorn trusts X-Forwarded-* only from nginx's side of the socket.
    FORWARDED_ALLOW_IPS = proxyHost;
    ROMM_DB_DRIVER = "postgresql";
    DB_NAME = cfg.database.name;
    DB_USER = cfg.database.user;
    DB_PORT = toString cfg.database.port;
    REDIS_HOST = redisHost;
    REDIS_PORT = toString cfg.redis.port;
    # The rq and rqscheduler CLIs do not read RomM's REDIS_* variables.
    RQ_REDIS_HOST = redisHost;
    RQ_REDIS_PORT = toString cfg.redis.port;
    RQ_REDIS_URL = "redis://${redisHost}:${toString cfg.redis.port}/0";
  }
  // lib.optionalAttrs cfg.watcher.enable {
    # Upstream defaults this to false, which turns the watcher into a no-op.
    ENABLE_RESCAN_ON_FILESYSTEM_CHANGE = "true";
  }
  // lib.optionalAttrs cfg.database.createLocally {
    # RomM refuses to start without a database password, but with peer
    # authentication over the unix socket it is never actually used.
    DB_PASSWD = "peer-auth-unused";
    DB_QUERY_JSON = builtins.toJSON { host = "/run/postgresql"; };
  }
  // lib.optionalAttrs (!cfg.database.createLocally) {
    DB_HOST = cfg.database.host;
  }
  // lib.optionalAttrs cfg.nginx.enable (
    let
      vhost = config.services.nginx.virtualHosts.${cfg.nginx.virtualHost};
      ssl = vhost.forceSSL || vhost.onlySSL;
    in
    {
      # Used to build user-facing absolute URLs (invite and password reset
      # links); upstream's default is a broken http://0.0.0.0.
      ROMM_BASE_URL = "http${lib.optionalString ssl "s"}://${cfg.nginx.virtualHost}";
    }
    // lib.optionalAttrs ssl {
      # Upstream leaves this off because the container cannot know how it is
      # served; here the virtual host tells us TLS is terminated in front.
      ROMM_SESSION_SECURE_COOKIE = "true";
    }
  )
  // cfg.extraEnvironment;

  environmentFiles = [
    "-${authSecretFile}"
  ]
  ++ lib.optional (cfg.environmentFile != null) cfg.environmentFile;

  commonServiceConfig = {
    User = cfg.user;
    Group = cfg.group;
    WorkingDirectory = cfg.dataDir;
    EnvironmentFile = environmentFiles;
    Restart = "on-failure";
    # Hardening
    NoNewPrivileges = true;
    PrivateTmp = true;
    ProtectHome = true;
    ProtectSystem = "strict";
    ReadWritePaths = [ cfg.dataDir ];
    Environment = [
      "PATH=${lib.makeBinPath [ pkgs.rahasher ]}:/run/current-system/sw/bin"
    ];
  };
in
{
  options.services.romm = {
    enable = lib.mkEnableOption "RomM, a self-hosted ROM manager";

    package = lib.mkPackageOption pkgs "romm" { };

    listenAddress = lib.mkOption {
      type = lib.types.str;
      default = "127.0.0.1";
      example = "0.0.0.0";
      description = ''
        Address the RomM API binds to. The frontend and downloads are served
        by the nginx virtual host, not by the API directly.
      '';
    };

    port = lib.mkOption {
      type = lib.types.port;
      default = 8080;
      description = "Port the RomM API listens on.";
    };

    user = lib.mkOption {
      type = lib.types.str;
      default = "romm";
      description = "User account under which RomM runs.";
    };

    group = lib.mkOption {
      type = lib.types.str;
      default = "romm";
      description = "Group under which RomM runs.";
    };

    dataDir = lib.mkOption {
      type = lib.types.path;
      default = "/var/lib/romm";
      description = ''
        Base path for the ROM library and runtime data
        (`''${dataDir}/{library,resources,assets,config,cache}`).

        The ROM library location is fixed to `''${dataDir}/library` by
        upstream. To use an existing ROM collection stored elsewhere,
        bind-mount it there and make sure it is readable and writable by
        {option}`services.romm.user`.
      '';
    };

    database = {
      createLocally = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = ''
          Whether to create the PostgreSQL database and user locally (peer
          authentication over the unix socket).
        '';
      };
      name = lib.mkOption {
        type = lib.types.str;
        default = "romm";
        description = "Database name.";
      };
      user = lib.mkOption {
        type = lib.types.str;
        default = "romm";
        description = ''
          Database user. With {option}`services.romm.database.createLocally`
          this must equal {option}`services.romm.user`.
        '';
      };
      host = lib.mkOption {
        type = lib.types.str;
        default = "127.0.0.1";
        description = ''
          PostgreSQL host. Ignored when
          {option}`services.romm.database.createLocally` is set; the password
          for a remote database is passed as `DB_PASSWD` via
          {option}`services.romm.environmentFile`.
        '';
      };
      port = lib.mkOption {
        type = lib.types.port;
        default = 5432;
        description = "PostgreSQL port.";
      };
    };

    redis = {
      createLocally = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = ''
          Whether to create a dedicated local Redis instance
          (`services.redis.servers.romm`).
        '';
      };
      host = lib.mkOption {
        type = lib.types.str;
        default = "127.0.0.1";
        description = ''
          Redis host. Ignored when
          {option}`services.romm.redis.createLocally` is set; credentials for
          a remote Redis are passed via
          {option}`services.romm.environmentFile` (`REDIS_PASSWORD`, and
          `RQ_REDIS_URL` for the job queue).
        '';
      };
      port = lib.mkOption {
        type = lib.types.port;
        default = 6379;
        description = ''
          Redis port. RomM does not support unix sockets for Redis.
        '';
      };
    };

    watcher.enable = lib.mkOption {
      type = lib.types.bool;
      default = true;
      description = "Whether to run the filesystem watcher that rescans the library on changes.";
    };

    nginx = {
      enable = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = ''
          Whether to serve RomM through a local nginx virtual host, as
          upstream's container image does. The backend only exposes the API;
          nginx serves the frontend, ROM downloads (`X-Accel-Redirect`,
          `mod_zip`) and the cross-origin isolation headers for the emulator.
          Point external reverse proxies at this virtual host.
        '';
      };
      virtualHost = lib.mkOption {
        type = lib.types.str;
        example = "romm.example.org";
        description = "Server name of the RomM virtual host.";
      };
    };

    environmentFile = lib.mkOption {
      type = lib.types.nullOr lib.types.path;
      default = null;
      description = ''
        Environment file with secrets such as metadata provider credentials
        (`IGDB_CLIENT_ID`, `IGDB_CLIENT_SECRET`, `SCREENSCRAPER_USER`, ...).
        `ROMM_AUTH_SECRET_KEY` may also be set here; when absent, one is
        generated and persisted under {option}`services.romm.dataDir`.
      '';
    };

    extraEnvironment = lib.mkOption {
      type = lib.types.attrsOf lib.types.str;
      default = { };
      example = {
        WEB_CONCURRENCY = "4";
      };
      description = ''
        Extra environment variables passed to all RomM services. See
        <https://docs.romm.app/latest/Getting-Started/Environment-Variables/>.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = [
      {
        assertion = cfg.database.createLocally -> cfg.database.user == cfg.user;
        message = "services.romm.database.user must equal services.romm.user when database.createLocally is enabled.";
      }
      {
        assertion = cfg.database.createLocally -> cfg.database.name == cfg.database.user;
        message = "services.romm.database.name must equal services.romm.database.user when database.createLocally is enabled.";
      }
    ];

    users.users.${cfg.user} = lib.mkIf (cfg.user == "romm") {
      isSystemUser = true;
      group = cfg.group;
      home = cfg.dataDir;
    };
    users.groups.${cfg.group} = lib.mkIf (cfg.group == "romm") { };

    systemd.tmpfiles.settings."10-romm" =
      lib.genAttrs
        (
          [ cfg.dataDir ]
          ++ map (dir: "${cfg.dataDir}/${dir}") [
            "assets"
            "cache"
            "config"
            "library"
            "resources"
          ]
        )
        (_: {
          d = {
            mode = "0750";
            user = cfg.user;
            group = cfg.group;
          };
        });

    services.postgresql = lib.mkIf cfg.database.createLocally {
      enable = true;
      ensureDatabases = [ cfg.database.name ];
      ensureUsers = [
        {
          name = cfg.database.user;
          ensureDBOwnership = true;
        }
      ];
    };

    services.redis.servers.romm = lib.mkIf cfg.redis.createLocally {
      enable = true;
      port = cfg.redis.port;
      bind = "127.0.0.1";
    };

    systemd.services.romm = {
      description = "RomM ROM manager";
      wantedBy = [ "multi-user.target" ];
      after = [
        "network.target"
      ]
      ++ lib.optional cfg.redis.createLocally "redis-romm.service"
      ++ lib.optional cfg.database.createLocally "postgresql.target";
      requires =
        lib.optional cfg.redis.createLocally "redis-romm.service"
        ++ lib.optional cfg.database.createLocally "postgresql.target";
      environment = commonEnv;

      # EnvironmentFile is already in effect during preStart, so this only
      # generates a secret when none was configured.
      preStart = ''
        if [ -z "''${ROMM_AUTH_SECRET_KEY:-}" ]; then
          umask 0077
          echo "ROMM_AUTH_SECRET_KEY=$(${lib.getExe pkgs.openssl} rand -hex 32)" > ${lib.escapeShellArg authSecretFile}
        fi
      '';

      serviceConfig = commonServiceConfig // {
        ExecStartPre = [
          "${cfg.package}/bin/romm-migrate"
          "${cfg.package}/bin/romm-startup"
        ];
        ExecStart = "${cfg.package}/bin/romm";
      };
    };

    systemd.services.romm-worker = {
      description = "RomM RQ worker";
      wantedBy = [ "multi-user.target" ];
      after = [ "romm.service" ];
      requires = [ "romm.service" ];
      environment = commonEnv;
      serviceConfig = commonServiceConfig // {
        ExecStart = "${cfg.package}/bin/romm-worker";
      };
    };

    systemd.services.romm-scheduler = {
      description = "RomM RQ scheduler";
      wantedBy = [ "multi-user.target" ];
      after = [ "romm.service" ];
      requires = [ "romm.service" ];
      environment = commonEnv;
      serviceConfig = commonServiceConfig // {
        ExecStart = "${cfg.package}/bin/romm-scheduler";
      };
    };

    systemd.services.romm-watcher = lib.mkIf cfg.watcher.enable {
      description = "RomM library watcher";
      wantedBy = [ "multi-user.target" ];
      after = [ "romm.service" ];
      requires = [ "romm.service" ];
      environment = commonEnv;
      serviceConfig = commonServiceConfig // {
        ExecStart = "${cfg.package}/bin/romm-watcher ${cfg.dataDir}/library";
      };
    };

    services.nginx = lib.mkIf cfg.nginx.enable {
      enable = true;
      # mod_zip assembles the streamed multi-file ZIP downloads; njs provides
      # the internal base64 /decode endpoint used in mod_zip manifests.
      additionalModules = with pkgs.nginxModules; [
        njs
        zip
      ];
      appendHttpConfig = ''
        js_import romm_decode from ${cfg.package}/share/romm/decode.js;

        # Cross-origin isolation is required on the emulator player paths for
        # SharedArrayBuffer, which multi-threaded EmulatorJS cores rely on.
        map $request_uri $romm_coep_header {
          default "";
          "~^/rom/.*/ejs$" "require-corp";
          "~^/console/rom/[0-9]+/play" "require-corp";
        }
        map $request_uri $romm_coop_header {
          default "";
          "~^/rom/.*/ejs$" "same-origin";
          "~^/console/rom/[0-9]+/play" "same-origin";
        }
      '';
      virtualHosts.${cfg.nginx.virtualHost} = {
        root = "${cfg.package.frontend}";
        extraConfig = ''
          client_max_body_size 0;
        '';
        locations = {
          "/" = {
            tryFiles = "$uri $uri/ /index.html";
            extraConfig = ''
              # never cache index.html: store paths have epoch mtimes, and
              # heuristic caching would keep serving it across upgrades
              add_header Cache-Control "no-cache";
              add_header Cross-Origin-Embedder-Policy $romm_coep_header;
              add_header Cross-Origin-Opener-Policy $romm_coop_header;
            '';
          };
          "/assets/romm/resources/" = {
            alias = "${cfg.dataDir}/resources/";
          };
          "/openapi.json" = {
            proxyPass = proxyTarget;
            # Per-location instead of the global recommendedProxySettings so
            # other virtual hosts on the same machine are left untouched.
            recommendedProxySettings = true;
          };
          "/api" = {
            proxyPass = proxyTarget;
            recommendedProxySettings = true;
            extraConfig = ''
              # Chunked request bodies can only be relayed unbuffered over
              # HTTP/1.1 (the global recommendedProxySettings used to set this).
              proxy_http_version 1.1;
              proxy_request_buffering off;
              proxy_buffering off;
              proxy_read_timeout 300s;
            '';
          };
          "~ ^/(ws|netplay)" = {
            proxyPass = proxyTarget;
            proxyWebsockets = true;
            recommendedProxySettings = true;
          };
          # X-Accel-Redirect targets used by the backend for ROM downloads.
          "/library/" = {
            alias = "${cfg.dataDir}/library/";
            extraConfig = "internal;";
          };
          "/cache/" = {
            alias = "${cfg.dataDir}/cache/";
            extraConfig = "internal;";
          };
          "/decode" = {
            extraConfig = ''
              internal;
              js_content romm_decode.decodeBase64;
            '';
          };
        };
      };
    };

    users.users.${config.services.nginx.user} = lib.mkIf cfg.nginx.enable {
      extraGroups = [ cfg.group ];
    };
  };

  meta.maintainers = with lib.maintainers; [
    denzonl
    jk
  ];
}