messaging.scm 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
  3. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  4. ;;; Copyright © 2015, 2017-2020, 2022 Ludovic Courtès <ludo@gnu.org>
  5. ;;; Copyright © 2018 Pierre-Antoine Rouby <contact@parouby.fr>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu services messaging)
  22. #:use-module (gnu packages admin)
  23. #:use-module (gnu packages base)
  24. #:use-module (gnu packages irc)
  25. #:use-module (gnu packages messaging)
  26. #:use-module (gnu packages tls)
  27. #:use-module (gnu services)
  28. #:use-module (gnu services shepherd)
  29. #:use-module (gnu services configuration)
  30. #:use-module (gnu system shadow)
  31. #:autoload (gnu build linux-container) (%namespaces)
  32. #:use-module ((gnu system file-systems) #:select (file-system-mapping))
  33. #:use-module (guix gexp)
  34. #:use-module (guix modules)
  35. #:use-module (guix records)
  36. #:use-module (guix packages)
  37. #:use-module (guix deprecation)
  38. #:use-module (guix least-authority)
  39. #:use-module (srfi srfi-1)
  40. #:use-module (srfi srfi-35)
  41. #:use-module (ice-9 match)
  42. #:export (prosody-service-type
  43. prosody-configuration
  44. opaque-prosody-configuration
  45. virtualhost-configuration
  46. int-component-configuration
  47. ext-component-configuration
  48. mod-muc-configuration
  49. ssl-configuration
  50. %default-modules-enabled
  51. prosody-configuration-pidfile
  52. bitlbee-configuration
  53. bitlbee-configuration?
  54. bitlbee-service-type
  55. quassel-configuration
  56. quassel-service-type))
  57. ;;; Commentary:
  58. ;;;
  59. ;;; Messaging services.
  60. ;;;
  61. ;;; Code:
  62. (define-syntax define-all-configurations
  63. (lambda (stx)
  64. (define-syntax-rule (id ctx parts ...)
  65. "Assemble PARTS into a raw (unhygienic) identifier."
  66. (datum->syntax ctx (symbol-append (syntax->datum parts) ...)))
  67. (define (make-pred arg)
  68. (lambda (field target)
  69. (and (memq (syntax->datum target) `(common ,arg)) field)))
  70. (syntax-case stx ()
  71. ;; TODO Also handle (field-type) form, without a default.
  72. ((_ stem (field (field-type def) doc target) ...)
  73. (with-syntax (((new-field-type ...)
  74. (map (lambda (field-type target)
  75. (if (and (eq? 'common (syntax->datum target))
  76. (not (string-prefix?
  77. "maybe-"
  78. (symbol->string
  79. (syntax->datum field-type)))))
  80. (id #'stem #'maybe- field-type) field-type))
  81. #'(field-type ...) #'(target ...)))
  82. ((new-def ...)
  83. (map (lambda (def target)
  84. (if (eq? 'common (syntax->datum target))
  85. ;; TODO Use the %unset-value variable, or
  86. ;; even better just simplify this so that it
  87. ;; doesn't interfere with
  88. ;; define-configuration and define-maybe
  89. ;; internals.
  90. #''%unset-marker% def))
  91. #'(def ...) #'(target ...)))
  92. ((new-doc ...)
  93. (map (lambda (doc target)
  94. (if (eq? 'common (syntax->datum target))
  95. "" doc))
  96. #'(doc ...) #'(target ...))))
  97. #`(begin
  98. (define #,(id #'stem #'common-fields)
  99. '(#,@(filter-map (make-pred #f) #'(field ...) #'(target ...))))
  100. (define-configuration #,(id #'stem #'prosody-configuration)
  101. #,@(filter-map (make-pred 'global)
  102. #'((field (field-type def) doc) ...)
  103. #'(target ...)))
  104. (define-configuration #,(id #'stem #'virtualhost-configuration)
  105. #,@(filter-map (make-pred 'virtualhost)
  106. #'((field (new-field-type new-def) new-doc) ...)
  107. #'(target ...)))
  108. (define-configuration #,(id #'stem #'int-component-configuration)
  109. #,@(filter-map (make-pred 'int-component)
  110. #'((field (new-field-type new-def) new-doc) ...)
  111. #'(target ...)))
  112. (define-configuration #,(id #'stem #'ext-component-configuration)
  113. #,@(filter-map (make-pred 'ext-component)
  114. #'((field (new-field-type new-def) new-doc) ...)
  115. #'(target ...)))))))))
  116. (define (uglify-field-name field-name)
  117. (let ((str (symbol->string field-name)))
  118. (string-join (string-split (if (string-suffix? "?" str)
  119. (substring str 0 (1- (string-length str)))
  120. str)
  121. #\-)
  122. "_")))
  123. (define (serialize-field field-name val)
  124. #~(format #f "~a = ~a;\n" #$(uglify-field-name field-name) #$val))
  125. (define (serialize-field-list field-name val)
  126. (serialize-field field-name #~(format #f "{\n~@{~a;\n~}}" #$@val)))
  127. (define (serialize-boolean field-name val)
  128. (serialize-field field-name (if val "true" "false")))
  129. (define-maybe boolean)
  130. (define (string-or-boolean? val)
  131. (or (string? val) (boolean? val)))
  132. (define (serialize-string-or-boolean field-name val)
  133. (if (string? val)
  134. (serialize-string field-name val)
  135. (serialize-boolean field-name val)))
  136. (define (non-negative-integer? val)
  137. (and (exact-integer? val) (not (negative? val))))
  138. (define (serialize-non-negative-integer field-name val)
  139. (serialize-field field-name (number->string val)))
  140. (define-maybe non-negative-integer)
  141. (define (non-negative-integer-list? val)
  142. (and (list? val) (and-map non-negative-integer? val)))
  143. (define (serialize-non-negative-integer-list field-name val)
  144. (serialize-field-list field-name (map number->string val)))
  145. (define-maybe non-negative-integer-list)
  146. (define (enclose-quotes s)
  147. #~(string-append "\"" #$s "\""))
  148. (define (serialize-string field-name val)
  149. (serialize-field field-name (enclose-quotes val)))
  150. (define-maybe string)
  151. (define (string-list? val)
  152. (and (list? val)
  153. (and-map (lambda (x)
  154. (and (string? x) (not (string-index x #\,))))
  155. val)))
  156. (define (serialize-string-list field-name val)
  157. (serialize-field-list field-name (map enclose-quotes val)))
  158. (define-maybe string-list)
  159. (define (module-list? val)
  160. (string-list? val))
  161. (define (serialize-module-list field-name val)
  162. (serialize-string-list field-name val))
  163. (define-maybe module-list)
  164. (define (file-name? val)
  165. (and (string? val)
  166. (string-prefix? "/" val)))
  167. (define (serialize-file-name field-name val)
  168. (serialize-string field-name val))
  169. (define-maybe file-name)
  170. (define (file-name-list? val)
  171. (and (list? val) (and-map file-name? val)))
  172. (define (serialize-file-name-list field-name val)
  173. (serialize-string-list field-name val))
  174. (define-maybe file-name-list)
  175. (define (file-object? val)
  176. (or (file-like? val) (file-name? val)))
  177. (define (serialize-file-object field-name val)
  178. (serialize-string field-name val))
  179. (define-maybe file-object)
  180. (define (file-object-list? val)
  181. (and (list? val) (and-map file-object? val)))
  182. (define (serialize-file-object-list field-name val)
  183. (serialize-string-list field-name val))
  184. (define-maybe file-object-list)
  185. (define (raw-content? val)
  186. (maybe-value-set? val))
  187. (define (serialize-raw-content field-name val)
  188. val)
  189. (define-maybe raw-content)
  190. (define-configuration mod-muc-configuration
  191. (name
  192. (string "Prosody Chatrooms")
  193. "The name to return in service discovery responses.")
  194. (restrict-room-creation
  195. (string-or-boolean #f)
  196. "If @samp{#t}, this will only allow admins to create new chatrooms.
  197. Otherwise anyone can create a room. The value @samp{\"local\"} restricts room
  198. creation to users on the service's parent domain. E.g. @samp{user@@example.com}
  199. can create rooms on @samp{rooms.example.com}. The value @samp{\"admin\"}
  200. restricts to service administrators only.")
  201. (max-history-messages
  202. (non-negative-integer 20)
  203. "Maximum number of history messages that will be sent to the member that has
  204. just joined the room."))
  205. (define (serialize-mod-muc-configuration field-name val)
  206. (serialize-configuration val mod-muc-configuration-fields))
  207. (define-maybe mod-muc-configuration)
  208. (define-configuration ssl-configuration
  209. (protocol
  210. maybe-string
  211. "This determines what handshake to use.")
  212. (key
  213. maybe-file-name
  214. "Path to your private key file.")
  215. (certificate
  216. maybe-file-name
  217. "Path to your certificate file.")
  218. (capath
  219. (file-object "/etc/ssl/certs")
  220. "Path to directory containing root certificates that you wish Prosody to
  221. trust when verifying the certificates of remote servers.")
  222. (cafile
  223. maybe-file-object
  224. "Path to a file containing root certificates that you wish Prosody to trust.
  225. Similar to @code{capath} but with all certificates concatenated together.")
  226. (verify
  227. maybe-string-list
  228. "A list of verification options (these mostly map to OpenSSL's
  229. @code{set_verify()} flags).")
  230. (options
  231. maybe-string-list
  232. "A list of general options relating to SSL/TLS. These map to OpenSSL's
  233. @code{set_options()}. For a full list of options available in LuaSec, see the
  234. LuaSec source.")
  235. (depth
  236. maybe-non-negative-integer
  237. "How long a chain of certificate authorities to check when looking for a
  238. trusted root certificate.")
  239. (ciphers
  240. maybe-string
  241. "An OpenSSL cipher string. This selects what ciphers Prosody will offer to
  242. clients, and in what order.")
  243. (dhparam
  244. maybe-file-name
  245. "A path to a file containing parameters for Diffie-Hellman key exchange. You
  246. can create such a file with:
  247. @code{openssl dhparam -out /etc/prosody/certs/dh-2048.pem 2048}")
  248. (curve
  249. maybe-string
  250. "Curve for Elliptic curve Diffie-Hellman. Prosody's default is
  251. @samp{\"secp384r1\"}.")
  252. (verifyext
  253. maybe-string-list
  254. "A list of \"extra\" verification options.")
  255. (password
  256. maybe-string
  257. "Password for encrypted private keys."))
  258. (define (serialize-ssl-configuration field-name val)
  259. #~(format #f "ssl = {\n~a};\n"
  260. #$(serialize-configuration val ssl-configuration-fields)))
  261. (define-maybe ssl-configuration)
  262. (define %default-modules-enabled
  263. '("roster"
  264. "saslauth"
  265. "tls"
  266. "dialback"
  267. "disco"
  268. "carbons"
  269. "private"
  270. "blocklist"
  271. "vcard"
  272. "version"
  273. "uptime"
  274. "time"
  275. "ping"
  276. "pep"
  277. "register"
  278. "admin_adhoc"))
  279. ;; Guile bug. Use begin wrapper, because otherwise virtualhost-configuration
  280. ;; is assumed to be a function. See
  281. ;; https://www.gnu.org/software/guile/manual/html_node/R6RS-Incompatibilities.html
  282. (begin
  283. (define (virtualhost-configuration-list? val)
  284. (and (list? val) (and-map virtualhost-configuration? val)))
  285. (define (serialize-virtualhost-configuration-list l)
  286. #~(string-append
  287. #$@(map (lambda (val)
  288. (serialize-virtualhost-configuration val)) l)))
  289. (define (int-component-configuration-list? val)
  290. (and (list? val) (and-map int-component-configuration? val)))
  291. (define (serialize-int-component-configuration-list l)
  292. #~(string-append
  293. #$@(map (lambda (val)
  294. (serialize-int-component-configuration val)) l)))
  295. (define (ext-component-configuration-list? val)
  296. (and (list? val) (and-map ext-component-configuration? val)))
  297. (define (serialize-ext-component-configuration-list l)
  298. #~(string-append
  299. #$@(map (lambda (val)
  300. (serialize-ext-component-configuration val)) l)))
  301. (define-all-configurations prosody-configuration
  302. (prosody
  303. (file-like prosody)
  304. "The Prosody package."
  305. global)
  306. (data-path
  307. (file-name "/var/lib/prosody")
  308. "Location of the Prosody data storage directory. See
  309. @url{https://prosody.im/doc/configure}."
  310. global)
  311. (plugin-paths
  312. (file-object-list '())
  313. "Additional plugin directories. They are searched in all the specified
  314. paths in order. See @url{https://prosody.im/doc/plugins_directory}."
  315. global)
  316. (certificates
  317. (file-name "/etc/prosody/certs")
  318. "Every virtual host and component needs a certificate so that clients and
  319. servers can securely verify its identity. Prosody will automatically load
  320. certificates/keys from the directory specified here."
  321. global)
  322. (admins
  323. (string-list '())
  324. "This is a list of accounts that are admins for the server. Note that you
  325. must create the accounts separately. See @url{https://prosody.im/doc/admins} and
  326. @url{https://prosody.im/doc/creating_accounts}.
  327. Example: @code{(admins '(\"user1@@example.com\" \"user2@@example.net\"))}"
  328. common)
  329. (use-libevent?
  330. (boolean #f)
  331. "Enable use of libevent for better performance under high load. See
  332. @url{https://prosody.im/doc/libevent}."
  333. common)
  334. (modules-enabled
  335. (module-list %default-modules-enabled)
  336. "This is the list of modules Prosody will load on startup. It looks for
  337. @code{mod_modulename.lua} in the plugins folder, so make sure that exists too.
  338. Documentation on modules can be found at:
  339. @url{https://prosody.im/doc/modules}."
  340. common)
  341. (modules-disabled
  342. (string-list '())
  343. "@samp{\"offline\"}, @samp{\"c2s\"} and @samp{\"s2s\"} are auto-loaded, but
  344. should you want to disable them then add them to this list."
  345. common)
  346. (groups-file
  347. (file-object "/var/lib/prosody/sharedgroups.txt")
  348. "Path to a text file where the shared groups are defined. If this path is
  349. empty then @samp{mod_groups} does nothing. See
  350. @url{https://prosody.im/doc/modules/mod_groups}."
  351. common)
  352. (allow-registration?
  353. (boolean #f)
  354. "Disable account creation by default, for security. See
  355. @url{https://prosody.im/doc/creating_accounts}."
  356. common)
  357. (ssl
  358. (maybe-ssl-configuration (ssl-configuration))
  359. "These are the SSL/TLS-related settings. Most of them are disabled so to
  360. use Prosody's defaults. If you do not completely understand these options, do
  361. not add them to your config, it is easy to lower the security of your server
  362. using them. See @url{https://prosody.im/doc/advanced_ssl_config}."
  363. common)
  364. (c2s-require-encryption?
  365. (boolean #f)
  366. "Whether to force all client-to-server connections to be encrypted or not.
  367. See @url{https://prosody.im/doc/modules/mod_tls}."
  368. common)
  369. (disable-sasl-mechanisms
  370. (string-list '("DIGEST-MD5"))
  371. "Set of mechanisms that will never be offered. See
  372. @url{https://prosody.im/doc/modules/mod_saslauth}."
  373. common)
  374. (s2s-require-encryption?
  375. (boolean #f)
  376. "Whether to force all server-to-server connections to be encrypted or not.
  377. See @url{https://prosody.im/doc/modules/mod_tls}."
  378. common)
  379. (s2s-secure-auth?
  380. (boolean #f)
  381. "Whether to require encryption and certificate authentication. This
  382. provides ideal security, but requires servers you communicate with to support
  383. encryption AND present valid, trusted certificates. See
  384. @url{https://prosody.im/doc/s2s#security}."
  385. common)
  386. (s2s-insecure-domains
  387. (string-list '())
  388. "Many servers don't support encryption or have invalid or self-signed
  389. certificates. You can list domains here that will not be required to
  390. authenticate using certificates. They will be authenticated using DNS. See
  391. @url{https://prosody.im/doc/s2s#security}."
  392. common)
  393. (s2s-secure-domains
  394. (string-list '())
  395. "Even if you leave @code{s2s-secure-auth?} disabled, you can still require
  396. valid certificates for some domains by specifying a list here. See
  397. @url{https://prosody.im/doc/s2s#security}."
  398. common)
  399. (authentication
  400. (string "internal_plain")
  401. "Select the authentication backend to use. The default provider stores
  402. passwords in plaintext and uses Prosody's configured data storage to store the
  403. authentication data. If you do not trust your server please see
  404. @url{https://prosody.im/doc/modules/mod_auth_internal_hashed} for information
  405. about using the hashed backend. See also
  406. @url{https://prosody.im/doc/authentication}"
  407. common)
  408. ;; TODO: Handle more complicated log structures.
  409. (log
  410. (maybe-string "*syslog")
  411. "Set logging options. Advanced logging configuration is not yet supported
  412. by the Prosody service. See @url{https://prosody.im/doc/logging}."
  413. common)
  414. (pidfile
  415. (file-name "/var/run/prosody/prosody.pid")
  416. "File to write pid in. See @url{https://prosody.im/doc/modules/mod_posix}."
  417. global)
  418. (http-max-content-size
  419. (maybe-non-negative-integer %unset-value)
  420. "Maximum allowed size of the HTTP body (in bytes)."
  421. common)
  422. (http-external-url
  423. (maybe-string %unset-value)
  424. "Some modules expose their own URL in various ways. This URL is built
  425. from the protocol, host and port used. If Prosody sits behind a proxy, the
  426. public URL will be @code{http-external-url} instead. See
  427. @url{https://prosody.im/doc/http#external_url}."
  428. common)
  429. (virtualhosts
  430. (virtualhost-configuration-list
  431. (list (virtualhost-configuration
  432. (domain "localhost"))))
  433. "A host in Prosody is a domain on which user accounts can be created. For
  434. example if you want your users to have addresses like
  435. @samp{\"john.smith@@example.com\"} then you need to add a host
  436. @samp{\"example.com\"}. All options in this list will apply only to this host.
  437. Note: the name \"virtual\" host is used in configuration to avoid confusion with
  438. the actual physical host that Prosody is installed on. A single Prosody
  439. instance can serve many domains, each one defined as a VirtualHost entry in
  440. Prosody's configuration. Conversely a server that hosts a single domain would
  441. have just one VirtualHost entry.
  442. See @url{https://prosody.im/doc/configure#virtual_host_settings}."
  443. global)
  444. (int-components
  445. (int-component-configuration-list '())
  446. "Components are extra services on a server which are available to clients,
  447. usually on a subdomain of the main server (such as
  448. @samp{\"mycomponent.example.com\"}). Example components might be chatroom
  449. servers, user directories, or gateways to other protocols.
  450. Internal components are implemented with Prosody-specific plugins. To add an
  451. internal component, you simply fill the hostname field, and the plugin you wish
  452. to use for the component.
  453. See @url{https://prosody.im/doc/components}."
  454. global)
  455. (ext-components
  456. (ext-component-configuration-list '())
  457. "External components use XEP-0114, which most standalone components
  458. support. To add an external component, you simply fill the hostname field. See
  459. @url{https://prosody.im/doc/components}."
  460. global)
  461. (component-secret
  462. (string (configuration-missing-field 'ext-component 'component-secret))
  463. "Password which the component will use to log in."
  464. ext-component)
  465. (component-ports
  466. (non-negative-integer-list '(5347))
  467. "Port(s) Prosody listens on for component connections."
  468. global)
  469. (component-interface
  470. (string "127.0.0.1")
  471. "Interface Prosody listens on for component connections."
  472. global)
  473. (domain
  474. (string (configuration-missing-field 'virtualhost 'domain))
  475. "Domain you wish Prosody to serve."
  476. virtualhost)
  477. (hostname
  478. (string (configuration-missing-field 'int-component 'hostname))
  479. "Hostname of the component."
  480. int-component)
  481. (plugin
  482. (string (configuration-missing-field 'int-component 'plugin))
  483. "Plugin you wish to use for the component."
  484. int-component)
  485. (mod-muc
  486. (maybe-mod-muc-configuration %unset-value)
  487. "Multi-user chat (MUC) is Prosody's module for allowing you to create
  488. hosted chatrooms/conferences for XMPP users.
  489. General information on setting up and using multi-user chatrooms can be found
  490. in the \"Chatrooms\" documentation (@url{https://prosody.im/doc/chatrooms}),
  491. which you should read if you are new to XMPP chatrooms.
  492. See also @url{https://prosody.im/doc/modules/mod_muc}."
  493. int-component)
  494. (hostname
  495. (string (configuration-missing-field 'ext-component 'hostname))
  496. "Hostname of the component."
  497. ext-component)
  498. (raw-content
  499. (maybe-raw-content %unset-value)
  500. "Raw content that will be added to the configuration file."
  501. common)))
  502. ;; Serialize Virtualhost line first.
  503. (define (serialize-virtualhost-configuration config)
  504. (define (rest? field)
  505. (not (memq (configuration-field-name field)
  506. '(domain))))
  507. (let ((domain (virtualhost-configuration-domain config))
  508. (rest (filter rest? virtualhost-configuration-fields)))
  509. #~(string-append
  510. #$(format #f "VirtualHost \"~a\"\n" domain)
  511. #$(serialize-configuration config rest))))
  512. ;; Serialize Component line first.
  513. (define (serialize-int-component-configuration config)
  514. (define (rest? field)
  515. (not (memq (configuration-field-name field)
  516. '(hostname plugin))))
  517. (let ((hostname (int-component-configuration-hostname config))
  518. (plugin (int-component-configuration-plugin config))
  519. (rest (filter rest? int-component-configuration-fields)))
  520. #~(string-append
  521. #$(format #f "Component \"~a\" \"~a\"\n" hostname plugin)
  522. #$(serialize-configuration config rest))))
  523. ;; Serialize Component line first.
  524. (define (serialize-ext-component-configuration config)
  525. (define (rest? field)
  526. (not (memq (configuration-field-name field)
  527. '(hostname))))
  528. (let ((hostname (ext-component-configuration-hostname config))
  529. (rest (filter rest? ext-component-configuration-fields)))
  530. #~(string-append
  531. #$(format #f "Component \"~a\"\n" hostname)
  532. #$(serialize-configuration config rest))))
  533. ;; Serialize virtualhosts and components last.
  534. (define (serialize-prosody-configuration config)
  535. (define (rest? field)
  536. (not (memq (configuration-field-name field)
  537. '(virtualhosts int-components ext-components))))
  538. #~(string-append
  539. #$(let ((rest (filter rest? prosody-configuration-fields)))
  540. (serialize-configuration config rest))
  541. #$(serialize-virtualhost-configuration-list
  542. (prosody-configuration-virtualhosts config))
  543. #$(serialize-int-component-configuration-list
  544. (prosody-configuration-int-components config))
  545. #$(serialize-ext-component-configuration-list
  546. (prosody-configuration-ext-components config))))
  547. (define-configuration opaque-prosody-configuration
  548. (prosody
  549. (file-like prosody)
  550. "The prosody package.")
  551. (prosody.cfg.lua
  552. (string (configuration-missing-field 'opaque-prosody-configuration
  553. 'prosody.cfg.lua))
  554. "The contents of the @code{prosody.cfg.lua} to use."))
  555. (define (prosody-shepherd-service config)
  556. "Return a <shepherd-service> for Prosody with CONFIG."
  557. (let* ((prosody (if (opaque-prosody-configuration? config)
  558. (opaque-prosody-configuration-prosody config)
  559. (prosody-configuration-prosody config)))
  560. (prosodyctl-bin (file-append prosody "/bin/prosodyctl"))
  561. (pid-file (prosody-configuration-pidfile config))
  562. (prosodyctl-action (lambda args
  563. #~(lambda _
  564. (invoke #$prosodyctl-bin #$@args)
  565. (match '#$args
  566. (("start")
  567. (call-with-input-file #$pid-file read))
  568. (_ #t))))))
  569. (list (shepherd-service
  570. (documentation "Run the Prosody XMPP server")
  571. (provision '(prosody xmpp-daemon))
  572. (requirement '(networking syslogd user-processes))
  573. (modules `((ice-9 match)
  574. ,@%default-modules))
  575. (start (prosodyctl-action "start"))
  576. (stop (prosodyctl-action "stop"))))))
  577. (define %prosody-accounts
  578. (list (user-group (name "prosody") (system? #t))
  579. (user-account
  580. (name "prosody")
  581. (group "prosody")
  582. (system? #t)
  583. (comment "Prosody daemon user")
  584. (home-directory "/var/empty")
  585. (shell (file-append shadow "/sbin/nologin")))))
  586. (define (prosody-activation config)
  587. "Return the activation gexp for CONFIG."
  588. (let* ((config-dir "/etc/prosody")
  589. (default-certs-dir "/etc/prosody/certs")
  590. (data-path (prosody-configuration-data-path config))
  591. (pidfile-dir (dirname (prosody-configuration-pidfile config)))
  592. (config-str (if (opaque-prosody-configuration? config)
  593. (opaque-prosody-configuration-prosody.cfg.lua config)
  594. #~(begin
  595. (use-modules (ice-9 format))
  596. #$(serialize-prosody-configuration config))))
  597. (config-file (mixed-text-file "prosody.cfg.lua" config-str)))
  598. #~(begin
  599. (use-modules (guix build utils))
  600. (define %user (getpw "prosody"))
  601. (mkdir-p #$config-dir)
  602. (chown #$config-dir (passwd:uid %user) (passwd:gid %user))
  603. (copy-file #$config-file (string-append #$config-dir
  604. "/prosody.cfg.lua"))
  605. (mkdir-p #$default-certs-dir)
  606. (chown #$default-certs-dir (passwd:uid %user) (passwd:gid %user))
  607. (chmod #$default-certs-dir #o750)
  608. (mkdir-p #$data-path)
  609. (chown #$data-path (passwd:uid %user) (passwd:gid %user))
  610. (chmod #$data-path #o750)
  611. (mkdir-p #$pidfile-dir)
  612. (chown #$pidfile-dir (passwd:uid %user) (passwd:gid %user)))))
  613. (define prosody-service-type
  614. (service-type (name 'prosody)
  615. (extensions
  616. (list (service-extension shepherd-root-service-type
  617. prosody-shepherd-service)
  618. (service-extension account-service-type
  619. (const %prosody-accounts))
  620. (service-extension activation-service-type
  621. prosody-activation)))
  622. (default-value (prosody-configuration
  623. (virtualhosts
  624. (list
  625. (virtualhost-configuration
  626. (domain "localhost"))))))
  627. (description
  628. "Run Prosody, a modern XMPP communication server.")))
  629. ;; A little helper to make it easier to document all those fields.
  630. (define (generate-documentation)
  631. (define documentation
  632. `((prosody-configuration
  633. ,prosody-configuration-fields
  634. (ssl ssl-configuration)
  635. (virtualhosts virtualhost-configuration)
  636. (int-components int-component-configuration)
  637. (ext-components ext-component-configuration))
  638. (ssl-configuration ,ssl-configuration-fields)
  639. (int-component-configuration ,int-component-configuration-fields
  640. (mod-muc mod-muc-configuration))
  641. (ext-component-configuration ,ext-component-configuration-fields)
  642. (mod-muc-configuration ,mod-muc-configuration-fields)
  643. (virtualhost-configuration ,virtualhost-configuration-fields)
  644. (opaque-prosody-configuration ,opaque-prosody-configuration-fields)))
  645. (define (generate configuration-name)
  646. (match (assq-ref documentation configuration-name)
  647. ((fields . sub-documentation)
  648. (format #t "\nAvailable @code{~a} fields are:\n\n" configuration-name)
  649. (when (memq configuration-name
  650. '(virtualhost-configuration
  651. int-component-configuration
  652. ext-component-configuration))
  653. (format #t "all these @code{prosody-configuration} fields: ~a, plus:\n"
  654. (string-join (map (lambda (s)
  655. (format #f "@code{~a}" s)) common-fields)
  656. ", ")))
  657. (for-each
  658. (lambda (f)
  659. (let ((field-name (configuration-field-name f))
  660. (field-type (configuration-field-type f))
  661. (field-docs (string-trim-both
  662. (configuration-field-documentation f)))
  663. (default (catch #t
  664. (configuration-field-default-value-thunk f)
  665. (lambda _ 'nope))))
  666. (define (escape-chars str chars escape)
  667. (with-output-to-string
  668. (lambda ()
  669. (string-for-each (lambda (c)
  670. (when (char-set-contains? chars c)
  671. (display escape))
  672. (display c))
  673. str))))
  674. (define (show-default? val)
  675. (or (string? val) (number? val) (boolean? val)
  676. (and (list? val) (and-map show-default? val))))
  677. (format #t "@deftypevr {@code{~a} parameter} ~a ~a\n~a\n"
  678. configuration-name field-type field-name field-docs)
  679. (when (show-default? default)
  680. (format #t "Defaults to @samp{~a}.\n"
  681. (escape-chars (format #f "~s" default)
  682. (char-set #\@ #\{ #\})
  683. #\@)))
  684. (for-each generate (or (assq-ref sub-documentation field-name) '()))
  685. (format #t "@end deftypevr\n\n")))
  686. (filter (lambda (f)
  687. (not (string=? "" (configuration-field-documentation f))))
  688. fields)))))
  689. (generate 'prosody-configuration)
  690. (format #t "It could be that you just want to get a @code{prosody.cfg.lua}
  691. up and running. In that case, you can pass an
  692. @code{opaque-prosody-configuration} record as the value of
  693. @code{prosody-service-type}. As its name indicates, an opaque configuration
  694. does not have easy reflective capabilities.")
  695. (generate 'opaque-prosody-configuration)
  696. (format #t "For example, if your @code{prosody.cfg.lua} is just the empty
  697. string, you could instantiate a prosody service like this:
  698. @example
  699. (service prosody-service-type
  700. (opaque-prosody-configuration
  701. (prosody.cfg.lua \"\")))
  702. @end example"))
  703. ;;;
  704. ;;; BitlBee.
  705. ;;;
  706. (define-record-type* <bitlbee-configuration>
  707. bitlbee-configuration make-bitlbee-configuration
  708. bitlbee-configuration?
  709. (bitlbee bitlbee-configuration-bitlbee
  710. (default bitlbee))
  711. (interface bitlbee-configuration-interface
  712. (default "127.0.0.1"))
  713. (port bitlbee-configuration-port
  714. (default 6667))
  715. (plugins bitlbee-plugins
  716. (default '()))
  717. (extra-settings bitlbee-configuration-extra-settings
  718. (default "")))
  719. (define bitlbee-shepherd-service
  720. (match-lambda
  721. (($ <bitlbee-configuration> bitlbee interface port
  722. plugins extra-settings)
  723. (let* ((plugins (directory-union "bitlbee-plugins" plugins))
  724. (conf (mixed-text-file "bitlbee.conf"
  725. "
  726. [settings]
  727. User = bitlbee
  728. ConfigDir = /var/lib/bitlbee
  729. DaemonInterface = " interface "
  730. DaemonPort = " (number->string port) "
  731. PluginDir = " plugins "/lib/bitlbee
  732. " extra-settings))
  733. (bitlbee* (least-authority-wrapper
  734. (file-append bitlbee "/sbin/bitlbee")
  735. #:name "bitlbee"
  736. #:preserved-environment-variables
  737. '("PURPLE_PLUGIN_PATH" "GUIX_LOCPATH" "LC_ALL")
  738. #:mappings (list (file-system-mapping
  739. (source "/var/lib/bitlbee")
  740. (target source)
  741. (writable? #t))
  742. (file-system-mapping
  743. (source "/run/current-system/locale")
  744. (target source))
  745. (file-system-mapping
  746. (source conf)
  747. (target conf)))
  748. #:namespaces (delq 'net %namespaces))))
  749. (with-imported-modules (source-module-closure
  750. '((gnu build shepherd)
  751. (gnu system file-systems)))
  752. (list (shepherd-service
  753. (provision '(bitlbee))
  754. ;; Note: If networking is not up, then /etc/resolv.conf
  755. ;; doesn't get mapped in the container, hence the dependency
  756. ;; on 'networking'.
  757. (requirement '(user-processes networking))
  758. (modules '((gnu build shepherd)
  759. (gnu system file-systems)))
  760. (start #~(if (defined? 'make-inetd-constructor)
  761. (make-inetd-constructor
  762. (list #$bitlbee* "-I" "-c" #$conf)
  763. (addrinfo:addr
  764. (car (getaddrinfo #$interface
  765. #$(number->string port)
  766. (logior AI_NUMERICHOST
  767. AI_NUMERICSERV))))
  768. #:service-name-stem "bitlbee"
  769. #:user "bitlbee" #:group "bitlbee"
  770. ;; Allow 'bitlbee-purple' to use libpurple plugins.
  771. #:environment-variables
  772. (list (string-append "PURPLE_PLUGIN_PATH="
  773. #$plugins "/lib/purple-2")
  774. "GUIX_LOCPATH=/run/current-system/locale"))
  775. (make-forkexec-constructor/container
  776. (list #$(file-append bitlbee "/sbin/bitlbee")
  777. "-n" "-F" "-u" "bitlbee" "-c" #$conf)
  778. ;; Allow 'bitlbee-purple' to use libpurple plugins.
  779. #:environment-variables
  780. (list (string-append "PURPLE_PLUGIN_PATH="
  781. #$plugins "/lib/purple-2"))
  782. #:pid-file "/var/run/bitlbee.pid"
  783. #:mappings (list (file-system-mapping
  784. (source "/var/lib/bitlbee")
  785. (target source)
  786. (writable? #t))))))
  787. (stop #~(if (defined? 'make-inetd-destructor)
  788. (make-inetd-destructor)
  789. (make-kill-destructor))))))))))
  790. (define %bitlbee-accounts
  791. ;; User group and account to run BitlBee.
  792. (list (user-group (name "bitlbee") (system? #t))
  793. (user-account
  794. (name "bitlbee")
  795. (group "bitlbee")
  796. (system? #t)
  797. (comment "BitlBee daemon user")
  798. (home-directory "/var/empty")
  799. (shell (file-append shadow "/sbin/nologin")))))
  800. (define %bitlbee-activation
  801. ;; Activation gexp for BitlBee.
  802. #~(begin
  803. (use-modules (guix build utils))
  804. ;; This directory is used to store OTR data.
  805. (mkdir-p "/var/lib/bitlbee")
  806. (let ((user (getpwnam "bitlbee")))
  807. (chown "/var/lib/bitlbee"
  808. (passwd:uid user) (passwd:gid user)))))
  809. (define bitlbee-service-type
  810. (service-type (name 'bitlbee)
  811. (extensions
  812. (list (service-extension shepherd-root-service-type
  813. bitlbee-shepherd-service)
  814. (service-extension account-service-type
  815. (const %bitlbee-accounts))
  816. (service-extension activation-service-type
  817. (const %bitlbee-activation))))
  818. (default-value (bitlbee-configuration))
  819. (description
  820. "Run @url{http://bitlbee.org,BitlBee}, a daemon that acts as
  821. a gateway between IRC and chat networks.")))
  822. ;;;
  823. ;;; Quassel.
  824. ;;;
  825. (define-record-type* <quassel-configuration>
  826. quassel-configuration make-quassel-configuration
  827. quassel-configuration?
  828. (quassel quassel-configuration-quassel
  829. (default quassel))
  830. (interface quassel-configuration-interface
  831. (default "::,0.0.0.0"))
  832. (port quassel-configuration-port
  833. (default 4242))
  834. (loglevel quassel-configuration-loglevel
  835. (default "Info")))
  836. (define quassel-shepherd-service
  837. (match-lambda
  838. (($ <quassel-configuration> quassel interface port loglevel)
  839. (let ((quassel (least-authority-wrapper
  840. (file-append quassel "/bin/quasselcore")
  841. #:name "quasselcore"
  842. #:mappings (list (file-system-mapping
  843. (source "/var/lib/quassel")
  844. (target source)
  845. (writable? #t))
  846. (file-system-mapping
  847. (source "/var/log/quassel")
  848. (target source)
  849. (writable? #t)))
  850. ;; XXX: The daemon needs to live in the main user
  851. ;; namespace, as root, so it can access /var/lib/quassel
  852. ;; owned by "quasselcore".
  853. #:namespaces (fold delq %namespaces '(net user)))))
  854. (list (shepherd-service
  855. (provision '(quassel))
  856. (requirement '(user-processes networking))
  857. (start #~(make-forkexec-constructor
  858. (list #$quassel
  859. "--configdir=/var/lib/quassel"
  860. "--logfile=/var/log/quassel/core.log"
  861. (string-append "--loglevel=" #$loglevel)
  862. (string-append "--port=" (number->string #$port))
  863. (string-append "--listen=" #$interface))))
  864. (stop #~(make-kill-destructor))))))))
  865. (define %quassel-account
  866. (list (user-group (name "quassel") (system? #t))
  867. (user-account
  868. (name "quasselcore")
  869. (group "quassel")
  870. (system? #t)
  871. (comment "Quassel daemon user")
  872. (home-directory "/var/lib/quassel")
  873. (shell (file-append shadow "/sbin/nologin")))))
  874. (define %quassel-activation
  875. #~(begin
  876. (use-modules (guix build utils))
  877. (mkdir-p "/var/lib/quassel")
  878. (mkdir-p "/var/log/quassel")
  879. (let ((cert "/var/lib/quassel/quasselCert.pem"))
  880. (unless (file-exists? cert)
  881. (invoke #$(file-append openssl "/bin/openssl")
  882. "req" "-x509" "-nodes" "-batch" "-days" "680" "-newkey"
  883. "rsa" "-keyout" cert "-out" cert)))))
  884. (define quassel-service-type
  885. (service-type (name 'quassel)
  886. (extensions
  887. (list (service-extension shepherd-root-service-type
  888. quassel-shepherd-service)
  889. (service-extension profile-service-type
  890. (compose list quassel-configuration-quassel))
  891. (service-extension account-service-type
  892. (const %quassel-account))
  893. (service-extension activation-service-type
  894. (const %quassel-activation))))
  895. (default-value (quassel-configuration))
  896. (description
  897. "Run @url{https://quassel-irc.org/,quasselcore}, the backend
  898. for the distributed IRC client quassel, which allows you to connect from
  899. multiple machines simultaneously.")))