about summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/outline.nix
blob: ba426ce89bbf26326b0691ea7200be19ce67fc81 (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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
{ config, lib, pkgs, ...}:

let
  defaultUser = "outline";
  cfg = config.services.outline;
  inherit (lib) mkRemovedOptionModule;
in
{
  imports = [
    (mkRemovedOptionModule [ "services" "outline" "sequelizeArguments" ] "Database migration are run agains configurated database by outline directly")
  ];
  # See here for a reference of all the options:
  #   https://github.com/outline/outline/blob/v0.67.0/.env.sample
  #   https://github.com/outline/outline/blob/v0.67.0/app.json
  #   https://github.com/outline/outline/blob/v0.67.0/server/env.ts
  #   https://github.com/outline/outline/blob/v0.67.0/shared/types.ts
  # The order is kept the same here to make updating easier.
  options.services.outline = {
    enable = lib.mkEnableOption "outline";

    package = lib.mkOption {
      default = pkgs.outline;
      defaultText = lib.literalExpression "pkgs.outline";
      type = lib.types.package;
      example = lib.literalExpression ''
        pkgs.outline.overrideAttrs (super: {
          # Ignore the domain part in emails that come from OIDC. This is might
          # be helpful if you want multiple users with different email providers
          # to still land in the same team. Note that this effectively makes
          # Outline a single-team instance.
          patchPhase = ${"''"}
            sed -i 's/const domain = parts\.length && parts\[1\];/const domain = "example.com";/g' plugins/oidc/server/auth/oidc.ts
          ${"''"};
        })
      '';
      description = "Outline package to use.";
    };

    user = lib.mkOption {
      type = lib.types.str;
      default = defaultUser;
      description = ''
        User under which the service should run. If this is the default value,
        the user will be created, with the specified group as the primary
        group.
      '';
    };

    group = lib.mkOption {
      type = lib.types.str;
      default = defaultUser;
      description = ''
        Group under which the service should run. If this is the default value,
        the group will be created.
      '';
    };

    #
    # Required options
    #

    secretKeyFile = lib.mkOption {
      type = lib.types.str;
      default = "/var/lib/outline/secret_key";
      description = ''
        File path that contains the application secret key. It must be 32
        bytes long and hex-encoded. If the file does not exist, a new key will
        be generated and saved here.
      '';
    };

    utilsSecretFile = lib.mkOption {
      type = lib.types.str;
      default = "/var/lib/outline/utils_secret";
      description = ''
        File path that contains the utility secret key. If the file does not
        exist, a new key will be generated and saved here.
      '';
    };

    databaseUrl = lib.mkOption {
      type = lib.types.str;
      default = "local";
      description = ''
        URI to use for the main PostgreSQL database. If this needs to include
        credentials that shouldn't be world-readable in the Nix store, set an
        environment file on the systemd service and override the
        `DATABASE_URL` entry. Pass the string
        `local` to setup a database on the local server.
      '';
    };

    redisUrl = lib.mkOption {
      type = lib.types.str;
      default = "local";
      description = ''
        Connection to a redis server. If this needs to include credentials
        that shouldn't be world-readable in the Nix store, set an environment
        file on the systemd service and override the
        `REDIS_URL` entry. Pass the string
        `local` to setup a local Redis database.
      '';
    };

    publicUrl = lib.mkOption {
      type = lib.types.str;
      default = "http://localhost:3000";
      description = "The fully qualified, publicly accessible URL";
    };

    port = lib.mkOption {
      type = lib.types.port;
      default = 3000;
      description = "Listening port.";
    };

    storage = lib.mkOption {
      description = ''
        To support uploading of images for avatars and document attachments an
        s3-compatible storage can be provided. AWS S3 is recommended for
        redundancy however if you want to keep all file storage local an
        alternative such as [minio](https://github.com/minio/minio)
        can be used.
        Local filesystem storage can also be used.

        A more detailed guide on setting up storage is available
        [here](https://docs.getoutline.com/s/hosting/doc/file-storage-N4M0T6Ypu7).
      '';
      example = lib.literalExpression ''
        {
          accessKey = "...";
          secretKeyFile = "/somewhere";
          uploadBucketUrl = "https://minio.example.com";
          uploadBucketName = "outline";
          region = "us-east-1";
        }
      '';
      type = lib.types.submodule {
        options = {
          storageType = lib.mkOption {
            type = lib.types.enum [ "local" "s3" ];
            description = "File storage type, it can be local or s3.";
            default = "s3";
          };
          localRootDir = lib.mkOption {
            type = lib.types.str;
            description = ''
              If `storageType` is `local`, this sets the parent directory
              under which all attachments/images go.
            '';
            default = "/var/lib/outline/data";
          };
          accessKey = lib.mkOption {
            type = lib.types.str;
            description = "S3 access key.";
          };
          secretKeyFile = lib.mkOption {
            type = lib.types.path;
            description = "File path that contains the S3 secret key.";
          };
          region = lib.mkOption {
            type = lib.types.str;
            default = "xx-xxxx-x";
            description = "AWS S3 region name.";
          };
          uploadBucketUrl = lib.mkOption {
            type = lib.types.str;
            description = ''
              URL endpoint of an S3-compatible API where uploads should be
              stored.
            '';
          };
          uploadBucketName = lib.mkOption {
            type = lib.types.str;
            description = "Name of the bucket where uploads should be stored.";
          };
          uploadMaxSize = lib.mkOption {
            type = lib.types.int;
            default = 26214400;
            description = "Maxmium file size for uploads.";
          };
          forcePathStyle = lib.mkOption {
            type = lib.types.bool;
            default = true;
            description = "Force S3 path style.";
          };
          acl = lib.mkOption {
            type = lib.types.str;
            default = "private";
            description = "ACL setting.";
          };
        };
      };
    };

    #
    # Authentication
    #

    slackAuthentication = lib.mkOption {
      description = ''
        To configure Slack auth, you'll need to create an Application at
        https://api.slack.com/apps

        When configuring the Client ID, add a redirect URL under "OAuth & Permissions"
        to `https://[publicUrl]/auth/slack.callback`.
      '';
      default = null;
      type = lib.types.nullOr (lib.types.submodule {
        options = {
          clientId = lib.mkOption {
            type = lib.types.str;
            description = "Authentication key.";
          };
          secretFile = lib.mkOption {
            type = lib.types.str;
            description = "File path containing the authentication secret.";
          };
        };
      });
    };

    googleAuthentication = lib.mkOption {
      description = ''
        To configure Google auth, you'll need to create an OAuth Client ID at
        https://console.cloud.google.com/apis/credentials

        When configuring the Client ID, add an Authorized redirect URI to
        `https://[publicUrl]/auth/google.callback`.
      '';
      default = null;
      type = lib.types.nullOr (lib.types.submodule {
        options = {
          clientId = lib.mkOption {
            type = lib.types.str;
            description = "Authentication client identifier.";
          };
          clientSecretFile = lib.mkOption {
            type = lib.types.str;
            description = "File path containing the authentication secret.";
          };
        };
      });
    };

    azureAuthentication = lib.mkOption {
      description = ''
        To configure Microsoft/Azure auth, you'll need to create an OAuth
        Client. See
        [the guide](https://wiki.generaloutline.com/share/dfa77e56-d4d2-4b51-8ff8-84ea6608faa4)
        for details on setting up your Azure App.
      '';
      default = null;
      type = lib.types.nullOr (lib.types.submodule {
        options = {
          clientId = lib.mkOption {
            type = lib.types.str;
            description = "Authentication client identifier.";
          };
          clientSecretFile = lib.mkOption {
            type = lib.types.str;
            description = "File path containing the authentication secret.";
          };
          resourceAppId = lib.mkOption {
            type = lib.types.str;
            description = "Authentication application resource ID.";
          };
        };
      });
    };

    oidcAuthentication = lib.mkOption {
      description = ''
        To configure generic OIDC auth, you'll need some kind of identity
        provider. See the documentation for whichever IdP you use to fill out
        all the fields. The redirect URL is
        `https://[publicUrl]/auth/oidc.callback`.
      '';
      default = null;
      type = lib.types.nullOr (lib.types.submodule {
        options = {
          clientId = lib.mkOption {
            type = lib.types.str;
            description = "Authentication client identifier.";
          };
          clientSecretFile = lib.mkOption {
            type = lib.types.str;
            description = "File path containing the authentication secret.";
          };
          authUrl = lib.mkOption {
            type = lib.types.str;
            description = "OIDC authentication URL endpoint.";
          };
          tokenUrl = lib.mkOption {
            type = lib.types.str;
            description = "OIDC token URL endpoint.";
          };
          userinfoUrl = lib.mkOption {
            type = lib.types.str;
            description = "OIDC userinfo URL endpoint.";
          };
          usernameClaim = lib.mkOption {
            type = lib.types.str;
            description = ''
              Specify which claims to derive user information from. Supports any
              valid JSON path with the JWT payload
            '';
            default = "preferred_username";
          };
          displayName = lib.mkOption {
            type = lib.types.str;
            description = "Display name for OIDC authentication.";
            default = "OpenID";
          };
          scopes = lib.mkOption {
            type = lib.types.listOf lib.types.str;
            description = "OpenID authentication scopes.";
            default = [ "openid" "profile" "email" ];
          };
        };
      });
    };

    #
    # Optional configuration
    #

    sslKeyFile = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      description = ''
        File path that contains the Base64-encoded private key for HTTPS
        termination. This is only required if you do not use an external reverse
        proxy. See
        [the documentation](https://wiki.generaloutline.com/share/dfa77e56-d4d2-4b51-8ff8-84ea6608faa4).
      '';
    };
    sslCertFile = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      description = ''
        File path that contains the Base64-encoded certificate for HTTPS
        termination. This is only required if you do not use an external reverse
        proxy. See
        [the documentation](https://wiki.generaloutline.com/share/dfa77e56-d4d2-4b51-8ff8-84ea6608faa4).
      '';
    };

    cdnUrl = lib.mkOption {
      type = lib.types.str;
      default = "";
      description = ''
        If using a Cloudfront/Cloudflare distribution or similar it can be set
        using this option. This will cause paths to JavaScript files,
        stylesheets and images to be updated to the hostname defined here. In
        your CDN configuration the origin server should be set to public URL.
      '';
    };

    forceHttps = lib.mkOption {
      type = lib.types.bool;
      default = true;
      description = ''
        Auto-redirect to HTTPS in production. The default is
        `true` but you may set this to `false`
        if you can be sure that SSL is terminated at an external loadbalancer.
      '';
    };

    enableUpdateCheck = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Have the installation check for updates by sending anonymized statistics
        to the maintainers.
      '';
    };

    concurrency = lib.mkOption {
      type = lib.types.int;
      default = 1;
      description = ''
        How many processes should be spawned. For a rough estimate, divide your
        server's available memory by 512.
      '';
    };

    maximumImportSize = lib.mkOption {
      type = lib.types.int;
      default = 5120000;
      description = ''
        The maximum size of document imports. Overriding this could be required
        if you have especially large Word documents with embedded imagery.
      '';
    };

    debugOutput = lib.mkOption {
      type = lib.types.nullOr (lib.types.enum [ "http" ]);
      default = null;
      description = "Set this to `http` log HTTP requests.";
    };

    slackIntegration = lib.mkOption {
      description = ''
        For a complete Slack integration with search and posting to channels
        this configuration is also needed. See here for details:
        https://wiki.generaloutline.com/share/be25efd1-b3ef-4450-b8e5-c4a4fc11e02a
      '';
      default = null;
      type = lib.types.nullOr (lib.types.submodule {
        options = {
          verificationTokenFile = lib.mkOption {
            type = lib.types.str;
            description = "File path containing the verification token.";
          };
          appId = lib.mkOption {
            type = lib.types.str;
            description = "Application ID.";
          };
          messageActions = lib.mkOption {
            type = lib.types.bool;
            default = true;
            description = "Whether to enable message actions.";
          };
        };
      });
    };

    googleAnalyticsId = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      description = ''
        Optionally enable Google Analytics to track page views in the knowledge
        base.
      '';
    };

    sentryDsn = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      description = ''
        Optionally enable [Sentry](https://sentry.io/) to
        track errors and performance.
      '';
    };

    sentryTunnel = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      description = ''
        Optionally add a
        [Sentry proxy tunnel](https://docs.sentry.io/platforms/javascript/troubleshooting/#using-the-tunnel-option)
        for bypassing ad blockers in the UI.
      '';
    };

    logo = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      description = ''
        Custom logo displayed on the authentication screen. This will be scaled
        to a height of 60px.
      '';
    };

    smtp = lib.mkOption {
      description = ''
        To support sending outgoing transactional emails such as
        "document updated" or "you've been invited" you'll need to provide
        authentication for an SMTP server.
      '';
      default = null;
      type = lib.types.nullOr (lib.types.submodule {
        options = {
          host = lib.mkOption {
            type = lib.types.str;
            description = "Host name or IP address of the SMTP server.";
          };
          port = lib.mkOption {
            type = lib.types.port;
            description = "TCP port of the SMTP server.";
          };
          username = lib.mkOption {
            type = lib.types.str;
            description = "Username to authenticate with.";
          };
          passwordFile = lib.mkOption {
            type = lib.types.str;
            description = ''
              File path containing the password to authenticate with.
            '';
          };
          fromEmail = lib.mkOption {
            type = lib.types.str;
            description = "Sender email in outgoing mail.";
          };
          replyEmail = lib.mkOption {
            type = lib.types.str;
            description = "Reply address in outgoing mail.";
          };
          tlsCiphers = lib.mkOption {
            type = lib.types.str;
            default = "";
            description = "Override SMTP cipher configuration.";
          };
          secure = lib.mkOption {
            type = lib.types.bool;
            default = true;
            description = "Use a secure SMTP connection.";
          };
        };
      });
    };

    defaultLanguage = lib.mkOption {
      type = lib.types.enum [
         "da_DK"
         "de_DE"
         "en_US"
         "es_ES"
         "fa_IR"
         "fr_FR"
         "it_IT"
         "ja_JP"
         "ko_KR"
         "nl_NL"
         "pl_PL"
         "pt_BR"
         "pt_PT"
         "ru_RU"
         "sv_SE"
         "th_TH"
         "vi_VN"
         "zh_CN"
         "zh_TW"
      ];
      default = "en_US";
      description = ''
        The default interface language. See
        [translate.getoutline.com](https://translate.getoutline.com/)
        for a list of available language codes and their rough percentage
        translated.
      '';
    };

    rateLimiter.enable = lib.mkEnableOption "rate limiter for the application web server";
    rateLimiter.requests = lib.mkOption {
      type = lib.types.int;
      default = 5000;
      description = "Maximum number of requests in a throttling window.";
    };
    rateLimiter.durationWindow = lib.mkOption {
      type = lib.types.int;
      default = 60;
      description = "Length of a throttling window.";
    };
  };

  config = lib.mkIf cfg.enable {
    users.users = lib.optionalAttrs (cfg.user == defaultUser) {
      ${defaultUser} = {
        isSystemUser = true;
        group = cfg.group;
      };
    };

    users.groups = lib.optionalAttrs (cfg.group == defaultUser) {
      ${defaultUser} = { };
    };

    systemd.tmpfiles.rules = [
      "f ${cfg.secretKeyFile} 0600 ${cfg.user} ${cfg.group} -"
      "f ${cfg.utilsSecretFile} 0600 ${cfg.user} ${cfg.group} -"
      (if (cfg.storage.storageType == "s3") then
        "f ${cfg.storage.secretKeyFile} 0600 ${cfg.user} ${cfg.group} -"
      else
        "d ${cfg.storage.localRootDir} 0700 ${cfg.user} ${cfg.group} - -")
    ];

    services.postgresql = lib.mkIf (cfg.databaseUrl == "local") {
      enable = true;
      ensureUsers = [{
        name = "outline";
        ensureDBOwnership = true;
      }];
      ensureDatabases = [ "outline" ];
    };

    services.redis.servers.outline = lib.mkIf (cfg.redisUrl == "local") {
      enable = true;
      user = config.services.outline.user;
      port = 0; # Disable the TCP listener
    };

    systemd.services.outline = let
      localRedisUrl = "redis+unix:///run/redis-outline/redis.sock";
      localPostgresqlUrl = "postgres://localhost/outline?host=/run/postgresql";
    in {
      description = "Outline wiki and knowledge base";
      wantedBy = [ "multi-user.target" ];
      after = [ "networking.target" ]
        ++ lib.optional (cfg.databaseUrl == "local") "postgresql.service"
        ++ lib.optional (cfg.redisUrl == "local") "redis-outline.service";
      requires = lib.optional (cfg.databaseUrl == "local") "postgresql.service"
        ++ lib.optional (cfg.redisUrl == "local") "redis-outline.service";
      path = [
        pkgs.openssl # Required by the preStart script
      ];


      environment = lib.mkMerge [
        {
          NODE_ENV = "production";

          REDIS_URL = if cfg.redisUrl == "local" then localRedisUrl else cfg.redisUrl;
          URL = cfg.publicUrl;
          PORT = builtins.toString cfg.port;

          CDN_URL = cfg.cdnUrl;
          FORCE_HTTPS = builtins.toString cfg.forceHttps;
          ENABLE_UPDATES = builtins.toString cfg.enableUpdateCheck;
          WEB_CONCURRENCY = builtins.toString cfg.concurrency;
          MAXIMUM_IMPORT_SIZE = builtins.toString cfg.maximumImportSize;
          DEBUG = cfg.debugOutput;
          GOOGLE_ANALYTICS_ID = lib.optionalString (cfg.googleAnalyticsId != null) cfg.googleAnalyticsId;
          SENTRY_DSN = lib.optionalString (cfg.sentryDsn != null) cfg.sentryDsn;
          SENTRY_TUNNEL = lib.optionalString (cfg.sentryTunnel != null) cfg.sentryTunnel;
          TEAM_LOGO = lib.optionalString (cfg.logo != null) cfg.logo;
          DEFAULT_LANGUAGE = cfg.defaultLanguage;

          RATE_LIMITER_ENABLED = builtins.toString cfg.rateLimiter.enable;
          RATE_LIMITER_REQUESTS = builtins.toString cfg.rateLimiter.requests;
          RATE_LIMITER_DURATION_WINDOW = builtins.toString cfg.rateLimiter.durationWindow;

          FILE_STORAGE = cfg.storage.storageType;
          FILE_STORAGE_UPLOAD_MAX_SIZE = builtins.toString cfg.storage.uploadMaxSize;
          FILE_STORAGE_LOCAL_ROOT_DIR = cfg.storage.localRootDir;
        }

        (lib.mkIf (cfg.storage.storageType == "s3") {
          AWS_ACCESS_KEY_ID = cfg.storage.accessKey;
          AWS_REGION = cfg.storage.region;
          AWS_S3_UPLOAD_BUCKET_URL = cfg.storage.uploadBucketUrl;
          AWS_S3_UPLOAD_BUCKET_NAME = cfg.storage.uploadBucketName;
          AWS_S3_FORCE_PATH_STYLE = builtins.toString cfg.storage.forcePathStyle;
          AWS_S3_ACL = cfg.storage.acl;
        })

        (lib.mkIf (cfg.slackAuthentication != null) {
          SLACK_CLIENT_ID = cfg.slackAuthentication.clientId;
        })

        (lib.mkIf (cfg.googleAuthentication != null) {
          GOOGLE_CLIENT_ID = cfg.googleAuthentication.clientId;
        })

        (lib.mkIf (cfg.azureAuthentication != null) {
          AZURE_CLIENT_ID = cfg.azureAuthentication.clientId;
          AZURE_RESOURCE_APP_ID = cfg.azureAuthentication.resourceAppId;
        })

        (lib.mkIf (cfg.oidcAuthentication != null) {
          OIDC_CLIENT_ID = cfg.oidcAuthentication.clientId;
          OIDC_AUTH_URI = cfg.oidcAuthentication.authUrl;
          OIDC_TOKEN_URI = cfg.oidcAuthentication.tokenUrl;
          OIDC_USERINFO_URI = cfg.oidcAuthentication.userinfoUrl;
          OIDC_USERNAME_CLAIM = cfg.oidcAuthentication.usernameClaim;
          OIDC_DISPLAY_NAME = cfg.oidcAuthentication.displayName;
          OIDC_SCOPES = lib.concatStringsSep " " cfg.oidcAuthentication.scopes;
        })

        (lib.mkIf (cfg.slackIntegration != null) {
          SLACK_APP_ID = cfg.slackIntegration.appId;
          SLACK_MESSAGE_ACTIONS = builtins.toString cfg.slackIntegration.messageActions;
        })

        (lib.mkIf (cfg.smtp != null) {
          SMTP_HOST = cfg.smtp.host;
          SMTP_PORT = builtins.toString cfg.smtp.port;
          SMTP_USERNAME = cfg.smtp.username;
          SMTP_FROM_EMAIL = cfg.smtp.fromEmail;
          SMTP_REPLY_EMAIL = cfg.smtp.replyEmail;
          SMTP_TLS_CIPHERS = cfg.smtp.tlsCiphers;
          SMTP_SECURE = builtins.toString cfg.smtp.secure;
        })
      ];

      preStart = ''
        if [ ! -s ${lib.escapeShellArg cfg.secretKeyFile} ]; then
          openssl rand -hex 32 > ${lib.escapeShellArg cfg.secretKeyFile}
        fi
        if [ ! -s ${lib.escapeShellArg cfg.utilsSecretFile} ]; then
          openssl rand -hex 32 > ${lib.escapeShellArg cfg.utilsSecretFile}
        fi

      '';

      script = ''
        export SECRET_KEY="$(head -n1 ${lib.escapeShellArg cfg.secretKeyFile})"
        export UTILS_SECRET="$(head -n1 ${lib.escapeShellArg cfg.utilsSecretFile})"
        ${lib.optionalString (cfg.storage.storageType == "s3") ''
          export AWS_SECRET_ACCESS_KEY="$(head -n1 ${lib.escapeShellArg cfg.storage.secretKeyFile})"
        ''}
        ${lib.optionalString (cfg.slackAuthentication != null) ''
          export SLACK_CLIENT_SECRET="$(head -n1 ${lib.escapeShellArg cfg.slackAuthentication.secretFile})"
        ''}
        ${lib.optionalString (cfg.googleAuthentication != null) ''
          export GOOGLE_CLIENT_SECRET="$(head -n1 ${lib.escapeShellArg cfg.googleAuthentication.clientSecretFile})"
        ''}
        ${lib.optionalString (cfg.azureAuthentication != null) ''
          export AZURE_CLIENT_SECRET="$(head -n1 ${lib.escapeShellArg cfg.azureAuthentication.clientSecretFile})"
        ''}
        ${lib.optionalString (cfg.oidcAuthentication != null) ''
          export OIDC_CLIENT_SECRET="$(head -n1 ${lib.escapeShellArg cfg.oidcAuthentication.clientSecretFile})"
        ''}
        ${lib.optionalString (cfg.sslKeyFile != null) ''
          export SSL_KEY="$(head -n1 ${lib.escapeShellArg cfg.sslKeyFile})"
        ''}
        ${lib.optionalString (cfg.sslCertFile != null) ''
          export SSL_CERT="$(head -n1 ${lib.escapeShellArg cfg.sslCertFile})"
        ''}
        ${lib.optionalString (cfg.slackIntegration != null) ''
          export SLACK_VERIFICATION_TOKEN="$(head -n1 ${lib.escapeShellArg cfg.slackIntegration.verificationTokenFile})"
        ''}
        ${lib.optionalString (cfg.smtp != null) ''
          export SMTP_PASSWORD="$(head -n1 ${lib.escapeShellArg cfg.smtp.passwordFile})"
        ''}

        ${if (cfg.databaseUrl == "local") then ''
          export DATABASE_URL=${lib.escapeShellArg localPostgresqlUrl}
          export PGSSLMODE=disable
        '' else ''
          export DATABASE_URL=${lib.escapeShellArg cfg.databaseUrl}
        ''}

        ${cfg.package}/bin/outline-server
      '';

      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        Restart = "always";
        ProtectSystem = "strict";
        PrivateHome = true;
        PrivateTmp = true;
        UMask = "0007";

        StateDirectory = "outline";
        StateDirectoryMode = "0750";
        RuntimeDirectory = "outline";
        RuntimeDirectoryMode = "0750";
        # This working directory is required to find stuff like the set of
        # onboarding files:
        WorkingDirectory = "${cfg.package}/share/outline";
        # In case this directory is not in /var/lib/outline, it needs to be made writable explicitly
        ReadWritePaths = lib.mkIf (cfg.storage.storageType == "local") [ cfg.storage.localRootDir ];
      };
    };
  };
}