messaging.scm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 (cons "posix" 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-configuration mod-muc-configuration
  162. (name
  163. (string "Prosody Chatrooms")
  164. "The name to return in service discovery responses.")
  165. (restrict-room-creation
  166. (string-or-boolean #f)
  167. "If @samp{#t}, this will only allow admins to create new chatrooms.
  168. Otherwise anyone can create a room. The value @samp{\"local\"} restricts room
  169. creation to users on the service's parent domain. E.g. @samp{user@@example.com}
  170. can create rooms on @samp{rooms.example.com}. The value @samp{\"admin\"}
  171. restricts to service administrators only.")
  172. (max-history-messages
  173. (non-negative-integer 20)
  174. "Maximum number of history messages that will be sent to the member that has
  175. just joined the room."))
  176. (define (serialize-mod-muc-configuration field-name val)
  177. (serialize-configuration val mod-muc-configuration-fields))
  178. (define-maybe mod-muc-configuration)
  179. (define-configuration ssl-configuration
  180. (protocol
  181. (maybe-string 'disabled)
  182. "This determines what handshake to use.")
  183. (key
  184. (file-name "/etc/prosody/certs/key.pem")
  185. "Path to your private key file, relative to @code{/etc/prosody}.")
  186. (certificate
  187. (file-name "/etc/prosody/certs/cert.pem")
  188. "Path to your certificate file, relative to @code{/etc/prosody}.")
  189. (capath
  190. (file-name "/etc/ssl/certs")
  191. "Path to directory containing root certificates that you wish Prosody to
  192. trust when verifying the certificates of remote servers.")
  193. (cafile
  194. (maybe-file-name 'disabled)
  195. "Path to a file containing root certificates that you wish Prosody to trust.
  196. Similar to @code{capath} but with all certificates concatenated together.")
  197. (verify
  198. (maybe-string-list 'disabled)
  199. "A list of verification options (these mostly map to OpenSSL's
  200. @code{set_verify()} flags).")
  201. (options
  202. (maybe-string-list 'disabled)
  203. "A list of general options relating to SSL/TLS. These map to OpenSSL's
  204. @code{set_options()}. For a full list of options available in LuaSec, see the
  205. LuaSec source.")
  206. (depth
  207. (maybe-non-negative-integer 'disabled)
  208. "How long a chain of certificate authorities to check when looking for a
  209. trusted root certificate.")
  210. (ciphers
  211. (maybe-string 'disabled)
  212. "An OpenSSL cipher string. This selects what ciphers Prosody will offer to
  213. clients, and in what order.")
  214. (dhparam
  215. (maybe-file-name 'disabled)
  216. "A path to a file containing parameters for Diffie-Hellman key exchange. You
  217. can create such a file with:
  218. @code{openssl dhparam -out /etc/prosody/certs/dh-2048.pem 2048}")
  219. (curve
  220. (maybe-string 'disabled)
  221. "Curve for Elliptic curve Diffie-Hellman. Prosody's default is
  222. @samp{\"secp384r1\"}.")
  223. (verifyext
  224. (maybe-string-list 'disabled)
  225. "A list of \"extra\" verification options.")
  226. (password
  227. (maybe-string 'disabled)
  228. "Password for encrypted private keys."))
  229. (define (serialize-ssl-configuration field-name val)
  230. (format #t "ssl = {\n")
  231. (serialize-configuration val ssl-configuration-fields)
  232. (format #t "};\n"))
  233. (define-maybe ssl-configuration)
  234. (define %default-modules-enabled
  235. '("roster"
  236. "saslauth"
  237. "tls"
  238. "dialback"
  239. "disco"
  240. "private"
  241. "vcard"
  242. "version"
  243. "uptime"
  244. "time"
  245. "ping"
  246. "pep"
  247. "register"
  248. "admin_adhoc"))
  249. ;; Guile bug. Use begin wrapper, because otherwise virtualhost-configuration
  250. ;; is assumed to be a function. See
  251. ;; https://www.gnu.org/software/guile/manual/html_node/R6RS-Incompatibilities.html
  252. (begin
  253. (define (virtualhost-configuration-list? val)
  254. (and (list? val) (and-map virtualhost-configuration? val)))
  255. (define (serialize-virtualhost-configuration-list l)
  256. (for-each
  257. (lambda (val) (serialize-virtualhost-configuration val)) l))
  258. (define (int-component-configuration-list? val)
  259. (and (list? val) (and-map int-component-configuration? val)))
  260. (define (serialize-int-component-configuration-list l)
  261. (for-each
  262. (lambda (val) (serialize-int-component-configuration val)) l))
  263. (define (ext-component-configuration-list? val)
  264. (and (list? val) (and-map ext-component-configuration? val)))
  265. (define (serialize-ext-component-configuration-list l)
  266. (for-each
  267. (lambda (val) (serialize-ext-component-configuration val)) l))
  268. (define-all-configurations prosody-configuration
  269. (prosody
  270. (package prosody)
  271. "The Prosody package."
  272. global)
  273. (data-path
  274. (file-name "/var/lib/prosody")
  275. "Location of the Prosody data storage directory. See
  276. @url{http://prosody.im/doc/configure}."
  277. global)
  278. (plugin-paths
  279. (file-name-list '())
  280. "Additional plugin directories. They are searched in all the specified
  281. paths in order. See @url{http://prosody.im/doc/plugins_directory}."
  282. global)
  283. (admins
  284. (string-list '())
  285. "This is a list of accounts that are admins for the server. Note that you
  286. must create the accounts separately. See @url{http://prosody.im/doc/admins} and
  287. @url{http://prosody.im/doc/creating_accounts}.
  288. Example: @code{(admins '(\"user1@@example.com\" \"user2@@example.net\"))}"
  289. common)
  290. (use-libevent?
  291. (boolean #f)
  292. "Enable use of libevent for better performance under high load. See
  293. @url{http://prosody.im/doc/libevent}."
  294. common)
  295. (modules-enabled
  296. (module-list %default-modules-enabled)
  297. "This is the list of modules Prosody will load on startup. It looks for
  298. @code{mod_modulename.lua} in the plugins folder, so make sure that exists too.
  299. Documentation on modules can be found at: @url{http://prosody.im/doc/modules}.
  300. Defaults to @samp{%default-modules-enabled}."
  301. common)
  302. (modules-disabled
  303. (string-list '())
  304. "@samp{\"offline\"}, @samp{\"c2s\"} and @samp{\"s2s\"} are auto-loaded, but
  305. should you want to disable them then add them to this list."
  306. common)
  307. (groups-file
  308. (file-name "/var/lib/prosody/sharedgroups.txt")
  309. "Path to a text file where the shared groups are defined. If this path is
  310. empty then @samp{mod_groups} does nothing. See
  311. @url{http://prosody.im/doc/modules/mod_groups}."
  312. common)
  313. (allow-registration?
  314. (boolean #f)
  315. "Disable account creation by default, for security. See
  316. @url{http://prosody.im/doc/creating_accounts}."
  317. common)
  318. (ssl
  319. (maybe-ssl-configuration (ssl-configuration))
  320. "These are the SSL/TLS-related settings. Most of them are disabled so to
  321. use Prosody's defaults. If you do not completely understand these options, do
  322. not add them to your config, it is easy to lower the security of your server
  323. using them. See @url{http://prosody.im/doc/advanced_ssl_config}."
  324. common)
  325. (c2s-require-encryption?
  326. (boolean #f)
  327. "Whether to force all client-to-server connections to be encrypted or not.
  328. See @url{http://prosody.im/doc/modules/mod_tls}."
  329. common)
  330. (s2s-require-encryption?
  331. (boolean #f)
  332. "Whether to force all server-to-server connections to be encrypted or not.
  333. See @url{http://prosody.im/doc/modules/mod_tls}."
  334. common)
  335. (s2s-secure-auth?
  336. (boolean #f)
  337. "Whether to require encryption and certificate authentication. This
  338. provides ideal security, but requires servers you communicate with to support
  339. encryption AND present valid, trusted certificates. See
  340. @url{http://prosody.im/doc/s2s#security}."
  341. common)
  342. (s2s-insecure-domains
  343. (string-list '())
  344. "Many servers don't support encryption or have invalid or self-signed
  345. certificates. You can list domains here that will not be required to
  346. authenticate using certificates. They will be authenticated using DNS. See
  347. @url{http://prosody.im/doc/s2s#security}."
  348. common)
  349. (s2s-secure-domains
  350. (string-list '())
  351. "Even if you leave @code{s2s-secure-auth?} disabled, you can still require
  352. valid certificates for some domains by specifying a list here. See
  353. @url{http://prosody.im/doc/s2s#security}."
  354. common)
  355. (authentication
  356. (string "internal_plain")
  357. "Select the authentication backend to use. The default provider stores
  358. passwords in plaintext and uses Prosody's configured data storage to store the
  359. authentication data. If you do not trust your server please see
  360. @url{http://prosody.im/doc/modules/mod_auth_internal_hashed} for information
  361. about using the hashed backend. See also
  362. @url{http://prosody.im/doc/authentication}"
  363. common)
  364. ;; TODO: Handle more complicated log structures.
  365. (log
  366. (maybe-string "*syslog")
  367. "Set logging options. Advanced logging configuration is not yet supported
  368. by the GuixSD Prosody Service. See @url{http://prosody.im/doc/logging}."
  369. common)
  370. (pidfile
  371. (file-name "/var/run/prosody/prosody.pid")
  372. "File to write pid in. See @url{http://prosody.im/doc/modules/mod_posix}."
  373. global)
  374. (virtualhosts
  375. (virtualhost-configuration-list
  376. (list (virtualhost-configuration
  377. (domain "localhost"))))
  378. "A host in Prosody is a domain on which user accounts can be created. For
  379. example if you want your users to have addresses like
  380. @samp{\"john.smith@@example.com\"} then you need to add a host
  381. @samp{\"example.com\"}. All options in this list will apply only to this host.
  382. Note: the name \"virtual\" host is used in configuration to avoid confusion with
  383. the actual physical host that Prosody is installed on. A single Prosody
  384. instance can serve many domains, each one defined as a VirtualHost entry in
  385. Prosody's configuration. Conversely a server that hosts a single domain would
  386. have just one VirtualHost entry.
  387. See @url{http://prosody.im/doc/configure#virtual_host_settings}."
  388. global)
  389. (int-components
  390. (int-component-configuration-list '())
  391. "Components are extra services on a server which are available to clients,
  392. usually on a subdomain of the main server (such as
  393. @samp{\"mycomponent.example.com\"}). Example components might be chatroom
  394. servers, user directories, or gateways to other protocols.
  395. Internal components are implemented with Prosody-specific plugins. To add an
  396. internal component, you simply fill the hostname field, and the plugin you wish
  397. to use for the component.
  398. See @url{http://prosody.im/doc/components}."
  399. global)
  400. (ext-components
  401. (ext-component-configuration-list '())
  402. "External components use XEP-0114, which most standalone components
  403. support. To add an external component, you simply fill the hostname field. See
  404. @url{http://prosody.im/doc/components}."
  405. global)
  406. (component-secret
  407. (string (configuration-missing-field 'ext-component 'component-secret))
  408. "Password which the component will use to log in."
  409. ext-component)
  410. (component-ports
  411. (non-negative-integer-list '(5347))
  412. "Port(s) Prosody listens on for component connections."
  413. global)
  414. (component-interface
  415. (string "127.0.0.1")
  416. "Interface Prosody listens on for component connections."
  417. global)
  418. (domain
  419. (string (configuration-missing-field 'virtualhost 'domain))
  420. "Domain you wish Prosody to serve."
  421. virtualhost)
  422. (hostname
  423. (string (configuration-missing-field 'int-component 'hostname))
  424. "Hostname of the component."
  425. int-component)
  426. (plugin
  427. (string (configuration-missing-field 'int-component 'plugin))
  428. "Plugin you wish to use for the component."
  429. int-component)
  430. (mod-muc
  431. (maybe-mod-muc-configuration 'disabled)
  432. "Multi-user chat (MUC) is Prosody's module for allowing you to create
  433. hosted chatrooms/conferences for XMPP users.
  434. General information on setting up and using multi-user chatrooms can be found
  435. in the \"Chatrooms\" documentation (@url{http://prosody.im/doc/chatrooms}),
  436. which you should read if you are new to XMPP chatrooms.
  437. See also @url{http://prosody.im/doc/modules/mod_muc}."
  438. int-component)
  439. (hostname
  440. (string (configuration-missing-field 'ext-component 'hostname))
  441. "Hostname of the component."
  442. ext-component)))
  443. ;; Serialize Virtualhost line first.
  444. (define (serialize-virtualhost-configuration config)
  445. (define (rest? field)
  446. (not (memq (configuration-field-name field)
  447. '(domain))))
  448. (let ((domain (virtualhost-configuration-domain config))
  449. (rest (filter rest? virtualhost-configuration-fields)))
  450. (format #t "VirtualHost \"~a\"\n" domain)
  451. (serialize-configuration config rest)))
  452. ;; Serialize Component line first.
  453. (define (serialize-int-component-configuration config)
  454. (define (rest? field)
  455. (not (memq (configuration-field-name field)
  456. '(hostname plugin))))
  457. (let ((hostname (int-component-configuration-hostname config))
  458. (plugin (int-component-configuration-plugin config))
  459. (rest (filter rest? int-component-configuration-fields)))
  460. (format #t "Component \"~a\" \"~a\"\n" hostname plugin)
  461. (serialize-configuration config rest)))
  462. ;; Serialize Component line first.
  463. (define (serialize-ext-component-configuration config)
  464. (define (rest? field)
  465. (not (memq (configuration-field-name field)
  466. '(hostname))))
  467. (let ((hostname (ext-component-configuration-hostname config))
  468. (rest (filter rest? ext-component-configuration-fields)))
  469. (format #t "Component \"~a\"\n" hostname)
  470. (serialize-configuration config rest)))
  471. ;; Serialize virtualhosts and components last.
  472. (define (serialize-prosody-configuration config)
  473. (define (rest? field)
  474. (not (memq (configuration-field-name field)
  475. '(virtualhosts int-components ext-components))))
  476. (let ((rest (filter rest? prosody-configuration-fields)))
  477. (serialize-configuration config rest))
  478. (serialize-virtualhost-configuration-list
  479. (prosody-configuration-virtualhosts config))
  480. (serialize-int-component-configuration-list
  481. (prosody-configuration-int-components config))
  482. (serialize-ext-component-configuration-list
  483. (prosody-configuration-ext-components config)))
  484. (define-configuration opaque-prosody-configuration
  485. (prosody
  486. (package prosody)
  487. "The prosody package.")
  488. (prosody.cfg.lua
  489. (string (configuration-missing-field 'opaque-prosody-configuration
  490. 'prosody.cfg.lua))
  491. "The contents of the @code{prosody.cfg.lua} to use."))
  492. (define (prosody-shepherd-service config)
  493. "Return a <shepherd-service> for Prosody with CONFIG."
  494. (let* ((prosody (if (opaque-prosody-configuration? config)
  495. (opaque-prosody-configuration-prosody config)
  496. (prosody-configuration-prosody config)))
  497. (prosodyctl-bin (file-append prosody "/bin/prosodyctl"))
  498. (prosodyctl-action (lambda args
  499. #~(lambda _
  500. (zero? (system* #$prosodyctl-bin #$@args))))))
  501. (list (shepherd-service
  502. (documentation "Run the Prosody XMPP server")
  503. (provision '(prosody xmpp-daemon))
  504. (requirement '(networking syslogd user-processes))
  505. (start (prosodyctl-action "start"))
  506. (stop (prosodyctl-action "stop"))))))
  507. (define %prosody-accounts
  508. (list (user-group (name "prosody") (system? #t))
  509. (user-account
  510. (name "prosody")
  511. (group "prosody")
  512. (system? #t)
  513. (comment "Prosody daemon user")
  514. (home-directory "/var/empty")
  515. (shell (file-append shadow "/sbin/nologin")))))
  516. (define (prosody-activation config)
  517. "Return the activation gexp for CONFIG."
  518. (let* ((config-dir "/etc/prosody")
  519. (default-certs-dir "/etc/prosody/certs")
  520. (data-path (prosody-configuration-data-path config))
  521. (pidfile-dir (dirname (prosody-configuration-pidfile config)))
  522. (config-str
  523. (if (opaque-prosody-configuration? config)
  524. (opaque-prosody-configuration-prosody.cfg.lua config)
  525. (with-output-to-string
  526. (lambda ()
  527. (serialize-prosody-configuration config)))))
  528. (config-file (plain-file "prosody.cfg.lua" config-str)))
  529. #~(begin
  530. (use-modules (guix build utils))
  531. (define %user (getpw "prosody"))
  532. (mkdir-p #$config-dir)
  533. (chown #$config-dir (passwd:uid %user) (passwd:gid %user))
  534. (copy-file #$config-file (string-append #$config-dir
  535. "/prosody.cfg.lua"))
  536. (mkdir-p #$default-certs-dir)
  537. (chown #$default-certs-dir (passwd:uid %user) (passwd:gid %user))
  538. (chmod #$default-certs-dir #o750)
  539. (mkdir-p #$data-path)
  540. (chown #$data-path (passwd:uid %user) (passwd:gid %user))
  541. (chmod #$data-path #o750)
  542. (mkdir-p #$pidfile-dir)
  543. (chown #$pidfile-dir (passwd:uid %user) (passwd:gid %user)))))
  544. (define prosody-service-type
  545. (service-type (name 'prosody)
  546. (extensions
  547. (list (service-extension shepherd-root-service-type
  548. prosody-shepherd-service)
  549. (service-extension account-service-type
  550. (const %prosody-accounts))
  551. (service-extension activation-service-type
  552. prosody-activation)))))
  553. ;; A little helper to make it easier to document all those fields.
  554. (define (generate-documentation)
  555. (define documentation
  556. `((prosody-configuration
  557. ,prosody-configuration-fields
  558. (ssl ssl-configuration)
  559. (virtualhosts virtualhost-configuration)
  560. (int-components int-component-configuration)
  561. (ext-components ext-component-configuration))
  562. (ssl-configuration ,ssl-configuration-fields)
  563. (int-component-configuration ,int-component-configuration-fields
  564. (mod-muc mod-muc-configuration))
  565. (ext-component-configuration ,ext-component-configuration-fields)
  566. (mod-muc-configuration ,mod-muc-configuration-fields)
  567. (virtualhost-configuration ,virtualhost-configuration-fields)
  568. (opaque-prosody-configuration ,opaque-prosody-configuration-fields)))
  569. (define (generate configuration-name)
  570. (match (assq-ref documentation configuration-name)
  571. ((fields . sub-documentation)
  572. (format #t "\nAvailable @code{~a} fields are:\n\n" configuration-name)
  573. (when (memq configuration-name
  574. '(virtualhost-configuration
  575. int-component-configuration
  576. ext-component-configuration))
  577. (format #t "all these @code{prosody-configuration} fields: ~a, plus:\n"
  578. (string-join (map (lambda (s)
  579. (format #f "@code{~a}" s)) common-fields)
  580. ", ")))
  581. (for-each
  582. (lambda (f)
  583. (let ((field-name (configuration-field-name f))
  584. (field-type (configuration-field-type f))
  585. (field-docs (string-trim-both
  586. (configuration-field-documentation f)))
  587. (default (catch #t
  588. (configuration-field-default-value-thunk f)
  589. (lambda _ 'nope))))
  590. (define (escape-chars str chars escape)
  591. (with-output-to-string
  592. (lambda ()
  593. (string-for-each (lambda (c)
  594. (when (char-set-contains? chars c)
  595. (display escape))
  596. (display c))
  597. str))))
  598. (define (show-default? val)
  599. (or (string? default) (number? default) (boolean? default)
  600. (and (list? val) (and-map show-default? val))))
  601. (format #t "@deftypevr {@code{~a} parameter} ~a ~a\n~a\n"
  602. configuration-name field-type field-name field-docs)
  603. (when (show-default? default)
  604. (format #t "Defaults to @samp{~a}.\n"
  605. (escape-chars (format #f "~s" default)
  606. (char-set #\@ #\{ #\})
  607. #\@)))
  608. (for-each generate (or (assq-ref sub-documentation field-name) '()))
  609. (format #t "@end deftypevr\n\n")))
  610. (filter (lambda (f)
  611. (not (string=? "" (configuration-field-documentation f))))
  612. fields)))))
  613. (generate 'prosody-configuration)
  614. (format #t "It could be that you just want to get a @code{prosody.cfg.lua}
  615. up and running. In that case, you can pass an
  616. @code{opaque-prosody-configuration} record as the value of
  617. @code{prosody-service-type}. As its name indicates, an opaque configuration
  618. does not have easy reflective capabilities.")
  619. (generate 'opaque-prosody-configuration)
  620. (format #t "For example, if your @code{prosody.cfg.lua} is just the empty
  621. string, you could instantiate a prosody service like this:
  622. @example
  623. (service prosody-service-type
  624. (opaque-prosody-configuration
  625. (prosody.cfg.lua \"\")))
  626. @end example"))