file-sharing.scm 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Simon South <simon@simonsouth.net>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu services file-sharing)
  19. #:use-module (gcrypt base16)
  20. #:use-module (gcrypt hash)
  21. #:use-module (gcrypt random)
  22. #:use-module (gnu services)
  23. #:use-module (gnu services admin)
  24. #:use-module (gnu services configuration)
  25. #:use-module (gnu services shepherd)
  26. #:use-module (gnu packages admin)
  27. #:use-module (gnu packages bittorrent)
  28. #:use-module (gnu packages gnupg)
  29. #:use-module (gnu packages guile)
  30. #:use-module (gnu system shadow)
  31. #:use-module (guix diagnostics)
  32. #:use-module (guix gexp)
  33. #:use-module (guix i18n)
  34. #:use-module (guix modules)
  35. #:use-module (guix packages)
  36. #:use-module (guix records)
  37. #:use-module (ice-9 format)
  38. #:use-module (ice-9 match)
  39. #:use-module (rnrs bytevectors)
  40. #:use-module (srfi srfi-1)
  41. #:use-module (srfi srfi-34)
  42. #:use-module (srfi srfi-35)
  43. #:export (transmission-daemon-configuration
  44. transmission-daemon-service-type
  45. transmission-password-hash
  46. transmission-random-salt))
  47. ;;;
  48. ;;; Transmission Daemon.
  49. ;;;
  50. (define %transmission-daemon-user "transmission")
  51. (define %transmission-daemon-group "transmission")
  52. (define %transmission-daemon-configuration-directory
  53. "/var/lib/transmission-daemon")
  54. (define %transmission-daemon-log-file
  55. "/var/log/transmission.log")
  56. (define %transmission-salt-length 8)
  57. (define (transmission-password-hash password salt)
  58. "Returns a string containing the result of hashing @var{password} together
  59. with @var{salt}, in the format recognized by Transmission clients for their
  60. @code{rpc-password} configuration setting.
  61. @var{salt} must be an eight-character string. The
  62. @code{transmission-random-salt} procedure can be used to generate a suitable
  63. salt value at random."
  64. (if (not (and (string? salt)
  65. (eq? (string-length salt) %transmission-salt-length)))
  66. (raise (formatted-message
  67. (G_ "salt value must be a string of ~d characters")
  68. %transmission-salt-length))
  69. (string-append "{"
  70. (bytevector->base16-string
  71. (sha1 (string->utf8 (string-append password salt))))
  72. salt)))
  73. (define (transmission-random-salt)
  74. "Returns a string containing a random, eight-character salt value of the
  75. type generated and used by Transmission clients, suitable for passing to the
  76. @code{transmission-password-hash} procedure."
  77. ;; This implementation matches a portion of Transmission's tr_ssha1
  78. ;; function. See libtransmission/crypto-utils.c in the Transmission source
  79. ;; distribution.
  80. (let ((salter (string-append "0123456789"
  81. "abcdefghijklmnopqrstuvwxyz"
  82. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  83. "./")))
  84. (list->string
  85. (map (lambda (u8)
  86. (string-ref salter (modulo u8 (string-length salter))))
  87. (bytevector->u8-list
  88. (gen-random-bv %transmission-salt-length %gcry-strong-random))))))
  89. (define (uglify-field-name field-name)
  90. (string-delete #\? (symbol->string field-name)))
  91. (define (serialize-field field-name val)
  92. ;; "Serialize" each configuration field as a G-expression containing a
  93. ;; name-value pair, the collection of which will subsequently be serialized
  94. ;; to disk as a JSON object.
  95. #~(#$(uglify-field-name field-name) . #$val))
  96. (define serialize-boolean serialize-field)
  97. (define serialize-integer serialize-field)
  98. (define serialize-rational serialize-field)
  99. (define serialize-string serialize-field)
  100. (define-maybe string)
  101. ;; Override the definition of "serialize-maybe-string", as we need to output a
  102. ;; name-value pair for the JSON builder.
  103. (set! serialize-maybe-string
  104. (lambda (field-name val)
  105. (serialize-string field-name (maybe-value val ""))))
  106. (define (string-list? val)
  107. (and (list? val)
  108. (and-map (lambda (x)
  109. (and (string? x)
  110. (not (string-index x #\,))))
  111. val)))
  112. (define (serialize-string-list field-name val)
  113. (serialize-field field-name (string-join val ",")))
  114. (define days
  115. '((sunday . #b0000001)
  116. (monday . #b0000010)
  117. (tuesday . #b0000100)
  118. (wednesday . #b0001000)
  119. (thursday . #b0010000)
  120. (friday . #b0100000)
  121. (saturday . #b1000000)))
  122. (define day-lists
  123. (list (cons 'weekdays '(monday tuesday wednesday thursday friday))
  124. (cons 'weekends '(saturday sunday))
  125. (cons 'all (map car days))))
  126. (define (day-list? val)
  127. (or (and (symbol? val)
  128. (assq val day-lists))
  129. (and (list? val)
  130. (and-map (lambda (x)
  131. (and (symbol? x)
  132. (assq x days)))
  133. val))))
  134. (define (serialize-day-list field-name val)
  135. (serialize-integer field-name
  136. (reduce logior
  137. #b0000000
  138. (map (lambda (day)
  139. (assq-ref days day))
  140. (if (symbol? val)
  141. (assq-ref day-lists val)
  142. val)))))
  143. (define encryption-modes
  144. '((prefer-unencrypted-connections . 0)
  145. (prefer-encrypted-connections . 1)
  146. (require-encrypted-connections . 2)))
  147. (define (encryption-mode? val)
  148. (and (symbol? val)
  149. (assq val encryption-modes)))
  150. (define (serialize-encryption-mode field-name val)
  151. (serialize-integer field-name (assq-ref encryption-modes val)))
  152. (define serialize-file-like serialize-field)
  153. (define (file-object? val)
  154. (or (string? val)
  155. (file-like? val)))
  156. (define (serialize-file-object field-name val)
  157. (if (file-like? val)
  158. (serialize-file-like field-name val)
  159. (serialize-string field-name val)))
  160. (define-maybe file-object)
  161. (set! serialize-maybe-file-object
  162. (lambda (field-name val)
  163. (if (maybe-value-set? val)
  164. (serialize-file-object field-name val)
  165. (serialize-string field-name ""))))
  166. (define (file-object-list? val)
  167. (and (list? val)
  168. (and-map file-object? val)))
  169. (define serialize-file-object-list serialize-field)
  170. (define message-levels
  171. '((none . 0)
  172. (error . 1)
  173. (info . 2)
  174. (debug . 3)))
  175. (define (message-level? val)
  176. (and (symbol? val)
  177. (assq val message-levels)))
  178. (define (serialize-message-level field-name val)
  179. (serialize-integer field-name (assq-ref message-levels val)))
  180. (define (non-negative-integer? val)
  181. (and (integer? val)
  182. (not (negative? val))))
  183. (define serialize-non-negative-integer serialize-integer)
  184. (define (non-negative-rational? val)
  185. (and (rational? val)
  186. (not (negative? val))))
  187. (define serialize-non-negative-rational serialize-rational)
  188. (define (port-number? val)
  189. (and (integer? val)
  190. (>= val 1)
  191. (<= val 65535)))
  192. (define serialize-port-number serialize-integer)
  193. (define preallocation-modes
  194. '((none . 0)
  195. (fast . 1)
  196. (sparse . 1)
  197. (full . 2)))
  198. (define (preallocation-mode? val)
  199. (and (symbol? val)
  200. (assq val preallocation-modes)))
  201. (define (serialize-preallocation-mode field-name val)
  202. (serialize-integer field-name (assq-ref preallocation-modes val)))
  203. (define tcp-types-of-service
  204. '((default . "default")
  205. (low-cost . "lowcost")
  206. (throughput . "throughput")
  207. (low-delay . "lowdelay")
  208. (reliability . "reliability")))
  209. (define (tcp-type-of-service? val)
  210. (and (symbol? val)
  211. (assq val tcp-types-of-service)))
  212. (define (serialize-tcp-type-of-service field-name val)
  213. (serialize-string field-name (assq-ref tcp-types-of-service val)))
  214. (define (transmission-password-hash? val)
  215. (and (string? val)
  216. (= (string-length val) 49)
  217. (eqv? (string-ref val 0) #\{)
  218. (string-every char-set:hex-digit val 1 41)))
  219. (define serialize-transmission-password-hash serialize-string)
  220. (define-maybe transmission-password-hash)
  221. (set! serialize-maybe-transmission-password-hash serialize-maybe-string)
  222. (define (umask? val)
  223. (and (integer? val)
  224. (>= val #o000)
  225. (<= val #o777)))
  226. (define serialize-umask serialize-integer) ; must use decimal representation
  227. (define-configuration transmission-daemon-configuration
  228. ;; Settings internal to this service definition.
  229. (transmission
  230. (file-like transmission)
  231. "The Transmission package to use.")
  232. (stop-wait-period
  233. (non-negative-integer 10)
  234. "The period, in seconds, to wait when stopping the service for
  235. @command{transmission-daemon} to exit before killing its process. This allows
  236. the daemon time to complete its housekeeping and send a final update to
  237. trackers as it shuts down. On slow hosts, or hosts with a slow network
  238. connection, this value may need to be increased.")
  239. ;; Files and directories.
  240. (download-dir
  241. (string (string-append %transmission-daemon-configuration-directory
  242. "/downloads"))
  243. "The directory to which torrent files are downloaded.")
  244. (incomplete-dir-enabled?
  245. (boolean #f)
  246. "If @code{#t}, files will be held in @code{incomplete-dir} while their
  247. torrent is being downloaded, then moved to @code{download-dir} once the
  248. torrent is complete. Otherwise, files for all torrents (including those still
  249. being downloaded) will be placed in @code{download-dir}.")
  250. (incomplete-dir
  251. maybe-string
  252. "The directory in which files from incompletely downloaded torrents will be
  253. held when @code{incomplete-dir-enabled?} is @code{#t}.")
  254. (umask
  255. (umask #o022)
  256. "The file mode creation mask used for downloaded files. (See the
  257. @command{umask} man page for more information.)")
  258. (rename-partial-files?
  259. (boolean #t)
  260. "When @code{#t}, ``.part'' is appended to the name of partially downloaded
  261. files.")
  262. (preallocation
  263. (preallocation-mode 'fast)
  264. "The mode by which space should be preallocated for downloaded files, one
  265. of @code{none}, @code{fast} (or @code{sparse}) and @code{full}. Specifying
  266. @code{full} will minimize disk fragmentation at a cost to file-creation
  267. speed.")
  268. (watch-dir-enabled?
  269. (boolean #f)
  270. "If @code{#t}, the directory specified by @code{watch-dir} will be watched
  271. for new @file{.torrent} files and the torrents they describe added
  272. automatically (and the original files removed, if
  273. @code{trash-original-torrent-files?} is @code{#t}).")
  274. (watch-dir
  275. maybe-string
  276. "The directory to be watched for @file{.torrent} files indicating new
  277. torrents to be added, when @code{watch-dir-enabled} is @code{#t}.")
  278. (trash-original-torrent-files?
  279. (boolean #f)
  280. "When @code{#t}, @file{.torrent} files will be deleted from the watch
  281. directory once their torrent has been added (see
  282. @code{watch-directory-enabled?}).")
  283. ;; Bandwidth limits.
  284. (speed-limit-down-enabled?
  285. (boolean #f)
  286. "When @code{#t}, the daemon's download speed will be limited to the rate
  287. specified by @code{speed-limit-down}.")
  288. (speed-limit-down
  289. (non-negative-integer 100)
  290. "The default global-maximum download speed, in kilobytes per second.")
  291. (speed-limit-up-enabled?
  292. (boolean #f)
  293. "When @code{#t}, the daemon's upload speed will be limited to the rate
  294. specified by @code{speed-limit-up}.")
  295. (speed-limit-up
  296. (non-negative-integer 100)
  297. "The default global-maximum upload speed, in kilobytes per second.")
  298. (alt-speed-enabled?
  299. (boolean #f)
  300. "When @code{#t}, the alternate speed limits @code{alt-speed-down} and
  301. @code{alt-speed-up} are used (in place of @code{speed-limit-down} and
  302. @code{speed-limit-up}, if they are enabled) to constrain the daemon's
  303. bandwidth usage. This can be scheduled to occur automatically at certain
  304. times during the week; see @code{alt-speed-time-enabled?}.")
  305. (alt-speed-down
  306. (non-negative-integer 50)
  307. "The alternate global-maximum download speed, in kilobytes per second.")
  308. (alt-speed-up
  309. (non-negative-integer 50)
  310. "The alternate global-maximum upload speed, in kilobytes per second.")
  311. ;; Bandwidth-limit scheduling.
  312. (alt-speed-time-enabled?
  313. (boolean #f)
  314. "When @code{#t}, the alternate speed limits @code{alt-speed-down} and
  315. @code{alt-speed-up} will be enabled automatically during the periods specified
  316. by @code{alt-speed-time-day}, @code{alt-speed-time-begin} and
  317. @code{alt-time-speed-end}.")
  318. (alt-speed-time-day
  319. (day-list 'all)
  320. "The days of the week on which the alternate-speed schedule should be used,
  321. specified either as a list of days (@code{sunday}, @code{monday}, and so on)
  322. or using one of the symbols @code{weekdays}, @code{weekends} or @code{all}.")
  323. (alt-speed-time-begin
  324. (non-negative-integer 540)
  325. "The time of day at which to enable the alternate speed limits,
  326. expressed as a number of minutes since midnight.")
  327. (alt-speed-time-end
  328. (non-negative-integer 1020)
  329. "The time of day at which to disable the alternate speed limits,
  330. expressed as a number of minutes since midnight.")
  331. ;; Peer networking.
  332. (bind-address-ipv4
  333. (string "0.0.0.0")
  334. "The IP address at which to listen for peer connections, or ``0.0.0.0'' to
  335. listen at all available IP addresses.")
  336. (bind-address-ipv6
  337. (string "::")
  338. "The IPv6 address at which to listen for peer connections, or ``::'' to
  339. listen at all available IPv6 addresses.")
  340. (peer-port-random-on-start?
  341. (boolean #f)
  342. "If @code{#t}, when the daemon starts it will select a port at random on
  343. which to listen for peer connections, from the range specified (inclusively)
  344. by @code{peer-port-random-low} and @code{peer-port-random-high}. Otherwise,
  345. it listens on the port specified by @code{peer-port}.")
  346. (peer-port-random-low
  347. (port-number 49152)
  348. "The lowest selectable port number when @code{peer-port-random-on-start?}
  349. is @code{#t}.")
  350. (peer-port-random-high
  351. (port-number 65535)
  352. "The highest selectable port number when @code{peer-port-random-on-start}
  353. is @code{#t}.")
  354. (peer-port
  355. (port-number 51413)
  356. "The port on which to listen for peer connections when
  357. @code{peer-port-random-on-start?} is @code{#f}.")
  358. (port-forwarding-enabled?
  359. (boolean #t)
  360. "If @code{#t}, the daemon will attempt to configure port-forwarding on an
  361. upstream gateway automatically using @acronym{UPnP} and @acronym{NAT-PMP}.")
  362. (encryption
  363. (encryption-mode 'prefer-encrypted-connections)
  364. "The encryption preference for peer connections, one of
  365. @code{prefer-unencrypted-connections}, @code{prefer-encrypted-connections} or
  366. @code{require-encrypted-connections}.")
  367. (peer-congestion-algorithm
  368. maybe-string
  369. "The TCP congestion-control algorithm to use for peer connections,
  370. specified using a string recognized by the operating system in calls to
  371. @code{setsockopt} (or leave it unset, in which case the operating-system
  372. default is used).
  373. Note that on GNU/Linux systems, the kernel must be configured to allow
  374. processes to use a congestion-control algorithm not in the default set;
  375. otherwise, it will deny these requests with ``Operation not permitted''. To
  376. see which algorithms are available on your system and which are currently
  377. permitted for use, look at the contents of the files
  378. @file{tcp_available_congestion_control} and
  379. @file{tcp_allowed_congestion_control} in the @file{/proc/sys/net/ipv4}
  380. directory.
  381. As an example, to have Transmission Daemon use
  382. @uref{http://www-ece.rice.edu/networks/TCP-LP/, the TCP Low Priority
  383. congestion-control algorithm}, you'll need to modify your kernel configuration
  384. to build in support for the algorithm, then update your operating-system
  385. configuration to allow its use by adding a @code{sysctl-service-type}
  386. service (or updating the existing one's configuration) with lines like the
  387. following:
  388. @lisp
  389. (service sysctl-service-type
  390. (sysctl-configuration
  391. (settings
  392. (\"net.ipv4.tcp_allowed_congestion_control\" .
  393. \"reno cubic lp\"))))
  394. @end lisp
  395. The Transmission Daemon configuration can then be updated with
  396. @lisp
  397. (peer-congestion-algorithm \"lp\")
  398. @end lisp
  399. and the system reconfigured to have the changes take effect.")
  400. (peer-socket-tos
  401. (tcp-type-of-service 'default)
  402. "The type of service to request in outgoing @acronym{TCP} packets,
  403. one of @code{default}, @code{low-cost}, @code{throughput}, @code{low-delay}
  404. and @code{reliability}.")
  405. (peer-limit-global
  406. (non-negative-integer 200)
  407. "The global limit on the number of connected peers.")
  408. (peer-limit-per-torrent
  409. (non-negative-integer 50)
  410. "The per-torrent limit on the number of connected peers.")
  411. (upload-slots-per-torrent
  412. (non-negative-integer 14)
  413. "The maximum number of peers to which the daemon will upload data
  414. simultaneously for each torrent.")
  415. (peer-id-ttl-hours
  416. (non-negative-integer 6)
  417. "The maximum lifespan, in hours, of the peer ID associated with each public
  418. torrent before it is regenerated.")
  419. ;; Peer blocklists.
  420. (blocklist-enabled?
  421. (boolean #f)
  422. "When @code{#t}, the daemon will ignore peers mentioned in the blocklist it
  423. has most recently downloaded from @code{blocklist-url}.")
  424. (blocklist-url
  425. maybe-string
  426. "The URL of a peer blocklist (in @acronym{P2P}-plaintext or eMule
  427. @file{.dat} format) to be periodically downloaded and applied when
  428. @code{blocklist-enabled?} is @code{#t}.")
  429. ;; Queueing.
  430. (download-queue-enabled?
  431. (boolean #t)
  432. "If @code{#t}, the daemon will be limited to downloading at most
  433. @code{download-queue-size} non-stalled torrents simultaneously.")
  434. (download-queue-size
  435. (non-negative-integer 5)
  436. "The size of the daemon's download queue, which limits the number of
  437. non-stalled torrents it will download at any one time when
  438. @code{download-queue-enabled?} is @code{#t}.")
  439. (seed-queue-enabled?
  440. (boolean #f)
  441. "If @code{#t}, the daemon will be limited to seeding at most
  442. @code{seed-queue-size} non-stalled torrents simultaneously.")
  443. (seed-queue-size
  444. (non-negative-integer 10)
  445. "The size of the daemon's seed queue, which limits the number of
  446. non-stalled torrents it will seed at any one time when
  447. @code{seed-queue-enabled?} is @code{#t}.")
  448. (queue-stalled-enabled?
  449. (boolean #t)
  450. "When @code{#t}, the daemon will consider torrents for which it has not
  451. shared data in the past @code{queue-stalled-minutes} minutes to be stalled and
  452. not count them against its @code{download-queue-size} and
  453. @code{seed-queue-size} limits.")
  454. (queue-stalled-minutes
  455. (non-negative-integer 30)
  456. "The maximum period, in minutes, a torrent may be idle before it is
  457. considered to be stalled, when @code{queue-stalled-enabled?} is @code{#t}.")
  458. ;; Seeding limits.
  459. (ratio-limit-enabled?
  460. (boolean #f)
  461. "When @code{#t}, a torrent being seeded will automatically be paused once
  462. it reaches the ratio specified by @code{ratio-limit}.")
  463. (ratio-limit
  464. (non-negative-rational 2.0)
  465. "The ratio at which a torrent being seeded will be paused, when
  466. @code{ratio-limit-enabled?} is @code{#t}.")
  467. (idle-seeding-limit-enabled?
  468. (boolean #f)
  469. "When @code{#t}, a torrent being seeded will automatically be paused once
  470. it has been idle for @code{idle-seeding-limit} minutes.")
  471. (idle-seeding-limit
  472. (non-negative-integer 30)
  473. "The maximum period, in minutes, a torrent being seeded may be idle before
  474. it is paused, when @code{idle-seeding-limit-enabled?} is @code{#t}.")
  475. ;; BitTorrent extensions.
  476. (dht-enabled?
  477. (boolean #t)
  478. "Enable @uref{http://bittorrent.org/beps/bep_0005.html, the distributed
  479. hash table (@acronym{DHT}) protocol}, which supports the use of trackerless
  480. torrents.")
  481. (lpd-enabled?
  482. (boolean #f)
  483. "Enable @url{https://en.wikipedia.org/wiki/Local_Peer_Discovery, local peer
  484. discovery} (@acronym{LPD}), which allows the discovery of peers on the local
  485. network and may reduce the amount of data sent over the public Internet.")
  486. (pex-enabled?
  487. (boolean #t)
  488. "Enable @url{https://en.wikipedia.org/wiki/Peer_exchange, peer
  489. exchange} (@acronym{PEX}), which reduces the daemon's reliance on external
  490. trackers and may improve its performance.")
  491. (utp-enabled?
  492. (boolean #t)
  493. "Enable @url{http://bittorrent.org/beps/bep_0029.html, the micro transport
  494. protocol} (@acronym{uTP}), which aims to reduce the impact of BitTorrent
  495. traffic on other users of the local network while maintaining full utilization
  496. of the available bandwidth.")
  497. ;; Remote procedure call (RPC) interface.
  498. (rpc-enabled?
  499. (boolean #t)
  500. "If @code{#t}, enable the remote procedure call (@acronym{RPC}) interface,
  501. which allows remote control of the daemon via its Web interface, the
  502. @command{transmission-remote} command-line client, and similar tools.")
  503. (rpc-bind-address
  504. (string "0.0.0.0")
  505. "The IP address at which to listen for @acronym{RPC} connections, or
  506. ``0.0.0.0'' to listen at all available IP addresses.")
  507. (rpc-port
  508. (port-number 9091)
  509. "The port on which to listen for @acronym{RPC} connections.")
  510. (rpc-url
  511. (string "/transmission/")
  512. "The path prefix to use in the @acronym{RPC}-endpoint @acronym{URL}.")
  513. (rpc-authentication-required?
  514. (boolean #f)
  515. "When @code{#t}, clients must authenticate (see @code{rpc-username} and
  516. @code{rpc-password}) when using the @acronym{RPC} interface. Note this has
  517. the side effect of disabling host-name whitelisting (see
  518. @code{rpc-host-whitelist-enabled?}.")
  519. (rpc-username
  520. maybe-string
  521. "The username required by clients to access the @acronym{RPC} interface
  522. when @code{rpc-authentication-required?} is @code{#t}.")
  523. (rpc-password
  524. maybe-transmission-password-hash
  525. "The password required by clients to access the @acronym{RPC} interface
  526. when @code{rpc-authentication-required?} is @code{#t}. This must be specified
  527. using a password hash in the format recognized by Transmission clients, either
  528. copied from an existing @file{settings.json} file or generated using the
  529. @code{transmission-password-hash} procedure.")
  530. (rpc-whitelist-enabled?
  531. (boolean #t)
  532. "When @code{#t}, @acronym{RPC} requests will be accepted only when they
  533. originate from an address specified in @code{rpc-whitelist}.")
  534. (rpc-whitelist
  535. (string-list '("127.0.0.1" "::1"))
  536. "The list of IP and IPv6 addresses from which @acronym{RPC} requests will
  537. be accepted when @code{rpc-whitelist-enabled?} is @code{#t}. Wildcards may be
  538. specified using @samp{*}.")
  539. (rpc-host-whitelist-enabled?
  540. (boolean #t)
  541. "When @code{#t}, @acronym{RPC} requests will be accepted only when they are
  542. addressed to a host named in @code{rpc-host-whitelist}. Note that requests to
  543. ``localhost'' or ``localhost.'', or to a numeric address, are always accepted
  544. regardless of these settings.
  545. Note also this functionality is disabled when
  546. @code{rpc-authentication-required?} is @code{#t}.")
  547. (rpc-host-whitelist
  548. (string-list '())
  549. "The list of host names recognized by the @acronym{RPC} server when
  550. @code{rpc-host-whitelist-enabled?} is @code{#t}.")
  551. ;; Miscellaneous.
  552. (message-level
  553. (message-level 'info)
  554. "The minimum severity level of messages to be logged (to
  555. @file{/var/log/transmission.log}) by the daemon, one of @code{none} (no
  556. logging), @code{error}, @code{info} and @code{debug}.")
  557. (start-added-torrents?
  558. (boolean #t)
  559. "When @code{#t}, torrents are started as soon as they are added; otherwise,
  560. they are added in ``paused'' state.")
  561. (script-torrent-done-enabled?
  562. (boolean #f)
  563. "When @code{#t}, the script specified by
  564. @code{script-torrent-done-filename} will be invoked each time a torrent
  565. completes.")
  566. (script-torrent-done-filename
  567. maybe-file-object
  568. "A file name or file-like object specifying a script to run each time a
  569. torrent completes, when @code{script-torrent-done-enabled?} is @code{#t}.")
  570. (scrape-paused-torrents-enabled?
  571. (boolean #t)
  572. "When @code{#t}, the daemon will scrape trackers for a torrent even when
  573. the torrent is paused.")
  574. (cache-size-mb
  575. (non-negative-integer 4)
  576. "The amount of memory, in megabytes, to allocate for the daemon's in-memory
  577. cache. A larger value may increase performance by reducing the frequency of
  578. disk I/O.")
  579. (prefetch-enabled?
  580. (boolean #t)
  581. "When @code{#t}, the daemon will try to improve I/O performance by hinting
  582. to the operating system which data is likely to be read next from disk to
  583. satisfy requests from peers."))
  584. (define (transmission-daemon-shepherd-service config)
  585. "Return a <shepherd-service> for Transmission Daemon with CONFIG."
  586. (let ((transmission
  587. (transmission-daemon-configuration-transmission config))
  588. (stop-wait-period
  589. (transmission-daemon-configuration-stop-wait-period config)))
  590. (list
  591. (shepherd-service
  592. (provision '(transmission-daemon transmission bittorrent))
  593. (requirement '(networking))
  594. (documentation "Share files using the BitTorrent protocol.")
  595. (start #~(make-forkexec-constructor
  596. '(#$(file-append transmission "/bin/transmission-daemon")
  597. "--config-dir"
  598. #$%transmission-daemon-configuration-directory
  599. "--foreground")
  600. #:user #$%transmission-daemon-user
  601. #:group #$%transmission-daemon-group
  602. #:directory #$%transmission-daemon-configuration-directory
  603. #:log-file #$%transmission-daemon-log-file
  604. #:environment-variables
  605. '("CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt")))
  606. (stop #~(lambda (pid)
  607. (kill pid SIGTERM)
  608. ;; Transmission Daemon normally needs some time to shut down,
  609. ;; as it will complete some housekeeping and send a final
  610. ;; update to trackers before it exits.
  611. ;;
  612. ;; Wait a reasonable period for it to stop before continuing.
  613. ;; If we don't do this, restarting the service can fail as the
  614. ;; new daemon process finds the old one still running and
  615. ;; attached to the port used for peer connections.
  616. (let wait-before-killing ((period #$stop-wait-period))
  617. (if (zero? (car (waitpid pid WNOHANG)))
  618. (if (positive? period)
  619. (begin
  620. (sleep 1)
  621. (wait-before-killing (- period 1)))
  622. (begin
  623. (format #t
  624. #$(G_ "Wait period expired; killing \
  625. transmission-daemon (pid ~a).~%")
  626. pid)
  627. (display #$(G_ "(If you see this message \
  628. regularly, you may need to increase the value
  629. of 'stop-wait-period' in the service configuration.)\n"))
  630. (kill pid SIGKILL)))))
  631. #f))
  632. (actions
  633. (list
  634. (shepherd-action
  635. (name 'reload)
  636. (documentation "Reload the settings file from disk.")
  637. (procedure #~(lambda (pid)
  638. (if pid
  639. (begin
  640. (kill pid SIGHUP)
  641. (display #$(G_ "Service transmission-daemon has \
  642. been asked to reload its settings file.")))
  643. (display #$(G_ "Service transmission-daemon is not \
  644. running."))))))))))))
  645. (define %transmission-daemon-accounts
  646. (list (user-group
  647. (name %transmission-daemon-group)
  648. (system? #t))
  649. (user-account
  650. (name %transmission-daemon-user)
  651. (group %transmission-daemon-group)
  652. (comment "Transmission Daemon service account")
  653. (home-directory %transmission-daemon-configuration-directory)
  654. (shell (file-append shadow "/sbin/nologin"))
  655. (system? #t))))
  656. (define %transmission-daemon-log-rotations
  657. (list (log-rotation
  658. (files (list %transmission-daemon-log-file)))))
  659. (define (transmission-daemon-computed-settings-file config)
  660. "Return a @code{computed-file} object that, when unquoted in a G-expression,
  661. produces a Transmission settings file (@file{settings.json}) matching CONFIG."
  662. (let ((settings
  663. ;; "Serialize" the configuration settings as a list of G-expressions
  664. ;; containing a name-value pair, which will ultimately be sorted and
  665. ;; serialized to the settings file as a JSON object.
  666. (map
  667. (lambda (field)
  668. ((configuration-field-serializer field)
  669. (configuration-field-name field)
  670. ((configuration-field-getter field) config)))
  671. (filter
  672. (lambda (field)
  673. ;; Omit configuration fields that are used only internally by
  674. ;; this service definition.
  675. (not (memq (configuration-field-name field)
  676. '(transmission stop-wait-period))))
  677. transmission-daemon-configuration-fields))))
  678. (computed-file
  679. "settings.json"
  680. (with-extensions (list guile-gcrypt guile-json-4)
  681. (with-imported-modules (source-module-closure '((json builder)))
  682. #~(begin
  683. (use-modules (json builder))
  684. (with-output-to-file #$output
  685. (lambda ()
  686. (scm->json (sort-list '(#$@settings)
  687. (lambda (x y)
  688. (string<=? (car x) (car y))))
  689. #:pretty #t)))))))))
  690. (define (transmission-daemon-activation config)
  691. "Return the Transmission Daemon activation GEXP for CONFIG."
  692. (let ((config-dir %transmission-daemon-configuration-directory)
  693. (incomplete-dir-enabled
  694. (transmission-daemon-configuration-incomplete-dir-enabled? config))
  695. (incomplete-dir
  696. (transmission-daemon-configuration-incomplete-dir config))
  697. (watch-dir-enabled
  698. (transmission-daemon-configuration-watch-dir-enabled? config))
  699. (watch-dir
  700. (transmission-daemon-configuration-watch-dir config)))
  701. (with-imported-modules (source-module-closure '((guix build utils)))
  702. #~(begin
  703. (use-modules (guix build utils))
  704. (let ((owner (getpwnam #$%transmission-daemon-user)))
  705. (define (mkdir-p/perms directory perms)
  706. (mkdir-p directory)
  707. (chown directory (passwd:uid owner) (passwd:gid owner))
  708. (chmod directory perms))
  709. ;; Create the directories Transmission Daemon is configured to use
  710. ;; and assign them suitable permissions.
  711. (for-each (lambda (directory-specification)
  712. (apply mkdir-p/perms directory-specification))
  713. '(#$@(append
  714. `((,config-dir #o750))
  715. (if incomplete-dir-enabled
  716. `((,incomplete-dir #o750))
  717. '())
  718. (if watch-dir-enabled
  719. `((,watch-dir #o770))
  720. '())))))
  721. ;; Generate and activate the daemon's settings file, settings.json.
  722. (activate-special-files
  723. '((#$(string-append config-dir "/settings.json")
  724. #$(transmission-daemon-computed-settings-file config))))))))
  725. (define transmission-daemon-service-type
  726. (service-type
  727. (name 'transmission)
  728. (extensions
  729. (list (service-extension shepherd-root-service-type
  730. transmission-daemon-shepherd-service)
  731. (service-extension account-service-type
  732. (const %transmission-daemon-accounts))
  733. (service-extension rottlog-service-type
  734. (const %transmission-daemon-log-rotations))
  735. (service-extension activation-service-type
  736. transmission-daemon-activation)))
  737. (default-value (transmission-daemon-configuration))
  738. (description "Share files using the BitTorrent protocol.")))
  739. (define (generate-transmission-daemon-documentation)
  740. (generate-documentation
  741. `((transmission-daemon-configuration
  742. ,transmission-daemon-configuration-fields))
  743. 'transmission-daemon-configuration))