about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/web-apps/slskd.nix
blob: 15a5fd1177adf599296411771883568023b1a5b8 (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
{ lib, pkgs, config, ... }:

let
  settingsFormat = pkgs.formats.yaml {};
  defaultUser = "slskd";
in {
  options.services.slskd = with lib; with types; {
    enable = mkEnableOption "enable slskd";

    package = mkPackageOptionMD pkgs "slskd" { };

    user = mkOption {
      type = types.str;
      default = defaultUser;
      description = "User account under which slskd runs.";
    };

    group = mkOption {
      type = types.str;
      default = defaultUser;
      description = "Group under which slskd runs.";
    };

    domain = mkOption {
      type = types.nullOr types.str;
      description = ''
        If non-null, enables an nginx reverse proxy virtual host at this FQDN,
        at the path configurated with `services.slskd.web.url_base`.
      '';
      example = "slskd.example.com";
    };

    nginx = mkOption {
      type = types.submodule (import ../web-servers/nginx/vhost-options.nix { inherit config lib; });
      default = {};
      example = lib.literalExpression ''
        {
          enableACME = true;
          forceHttps = true;
        }
      '';
      description = ''
        This option customizes the nginx virtual host set up for slskd.
      '';
    };

    environmentFile = mkOption {
      type = path;
      description = ''
        Path to the environment file sourced on startup.
        It must at least contain the variables `SLSKD_SLSK_USERNAME` and `SLSKD_SLSK_PASSWORD`.
        Web interface credentials should also be set here in `SLSKD_USERNAME` and `SLSKD_PASSWORD`.
        Other, optional credentials like SOCKS5 with `SLSKD_SLSK_PROXY_USERNAME` and `SLSKD_SLSK_PROXY_PASSWORD`
        should all reside here instead of in the world-readable nix store.
        Variables are documented at https://github.com/slskd/slskd/blob/master/docs/config.md
      '';
    };

    openFirewall = mkOption {
      type = bool;
      description = "Whether to open the firewall for the soulseek network listen port (not the web interface port).";
      default = false;
    };

    settings = mkOption {
      description = ''
        Application configuration for slskd. See
        [documentation](https://github.com/slskd/slskd/blob/master/docs/config.md).
      '';
      default = {};
      type = submodule {
        freeformType = settingsFormat.type;
        options = {
          remote_file_management = mkEnableOption "modification of share contents through the web ui";

          flags = {
            force_share_scan = mkOption {
              type = bool;
              description = "Force a rescan of shares on every startup.";
            };
            no_version_check = mkOption {
              type = bool;
              default = true;
              visible = false;
              description = "Don't perform a version check on startup.";
            };
          };

          directories = {
            incomplete = mkOption {
              type = nullOr path;
              description = "Directory where incomplete downloading files are stored.";
              defaultText = "/var/lib/slskd/incomplete";
              default = null;
            };
            downloads = mkOption {
              type = nullOr path;
              description = "Directory where downloaded files are stored.";
              defaultText = "/var/lib/slskd/downloads";
              default = null;
            };
          };

          shares = {
            directories = mkOption {
              type = listOf str;
              description = ''
                Paths to shared directories. See
                [documentation](https://github.com/slskd/slskd/blob/master/docs/config.md#directories)
                for advanced usage.
              '';
              example = lib.literalExpression ''[ "/home/John/Music" "!/home/John/Music/Recordings" "[Music Drive]/mnt" ]'';
            };
            filters = mkOption {
              type = listOf str;
              example = lib.literalExpression ''[ "\.ini$" "Thumbs.db$" "\.DS_Store$" ]'';
              description = "Regular expressions of files to exclude from sharing.";
            };
          };

          rooms = mkOption {
            type = listOf str;
            description = "Chat rooms to join on startup.";
          };

          soulseek = {
            description = mkOption {
              type = str;
              description = "The user description for the Soulseek network.";
              defaultText = "A slskd user. https://github.com/slskd/slskd";
            };
            listen_port = mkOption {
              type = port;
              description = "The port on which to listen for incoming connections.";
              default = 50300;
            };
          };

          global = {
            # TODO speed units
            upload = {
              slots = mkOption {
                type = ints.unsigned;
                description = "Limit of the number of concurrent upload slots.";
              };
              speed_limit = mkOption {
                type = ints.unsigned;
                description = "Total upload speed limit.";
              };
            };
            download = {
              slots = mkOption {
                type = ints.unsigned;
                description = "Limit of the number of concurrent download slots.";
              };
              speed_limit = mkOption {
                type = ints.unsigned;
                description = "Total upload download limit";
              };
            };
          };

          filters.search.request = mkOption {
            type = listOf str;
            example = lib.literalExpression ''[ "^.{1,2}$" ]'';
            description = "Incoming search requests which match this filter are ignored.";
          };

          web = {
            port = mkOption {
              type = port;
              default = 5030;
              description = "The HTTP listen port.";
            };
            url_base = mkOption {
              type = path;
              default = "/";
              description = "The base path in the url for web requests.";
            };
            # Users should use a reverse proxy instead for https
            https.disabled = mkOption {
              type = bool;
              default = true;
              description = "Disable the built-in HTTPS server";
            };
          };

          retention = {
            transfers = {
              upload = {
                succeeded = mkOption {
                  type = ints.unsigned;
                  description = "Lifespan of succeeded upload tasks.";
                  defaultText = "(indefinite)";
                };
                errored = mkOption {
                  type = ints.unsigned;
                  description = "Lifespan of errored upload tasks.";
                  defaultText = "(indefinite)";
                };
                cancelled = mkOption {
                  type = ints.unsigned;
                  description = "Lifespan of cancelled upload tasks.";
                  defaultText = "(indefinite)";
                };
              };
              download = {
                succeeded = mkOption {
                  type = ints.unsigned;
                  description = "Lifespan of succeeded download tasks.";
                  defaultText = "(indefinite)";
                };
                errored = mkOption {
                  type = ints.unsigned;
                  description = "Lifespan of errored download tasks.";
                  defaultText = "(indefinite)";
                };
                cancelled = mkOption {
                  type = ints.unsigned;
                  description = "Lifespan of cancelled download tasks.";
                  defaultText = "(indefinite)";
                };
              };
            };
            files = {
              complete = mkOption {
                type = ints.unsigned;
                description = "Lifespan of completely downloaded files in minutes.";
                example = 20160;
                defaultText = "(indefinite)";
              };
              incomplete = mkOption {
                type = ints.unsigned;
                description = "Lifespan of incomplete downloading files in minutes.";
                defaultText = "(indefinite)";
              };
            };
          };

          logger = {
            # Disable by default, journald already retains as needed
            disk = mkOption {
              type = bool;
              description = "Whether to log to the application directory.";
              default = false;
              visible = false;
            };
          };
        };
      };
    };
  };

  config = let
    cfg = config.services.slskd;

    confWithoutNullValues = (lib.filterAttrsRecursive (key: value: (builtins.tryEval value).success && value != null) cfg.settings);

    configurationYaml = settingsFormat.generate "slskd.yml" confWithoutNullValues;

  in lib.mkIf cfg.enable {

    # Force off, configuration file is in nix store and is immutable
    services.slskd.settings.remote_configuration = lib.mkForce false;

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

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

    systemd.services.slskd = {
      description = "A modern client-server application for the Soulseek file sharing network";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        Type = "simple";
        User = cfg.user;
        Group = cfg.group;
        EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
        StateDirectory = "slskd";  # Creates /var/lib/slskd and manages permissions
        ExecStart = "${cfg.package}/bin/slskd --app-dir /var/lib/slskd --config ${configurationYaml}";
        Restart = "on-failure";
        ReadOnlyPaths = map (d: builtins.elemAt (builtins.split "[^/]*(/.+)" d) 1) cfg.settings.shares.directories;
        ReadWritePaths =
          (lib.optional (cfg.settings.directories.incomplete != null) cfg.settings.directories.incomplete) ++
          (lib.optional (cfg.settings.directories.downloads != null) cfg.settings.directories.downloads);
        LockPersonality = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateMounts = true;
        PrivateTmp = true;
        PrivateUsers = true;
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectSystem = "strict";
        RemoveIPC = true;
        RestrictNamespaces = true;
        RestrictSUIDSGID = true;
      };
    };

    networking.firewall.allowedTCPPorts = lib.optional cfg.openFirewall cfg.settings.soulseek.listen_port;

    services.nginx = lib.mkIf (cfg.domain != null) {
      enable = lib.mkDefault true;
      virtualHosts."${cfg.domain}" = lib.mkMerge [
        cfg.nginx
        {
          locations."${cfg.settings.web.url_base}" = {
            proxyPass = "http://127.0.0.1:${toString cfg.settings.web.port}";
            proxyWebsockets = true;
          };
        }
      ];
    };
  };

  meta = {
    maintainers = with lib.maintainers; [ ppom melvyn2 ];
  };
}