2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
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;
|
2014-04-15 13:50:39 +02:00
|
|
|
apparmor = config.security.apparmor.enable;
|
|
|
|
|
2013-03-01 21:12:38 +01:00
|
|
|
homeDir = "/var/lib/transmission";
|
2014-04-15 13:50:39 +02:00
|
|
|
downloadDir = "${homeDir}/Downloads";
|
|
|
|
incompleteDir = "${homeDir}/.incomplete";
|
2013-03-01 21:12:38 +01:00
|
|
|
settingsDir = "${homeDir}/.config/transmission-daemon";
|
|
|
|
settingsFile = "${settingsDir}/settings.json";
|
|
|
|
|
|
|
|
# Strings must be quoted, ints and bools must not (for settings.json).
|
|
|
|
toOption = x:
|
|
|
|
if x == true then "true"
|
|
|
|
else if x == false then "false"
|
2013-11-12 13:48:19 +01:00
|
|
|
else if isInt x then toString x
|
2013-03-01 21:12:38 +01:00
|
|
|
else toString ''\"${x}\"'';
|
|
|
|
|
|
|
|
# All lines in settings.json end with a ',' (comma), except for the last
|
|
|
|
# line. This is standard JSON. But a comma can also appear *inside* some
|
|
|
|
# fields, notably the "rpc-whitelist" field. This is difficult to handle in
|
|
|
|
# sed so we simply ignore it and say that if you want to change the option at
|
|
|
|
# the last line of settings.json, you have to do it manually. At this time of
|
|
|
|
# writing, the last option is "utp-enable":true.
|
|
|
|
attrsToSedArgs = as:
|
|
|
|
concatStrings (concatLists (mapAttrsToList (name: value:
|
|
|
|
#map (x: '' -e 's=\(\"${name}\":\)[^,]*\(.*\)=\1 ${toOption x}\2=' '') # breaks if comma inside value field
|
|
|
|
map (x: '' -e 's=\(\"${name}\":\).*=\1 ${toOption x},=' '') # always append ',' (breaks last line in settings.json)
|
|
|
|
(if isList value then value else [value]))
|
|
|
|
as));
|
|
|
|
|
2014-04-15 13:50:39 +02:00
|
|
|
# for users in group "transmission" to have access to torrents
|
|
|
|
fullSettings = cfg.settings // { umask = 2; };
|
2013-03-01 21:12:38 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.transmission = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.uniq types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether or not to enable the headless Transmission BitTorrent daemon.
|
|
|
|
|
|
|
|
Transmission daemon can be controlled via the RPC interface using
|
|
|
|
transmission-remote or the WebUI (http://localhost:9091/ by default).
|
|
|
|
|
|
|
|
Torrents are downloaded to ${homeDir}/Downloads/ by default and are
|
|
|
|
accessible to users in the "transmission" group.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default =
|
|
|
|
{
|
2014-04-15 13:50:39 +02:00
|
|
|
download-dir = downloadDir;
|
|
|
|
incomplete-dir = incompleteDir;
|
|
|
|
incomplete-dir-enabled = true;
|
|
|
|
};
|
2013-03-01 21:12:38 +01:00
|
|
|
example =
|
|
|
|
{
|
|
|
|
download-dir = "/srv/torrents/";
|
|
|
|
incomplete-dir = "/srv/torrents/.incomplete/";
|
|
|
|
incomplete-dir-enabled = true;
|
|
|
|
rpc-whitelist = "127.0.0.1,192.168.*.*";
|
2014-04-15 13:50:39 +02:00
|
|
|
};
|
2013-03-01 21:12:38 +01:00
|
|
|
description = ''
|
|
|
|
Attribute set whos fields overwrites fields in settings.json (each
|
|
|
|
time the service starts). String values must be quoted, integer and
|
|
|
|
boolean values must not.
|
|
|
|
|
2014-04-15 13:50:39 +02:00
|
|
|
See https://trac.transmissionbt.com/wiki/EditConfigFiles for
|
|
|
|
documentation and/or look at ${settingsFile}.
|
2013-03-01 21:12:38 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-04-15 13:50:39 +02:00
|
|
|
port = mkOption {
|
2013-03-01 21:12:38 +01:00
|
|
|
type = types.uniq types.int;
|
|
|
|
default = 9091;
|
|
|
|
description = "TCP port number to run the RPC/web interface.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.transmission = {
|
2014-04-15 13:50:39 +02:00
|
|
|
description = "Transmission BitTorrent Service";
|
|
|
|
after = [ "network.target" ] ++ optional apparmor "apparmor.service";
|
|
|
|
requires = mkIf apparmor [ "apparmor.service" ];
|
2013-03-01 21:12:38 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2013-05-28 19:48:08 +02:00
|
|
|
|
2013-03-01 21:12:38 +01:00
|
|
|
# 1) Only the "transmission" user and group have access to torrents.
|
|
|
|
# 2) Optionally update/force specific fields into the configuration file.
|
2014-04-15 13:50:39 +02:00
|
|
|
serviceConfig.ExecStartPre = ''
|
|
|
|
${pkgs.stdenv.shell} -c "chmod 770 ${homeDir} && mkdir -p ${settingsDir} ${downloadDir} ${incompleteDir} && ${pkgs.transmission}/bin/transmission-daemon -d |& sed ${attrsToSedArgs fullSettings} > ${settingsFile}.tmp && mv ${settingsFile}.tmp ${settingsFile}"
|
|
|
|
'';
|
|
|
|
serviceConfig.ExecStart = "${pkgs.transmission}/bin/transmission-daemon -f --port ${toString config.services.transmission.port}";
|
2013-05-11 12:15:32 +02:00
|
|
|
serviceConfig.ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2013-03-01 21:12:38 +01:00
|
|
|
serviceConfig.User = "transmission";
|
|
|
|
# NOTE: transmission has an internal umask that also must be set (in settings.json)
|
|
|
|
serviceConfig.UMask = "0002";
|
|
|
|
};
|
|
|
|
|
|
|
|
# It's useful to have transmission in path, e.g. for remote control
|
|
|
|
environment.systemPackages = [ pkgs.transmission ];
|
|
|
|
|
2014-04-15 13:50:39 +02:00
|
|
|
users.extraGroups.transmission.gid = config.ids.gids.transmission;
|
2013-03-01 21:12:38 +01:00
|
|
|
users.extraUsers.transmission = {
|
|
|
|
group = "transmission";
|
2013-08-23 11:33:24 +02:00
|
|
|
uid = config.ids.uids.transmission;
|
2013-03-01 21:12:38 +01:00
|
|
|
description = "Transmission BitTorrent user";
|
|
|
|
home = homeDir;
|
|
|
|
createHome = true;
|
|
|
|
};
|
|
|
|
|
2013-05-28 19:19:15 +02:00
|
|
|
# AppArmor profile
|
2014-04-15 13:50:39 +02:00
|
|
|
security.apparmor.profiles = mkIf apparmor [
|
2013-05-28 19:19:15 +02:00
|
|
|
(pkgs.writeText "apparmor-transmission-daemon" ''
|
|
|
|
#include <tunables/global>
|
|
|
|
|
|
|
|
${pkgs.transmission}/bin/transmission-daemon {
|
|
|
|
#include <abstractions/base>
|
|
|
|
#include <abstractions/nameservice>
|
|
|
|
|
2014-05-22 19:10:46 +02:00
|
|
|
${pkgs.glibc}/lib/*.so mr,
|
|
|
|
${pkgs.libevent}/lib/libevent*.so* mr,
|
|
|
|
${pkgs.curl}/lib/libcurl*.so* mr,
|
|
|
|
${pkgs.openssl}/lib/libssl*.so* mr,
|
|
|
|
${pkgs.openssl}/lib/libcrypto*.so* mr,
|
|
|
|
${pkgs.zlib}/lib/libz*.so* mr,
|
|
|
|
${pkgs.libssh2}/lib/libssh2*.so* mr,
|
|
|
|
${pkgs.systemd}/lib/libsystemd*.so* mr,
|
|
|
|
${pkgs.xz}/lib/liblzma*.so* mr,
|
|
|
|
${pkgs.libgcrypt}/lib/libgcrypt*.so* mr,
|
|
|
|
${pkgs.libgpgerror}/lib/libgpg-error*.so* mr,
|
2013-05-28 19:19:15 +02:00
|
|
|
|
|
|
|
@{PROC}/sys/kernel/random/uuid r,
|
|
|
|
@{PROC}/sys/vm/overcommit_memory r,
|
|
|
|
|
|
|
|
${pkgs.transmission}/share/transmission/** r,
|
|
|
|
|
|
|
|
owner ${settingsDir}/** rw,
|
|
|
|
|
2014-04-15 13:50:39 +02:00
|
|
|
${fullSettings.download-dir}/** rw,
|
|
|
|
${optionalString fullSettings.incomplete-dir-enabled ''
|
|
|
|
${fullSettings.incomplete-dir}/** rw,
|
2013-05-28 19:19:15 +02:00
|
|
|
''}
|
|
|
|
}
|
|
|
|
'')
|
|
|
|
];
|
2013-03-01 21:12:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|