2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-02-24 17:33:48 +01:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2013-02-24 17:33:48 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.deluge;
|
|
|
|
cfg_web = config.services.deluge.web;
|
2020-03-19 03:19:26 +01:00
|
|
|
isDeluge1 = versionOlder cfg.package.version "2.0.0";
|
2019-03-29 15:51:07 +01:00
|
|
|
|
2016-05-23 16:12:52 +02:00
|
|
|
openFilesLimit = 4096;
|
2019-03-29 15:51:07 +01:00
|
|
|
listenPortsDefault = [ 6881 6889 ];
|
|
|
|
|
|
|
|
listToRange = x: { from = elemAt x 0; to = elemAt x 1; };
|
|
|
|
|
|
|
|
configDir = "${cfg.dataDir}/.config/deluge";
|
|
|
|
configFile = pkgs.writeText "core.conf" (builtins.toJSON cfg.config);
|
|
|
|
declarativeLockFile = "${configDir}/.declarative";
|
2016-05-23 16:12:52 +02:00
|
|
|
|
2019-03-29 15:51:07 +01:00
|
|
|
preStart = if cfg.declarative then ''
|
|
|
|
if [ -e ${declarativeLockFile} ]; then
|
|
|
|
# Was declarative before, no need to back up anything
|
2020-03-19 03:19:26 +01:00
|
|
|
${if isDeluge1 then "ln -sf" else "cp"} ${configFile} ${configDir}/core.conf
|
2019-03-29 15:51:07 +01:00
|
|
|
ln -sf ${cfg.authFile} ${configDir}/auth
|
|
|
|
else
|
|
|
|
# Declarative for the first time, backup stateful files
|
2020-03-19 03:19:26 +01:00
|
|
|
${if isDeluge1 then "ln -s" else "cp"} -b --suffix=.stateful ${configFile} ${configDir}/core.conf
|
2019-03-29 15:51:07 +01:00
|
|
|
ln -sb --suffix=.stateful ${cfg.authFile} ${configDir}/auth
|
|
|
|
echo "Autogenerated file that signifies that this server configuration is managed declaratively by NixOS" \
|
|
|
|
> ${declarativeLockFile}
|
|
|
|
fi
|
|
|
|
'' else ''
|
|
|
|
if [ -e ${declarativeLockFile} ]; then
|
|
|
|
rm ${declarativeLockFile}
|
|
|
|
fi
|
|
|
|
'';
|
2013-02-24 17:33:48 +01:00
|
|
|
in {
|
|
|
|
options = {
|
2016-05-23 16:12:52 +02:00
|
|
|
services = {
|
|
|
|
deluge = {
|
2018-01-10 01:50:28 +01:00
|
|
|
enable = mkEnableOption (lib.mdDoc "Deluge daemon");
|
2016-05-23 16:12:52 +02:00
|
|
|
|
|
|
|
openFilesLimit = mkOption {
|
|
|
|
default = openFilesLimit;
|
2021-01-26 20:51:16 +01:00
|
|
|
type = types.either types.int types.str;
|
2016-05-23 16:12:52 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Number of files to allow deluged to open.
|
|
|
|
'';
|
|
|
|
};
|
2019-03-29 15:51:07 +01:00
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''
|
2019-03-29 15:51:07 +01:00
|
|
|
{
|
|
|
|
download_location = "/srv/torrents/";
|
|
|
|
max_upload_speed = "1000.0";
|
|
|
|
share_ratio_limit = "2.0";
|
|
|
|
allow_remote = true;
|
|
|
|
daemon_port = 58846;
|
|
|
|
listen_ports = [ ${toString listenPortsDefault} ];
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Deluge core configuration for the core.conf file. Only has an effect
|
|
|
|
when {option}`services.deluge.declarative` is set to
|
|
|
|
`true`. String values must be quoted, integer and
|
|
|
|
boolean values must not. See
|
|
|
|
<https://git.deluge-torrent.org/deluge/tree/deluge/core/preferencesmanager.py#n41>
|
2022-12-18 01:31:14 +01:00
|
|
|
for the available options.
|
2019-03-29 15:51:07 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
declarative = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to use a declarative deluge configuration.
|
|
|
|
Only if set to `true`, the options
|
|
|
|
{option}`services.deluge.config`,
|
|
|
|
{option}`services.deluge.openFirewall` and
|
|
|
|
{option}`services.deluge.authFile` will be
|
|
|
|
applied.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to open the firewall for the ports in
|
|
|
|
{option}`services.deluge.config.listen_ports`. It only takes effet if
|
|
|
|
{option}`services.deluge.declarative` is set to
|
|
|
|
`true`.
|
|
|
|
|
|
|
|
It does NOT apply to the daemon port nor the web UI port. To access those
|
2023-05-08 20:05:27 +02:00
|
|
|
ports securely check the documentation
|
2019-03-29 15:51:07 +01:00
|
|
|
<https://dev.deluge-torrent.org/wiki/UserGuide/ThinClient#CreateSSHTunnel>
|
|
|
|
or use a VPN or configure certificates for deluge.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/deluge";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
The directory where deluge will create files.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
authFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
example = "/run/keys/deluge-auth";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
The file managing the authentication for deluge, the format of this
|
|
|
|
file is straightforward, each line contains a
|
|
|
|
username:password:level tuple in plaintext. It only has an effect
|
|
|
|
when {option}`services.deluge.declarative` is set to
|
|
|
|
`true`.
|
|
|
|
See <https://dev.deluge-torrent.org/wiki/UserGuide/Authentication> for
|
2022-12-18 01:31:14 +01:00
|
|
|
more information.
|
2019-03-29 15:51:07 +01:00
|
|
|
'';
|
|
|
|
};
|
2019-07-01 17:08:27 +02:00
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "deluge";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
User account under which deluge runs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "deluge";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Group under which deluge runs.
|
|
|
|
'';
|
|
|
|
};
|
2019-07-01 18:14:42 +02:00
|
|
|
|
|
|
|
extraPackages = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [];
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Extra packages available at runtime to enable Deluge's plugins. For example,
|
|
|
|
extraction utilities are required for the built-in "Extractor" plugin.
|
2020-05-03 00:45:00 +02:00
|
|
|
This always contains unzip, gnutar, xz and bzip2.
|
2019-07-01 18:14:42 +02:00
|
|
|
'';
|
|
|
|
};
|
2020-03-19 03:19:26 +01:00
|
|
|
|
2023-11-27 01:19:27 +01:00
|
|
|
package = mkPackageOption pkgs "deluge-2_x" { };
|
2016-05-23 16:12:52 +02:00
|
|
|
};
|
2013-02-24 17:33:48 +01:00
|
|
|
|
2019-03-29 15:51:07 +01:00
|
|
|
deluge.web = {
|
|
|
|
enable = mkEnableOption (lib.mdDoc "Deluge Web daemon");
|
2019-07-01 17:08:27 +02:00
|
|
|
|
2019-03-29 15:51:07 +01:00
|
|
|
port = mkOption {
|
2019-07-01 17:08:27 +02:00
|
|
|
type = types.port;
|
2019-03-29 15:51:07 +01:00
|
|
|
default = 8112;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Deluge web UI port.
|
|
|
|
'';
|
|
|
|
};
|
2019-07-01 17:08:27 +02:00
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Open ports in the firewall for deluge web daemon
|
|
|
|
'';
|
|
|
|
};
|
2019-03-29 15:51:07 +01:00
|
|
|
};
|
2013-02-24 17:33:48 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2020-03-19 03:19:26 +01:00
|
|
|
services.deluge.package = mkDefault (
|
|
|
|
if versionAtLeast config.system.stateVersion "20.09" then
|
|
|
|
pkgs.deluge-2_x
|
|
|
|
else
|
2021-08-19 09:18:18 +02:00
|
|
|
# deluge-1_x is no longer packaged and this will resolve to an error
|
|
|
|
# thanks to the alias for this name. This is left here so that anyone
|
|
|
|
# using NixOS older than 20.09 receives that error when they upgrade
|
|
|
|
# and is forced to make an intentional choice to switch to deluge-2_x.
|
|
|
|
# That might be slightly inconvenient but there is no path to
|
|
|
|
# downgrade from 2.x to 1.x so NixOS should not automatically perform
|
|
|
|
# this state migration.
|
2020-03-19 03:19:26 +01:00
|
|
|
pkgs.deluge-1_x
|
|
|
|
);
|
|
|
|
|
2019-07-01 18:14:42 +02:00
|
|
|
# Provide a default set of `extraPackages`.
|
2020-05-03 00:45:00 +02:00
|
|
|
services.deluge.extraPackages = with pkgs; [ unzip gnutar xz bzip2 ];
|
2019-07-01 18:14:42 +02:00
|
|
|
|
2019-09-01 16:20:42 +02:00
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.dataDir}' 0770 ${cfg.user} ${cfg.group}"
|
|
|
|
"d '${cfg.dataDir}/.config' 0770 ${cfg.user} ${cfg.group}"
|
|
|
|
"d '${cfg.dataDir}/.config/deluge' 0770 ${cfg.user} ${cfg.group}"
|
|
|
|
]
|
2019-09-08 08:42:53 +02:00
|
|
|
++ optional (cfg.config ? download_location)
|
2019-07-01 17:08:27 +02:00
|
|
|
"d '${cfg.config.download_location}' 0770 ${cfg.user} ${cfg.group}"
|
2019-08-13 23:52:01 +02:00
|
|
|
++ optional (cfg.config ? torrentfiles_location)
|
2019-07-01 17:08:27 +02:00
|
|
|
"d '${cfg.config.torrentfiles_location}' 0770 ${cfg.user} ${cfg.group}"
|
2019-08-13 23:52:01 +02:00
|
|
|
++ optional (cfg.config ? move_completed_path)
|
2019-07-01 17:08:27 +02:00
|
|
|
"d '${cfg.config.move_completed_path}' 0770 ${cfg.user} ${cfg.group}";
|
2019-03-29 15:51:07 +01:00
|
|
|
|
2013-02-24 17:33:48 +01:00
|
|
|
systemd.services.deluged = {
|
|
|
|
after = [ "network.target" ];
|
2013-02-26 22:53:52 +01:00
|
|
|
description = "Deluge BitTorrent Daemon";
|
2013-02-24 17:33:48 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2020-03-19 03:19:26 +01:00
|
|
|
path = [ cfg.package ] ++ cfg.extraPackages;
|
2016-05-23 16:12:52 +02:00
|
|
|
serviceConfig = {
|
2019-03-29 15:51:07 +01:00
|
|
|
ExecStart = ''
|
2020-03-19 03:19:26 +01:00
|
|
|
${cfg.package}/bin/deluged \
|
2019-03-29 15:51:07 +01:00
|
|
|
--do-not-daemonize \
|
|
|
|
--config ${configDir}
|
|
|
|
'';
|
|
|
|
# To prevent "Quit & shutdown daemon" from working; we want systemd to
|
|
|
|
# manage it!
|
2016-05-23 16:12:52 +02:00
|
|
|
Restart = "on-success";
|
2019-07-01 17:08:27 +02:00
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
2019-03-29 15:51:07 +01:00
|
|
|
UMask = "0002";
|
2016-05-23 16:12:52 +02:00
|
|
|
LimitNOFILE = cfg.openFilesLimit;
|
|
|
|
};
|
2019-03-29 15:51:07 +01:00
|
|
|
preStart = preStart;
|
2013-02-24 17:33:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.delugeweb = mkIf cfg_web.enable {
|
2019-03-29 15:51:07 +01:00
|
|
|
after = [ "network.target" "deluged.service"];
|
|
|
|
requires = [ "deluged.service" ];
|
2013-02-26 22:53:52 +01:00
|
|
|
description = "Deluge BitTorrent WebUI";
|
2013-02-24 17:33:48 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2020-03-19 03:19:26 +01:00
|
|
|
path = [ cfg.package ];
|
2019-03-29 15:51:07 +01:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = ''
|
2020-03-19 03:19:26 +01:00
|
|
|
${cfg.package}/bin/deluge-web \
|
|
|
|
${optionalString (!isDeluge1) "--do-not-daemonize"} \
|
2019-03-29 15:51:07 +01:00
|
|
|
--config ${configDir} \
|
|
|
|
--port ${toString cfg.web.port}
|
|
|
|
'';
|
2019-07-01 17:08:27 +02:00
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
2019-03-29 15:51:07 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-07-01 17:08:27 +02:00
|
|
|
networking.firewall = mkMerge [
|
|
|
|
(mkIf (cfg.declarative && cfg.openFirewall && !(cfg.config.random_port or true)) {
|
|
|
|
allowedTCPPortRanges = singleton (listToRange (cfg.config.listen_ports or listenPortsDefault));
|
|
|
|
allowedUDPPortRanges = singleton (listToRange (cfg.config.listen_ports or listenPortsDefault));
|
|
|
|
})
|
|
|
|
(mkIf (cfg.web.openFirewall) {
|
|
|
|
allowedTCPPorts = [ cfg.web.port ];
|
|
|
|
})
|
|
|
|
];
|
2013-02-24 17:33:48 +01:00
|
|
|
|
2020-03-19 03:19:26 +01:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2013-02-24 17:33:48 +01:00
|
|
|
|
2019-07-01 17:08:27 +02:00
|
|
|
users.users = mkIf (cfg.user == "deluge") {
|
|
|
|
deluge = {
|
|
|
|
group = cfg.group;
|
|
|
|
uid = config.ids.uids.deluge;
|
|
|
|
home = cfg.dataDir;
|
|
|
|
description = "Deluge Daemon user";
|
|
|
|
};
|
2013-02-24 17:33:48 +01:00
|
|
|
};
|
|
|
|
|
2019-07-01 17:08:27 +02:00
|
|
|
users.groups = mkIf (cfg.group == "deluge") {
|
|
|
|
deluge = {
|
|
|
|
gid = config.ids.gids.deluge;
|
|
|
|
};
|
|
|
|
};
|
2013-02-24 17:33:48 +01:00
|
|
|
};
|
|
|
|
}
|