about summary refs log tree commit diff
path: root/nixos/modules/services/mail/listmonk.nix
blob: 482bc42696f98ed434442428573d15e81003ecad (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
{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.services.listmonk;
  tomlFormat = pkgs.formats.toml { };
  cfgFile = tomlFormat.generate "listmonk.toml" cfg.settings;
  # Escaping is done according to https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
  setDatabaseOption = key: value:
    "UPDATE settings SET value = '${
      lib.replaceStrings [ "'" ] [ "''" ] (builtins.toJSON value)
    }' WHERE key = '${key}';";
  updateDatabaseConfigSQL = pkgs.writeText "update-database-config.sql"
    (concatStringsSep "\n" (mapAttrsToList setDatabaseOption
      (if (cfg.database.settings != null) then
        cfg.database.settings
      else
        { })));
  updateDatabaseConfigScript =
    pkgs.writeShellScriptBin "update-database-config.sh" ''
      ${if cfg.database.mutableSettings then ''
        if [ ! -f /var/lib/listmonk/.db_settings_initialized ]; then
          ${pkgs.postgresql}/bin/psql -d listmonk -f ${updateDatabaseConfigSQL} ;
          touch /var/lib/listmonk/.db_settings_initialized
        fi
      '' else
        "${pkgs.postgresql}/bin/psql -d listmonk -f ${updateDatabaseConfigSQL}"}
    '';

  databaseSettingsOpts = with types; {
    freeformType =
      oneOf [ (listOf str) (listOf (attrsOf anything)) str int bool ];

    options = {
      "app.notify_emails" = mkOption {
        type = listOf str;
        default = [ ];
        description = "Administrator emails for system notifications";
      };

      "privacy.exportable" = mkOption {
        type = listOf str;
        default = [ "profile" "subscriptions" "campaign_views" "link_clicks" ];
        description =
          "List of fields which can be exported through an automatic export request";
      };

      "privacy.domain_blocklist" = mkOption {
        type = listOf str;
        default = [ ];
        description =
          "E-mail addresses with these domains are disallowed from subscribing.";
      };

      smtp = mkOption {
        type = listOf (submodule {
          freeformType = with types; attrsOf anything;

          options = {
            enabled = mkEnableOption "this SMTP server for listmonk";
            host = mkOption {
              type = types.str;
              description = "Hostname for the SMTP server";
            };
            port = mkOption {
              type = types.port;
              description = "Port for the SMTP server";
            };
            max_conns = mkOption {
              type = types.int;
              description =
                "Maximum number of simultaneous connections, defaults to 1";
              default = 1;
            };
            tls_type = mkOption {
              type = types.enum [ "none" "STARTTLS" "TLS" ];
              description = "Type of TLS authentication with the SMTP server";
            };
          };
        });

        description = "List of outgoing SMTP servers";
      };

      # TODO: refine this type based on the smtp one.
      "bounce.mailboxes" = mkOption {
        type = listOf
          (submodule { freeformType = with types; listOf (attrsOf anything); });
        default = [ ];
        description = "List of bounce mailboxes";
      };

      messengers = mkOption {
        type = listOf str;
        default = [ ];
        description =
          "List of messengers, see: <https://github.com/knadh/listmonk/blob/master/models/settings.go#L64-L74> for options.";
      };
    };
  };
in {
  ###### interface
  options = {
    services.listmonk = {
      enable = mkEnableOption "Listmonk, this module assumes a reverse proxy to be set";
      database = {
        createLocally = mkOption {
          type = types.bool;
          default = false;
          description =
            "Create the PostgreSQL database and database user locally.";
        };

        settings = mkOption {
          default = null;
          type = with types; nullOr (submodule databaseSettingsOpts);
          description =
            "Dynamic settings in the PostgreSQL database, set by a SQL script, see <https://github.com/knadh/listmonk/blob/master/schema.sql#L177-L230> for details.";
        };
        mutableSettings = mkOption {
          type = types.bool;
          default = true;
          description = ''
            Database settings will be reset to the value set in this module if this is not enabled.
            Enable this if you want to persist changes you have done in the application.
          '';
        };
      };
      package = mkPackageOption pkgs "listmonk" {};
      settings = mkOption {
        type = types.submodule { freeformType = tomlFormat.type; };
        description = ''
          Static settings set in the config.toml, see <https://github.com/knadh/listmonk/blob/master/config.toml.sample> for details.
          You can set secrets using the secretFile option with environment variables following <https://listmonk.app/docs/configuration/#environment-variables>.
        '';
      };
      secretFile = mkOption {
        type = types.nullOr types.str;
        default = null;
        description =
          "A file containing secrets as environment variables. See <https://listmonk.app/docs/configuration/#environment-variables> for details on supported values.";
      };
    };
  };

  ###### implementation
  config = mkIf cfg.enable {
    # Default parameters from https://github.com/knadh/listmonk/blob/master/config.toml.sample
    services.listmonk.settings."app".address = mkDefault "localhost:9000";
    services.listmonk.settings."db" = mkMerge [
      ({
        max_open = mkDefault 25;
        max_idle = mkDefault 25;
        max_lifetime = mkDefault "300s";
      })
      (mkIf cfg.database.createLocally {
        host = mkDefault "/run/postgresql";
        port = mkDefault 5432;
        user = mkDefault "listmonk";
        database = mkDefault "listmonk";
      })
    ];

    services.postgresql = mkIf cfg.database.createLocally {
      enable = true;

      ensureUsers = [{
        name = "listmonk";
        ensureDBOwnership = true;
      }];

      ensureDatabases = [ "listmonk" ];
    };

    systemd.services.listmonk = {
      description = "Listmonk - newsletter and mailing list manager";
      after = [ "network.target" ]
        ++ optional cfg.database.createLocally "postgresql.service";
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        Type = "exec";
        EnvironmentFile = mkIf (cfg.secretFile != null) [ cfg.secretFile ];
        ExecStartPre = [
          # StateDirectory cannot be used when DynamicUser = true is set this way.
          # Indeed, it will try to create all the folders and realize one of them already exist.
          # Therefore, we have to create it ourselves.
          ''${pkgs.coreutils}/bin/mkdir -p "''${STATE_DIRECTORY}/listmonk/uploads"''
          # setup database if not already done
          "${cfg.package}/bin/listmonk --config ${cfgFile} --idempotent --install --yes"
          # apply db migrations (setup and migrations can not be done in one step
          # with "--install --upgrade" listmonk ignores the upgrade)
          "${cfg.package}/bin/listmonk --config ${cfgFile} --upgrade --yes"
          "${updateDatabaseConfigScript}/bin/update-database-config.sh"
        ];
        ExecStart = "${cfg.package}/bin/listmonk --config ${cfgFile}";

        Restart = "on-failure";

        StateDirectory = [ "listmonk" ];

        User = "listmonk";
        Group = "listmonk";
        DynamicUser = true;
        NoNewPrivileges = true;
        CapabilityBoundingSet = "";
        SystemCallArchitectures = "native";
        SystemCallFilter = [ "@system-service" "~@privileged" ];
        PrivateDevices = true;
        ProtectControlGroups = true;
        ProtectKernelTunables = true;
        ProtectHome = true;
        RestrictNamespaces = true;
        RestrictRealtime = true;
        UMask = "0027";
        MemoryDenyWriteExecute = true;
        LockPersonality = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
        ProtectKernelModules = true;
        PrivateUsers = true;
      };
    };
  };
}