messaging.scm 38 KB

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