2020-07-30 00:09:44 +02:00
|
|
|
{ config, lib, pkgs, options, ... }:
|
2013-03-01 21:12:38 +01:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2013-03-01 21:12:38 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.transmission;
|
2021-12-05 22:56:22 +01:00
|
|
|
opt = options.services.transmission;
|
2020-07-30 00:09:44 +02:00
|
|
|
inherit (config.environment) etc;
|
2020-10-18 15:36:24 +02:00
|
|
|
apparmor = config.security.apparmor;
|
2020-07-30 00:09:44 +02:00
|
|
|
rootDir = "/run/transmission";
|
|
|
|
settingsDir = ".config/transmission-daemon";
|
|
|
|
downloadsDir = "Downloads";
|
|
|
|
incompleteDir = ".incomplete";
|
2020-08-15 20:45:34 +02:00
|
|
|
watchDir = "watchdir";
|
2020-12-03 18:08:55 +01:00
|
|
|
settingsFormat = pkgs.formats.json {};
|
|
|
|
settingsFile = settingsFormat.generate "settings.json" cfg.settings;
|
2013-03-01 21:12:38 +01:00
|
|
|
in
|
|
|
|
{
|
2020-12-03 18:08:55 +01:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule ["services" "transmission" "port"]
|
|
|
|
["services" "transmission" "settings" "rpc-port"])
|
2022-12-30 20:43:53 +01:00
|
|
|
(mkAliasOptionModuleMD ["services" "transmission" "openFirewall"]
|
|
|
|
["services" "transmission" "openPeerPorts"])
|
2020-12-03 18:08:55 +01:00
|
|
|
];
|
2013-03-01 21:12:38 +01:00
|
|
|
options = {
|
|
|
|
services.transmission = {
|
2022-08-29 19:19:32 +02:00
|
|
|
enable = mkEnableOption "transmission" // {
|
|
|
|
description = ''
|
|
|
|
Whether to enable the headless Transmission BitTorrent daemon.
|
2013-03-01 21:12:38 +01:00
|
|
|
|
2022-08-29 19:19:32 +02:00
|
|
|
Transmission daemon can be controlled via the RPC interface using
|
|
|
|
transmission-remote, the WebUI (http://127.0.0.1:9091/ by default),
|
|
|
|
or other clients like stig or tremc.
|
2013-03-01 21:12:38 +01:00
|
|
|
|
2022-08-29 19:19:32 +02:00
|
|
|
Torrents are downloaded to [](#opt-services.transmission.home)/${downloadsDir} by default and are
|
|
|
|
accessible to users in the "transmission" group.
|
|
|
|
'';
|
|
|
|
};
|
2013-03-01 21:12:38 +01:00
|
|
|
|
2020-12-03 18:08:55 +01:00
|
|
|
settings = mkOption {
|
2013-03-01 21:12:38 +01:00
|
|
|
description = ''
|
2020-12-03 18:08:55 +01:00
|
|
|
Settings whose options overwrite fields in
|
2020-07-30 00:09:44 +02:00
|
|
|
`.config/transmission-daemon/settings.json`
|
2020-12-03 18:08:55 +01:00
|
|
|
(each time the service starts).
|
2013-03-01 21:12:38 +01:00
|
|
|
|
2020-07-30 00:09:44 +02:00
|
|
|
See [Transmission's Wiki](https://github.com/transmission/transmission/wiki/Editing-Configuration-Files)
|
2022-12-18 01:31:14 +01:00
|
|
|
for documentation of settings not explicitly covered by this module.
|
2013-03-01 21:12:38 +01:00
|
|
|
'';
|
2020-12-03 18:08:55 +01:00
|
|
|
default = {};
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
options.download-dir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "${cfg.home}/${downloadsDir}";
|
2021-12-05 22:56:22 +01:00
|
|
|
defaultText = literalExpression ''"''${config.${opt.home}}/${downloadsDir}"'';
|
2020-12-03 18:08:55 +01:00
|
|
|
description = "Directory where to download torrents.";
|
|
|
|
};
|
|
|
|
options.incomplete-dir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "${cfg.home}/${incompleteDir}";
|
2021-12-05 22:56:22 +01:00
|
|
|
defaultText = literalExpression ''"''${config.${opt.home}}/${incompleteDir}"'';
|
2020-12-03 18:08:55 +01:00
|
|
|
description = ''
|
|
|
|
When enabled with
|
|
|
|
services.transmission.home
|
|
|
|
[](#opt-services.transmission.settings.incomplete-dir-enabled),
|
|
|
|
new torrents will download the files to this directory.
|
|
|
|
When complete, the files will be moved to download-dir
|
|
|
|
[](#opt-services.transmission.settings.download-dir).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.incomplete-dir-enabled = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-08-29 16:57:18 +02:00
|
|
|
description = "";
|
2020-12-03 18:08:55 +01:00
|
|
|
};
|
|
|
|
options.message-level = mkOption {
|
2024-02-09 20:47:48 +01:00
|
|
|
type = types.ints.between 0 6;
|
2020-12-03 18:08:55 +01:00
|
|
|
default = 2;
|
|
|
|
description = "Set verbosity of transmission messages.";
|
|
|
|
};
|
|
|
|
options.peer-port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 51413;
|
|
|
|
description = "The peer port to listen for incoming connections.";
|
|
|
|
};
|
|
|
|
options.peer-port-random-high = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 65535;
|
|
|
|
description = ''
|
|
|
|
The maximum peer port to listen to for incoming connections
|
|
|
|
when [](#opt-services.transmission.settings.peer-port-random-on-start) is enabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.peer-port-random-low = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 65535;
|
|
|
|
description = ''
|
|
|
|
The minimal peer port to listen to for incoming connections
|
|
|
|
when [](#opt-services.transmission.settings.peer-port-random-on-start) is enabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.peer-port-random-on-start = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Randomize the peer port.";
|
|
|
|
};
|
|
|
|
options.rpc-bind-address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
|
|
|
example = "0.0.0.0";
|
2022-08-29 21:48:56 +02:00
|
|
|
description = ''
|
2020-12-03 18:08:55 +01:00
|
|
|
Where to listen for RPC connections.
|
2022-08-29 21:48:56 +02:00
|
|
|
Use `0.0.0.0` to listen on all interfaces.
|
2020-12-03 18:08:55 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.rpc-port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 9091;
|
|
|
|
description = "The RPC port to listen to.";
|
|
|
|
};
|
|
|
|
options.script-torrent-done-enabled = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to run
|
|
|
|
[](#opt-services.transmission.settings.script-torrent-done-filename)
|
|
|
|
at torrent completion.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.script-torrent-done-filename = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = "Executable to be run at torrent completion.";
|
|
|
|
};
|
|
|
|
options.umask = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 2;
|
|
|
|
description = ''
|
|
|
|
Sets transmission's file mode creation mask.
|
|
|
|
See the umask(2) manpage for more information.
|
|
|
|
Users who want their saved torrents to be world-writable
|
|
|
|
may want to set this value to 0.
|
|
|
|
Bear in mind that the json markup language only accepts numbers in base 10,
|
|
|
|
so the standard umask(2) octal notation "022" is written in settings.json as 18.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.utp-enabled = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
2023-10-30 21:41:44 +01:00
|
|
|
Whether to enable [Micro Transport Protocol (µTP)](https://en.wikipedia.org/wiki/Micro_Transport_Protocol).
|
2020-12-03 18:08:55 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.watch-dir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "${cfg.home}/${watchDir}";
|
2021-12-05 22:56:22 +01:00
|
|
|
defaultText = literalExpression ''"''${config.${opt.home}}/${watchDir}"'';
|
2020-12-03 18:08:55 +01:00
|
|
|
description = "Watch a directory for torrent files and add them to transmission.";
|
|
|
|
};
|
|
|
|
options.watch-dir-enabled = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''Whether to enable the
|
|
|
|
[](#opt-services.transmission.settings.watch-dir).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.trash-original-torrent-files = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''Whether to delete torrents added from the
|
|
|
|
[](#opt-services.transmission.settings.watch-dir).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2013-03-01 21:12:38 +01:00
|
|
|
};
|
|
|
|
|
2024-06-10 09:54:49 +02:00
|
|
|
package = mkPackageOption pkgs "transmission" {
|
|
|
|
default = "transmission_3";
|
|
|
|
example = "pkgs.transmission_4";
|
|
|
|
};
|
2022-12-13 11:17:02 +01:00
|
|
|
|
2019-10-06 23:18:32 +02:00
|
|
|
downloadDirPermissions = mkOption {
|
2021-11-25 03:43:30 +01:00
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
example = "770";
|
2019-10-06 23:18:32 +02:00
|
|
|
description = ''
|
2021-11-25 03:43:30 +01:00
|
|
|
If not `null`, is used as the permissions
|
2023-09-27 18:55:37 +02:00
|
|
|
set by `system.activationScripts.transmission-daemon`
|
2021-11-25 03:43:30 +01:00
|
|
|
on the directories [](#opt-services.transmission.settings.download-dir),
|
|
|
|
[](#opt-services.transmission.settings.incomplete-dir).
|
|
|
|
and [](#opt-services.transmission.settings.watch-dir).
|
2020-07-30 00:09:44 +02:00
|
|
|
Note that you may also want to change
|
2020-12-03 18:08:55 +01:00
|
|
|
[](#opt-services.transmission.settings.umask).
|
2020-07-30 00:09:44 +02:00
|
|
|
'';
|
2013-03-01 21:12:38 +01:00
|
|
|
};
|
2017-09-09 22:19:35 +02:00
|
|
|
|
|
|
|
home = mkOption {
|
|
|
|
type = types.path;
|
2020-12-03 18:08:55 +01:00
|
|
|
default = "/var/lib/transmission";
|
2017-09-09 22:19:35 +02:00
|
|
|
description = ''
|
2020-07-30 00:09:44 +02:00
|
|
|
The directory where Transmission will create `${settingsDir}`.
|
2020-12-03 18:08:55 +01:00
|
|
|
as well as `${downloadsDir}/` unless
|
|
|
|
[](#opt-services.transmission.settings.download-dir) is changed,
|
|
|
|
and `${incompleteDir}/` unless
|
|
|
|
[](#opt-services.transmission.settings.incomplete-dir) is changed.
|
2017-09-09 22:19:35 +02:00
|
|
|
'';
|
|
|
|
};
|
2019-08-24 16:44:14 +02:00
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "transmission";
|
|
|
|
description = "User account under which Transmission runs.";
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "transmission";
|
|
|
|
description = "Group account under which Transmission runs.";
|
|
|
|
};
|
2020-07-30 00:09:44 +02:00
|
|
|
|
|
|
|
credentialsFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = ''
|
|
|
|
Path to a JSON file to be merged with the settings.
|
|
|
|
Useful to merge a file which is better kept out of the Nix store
|
2020-12-03 18:08:55 +01:00
|
|
|
to set secret config parameters like `rpc-password`.
|
2020-07-30 00:09:44 +02:00
|
|
|
'';
|
|
|
|
default = "/dev/null";
|
|
|
|
example = "/var/lib/secrets/transmission/settings.json";
|
|
|
|
};
|
|
|
|
|
2021-10-16 14:09:43 +02:00
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "--log-debug" ];
|
|
|
|
description = ''
|
|
|
|
Extra flags passed to the transmission command in the service definition.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-12-03 18:08:55 +01:00
|
|
|
openPeerPorts = mkEnableOption "opening of the peer port(s) in the firewall";
|
|
|
|
|
|
|
|
openRPCPort = mkEnableOption "opening of the RPC port in the firewall";
|
2020-07-30 00:09:44 +02:00
|
|
|
|
2022-08-29 19:19:32 +02:00
|
|
|
performanceNetParameters = mkEnableOption "performance tweaks" // {
|
|
|
|
description = ''
|
|
|
|
Whether to enable tweaking of kernel parameters
|
|
|
|
to open many more connections at the same time.
|
2020-07-30 00:09:44 +02:00
|
|
|
|
2022-08-29 19:19:32 +02:00
|
|
|
Note that you may also want to increase
|
|
|
|
`peer-limit-global`.
|
|
|
|
And be aware that these settings are quite aggressive
|
|
|
|
and might not suite your regular desktop use.
|
|
|
|
For instance, SSH sessions may time out more easily.
|
|
|
|
'';
|
|
|
|
};
|
2023-12-26 23:43:14 +01:00
|
|
|
|
|
|
|
webHome = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "pkgs.flood-for-transmission";
|
|
|
|
description = ''
|
|
|
|
If not `null`, sets the value of the `TRANSMISSION_WEB_HOME`
|
|
|
|
environment variable used by the service. Useful for overriding
|
|
|
|
the web interface files, without overriding the transmission
|
|
|
|
package and thus requiring rebuilding it locally. Use this if
|
|
|
|
you want to use an alternative web interface, such as
|
|
|
|
`pkgs.flood-for-transmission`.
|
|
|
|
'';
|
|
|
|
};
|
2013-03-01 21:12:38 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2020-07-30 00:09:44 +02:00
|
|
|
# Note that using systemd.tmpfiles would not work here
|
|
|
|
# because it would fail when creating a directory
|
|
|
|
# with a different owner than its parent directory, by saying:
|
|
|
|
# Detected unsafe path transition /home/foo → /home/foo/Downloads during canonicalization of /home/foo/Downloads
|
|
|
|
# when /home/foo is not owned by cfg.user.
|
|
|
|
# Note also that using an ExecStartPre= wouldn't work either
|
|
|
|
# because BindPaths= needs these directories before.
|
2021-11-25 03:43:30 +01:00
|
|
|
system.activationScripts = mkIf (cfg.downloadDirPermissions != null)
|
|
|
|
{ transmission-daemon = ''
|
|
|
|
install -d -m 700 '${cfg.home}/${settingsDir}'
|
|
|
|
chown -R '${cfg.user}:${cfg.group}' ${cfg.home}/${settingsDir}
|
|
|
|
install -d -m '${cfg.downloadDirPermissions}' -o '${cfg.user}' -g '${cfg.group}' '${cfg.settings.download-dir}'
|
|
|
|
'' + optionalString cfg.settings.incomplete-dir-enabled ''
|
|
|
|
install -d -m '${cfg.downloadDirPermissions}' -o '${cfg.user}' -g '${cfg.group}' '${cfg.settings.incomplete-dir}'
|
|
|
|
'' + optionalString cfg.settings.watch-dir-enabled ''
|
|
|
|
install -d -m '${cfg.downloadDirPermissions}' -o '${cfg.user}' -g '${cfg.group}' '${cfg.settings.watch-dir}'
|
|
|
|
'';
|
|
|
|
};
|
2020-07-30 00:09:44 +02:00
|
|
|
|
2013-03-01 21:12:38 +01:00
|
|
|
systemd.services.transmission = {
|
2014-04-15 13:50:39 +02:00
|
|
|
description = "Transmission BitTorrent Service";
|
2020-10-18 15:36:24 +02:00
|
|
|
after = [ "network.target" ] ++ optional apparmor.enable "apparmor.service";
|
|
|
|
requires = optional apparmor.enable "apparmor.service";
|
2013-03-01 21:12:38 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2020-07-30 00:09:44 +02:00
|
|
|
environment.CURL_CA_BUNDLE = etc."ssl/certs/ca-certificates.crt".source;
|
2024-01-05 20:24:24 +01:00
|
|
|
environment.TRANSMISSION_WEB_HOME = lib.mkIf (cfg.webHome != null) cfg.webHome;
|
2013-05-28 19:48:08 +02:00
|
|
|
|
2020-07-30 00:09:44 +02:00
|
|
|
serviceConfig = {
|
|
|
|
# Use "+" because credentialsFile may not be accessible to User= or Group=.
|
|
|
|
ExecStartPre = [("+" + pkgs.writeShellScript "transmission-prestart" ''
|
|
|
|
set -eu${lib.optionalString (cfg.settings.message-level >= 3) "x"}
|
|
|
|
${pkgs.jq}/bin/jq --slurp add ${settingsFile} '${cfg.credentialsFile}' |
|
|
|
|
install -D -m 600 -o '${cfg.user}' -g '${cfg.group}' /dev/stdin \
|
|
|
|
'${cfg.home}/${settingsDir}/settings.json'
|
|
|
|
'')];
|
2022-12-13 11:17:02 +01:00
|
|
|
ExecStart="${cfg.package}/bin/transmission-daemon -f -g ${cfg.home}/${settingsDir} ${escapeShellArgs cfg.extraFlags}";
|
2020-07-30 00:09:44 +02:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
|
|
|
# Create rootDir in the host's mount namespace.
|
|
|
|
RuntimeDirectory = [(baseNameOf rootDir)];
|
|
|
|
RuntimeDirectoryMode = "755";
|
|
|
|
# This is for BindPaths= and BindReadOnlyPaths=
|
|
|
|
# to allow traversal of directories they create in RootDirectory=.
|
|
|
|
UMask = "0066";
|
|
|
|
# Using RootDirectory= makes it possible
|
|
|
|
# to use the same paths download-dir/incomplete-dir
|
|
|
|
# (which appear in user's interfaces) without requiring cfg.user
|
|
|
|
# to have access to their parent directories,
|
|
|
|
# by using BindPaths=/BindReadOnlyPaths=.
|
|
|
|
# Note that TemporaryFileSystem= could have been used instead
|
|
|
|
# but not without adding some BindPaths=/BindReadOnlyPaths=
|
|
|
|
# that would only be needed for ExecStartPre=,
|
|
|
|
# because RootDirectoryStartOnly=true would not help.
|
|
|
|
RootDirectory = rootDir;
|
|
|
|
RootDirectoryStartOnly = true;
|
|
|
|
MountAPIVFS = true;
|
|
|
|
BindPaths =
|
|
|
|
[ "${cfg.home}/${settingsDir}"
|
|
|
|
cfg.settings.download-dir
|
2023-11-13 23:23:15 +01:00
|
|
|
# Transmission may need to read in the host's /run (eg. /run/systemd/resolve)
|
|
|
|
# or write in its private /run (eg. /run/host).
|
|
|
|
"/run"
|
2020-07-30 00:09:44 +02:00
|
|
|
] ++
|
|
|
|
optional cfg.settings.incomplete-dir-enabled
|
2020-12-03 18:08:55 +01:00
|
|
|
cfg.settings.incomplete-dir ++
|
|
|
|
optional (cfg.settings.watch-dir-enabled && cfg.settings.trash-original-torrent-files)
|
|
|
|
cfg.settings.watch-dir;
|
2020-07-30 00:09:44 +02:00
|
|
|
BindReadOnlyPaths = [
|
|
|
|
# No confinement done of /nix/store here like in systemd-confinement.nix,
|
|
|
|
# an AppArmor profile is provided to get a confinement based upon paths and rights.
|
|
|
|
builtins.storeDir
|
2020-08-17 14:09:12 +02:00
|
|
|
"/etc"
|
2020-07-30 00:09:44 +02:00
|
|
|
] ++
|
|
|
|
optional (cfg.settings.script-torrent-done-enabled &&
|
2020-12-03 18:08:55 +01:00
|
|
|
cfg.settings.script-torrent-done-filename != null)
|
|
|
|
cfg.settings.script-torrent-done-filename ++
|
|
|
|
optional (cfg.settings.watch-dir-enabled && !cfg.settings.trash-original-torrent-files)
|
|
|
|
cfg.settings.watch-dir;
|
2021-11-25 03:43:30 +01:00
|
|
|
StateDirectory = [
|
|
|
|
"transmission"
|
2023-11-26 04:32:22 +01:00
|
|
|
"transmission/${settingsDir}"
|
|
|
|
"transmission/${incompleteDir}"
|
|
|
|
"transmission/${downloadsDir}"
|
|
|
|
"transmission/${watchDir}"
|
2021-11-25 03:43:30 +01:00
|
|
|
];
|
|
|
|
StateDirectoryMode = mkDefault 750;
|
2020-07-30 00:09:44 +02:00
|
|
|
# The following options are only for optimizing:
|
|
|
|
# systemd-analyze security transmission
|
|
|
|
AmbientCapabilities = "";
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
# ProtectClock= adds DeviceAllow=char-rtc r
|
|
|
|
DeviceAllow = "";
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
2023-11-13 23:24:55 +01:00
|
|
|
PrivateMounts = mkDefault true;
|
2020-07-30 00:09:44 +02:00
|
|
|
PrivateNetwork = mkDefault false;
|
|
|
|
PrivateTmp = true;
|
2023-11-13 23:24:55 +01:00
|
|
|
PrivateUsers = mkDefault true;
|
2020-07-30 00:09:44 +02:00
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
2022-12-18 01:31:14 +01:00
|
|
|
# ProtectHome=true would not allow BindPaths= to work across /home,
|
2020-07-30 00:09:44 +02:00
|
|
|
# and ProtectHome=tmpfs would break statfs(),
|
|
|
|
# preventing transmission-daemon to report the available free space.
|
|
|
|
# However, RootDirectory= is used, so this is not a security concern
|
|
|
|
# since there would be nothing in /home but any BindPaths= wanted by the user.
|
|
|
|
ProtectHome = "read-only";
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
RemoveIPC = true;
|
|
|
|
# AF_UNIX may become usable one day:
|
|
|
|
# https://github.com/transmission/transmission/issues/441
|
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
SystemCallFilter = [
|
|
|
|
"@system-service"
|
|
|
|
# Groups in @system-service which do not contain a syscall
|
|
|
|
# listed by perf stat -e 'syscalls:sys_enter_*' transmission-daemon -f
|
|
|
|
# in tests, and seem likely not necessary for transmission-daemon.
|
|
|
|
"~@aio" "~@chown" "~@keyring" "~@memlock" "~@resources" "~@setuid" "~@timer"
|
|
|
|
# In the @privileged group, but reached when querying infos through RPC (eg. with stig).
|
|
|
|
"quotactl"
|
|
|
|
];
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
};
|
2013-03-01 21:12:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
# It's useful to have transmission in path, e.g. for remote control
|
2022-12-13 11:17:02 +01:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2013-03-01 21:12:38 +01:00
|
|
|
|
2020-01-14 10:10:45 +01:00
|
|
|
users.users = optionalAttrs (cfg.user == "transmission") ({
|
|
|
|
transmission = {
|
2019-08-24 16:44:14 +02:00
|
|
|
group = cfg.group;
|
|
|
|
uid = config.ids.uids.transmission;
|
|
|
|
description = "Transmission BitTorrent user";
|
2020-07-30 00:09:44 +02:00
|
|
|
home = cfg.home;
|
2020-01-14 10:10:45 +01:00
|
|
|
};
|
|
|
|
});
|
2019-08-24 16:44:14 +02:00
|
|
|
|
2020-01-14 10:10:45 +01:00
|
|
|
users.groups = optionalAttrs (cfg.group == "transmission") ({
|
|
|
|
transmission = {
|
2019-08-24 16:44:14 +02:00
|
|
|
gid = config.ids.gids.transmission;
|
2020-01-14 10:10:45 +01:00
|
|
|
};
|
|
|
|
});
|
2013-03-01 21:12:38 +01:00
|
|
|
|
2020-12-03 18:08:55 +01:00
|
|
|
networking.firewall = mkMerge [
|
|
|
|
(mkIf cfg.openPeerPorts (
|
|
|
|
if cfg.settings.peer-port-random-on-start
|
|
|
|
then
|
|
|
|
{ allowedTCPPortRanges =
|
|
|
|
[ { from = cfg.settings.peer-port-random-low;
|
|
|
|
to = cfg.settings.peer-port-random-high;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
allowedUDPPortRanges =
|
|
|
|
[ { from = cfg.settings.peer-port-random-low;
|
|
|
|
to = cfg.settings.peer-port-random-high;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ allowedTCPPorts = [ cfg.settings.peer-port ];
|
|
|
|
allowedUDPPorts = [ cfg.settings.peer-port ];
|
|
|
|
}
|
|
|
|
))
|
|
|
|
(mkIf cfg.openRPCPort { allowedTCPPorts = [ cfg.settings.rpc-port ]; })
|
|
|
|
];
|
2020-07-30 00:09:44 +02:00
|
|
|
|
|
|
|
boot.kernel.sysctl = mkMerge [
|
|
|
|
# Transmission uses a single UDP socket in order to implement multiple uTP sockets,
|
|
|
|
# and thus expects large kernel buffers for the UDP socket,
|
|
|
|
# https://trac.transmissionbt.com/browser/trunk/libtransmission/tr-udp.c?rev=11956.
|
|
|
|
# at least up to the values hardcoded here:
|
|
|
|
(mkIf cfg.settings.utp-enabled {
|
2022-11-08 14:14:00 +01:00
|
|
|
"net.core.rmem_max" = mkDefault 4194304; # 4MB
|
2023-12-13 02:51:17 +01:00
|
|
|
"net.core.wmem_max" = mkDefault 1048576; # 1MB
|
2020-07-30 00:09:44 +02:00
|
|
|
})
|
|
|
|
(mkIf cfg.performanceNetParameters {
|
|
|
|
# Increase the number of available source (local) TCP and UDP ports to 49151.
|
|
|
|
# Usual default is 32768 60999, ie. 28231 ports.
|
|
|
|
# Find out your current usage with: ss -s
|
2020-12-03 18:08:55 +01:00
|
|
|
"net.ipv4.ip_local_port_range" = mkDefault "16384 65535";
|
2020-07-30 00:09:44 +02:00
|
|
|
# Timeout faster generic TCP states.
|
|
|
|
# Usual default is 600.
|
|
|
|
# Find out your current usage with: watch -n 1 netstat -nptuo
|
2020-12-03 18:08:55 +01:00
|
|
|
"net.netfilter.nf_conntrack_generic_timeout" = mkDefault 60;
|
2020-07-30 00:09:44 +02:00
|
|
|
# Timeout faster established but inactive connections.
|
|
|
|
# Usual default is 432000.
|
2020-12-03 18:08:55 +01:00
|
|
|
"net.netfilter.nf_conntrack_tcp_timeout_established" = mkDefault 600;
|
2020-07-30 00:09:44 +02:00
|
|
|
# Clear immediately TCP states after timeout.
|
|
|
|
# Usual default is 120.
|
2020-12-03 18:08:55 +01:00
|
|
|
"net.netfilter.nf_conntrack_tcp_timeout_time_wait" = mkDefault 1;
|
2020-07-30 00:09:44 +02:00
|
|
|
# Increase the number of trackable connections.
|
|
|
|
# Usual default is 262144.
|
|
|
|
# Find out your current usage with: conntrack -C
|
2020-12-03 18:08:55 +01:00
|
|
|
"net.netfilter.nf_conntrack_max" = mkDefault 1048576;
|
2020-07-30 00:09:44 +02:00
|
|
|
})
|
|
|
|
];
|
|
|
|
|
2020-10-18 15:36:24 +02:00
|
|
|
security.apparmor.policies."bin.transmission-daemon".profile = ''
|
2022-12-13 11:17:02 +01:00
|
|
|
include "${cfg.package.apparmor}/bin.transmission-daemon"
|
2020-12-03 18:03:32 +01:00
|
|
|
'';
|
|
|
|
security.apparmor.includes."local/bin.transmission-daemon" = ''
|
|
|
|
r ${config.systemd.services.transmission.environment.CURL_CA_BUNDLE},
|
|
|
|
|
|
|
|
owner rw ${cfg.home}/${settingsDir}/**,
|
|
|
|
rw ${cfg.settings.download-dir}/**,
|
|
|
|
${optionalString cfg.settings.incomplete-dir-enabled ''
|
|
|
|
rw ${cfg.settings.incomplete-dir}/**,
|
|
|
|
''}
|
|
|
|
${optionalString cfg.settings.watch-dir-enabled ''
|
2020-12-03 18:08:55 +01:00
|
|
|
r${optionalString cfg.settings.trash-original-torrent-files "w"} ${cfg.settings.watch-dir}/**,
|
2020-12-03 18:03:32 +01:00
|
|
|
''}
|
|
|
|
profile dirs {
|
|
|
|
rw ${cfg.settings.download-dir}/**,
|
|
|
|
${optionalString cfg.settings.incomplete-dir-enabled ''
|
|
|
|
rw ${cfg.settings.incomplete-dir}/**,
|
|
|
|
''}
|
|
|
|
${optionalString cfg.settings.watch-dir-enabled ''
|
2020-12-03 18:08:55 +01:00
|
|
|
r${optionalString cfg.settings.trash-original-torrent-files "w"} ${cfg.settings.watch-dir}/**,
|
2020-12-03 18:03:32 +01:00
|
|
|
''}
|
|
|
|
}
|
2020-07-30 00:09:44 +02:00
|
|
|
|
2020-12-03 18:03:32 +01:00
|
|
|
${optionalString (cfg.settings.script-torrent-done-enabled &&
|
2020-12-03 18:08:55 +01:00
|
|
|
cfg.settings.script-torrent-done-filename != null) ''
|
2020-12-03 18:03:32 +01:00
|
|
|
# Stack transmission_directories profile on top of
|
|
|
|
# any existing profile for script-torrent-done-filename
|
|
|
|
# FIXME: to be tested as I'm not sure it works well with NoNewPrivileges=
|
|
|
|
# https://gitlab.com/apparmor/apparmor/-/wikis/AppArmorStacking#seccomp-and-no_new_privs
|
|
|
|
px ${cfg.settings.script-torrent-done-filename} -> &@{dirs},
|
|
|
|
''}
|
2023-12-26 23:43:14 +01:00
|
|
|
|
|
|
|
${optionalString (cfg.webHome != null) ''
|
|
|
|
r ${cfg.webHome}/**,
|
|
|
|
''}
|
2020-10-18 15:36:24 +02:00
|
|
|
'';
|
2013-03-01 21:12:38 +01:00
|
|
|
};
|
|
|
|
|
2020-07-30 00:09:44 +02:00
|
|
|
meta.maintainers = with lib.maintainers; [ julm ];
|
2013-03-01 21:12:38 +01:00
|
|
|
}
|