messaging.scm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
  3. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu services messaging)
  20. #:use-module (gnu packages messaging)
  21. #:use-module (gnu packages admin)
  22. #:use-module (gnu services)
  23. #:use-module (gnu services shepherd)
  24. #:use-module (gnu services configuration)
  25. #:use-module (gnu system shadow)
  26. #:use-module (guix gexp)
  27. #:use-module (guix records)
  28. #:use-module (guix packages)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-35)
  31. #:use-module (ice-9 match)
  32. #:export (prosody-service-type
  33. prosody-configuration
  34. opaque-prosody-configuration
  35. virtualhost-configuration
  36. int-component-configuration
  37. ext-component-configuration
  38. mod-muc-configuration
  39. ssl-configuration
  40. %default-modules-enabled
  41. prosody-configuration-pidfile))
  42. ;;; Commentary:
  43. ;;;
  44. ;;; Messaging services.
  45. ;;;
  46. ;;; Code:
  47. (define-syntax define-all-configurations
  48. (lambda (stx)
  49. (define-syntax-rule (id ctx parts ...)
  50. "Assemble PARTS into a raw (unhygienic) identifier."
  51. (datum->syntax ctx (symbol-append (syntax->datum parts) ...)))
  52. (define (make-pred arg)
  53. (lambda (field target)
  54. (and (memq (syntax->datum target) `(common ,arg)) field)))
  55. (syntax-case stx ()
  56. ((_ stem (field (field-type def) doc target) ...)
  57. (with-syntax (((new-field-type ...)
  58. (map (lambda (field-type target)
  59. (if (and (eq? 'common (syntax->datum target))
  60. (not (string-prefix?
  61. "maybe-"
  62. (symbol->string
  63. (syntax->datum field-type)))))
  64. (id #'stem #'maybe- field-type) field-type))
  65. #'(field-type ...) #'(target ...)))
  66. ((new-def ...)
  67. (map (lambda (def target)
  68. (if (eq? 'common (syntax->datum target))
  69. #''disabled def))
  70. #'(def ...) #'(target ...)))
  71. ((new-doc ...)
  72. (map (lambda (doc target)
  73. (if (eq? 'common (syntax->datum target))
  74. "" doc))
  75. #'(doc ...) #'(target ...))))
  76. #`(begin
  77. (define #,(id #'stem #'common-fields)
  78. '(#,@(filter-map (make-pred #f) #'(field ...) #'(target ...))))
  79. (define-configuration #,(id #'stem #'prosody-configuration)
  80. #,@(filter-map (make-pred 'global)
  81. #'((field (field-type def) doc) ...)
  82. #'(target ...)))
  83. (define-configuration #,(id #'stem #'virtualhost-configuration)
  84. #,@(filter-map (make-pred 'virtualhost)
  85. #'((field (new-field-type new-def) new-doc) ...)
  86. #'(target ...)))
  87. (define-configuration #,(id #'stem #'int-component-configuration)
  88. #,@(filter-map (make-pred 'int-component)
  89. #'((field (new-field-type new-def) new-doc) ...)
  90. #'(target ...)))
  91. (define-configuration #,(id #'stem #'ext-component-configuration)
  92. #,@(filter-map (make-pred 'ext-component)
  93. #'((field (new-field-type new-def) new-doc) ...)
  94. #'(target ...)))))))))
  95. (define (uglify-field-name field-name)
  96. (let ((str (symbol->string field-name)))
  97. (string-join (string-split (if (string-suffix? "?" str)
  98. (substring str 0 (1- (string-length str)))
  99. str)
  100. #\-)
  101. "_")))
  102. (define (serialize-field field-name val)
  103. (format #t "~a = ~a;\n" (uglify-field-name field-name) val))
  104. (define (serialize-field-list field-name val)
  105. (serialize-field field-name
  106. (with-output-to-string
  107. (lambda ()
  108. (format #t "{\n")
  109. (for-each (lambda (x)
  110. (format #t "~a;\n" x))
  111. val)
  112. (format #t "}")))))
  113. (define (serialize-boolean field-name val)
  114. (serialize-field field-name (if val "true" "false")))
  115. (define-maybe boolean)
  116. (define (string-or-boolean? val)
  117. (or (string? val) (boolean? val)))
  118. (define (serialize-string-or-boolean field-name val)
  119. (if (string? val)
  120. (serialize-string field-name val)
  121. (serialize-boolean field-name val)))
  122. (define (non-negative-integer? val)
  123. (and (exact-integer? val) (not (negative? val))))
  124. (define (serialize-non-negative-integer field-name val)
  125. (serialize-field field-name val))
  126. (define-maybe non-negative-integer)
  127. (define (non-negative-integer-list? val)
  128. (and (list? val) (and-map non-negative-integer? val)))
  129. (define (serialize-non-negative-integer-list field-name val)
  130. (serialize-field-list field-name val))
  131. (define-maybe non-negative-integer-list)
  132. (define (enclose-quotes s)
  133. (format #f "\"~a\"" s))
  134. (define (serialize-string field-name val)
  135. (serialize-field field-name (enclose-quotes val)))
  136. (define-maybe string)
  137. (define (string-list? val)
  138. (and (list? val)
  139. (and-map (lambda (x)
  140. (and (string? x) (not (string-index x #\,))))
  141. val)))
  142. (define (serialize-string-list field-name val)
  143. (serialize-field-list field-name (map enclose-quotes val)))
  144. (define-maybe string-list)
  145. (define (module-list? val)
  146. (string-list? val))
  147. (define (serialize-module-list field-name val)
  148. (serialize-string-list field-name val))
  149. (define-maybe module-list)
  150. (define (file-name? val)
  151. (and (string? val)
  152. (string-prefix? "/" val)))
  153. (define (serialize-file-name field-name val)
  154. (serialize-string field-name val))
  155. (define-maybe file-name)
  156. (define (file-name-list? val)
  157. (and (list? val) (and-map file-name? val)))
  158. (define (serialize-file-name-list field-name val)
  159. (serialize-string-list field-name val))
  160. (define-maybe file-name)
  161. (define (raw-content? val)
  162. (not (eq? val 'disabled)))
  163. (define (serialize-raw-content field-name val)
  164. (format #t "~a" val))
  165. (define-maybe raw-content)
  166. (define-configuration mod-muc-configuration
  167. (name
  168. (string "Prosody Chatrooms")
  169. "The name to return in service discovery responses.")
  170. (restrict-room-creation
  171. (string-or-boolean #f)
  172. "If @samp{#t}, this will only allow admins to create new chatrooms.
  173. Otherwise anyone can create a room. The value @samp{\"local\"} restricts room
  174. creation to users on the service's parent domain. E.g. @samp{user@@example.com}
  175. can create rooms on @samp{rooms.example.com}. The value @samp{\"admin\"}
  176. restricts to service administrators only.")
  177. (max-history-messages
  178. (non-negative-integer 20)
  179. "Maximum number of history messages that will be sent to the member that has
  180. just joined the room."))
  181. (define (serialize-mod-muc-configuration field-name val)
  182. (serialize-configuration val mod-muc-configuration-fields))
  183. (define-maybe mod-muc-configuration)
  184. (define-configuration ssl-configuration
  185. (protocol
  186. (maybe-string 'disabled)
  187. "This determines what handshake to use.")
  188. (key
  189. (maybe-file-name 'disabled)
  190. "Path to your private key file.")
  191. (certificate
  192. (maybe-file-name 'disabled)
  193. "Path to your certificate file.")
  194. (capath
  195. (file-name "/etc/ssl/certs")
  196. "Path to directory containing root certificates that you wish Prosody to
  197. trust when verifying the certificates of remote servers.")
  198. (cafile
  199. (maybe-file-name 'disabled)
  200. "Path to a file containing root certificates that you wish Prosody to trust.
  201. Similar to @code{capath} but with all certificates concatenated together.")
  202. (verify
  203. (maybe-string-list 'disabled)
  204. "A list of verification options (these mostly map to OpenSSL's
  205. @code{set_verify()} flags).")
  206. (options
  207. (maybe-string-list 'disabled)
  208. "A list of general options relating to SSL/TLS. These map to OpenSSL's
  209. @code{set_options()}. For a full list of options available in LuaSec, see the
  210. LuaSec source.")
  211. (depth
  212. (maybe-non-negative-integer 'disabled)
  213. "How long a chain of certificate authorities to check when looking for a
  214. trusted root certificate.")
  215. (ciphers
  216. (maybe-string 'disabled)
  217. "An OpenSSL cipher string. This selects what ciphers Prosody will offer to
  218. clients, and in what order.")
  219. (dhparam
  220. (maybe-file-name 'disabled)
  221. "A path to a file containing parameters for Diffie-Hellman key exchange. You
  222. can create such a file with:
  223. @code{openssl dhparam -out /etc/prosody/certs/dh-2048.pem 2048}")
  224. (curve
  225. (maybe-string 'disabled)
  226. "Curve for Elliptic curve Diffie-Hellman. Prosody's default is
  227. @samp{\"secp384r1\"}.")
  228. (verifyext
  229. (maybe-string-list 'disabled)
  230. "A list of \"extra\" verification options.")
  231. (password
  232. (maybe-string 'disabled)
  233. "Password for encrypted private keys."))
  234. (define (serialize-ssl-configuration field-name val)
  235. (format #t "ssl = {\n")
  236. (serialize-configuration val ssl-configuration-fields)
  237. (format #t "};\n"))
  238. (define-maybe ssl-configuration)
  239. (define %default-modules-enabled
  240. '("roster"
  241. "saslauth"
  242. "tls"
  243. "dialback"
  244. "disco"
  245. "carbons"
  246. "private"
  247. "blocklist"
  248. "vcard"
  249. "version"
  250. "uptime"
  251. "time"
  252. "ping"
  253. "pep"
  254. "register"
  255. "admin_adhoc"))
  256. ;; Guile bug. Use begin wrapper, because otherwise virtualhost-configuration
  257. ;; is assumed to be a function. See
  258. ;; https://www.gnu.org/software/guile/manual/html_node/R6RS-Incompatibilities.html
  259. (begin
  260. (define (virtualhost-configuration-list? val)
  261. (and (list? val) (and-map virtualhost-configuration? val)))
  262. (define (serialize-virtualhost-configuration-list l)
  263. (for-each
  264. (lambda (val) (serialize-virtualhost-configuration val)) l))
  265. (define (int-component-configuration-list? val)
  266. (and (list? val) (and-map int-component-configuration? val)))
  267. (define (serialize-int-component-configuration-list l)
  268. (for-each
  269. (lambda (val) (serialize-int-component-configuration val)) l))
  270. (define (ext-component-configuration-list? val)
  271. (and (list? val) (and-map ext-component-configuration? val)))
  272. (define (serialize-ext-component-configuration-list l)
  273. (for-each
  274. (lambda (val) (serialize-ext-component-configuration val)) l))
  275. (define-all-configurations prosody-configuration
  276. (prosody
  277. (package prosody)
  278. "The Prosody package."
  279. global)
  280. (data-path
  281. (file-name "/var/lib/prosody")
  282. "Location of the Prosody data storage directory. See
  283. @url{https://prosody.im/doc/configure}."
  284. global)
  285. (plugin-paths
  286. (file-name-list '())
  287. "Additional plugin directories. They are searched in all the specified
  288. paths in order. See @url{https://prosody.im/doc/plugins_directory}."
  289. global)
  290. (certificates
  291. (file-name "/etc/prosody/certs")
  292. "Every virtual host and component needs a certificate so that clients and
  293. servers can securely verify its identity. Prosody will automatically load
  294. certificates/keys from the directory specified here."
  295. global)
  296. (admins
  297. (string-list '())
  298. "This is a list of accounts that are admins for the server. Note that you
  299. must create the accounts separately. See @url{https://prosody.im/doc/admins} and
  300. @url{https://prosody.im/doc/creating_accounts}.
  301. Example: @code{(admins '(\"user1@@example.com\" \"user2@@example.net\"))}"
  302. common)
  303. (use-libevent?
  304. (boolean #f)
  305. "Enable use of libevent for better performance under high load. See
  306. @url{https://prosody.im/doc/libevent}."
  307. common)
  308. (modules-enabled
  309. (module-list %default-modules-enabled)
  310. "This is the list of modules Prosody will load on startup. It looks for
  311. @code{mod_modulename.lua} in the plugins folder, so make sure that exists too.
  312. Documentation on modules can be found at:
  313. @url{https://prosody.im/doc/modules}."
  314. common)
  315. (modules-disabled
  316. (string-list '())
  317. "@samp{\"offline\"}, @samp{\"c2s\"} and @samp{\"s2s\"} are auto-loaded, but
  318. should you want to disable them then add them to this list."
  319. common)
  320. (groups-file
  321. (file-name "/var/lib/prosody/sharedgroups.txt")
  322. "Path to a text file where the shared groups are defined. If this path is
  323. empty then @samp{mod_groups} does nothing. See
  324. @url{https://prosody.im/doc/modules/mod_groups}."
  325. common)
  326. (allow-registration?
  327. (boolean #f)
  328. "Disable account creation by default, for security. See
  329. @url{https://prosody.im/doc/creating_accounts}."
  330. common)
  331. (ssl
  332. (maybe-ssl-configuration (ssl-configuration))
  333. "These are the SSL/TLS-related settings. Most of them are disabled so to
  334. use Prosody's defaults. If you do not completely understand these options, do
  335. not add them to your config, it is easy to lower the security of your server
  336. using them. See @url{https://prosody.im/doc/advanced_ssl_config}."
  337. common)
  338. (c2s-require-encryption?
  339. (boolean #f)
  340. "Whether to force all client-to-server connections to be encrypted or not.
  341. See @url{https://prosody.im/doc/modules/mod_tls}."
  342. common)
  343. (disable-sasl-mechanisms
  344. (string-list '("DIGEST-MD5"))
  345. "Set of mechanisms that will never be offered. See
  346. @url{https://prosody.im/doc/modules/mod_saslauth}."
  347. common)
  348. (s2s-require-encryption?
  349. (boolean #f)
  350. "Whether to force all server-to-server connections to be encrypted or not.
  351. See @url{https://prosody.im/doc/modules/mod_tls}."
  352. common)
  353. (s2s-secure-auth?
  354. (boolean #f)
  355. "Whether to require encryption and certificate authentication. This
  356. provides ideal security, but requires servers you communicate with to support
  357. encryption AND present valid, trusted certificates. See
  358. @url{https://prosody.im/doc/s2s#security}."
  359. common)
  360. (s2s-insecure-domains
  361. (string-list '())
  362. "Many servers don't support encryption or have invalid or self-signed
  363. certificates. You can list domains here that will not be required to
  364. authenticate using certificates. They will be authenticated using DNS. See
  365. @url{https://prosody.im/doc/s2s#security}."
  366. common)
  367. (s2s-secure-domains
  368. (string-list '())
  369. "Even if you leave @code{s2s-secure-auth?} disabled, you can still require
  370. valid certificates for some domains by specifying a list here. See
  371. @url{https://prosody.im/doc/s2s#security}."
  372. common)
  373. (authentication
  374. (string "internal_plain")
  375. "Select the authentication backend to use. The default provider stores
  376. passwords in plaintext and uses Prosody's configured data storage to store the
  377. authentication data. If you do not trust your server please see
  378. @url{https://prosody.im/doc/modules/mod_auth_internal_hashed} for information
  379. about using the hashed backend. See also
  380. @url{https://prosody.im/doc/authentication}"
  381. common)
  382. ;; TODO: Handle more complicated log structures.
  383. (log
  384. (maybe-string "*syslog")
  385. "Set logging options. Advanced logging configuration is not yet supported
  386. by the GuixSD Prosody Service. See @url{https://prosody.im/doc/logging}."
  387. common)
  388. (pidfile
  389. (file-name "/var/run/prosody/prosody.pid")
  390. "File to write pid in. See @url{https://prosody.im/doc/modules/mod_posix}."
  391. global)
  392. (http-max-content-size
  393. (maybe-non-negative-integer 'disabled)
  394. "Maximum allowed size of the HTTP body (in bytes)."
  395. common)
  396. (http-external-url
  397. (maybe-string 'disabled)
  398. "Some modules expose their own URL in various ways. This URL is built
  399. from the protocol, host and port used. If Prosody sits behind a proxy, the
  400. public URL will be @code{http-external-url} instead. See
  401. @url{https://prosody.im/doc/http#external_url}."
  402. common)
  403. (virtualhosts
  404. (virtualhost-configuration-list
  405. (list (virtualhost-configuration
  406. (domain "localhost"))))
  407. "A host in Prosody is a domain on which user accounts can be created. For
  408. example if you want your users to have addresses like
  409. @samp{\"john.smith@@example.com\"} then you need to add a host
  410. @samp{\"example.com\"}. All options in this list will apply only to this host.
  411. Note: the name \"virtual\" host is used in configuration to avoid confusion with
  412. the actual physical host that Prosody is installed on. A single Prosody
  413. instance can serve many domains, each one defined as a VirtualHost entry in
  414. Prosody's configuration. Conversely a server that hosts a single domain would
  415. have just one VirtualHost entry.
  416. See @url{https://prosody.im/doc/configure#virtual_host_settings}."
  417. global)
  418. (int-components
  419. (int-component-configuration-list '())
  420. "Components are extra services on a server which are available to clients,
  421. usually on a subdomain of the main server (such as
  422. @samp{\"mycomponent.example.com\"}). Example components might be chatroom
  423. servers, user directories, or gateways to other protocols.
  424. Internal components are implemented with Prosody-specific plugins. To add an
  425. internal component, you simply fill the hostname field, and the plugin you wish
  426. to use for the component.
  427. See @url{https://prosody.im/doc/components}."
  428. global)
  429. (ext-components
  430. (ext-component-configuration-list '())
  431. "External components use XEP-0114, which most standalone components
  432. support. To add an external component, you simply fill the hostname field. See
  433. @url{https://prosody.im/doc/components}."
  434. global)
  435. (component-secret
  436. (string (configuration-missing-field 'ext-component 'component-secret))
  437. "Password which the component will use to log in."
  438. ext-component)
  439. (component-ports
  440. (non-negative-integer-list '(5347))
  441. "Port(s) Prosody listens on for component connections."
  442. global)
  443. (component-interface
  444. (string "127.0.0.1")
  445. "Interface Prosody listens on for component connections."
  446. global)
  447. (domain
  448. (string (configuration-missing-field 'virtualhost 'domain))
  449. "Domain you wish Prosody to serve."
  450. virtualhost)
  451. (hostname
  452. (string (configuration-missing-field 'int-component 'hostname))
  453. "Hostname of the component."
  454. int-component)
  455. (plugin
  456. (string (configuration-missing-field 'int-component 'plugin))
  457. "Plugin you wish to use for the component."
  458. int-component)
  459. (mod-muc
  460. (maybe-mod-muc-configuration 'disabled)
  461. "Multi-user chat (MUC) is Prosody's module for allowing you to create
  462. hosted chatrooms/conferences for XMPP users.
  463. General information on setting up and using multi-user chatrooms can be found
  464. in the \"Chatrooms\" documentation (@url{https://prosody.im/doc/chatrooms}),
  465. which you should read if you are new to XMPP chatrooms.
  466. See also @url{https://prosody.im/doc/modules/mod_muc}."
  467. int-component)
  468. (hostname
  469. (string (configuration-missing-field 'ext-component 'hostname))
  470. "Hostname of the component."
  471. ext-component)
  472. (raw-content
  473. (maybe-raw-content 'disabled)
  474. "Raw content that will be added to the configuration file."
  475. common)))
  476. ;; Serialize Virtualhost line first.
  477. (define (serialize-virtualhost-configuration config)
  478. (define (rest? field)
  479. (not (memq (configuration-field-name field)
  480. '(domain))))
  481. (let ((domain (virtualhost-configuration-domain config))
  482. (rest (filter rest? virtualhost-configuration-fields)))
  483. (format #t "VirtualHost \"~a\"\n" domain)
  484. (serialize-configuration config rest)))
  485. ;; Serialize Component line first.
  486. (define (serialize-int-component-configuration config)
  487. (define (rest? field)
  488. (not (memq (configuration-field-name field)
  489. '(hostname plugin))))
  490. (let ((hostname (int-component-configuration-hostname config))
  491. (plugin (int-component-configuration-plugin config))
  492. (rest (filter rest? int-component-configuration-fields)))
  493. (format #t "Component \"~a\" \"~a\"\n" hostname plugin)
  494. (serialize-configuration config rest)))
  495. ;; Serialize Component line first.
  496. (define (serialize-ext-component-configuration config)
  497. (define (rest? field)
  498. (not (memq (configuration-field-name field)
  499. '(hostname))))
  500. (let ((hostname (ext-component-configuration-hostname config))
  501. (rest (filter rest? ext-component-configuration-fields)))
  502. (format #t "Component \"~a\"\n" hostname)
  503. (serialize-configuration config rest)))
  504. ;; Serialize virtualhosts and components last.
  505. (define (serialize-prosody-configuration config)
  506. (define (rest? field)
  507. (not (memq (configuration-field-name field)
  508. '(virtualhosts int-components ext-components))))
  509. (let ((rest (filter rest? prosody-configuration-fields)))
  510. (serialize-configuration config rest))
  511. (serialize-virtualhost-configuration-list
  512. (prosody-configuration-virtualhosts config))
  513. (serialize-int-component-configuration-list
  514. (prosody-configuration-int-components config))
  515. (serialize-ext-component-configuration-list
  516. (prosody-configuration-ext-components config)))
  517. (define-configuration opaque-prosody-configuration
  518. (prosody
  519. (package prosody)
  520. "The prosody package.")
  521. (prosody.cfg.lua
  522. (string (configuration-missing-field 'opaque-prosody-configuration
  523. 'prosody.cfg.lua))
  524. "The contents of the @code{prosody.cfg.lua} to use."))
  525. (define (prosody-shepherd-service config)
  526. "Return a <shepherd-service> for Prosody with CONFIG."
  527. (let* ((prosody (if (opaque-prosody-configuration? config)
  528. (opaque-prosody-configuration-prosody config)
  529. (prosody-configuration-prosody config)))
  530. (prosodyctl-bin (file-append prosody "/bin/prosodyctl"))
  531. (prosodyctl-action (lambda args
  532. #~(lambda _
  533. (zero? (system* #$prosodyctl-bin #$@args))))))
  534. (list (shepherd-service
  535. (documentation "Run the Prosody XMPP server")
  536. (provision '(prosody xmpp-daemon))
  537. (requirement '(networking syslogd user-processes))
  538. (start (prosodyctl-action "start"))
  539. (stop (prosodyctl-action "stop"))))))
  540. (define %prosody-accounts
  541. (list (user-group (name "prosody") (system? #t))
  542. (user-account
  543. (name "prosody")
  544. (group "prosody")
  545. (system? #t)
  546. (comment "Prosody daemon user")
  547. (home-directory "/var/empty")
  548. (shell (file-append shadow "/sbin/nologin")))))
  549. (define (prosody-activation config)
  550. "Return the activation gexp for CONFIG."
  551. (let* ((config-dir "/etc/prosody")
  552. (default-certs-dir "/etc/prosody/certs")
  553. (data-path (prosody-configuration-data-path config))
  554. (pidfile-dir (dirname (prosody-configuration-pidfile config)))
  555. (config-str
  556. (if (opaque-prosody-configuration? config)
  557. (opaque-prosody-configuration-prosody.cfg.lua config)
  558. (with-output-to-string
  559. (lambda ()
  560. (serialize-prosody-configuration config)))))
  561. (config-file (plain-file "prosody.cfg.lua" config-str)))
  562. #~(begin
  563. (use-modules (guix build utils))
  564. (define %user (getpw "prosody"))
  565. (mkdir-p #$config-dir)
  566. (chown #$config-dir (passwd:uid %user) (passwd:gid %user))
  567. (copy-file #$config-file (string-append #$config-dir
  568. "/prosody.cfg.lua"))
  569. (mkdir-p #$default-certs-dir)
  570. (chown #$default-certs-dir (passwd:uid %user) (passwd:gid %user))
  571. (chmod #$default-certs-dir #o750)
  572. (mkdir-p #$data-path)
  573. (chown #$data-path (passwd:uid %user) (passwd:gid %user))
  574. (chmod #$data-path #o750)
  575. (mkdir-p #$pidfile-dir)
  576. (chown #$pidfile-dir (passwd:uid %user) (passwd:gid %user)))))
  577. (define prosody-service-type
  578. (service-type (name 'prosody)
  579. (extensions
  580. (list (service-extension shepherd-root-service-type
  581. prosody-shepherd-service)
  582. (service-extension account-service-type
  583. (const %prosody-accounts))
  584. (service-extension activation-service-type
  585. prosody-activation)))))
  586. ;; A little helper to make it easier to document all those fields.
  587. (define (generate-documentation)
  588. (define documentation
  589. `((prosody-configuration
  590. ,prosody-configuration-fields
  591. (ssl ssl-configuration)
  592. (virtualhosts virtualhost-configuration)
  593. (int-components int-component-configuration)
  594. (ext-components ext-component-configuration))
  595. (ssl-configuration ,ssl-configuration-fields)
  596. (int-component-configuration ,int-component-configuration-fields
  597. (mod-muc mod-muc-configuration))
  598. (ext-component-configuration ,ext-component-configuration-fields)
  599. (mod-muc-configuration ,mod-muc-configuration-fields)
  600. (virtualhost-configuration ,virtualhost-configuration-fields)
  601. (opaque-prosody-configuration ,opaque-prosody-configuration-fields)))
  602. (define (generate configuration-name)
  603. (match (assq-ref documentation configuration-name)
  604. ((fields . sub-documentation)
  605. (format #t "\nAvailable @code{~a} fields are:\n\n" configuration-name)
  606. (when (memq configuration-name
  607. '(virtualhost-configuration
  608. int-component-configuration
  609. ext-component-configuration))
  610. (format #t "all these @code{prosody-configuration} fields: ~a, plus:\n"
  611. (string-join (map (lambda (s)
  612. (format #f "@code{~a}" s)) common-fields)
  613. ", ")))
  614. (for-each
  615. (lambda (f)
  616. (let ((field-name (configuration-field-name f))
  617. (field-type (configuration-field-type f))
  618. (field-docs (string-trim-both
  619. (configuration-field-documentation f)))
  620. (default (catch #t
  621. (configuration-field-default-value-thunk f)
  622. (lambda _ 'nope))))
  623. (define (escape-chars str chars escape)
  624. (with-output-to-string
  625. (lambda ()
  626. (string-for-each (lambda (c)
  627. (when (char-set-contains? chars c)
  628. (display escape))
  629. (display c))
  630. str))))
  631. (define (show-default? val)
  632. (or (string? val) (number? val) (boolean? val)
  633. (and (list? val) (and-map show-default? val))))
  634. (format #t "@deftypevr {@code{~a} parameter} ~a ~a\n~a\n"
  635. configuration-name field-type field-name field-docs)
  636. (when (show-default? default)
  637. (format #t "Defaults to @samp{~a}.\n"
  638. (escape-chars (format #f "~s" default)
  639. (char-set #\@ #\{ #\})
  640. #\@)))
  641. (for-each generate (or (assq-ref sub-documentation field-name) '()))
  642. (format #t "@end deftypevr\n\n")))
  643. (filter (lambda (f)
  644. (not (string=? "" (configuration-field-documentation f))))
  645. fields)))))
  646. (generate 'prosody-configuration)
  647. (format #t "It could be that you just want to get a @code{prosody.cfg.lua}
  648. up and running. In that case, you can pass an
  649. @code{opaque-prosody-configuration} record as the value of
  650. @code{prosody-service-type}. As its name indicates, an opaque configuration
  651. does not have easy reflective capabilities.")
  652. (generate 'opaque-prosody-configuration)
  653. (format #t "For example, if your @code{prosody.cfg.lua} is just the empty
  654. string, you could instantiate a prosody service like this:
  655. @example
  656. (service prosody-service-type
  657. (opaque-prosody-configuration
  658. (prosody.cfg.lua \"\")))
  659. @end example"))