1 { config, options, pkgs, lib, ... }:
7 cfg = config.services.rtorrent;
8 opt = options.services.rtorrent;
11 options.services.rtorrent = {
12 enable = mkEnableOption (lib.mdDoc "rtorrent");
16 default = "/var/lib/rtorrent";
17 description = lib.mdDoc ''
18 The directory where rtorrent stores its data files.
22 downloadDir = mkOption {
24 default = "${cfg.dataDir}/download";
25 defaultText = literalExpression ''"''${config.${opt.dataDir}}/download"'';
26 description = lib.mdDoc ''
27 Where to put downloaded files.
34 description = lib.mdDoc ''
35 User account under which rtorrent runs.
42 description = lib.mdDoc ''
43 Group under which rtorrent runs.
49 default = pkgs.rtorrent;
50 defaultText = literalExpression "pkgs.rtorrent";
51 description = lib.mdDoc ''
52 The rtorrent package to use.
59 description = lib.mdDoc ''
64 openFirewall = mkOption {
67 description = lib.mdDoc ''
68 Whether to open the firewall for the port in {option}`services.rtorrent.port`.
72 rpcSocket = mkOption {
75 default = "/run/rtorrent/rpc.sock";
76 description = lib.mdDoc ''
81 configText = mkOption {
84 description = lib.mdDoc ''
85 The content of {file}`rtorrent.rc`. The [modernized configuration template](https://rtorrent-docs.readthedocs.io/en/latest/cookbook.html#modernized-configuration-template) with the values specified in this module will be prepended using mkBefore. You can use mkForce to overwrite the config completly.
90 config = mkIf cfg.enable {
92 users.groups = mkIf (cfg.group == "rtorrent") {
96 users.users = mkIf (cfg.user == "rtorrent") {
99 shell = pkgs.bashInteractive;
101 description = "rtorrent Daemon user";
106 networking.firewall.allowedTCPPorts = mkIf (cfg.openFirewall) [ cfg.port ];
108 services.rtorrent.configText = mkBefore ''
109 # Instance layout (base paths)
110 method.insert = cfg.basedir, private|const|string, (cat,"${cfg.dataDir}/")
111 method.insert = cfg.watch, private|const|string, (cat,(cfg.basedir),"watch/")
112 method.insert = cfg.logs, private|const|string, (cat,(cfg.basedir),"log/")
113 method.insert = cfg.logfile, private|const|string, (cat,(cfg.logs),(system.time),".log")
114 method.insert = cfg.rpcsock, private|const|string, (cat,"${cfg.rpcSocket}")
116 # Create instance directories
117 execute.throw = sh, -c, (cat, "mkdir -p ", (cfg.basedir), "/session ", (cfg.watch), " ", (cfg.logs))
119 # Listening port for incoming peer traffic (fixed; you can also randomize it)
120 network.port_range.set = ${toString cfg.port}-${toString cfg.port}
121 network.port_random.set = no
123 # Tracker-less torrent and UDP tracker support
124 # (conservative settings for 'private' trackers, change for 'public')
125 dht.mode.set = disable
126 protocol.pex.set = no
127 trackers.use_udp.set = no
130 throttle.max_uploads.set = 100
131 throttle.max_uploads.global.set = 250
133 throttle.min_peers.normal.set = 20
134 throttle.max_peers.normal.set = 60
135 throttle.min_peers.seed.set = 30
136 throttle.max_peers.seed.set = 80
137 trackers.numwant.set = 80
139 protocol.encryption.set = allow_incoming,try_outgoing,enable_retry
141 # Limits for file handle resources, this is optimized for
142 # an `ulimit` of 1024 (a common default). You MUST leave
143 # a ceiling of handles reserved for rTorrent's internal needs!
144 network.http.max_open.set = 50
145 network.max_open_files.set = 600
146 network.max_open_sockets.set = 3000
148 # Memory resource usage (increase if you have a large number of items loaded,
149 # and/or the available resources to spend)
150 pieces.memory.max.set = 1800M
151 network.xmlrpc.size_limit.set = 4M
153 # Basic operational settings (no need to change these)
154 session.path.set = (cat, (cfg.basedir), "session/")
155 directory.default.set = "${cfg.downloadDir}"
156 log.execute = (cat, (cfg.logs), "execute.log")
157 ##log.xmlrpc = (cat, (cfg.logs), "xmlrpc.log")
158 execute.nothrow = sh, -c, (cat, "echo >", (session.path), "rtorrent.pid", " ", (system.pid))
160 # Other operational settings (check & adapt)
162 system.umask.set = 0027
163 system.cwd.set = (cfg.basedir)
164 network.http.dns_cache_timeout.set = 25
165 schedule2 = monitor_diskspace, 15, 60, ((close_low_diskspace, 1000M))
167 # Watch directories (add more as you like, but use unique schedule names)
168 #schedule2 = watch_start, 10, 10, ((load.start, (cat, (cfg.watch), "start/*.torrent")))
169 #schedule2 = watch_load, 11, 10, ((load.normal, (cat, (cfg.watch), "load/*.torrent")))
172 # Levels = critical error warn notice info debug
173 # Groups = connection_* dht_* peer_* rpc_* storage_* thread_* tracker_* torrent_*
174 print = (cat, "Logging to ", (cfg.logfile))
175 log.open_file = "log", (cfg.logfile)
176 log.add_output = "info", "log"
177 ##log.add_output = "tracker_debug", "log"
180 scgi_local = (cfg.rpcsock)
181 schedule = scgi_group,0,0,"execute.nothrow=chown,\":rtorrent\",(cfg.rpcsock)"
182 schedule = scgi_permission,0,0,"execute.nothrow=chmod,\"g+w,o=\",(cfg.rpcsock)"
188 rtorrentConfigFile = pkgs.writeText "rtorrent.rc" cfg.configText;
190 description = "rTorrent system service";
191 after = [ "network.target" ];
192 path = [ cfg.package pkgs.bash ];
193 wantedBy = [ "multi-user.target" ];
198 Restart = "on-failure";
199 WorkingDirectory = cfg.dataDir;
200 ExecStartPre=''${pkgs.bash}/bin/bash -c "if test -e ${cfg.dataDir}/session/rtorrent.lock && test -z $(${pkgs.procps}/bin/pidof rtorrent); then rm -f ${cfg.dataDir}/session/rtorrent.lock; fi"'';
201 ExecStart="${cfg.package}/bin/rtorrent -n -o system.daemon.set=true -o import=${rtorrentConfigFile}";
202 RuntimeDirectory = "rtorrent";
203 RuntimeDirectoryMode = 755;
208 tmpfiles.rules = [ "d '${cfg.dataDir}' 0750 ${cfg.user} ${cfg.group} -" ];