messaging.scm 37 KB

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