messaging.scm 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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, 2023 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. (list (endpoint
  764. (addrinfo:addr
  765. (car (getaddrinfo #$interface
  766. #$(number->string port)
  767. (logior AI_NUMERICHOST
  768. AI_NUMERICSERV))))))
  769. #:requirements '#$requirement
  770. #:service-name-stem "bitlbee"
  771. #:user "bitlbee" #:group "bitlbee"
  772. ;; Allow 'bitlbee-purple' to use libpurple plugins.
  773. #:environment-variables
  774. (list (string-append "PURPLE_PLUGIN_PATH="
  775. #$plugins "/lib/purple-2")
  776. "GUIX_LOCPATH=/run/current-system/locale"))
  777. (make-forkexec-constructor/container
  778. (list #$(file-append bitlbee "/sbin/bitlbee")
  779. "-n" "-F" "-u" "bitlbee" "-c" #$conf)
  780. ;; Allow 'bitlbee-purple' to use libpurple plugins.
  781. #:environment-variables
  782. (list (string-append "PURPLE_PLUGIN_PATH="
  783. #$plugins "/lib/purple-2"))
  784. #:pid-file "/var/run/bitlbee.pid"
  785. #:mappings (list (file-system-mapping
  786. (source "/var/lib/bitlbee")
  787. (target source)
  788. (writable? #t))))))
  789. (stop #~(if (defined? 'make-inetd-destructor)
  790. (make-inetd-destructor)
  791. (make-kill-destructor))))))))))
  792. (define %bitlbee-accounts
  793. ;; User group and account to run BitlBee.
  794. (list (user-group (name "bitlbee") (system? #t))
  795. (user-account
  796. (name "bitlbee")
  797. (group "bitlbee")
  798. (system? #t)
  799. (comment "BitlBee daemon user")
  800. (home-directory "/var/empty")
  801. (shell (file-append shadow "/sbin/nologin")))))
  802. (define %bitlbee-activation
  803. ;; Activation gexp for BitlBee.
  804. #~(begin
  805. (use-modules (guix build utils))
  806. ;; This directory is used to store OTR data.
  807. (mkdir-p "/var/lib/bitlbee")
  808. (let ((user (getpwnam "bitlbee")))
  809. (chown "/var/lib/bitlbee"
  810. (passwd:uid user) (passwd:gid user)))))
  811. (define bitlbee-service-type
  812. (service-type (name 'bitlbee)
  813. (extensions
  814. (list (service-extension shepherd-root-service-type
  815. bitlbee-shepherd-service)
  816. (service-extension account-service-type
  817. (const %bitlbee-accounts))
  818. (service-extension activation-service-type
  819. (const %bitlbee-activation))))
  820. (default-value (bitlbee-configuration))
  821. (description
  822. "Run @url{http://bitlbee.org,BitlBee}, a daemon that acts as
  823. a gateway between IRC and chat networks.")))
  824. ;;;
  825. ;;; Quassel.
  826. ;;;
  827. (define-record-type* <quassel-configuration>
  828. quassel-configuration make-quassel-configuration
  829. quassel-configuration?
  830. (quassel quassel-configuration-quassel
  831. (default quassel))
  832. (interface quassel-configuration-interface
  833. (default "::,0.0.0.0"))
  834. (port quassel-configuration-port
  835. (default 4242))
  836. (loglevel quassel-configuration-loglevel
  837. (default "Info")))
  838. (define quassel-shepherd-service
  839. (match-lambda
  840. (($ <quassel-configuration> quassel interface port loglevel)
  841. (let ((quassel (least-authority-wrapper
  842. (file-append quassel "/bin/quasselcore")
  843. #:name "quasselcore"
  844. #:mappings (list (file-system-mapping
  845. (source "/var/lib/quassel")
  846. (target source)
  847. (writable? #t))
  848. (file-system-mapping
  849. (source "/var/log/quassel")
  850. (target source)
  851. (writable? #t)))
  852. ;; XXX: The daemon needs to live in the main user
  853. ;; namespace, as root, so it can access /var/lib/quassel
  854. ;; owned by "quasselcore".
  855. #:namespaces (fold delq %namespaces '(net user)))))
  856. (list (shepherd-service
  857. (provision '(quassel))
  858. (requirement '(user-processes networking))
  859. (start #~(make-forkexec-constructor
  860. (list #$quassel
  861. "--configdir=/var/lib/quassel"
  862. "--logfile=/var/log/quassel/core.log"
  863. (string-append "--loglevel=" #$loglevel)
  864. (string-append "--port=" (number->string #$port))
  865. (string-append "--listen=" #$interface))))
  866. (stop #~(make-kill-destructor))))))))
  867. (define %quassel-account
  868. (list (user-group (name "quassel") (system? #t))
  869. (user-account
  870. (name "quasselcore")
  871. (group "quassel")
  872. (system? #t)
  873. (comment "Quassel daemon user")
  874. (home-directory "/var/lib/quassel")
  875. (shell (file-append shadow "/sbin/nologin")))))
  876. (define %quassel-activation
  877. #~(begin
  878. (use-modules (guix build utils))
  879. (mkdir-p "/var/lib/quassel")
  880. (mkdir-p "/var/log/quassel")
  881. (let ((cert "/var/lib/quassel/quasselCert.pem"))
  882. (unless (file-exists? cert)
  883. (invoke #$(file-append openssl "/bin/openssl")
  884. "req" "-x509" "-nodes" "-batch" "-days" "680" "-newkey"
  885. "rsa" "-keyout" cert "-out" cert)))))
  886. (define quassel-service-type
  887. (service-type (name 'quassel)
  888. (extensions
  889. (list (service-extension shepherd-root-service-type
  890. quassel-shepherd-service)
  891. (service-extension profile-service-type
  892. (compose list quassel-configuration-quassel))
  893. (service-extension account-service-type
  894. (const %quassel-account))
  895. (service-extension activation-service-type
  896. (const %quassel-activation))))
  897. (default-value (quassel-configuration))
  898. (description
  899. "Run @url{https://quassel-irc.org/,quasselcore}, the backend
  900. for the distributed IRC client quassel, which allows you to connect from
  901. multiple machines simultaneously.")))