summary refs log tree commit diff
path: root/nixos/modules/services/misc/sourcehut/lists.nix
blob: 76f155caa05bc989cb0a8419911bdf4254429b0b (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
# Email setup is fairly involved, useful references:
# https://drewdevault.com/2018/08/05/Local-mail-server.html

{ config, lib, options, pkgs, ... }:

with lib;
let
  cfg = config.services.sourcehut;
  opt = options.services.sourcehut;
  cfgIni = cfg.settings;
  scfg = cfg.lists;
  iniKey = "lists.sr.ht";

  rcfg = config.services.redis;
  drv = pkgs.sourcehut.listssrht;
in
{
  options.services.sourcehut.lists = {
    user = mkOption {
      type = types.str;
      default = "listssrht";
      description = ''
        User for lists.sr.ht.
      '';
    };

    port = mkOption {
      type = types.port;
      default = 5006;
      description = ''
        Port on which the "lists" module should listen.
      '';
    };

    database = mkOption {
      type = types.str;
      default = "lists.sr.ht";
      description = ''
        PostgreSQL database name for lists.sr.ht.
      '';
    };

    statePath = mkOption {
      type = types.path;
      default = "${cfg.statePath}/listssrht";
      defaultText = literalExpression ''"''${config.${opt.statePath}}/listssrht"'';
      description = ''
        State path for lists.sr.ht.
      '';
    };
  };

  config = with scfg; lib.mkIf (cfg.enable && elem "lists" cfg.services) {
    users = {
      users = {
        "${user}" = {
          isSystemUser = true;
          group = user;
          extraGroups = [ "postfix" ];
          description = "lists.sr.ht user";
        };
      };
      groups = {
        "${user}" = { };
      };
    };

    services.postgresql = {
      authentication = ''
        local ${database} ${user} trust
      '';
      ensureDatabases = [ database ];
      ensureUsers = [
        {
          name = user;
          ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
        }
      ];
    };

    systemd = {
      tmpfiles.rules = [
        "d ${statePath} 0750 ${user} ${user} -"
      ];

      services = {
        listssrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey {
          after = [ "postgresql.service" "network.target" ];
          requires = [ "postgresql.service" ];
          wantedBy = [ "multi-user.target" ];

          description = "lists.sr.ht website service";

          serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";
        };

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

          description = "lists.sr.ht process service";
          serviceConfig = {
            Type = "simple";
            User = user;
            Restart = "always";
            ExecStart = "${cfg.python}/bin/celery -A ${drv.pname}.process worker --loglevel=info";
          };
        };

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

          description = "lists.sr.ht process service";
          serviceConfig = {
            Type = "simple";
            User = user;
            Restart = "always";
            ExecStart = "${cfg.python}/bin/listssrht-lmtp";
          };
        };


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

          description = "lists.sr.ht webhooks service";
          serviceConfig = {
            Type = "simple";
            User = user;
            Restart = "always";
            ExecStart = "${cfg.python}/bin/celery -A ${drv.pname}.webhooks worker --loglevel=info";
          };
        };
      };
    };

    services.sourcehut.settings = {
      # URL lists.sr.ht is being served at (protocol://domain)
      "lists.sr.ht".origin = mkDefault "http://lists.${cfg.originBase}";
      # Address and port to bind the debug server to
      "lists.sr.ht".debug-host = mkDefault "0.0.0.0";
      "lists.sr.ht".debug-port = mkDefault port;
      # Configures the SQLAlchemy connection string for the database.
      "lists.sr.ht".connection-string = mkDefault "postgresql:///${database}?user=${user}&host=/var/run/postgresql";
      # Set to "yes" to automatically run migrations on package upgrade.
      "lists.sr.ht".migrate-on-upgrade = mkDefault "yes";
      # lists.sr.ht's OAuth client ID and secret for meta.sr.ht
      # Register your client at meta.example.org/oauth
      "lists.sr.ht".oauth-client-id = mkDefault null;
      "lists.sr.ht".oauth-client-secret = mkDefault null;
      # Outgoing email for notifications generated by users
      "lists.sr.ht".notify-from = mkDefault "CHANGEME@example.org";
      # The redis connection used for the webhooks worker
      "lists.sr.ht".webhooks = mkDefault "redis://${rcfg.bind}:${toString rcfg.port}/2";
      # The redis connection used for the celery worker
      "lists.sr.ht".redis = mkDefault "redis://${rcfg.bind}:${toString rcfg.port}/4";
      # Network-key
      "lists.sr.ht".network-key = mkDefault null;
      # Allow creation
      "lists.sr.ht".allow-new-lists = mkDefault "no";
      # Posting Domain
      "lists.sr.ht".posting-domain = mkDefault "lists.${cfg.originBase}";

      # Path for the lmtp daemon's unix socket. Direct incoming mail to this socket.
      # Alternatively, specify IP:PORT and an SMTP server will be run instead.
      "lists.sr.ht::worker".sock = mkDefault "/tmp/lists.sr.ht-lmtp.sock";
      # The lmtp daemon will make the unix socket group-read/write for users in this
      # group.
      "lists.sr.ht::worker".sock-group = mkDefault "postfix";
      "lists.sr.ht::worker".reject-url = mkDefault "https://man.sr.ht/lists.sr.ht/etiquette.md";
      "lists.sr.ht::worker".reject-mimetypes = mkDefault "text/html";

    };

    services.nginx.virtualHosts."lists.${cfg.originBase}" = {
      forceSSL = true;
      locations."/".proxyPass = "http://${cfg.address}:${toString port}";
      locations."/query".proxyPass = "http://${cfg.address}:${toString (port + 100)}";
      locations."/static".root = "${pkgs.sourcehut.listssrht}/${pkgs.sourcehut.python.sitePackages}/listssrht";
    };
  };
}