dict.scm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Sou Bunnbu <iyzsong@gmail.com>
  3. ;;; Copyright © 2016, 2017, 2018, 2020, 2022 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2017 Huang Ying <huang.ying.caritas@gmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gnu services dict)
  21. #:use-module (guix gexp)
  22. #:use-module (guix records)
  23. #:use-module (guix modules)
  24. #:use-module (guix least-authority)
  25. #:use-module (gnu services)
  26. #:use-module (gnu services shepherd)
  27. #:use-module (gnu system shadow)
  28. #:use-module ((gnu packages admin) #:select (shadow))
  29. #:use-module (gnu packages dico)
  30. #:use-module (gnu packages dictionaries)
  31. #:autoload (gnu build linux-container) (%namespaces)
  32. #:autoload (gnu system file-systems) (file-system-mapping)
  33. #:use-module (srfi srfi-1)
  34. #:use-module (srfi srfi-26)
  35. #:use-module (ice-9 match)
  36. #:export (dicod-service
  37. dicod-service-type
  38. dicod-configuration
  39. dicod-handler
  40. dicod-database
  41. %dicod-database:gcide))
  42. ;;;
  43. ;;; GNU Dico.
  44. ;;;
  45. (define-record-type* <dicod-configuration>
  46. dicod-configuration make-dicod-configuration
  47. dicod-configuration?
  48. (dico dicod-configuration-dico (default dico))
  49. (interfaces dicod-configuration-interfaces ;list of strings
  50. (default '("localhost")))
  51. (handlers dicod-configuration-handlers ;list of <dicod-handler>
  52. (default '()))
  53. (databases dicod-configuration-databases ;list of <dicod-database>
  54. (default (list %dicod-database:gcide))))
  55. (define-record-type* <dicod-handler>
  56. dicod-handler make-dicod-handler
  57. dicod-handler?
  58. (name dicod-handler-name)
  59. (module dicod-handler-module (default #f))
  60. (options dicod-handler-options (default '())))
  61. (define-record-type* <dicod-database>
  62. dicod-database make-dicod-database
  63. dicod-database?
  64. (name dicod-database-name)
  65. (handler dicod-database-handler)
  66. (complex? dicod-database-complex? (default #f))
  67. (options dicod-database-options (default '())))
  68. (define %dicod-database:gcide
  69. (dicod-database
  70. (name "gcide")
  71. (handler "gcide")
  72. (options (list #~(string-append "dbdir=" #$gcide "/share/gcide")
  73. "idxdir=/var/run/dicod"))))
  74. (define %dicod-accounts
  75. (list (user-group
  76. (name "dicod")
  77. (system? #t))
  78. (user-account
  79. (name "dicod")
  80. (group "dicod")
  81. (system? #t)
  82. (home-directory "/var/empty")
  83. (shell (file-append shadow "/sbin/nologin")))))
  84. (define (dicod-configuration-file config)
  85. (define handler->text
  86. (match-lambda
  87. (($ <dicod-handler> name #f '())
  88. `("
  89. load-module " ,name ";"))
  90. (($ <dicod-handler> name #f options)
  91. (handler->text (dicod-handler
  92. (name name)
  93. (module name)
  94. (options options))))
  95. (($ <dicod-handler> name module options)
  96. `("
  97. load-module " ,name " {
  98. command \"" ,module (string-join (list ,@options) " " 'prefix) "\";
  99. }\n"))))
  100. (define database->text
  101. (match-lambda
  102. (($ <dicod-database> name handler #f options)
  103. (append
  104. (handler->text (dicod-handler
  105. (name handler)))
  106. (database->text (dicod-database
  107. (name name)
  108. (handler handler)
  109. (complex? #t)
  110. (options options)))))
  111. (($ <dicod-database> name handler complex? options)
  112. `("
  113. database {
  114. name \"" ,name "\";
  115. handler \"" ,handler
  116. (string-join (list ,@options) " " 'prefix) "\";
  117. }\n"))))
  118. (define configuration->text
  119. (match-lambda
  120. (($ <dicod-configuration> dico (interfaces ...) handlers databases)
  121. (append `("listen ("
  122. ,(string-join interfaces ", ") ");\n")
  123. (append-map handler->text handlers)
  124. (append-map database->text databases)))))
  125. (apply mixed-text-file "dicod.conf" (configuration->text config)))
  126. (define %dicod-activation
  127. #~(begin
  128. (use-modules (guix build utils))
  129. (let ((user (getpwnam "dicod"))
  130. (rundir "/var/run/dicod"))
  131. (mkdir-p rundir)
  132. (chown rundir (passwd:uid user) (passwd:gid user)))))
  133. (define (dicod-shepherd-service config)
  134. (let* ((dicod.conf (dicod-configuration-file config))
  135. (interfaces (dicod-configuration-interfaces config))
  136. (dicod (least-authority-wrapper
  137. (file-append (dicod-configuration-dico config)
  138. "/bin/dicod")
  139. #:name "dicod"
  140. #:mappings (list (file-system-mapping
  141. (source "/var/run/dicod")
  142. (target source)
  143. (writable? #t))
  144. (file-system-mapping
  145. (source "/dev/log")
  146. (target source))
  147. (file-system-mapping
  148. (source dicod.conf)
  149. (target source)))
  150. #:namespaces (delq 'net %namespaces))))
  151. (list (shepherd-service
  152. (provision '(dicod))
  153. (requirement '(user-processes))
  154. (documentation "Run the dicod daemon.")
  155. (start #~(if (and (defined? 'make-inetd-constructor)
  156. #$(= 1 (length interfaces))) ;XXX
  157. (make-inetd-constructor
  158. (list #$dicod "--inetd" "--foreground"
  159. (string-append "--config=" #$dicod.conf))
  160. (addrinfo:addr
  161. (car (getaddrinfo #$(first interfaces) "dict")))
  162. #:user "dicod" #:group "dicod"
  163. #:service-name-stem "dicod")
  164. (make-forkexec-constructor
  165. (list #$dicod "--foreground"
  166. (string-append "--config=" #$dicod.conf))
  167. #:user "dicod" #:group "dicod")))
  168. (stop #~(if (and (defined? 'make-inetd-destructor)
  169. #$(= 1 (length interfaces))) ;XXX
  170. (make-inetd-destructor)
  171. (make-kill-destructor)))))))
  172. (define dicod-service-type
  173. (service-type
  174. (name 'dict)
  175. (extensions
  176. (list (service-extension account-service-type
  177. (const %dicod-accounts))
  178. (service-extension activation-service-type
  179. (const %dicod-activation))
  180. (service-extension shepherd-root-service-type
  181. dicod-shepherd-service)))
  182. (default-value (dicod-configuration))
  183. (description
  184. "Run @command{dicod}, the dictionary server of
  185. @uref{https://www.gnu.org/software/dico, GNU Dico}. @command{dicod}
  186. implements the standard DICT protocol supported by clients such as
  187. @command{dico} and GNOME Dictionary.")))
  188. (define* (dicod-service #:key (config (dicod-configuration)))
  189. "Return a service that runs the @command{dicod} daemon, an implementation
  190. of DICT server (@pxref{Dicod,,, dico, GNU Dico Manual}).
  191. The optional @var{config} argument specifies the configuration for
  192. @command{dicod}, which should be a @code{<dicod-configuration>} object, by
  193. default it serves the GNU Collaborative International Dictionary of English.
  194. You can add @command{open localhost} to your @file{~/.dico} file to make
  195. @code{localhost} the default server for @command{dico}
  196. client (@pxref{Initialization File,,, dico, GNU Dico Manual})."
  197. (service dicod-service-type config))