dict.scm 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Sou Bunnbu <iyzsong@gmail.com>
  3. ;;; Copyright © 2016, 2017, 2018, 2020, 2022, 2023 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 deprecation)
  22. #:use-module (guix gexp)
  23. #:use-module (guix records)
  24. #:use-module (guix modules)
  25. #:use-module (guix least-authority)
  26. #:use-module (gnu services)
  27. #:use-module (gnu services shepherd)
  28. #:use-module (gnu system shadow)
  29. #:use-module ((gnu packages admin) #:select (shadow))
  30. #:use-module (gnu packages dico)
  31. #:use-module (gnu packages dictionaries)
  32. #:autoload (gnu build linux-container) (%namespaces)
  33. #:autoload (gnu system file-systems) (file-system-mapping)
  34. #:use-module (srfi srfi-1)
  35. #:use-module (srfi srfi-26)
  36. #:use-module (ice-9 match)
  37. #:export (dicod-service ; deprecated
  38. dicod-service-type
  39. dicod-configuration
  40. dicod-handler
  41. dicod-database
  42. %dicod-database:gcide))
  43. ;;;
  44. ;;; GNU Dico.
  45. ;;;
  46. (define-record-type* <dicod-configuration>
  47. dicod-configuration make-dicod-configuration
  48. dicod-configuration?
  49. (dico dicod-configuration-dico (default dico))
  50. (interfaces dicod-configuration-interfaces ;list of strings
  51. (default '("localhost")))
  52. (handlers dicod-configuration-handlers ;list of <dicod-handler>
  53. (default '()))
  54. (databases dicod-configuration-databases ;list of <dicod-database>
  55. (default (list %dicod-database:gcide)))
  56. (home-service? dicod-configuration-home-service? ;boolean
  57. (default for-home?) (innate)))
  58. (define-record-type* <dicod-handler>
  59. dicod-handler make-dicod-handler
  60. dicod-handler?
  61. (name dicod-handler-name)
  62. (module dicod-handler-module (default #f))
  63. (options dicod-handler-options (default '())))
  64. (define-record-type* <dicod-database>
  65. dicod-database make-dicod-database
  66. dicod-database?
  67. (name dicod-database-name)
  68. (handler dicod-database-handler)
  69. (complex? dicod-database-complex? (default #f))
  70. (options dicod-database-options (default '())))
  71. (define %dicod-gcide-index
  72. ;; The GCIDE pre-built index. The Dico 'gcide' module can build it lazily;
  73. ;; do it upfront so there's no need for a writable directory at run-time.
  74. (computed-file "dicod-gcide-index"
  75. (with-imported-modules '((guix build utils))
  76. #~(begin
  77. (use-modules (guix build utils))
  78. (mkdir #$output)
  79. (invoke #+(file-append dico "/libexec/idxgcide")
  80. #+(file-append gcide "/share/gcide")
  81. #$output)))))
  82. (define %dicod-database:gcide
  83. (dicod-database
  84. (name "gcide")
  85. (handler "gcide")
  86. (options (list #~(string-append "dbdir=" #$gcide "/share/gcide")
  87. #~(string-append "idxdir=" #$%dicod-gcide-index)))))
  88. (define %dicod-accounts
  89. (list (user-group
  90. (name "dicod")
  91. (system? #t))
  92. (user-account
  93. (name "dicod")
  94. (group "dicod")
  95. (system? #t)
  96. (home-directory "/var/empty")
  97. (shell (file-append shadow "/sbin/nologin")))))
  98. (define (dicod-configuration-file config)
  99. (define handler->text
  100. (match-lambda
  101. (($ <dicod-handler> name #f '())
  102. `("
  103. load-module " ,name ";"))
  104. (($ <dicod-handler> name #f options)
  105. (handler->text (dicod-handler
  106. (name name)
  107. (module name)
  108. (options options))))
  109. (($ <dicod-handler> name module options)
  110. `("
  111. load-module " ,name " {
  112. command \"" ,module (string-join (list ,@options) " " 'prefix) "\";
  113. }\n"))))
  114. (define database->text
  115. (match-lambda
  116. (($ <dicod-database> name handler #f options)
  117. (append
  118. (handler->text (dicod-handler
  119. (name handler)))
  120. (database->text (dicod-database
  121. (name name)
  122. (handler handler)
  123. (complex? #t)
  124. (options options)))))
  125. (($ <dicod-database> name handler complex? options)
  126. `("
  127. database {
  128. name \"" ,name "\";
  129. handler \"" ,handler
  130. (string-join (list ,@options) " " 'prefix) "\";
  131. }\n"))))
  132. (define configuration->text
  133. (match-lambda
  134. (($ <dicod-configuration> dico (interfaces ...) handlers databases)
  135. (append `("listen ("
  136. ,(string-join interfaces ", ") ");\n")
  137. (append-map handler->text handlers)
  138. (append-map database->text databases)))))
  139. (apply mixed-text-file "dicod.conf" (configuration->text config)))
  140. (define (dicod-shepherd-service config)
  141. (let* ((dicod.conf (dicod-configuration-file config))
  142. (interfaces (dicod-configuration-interfaces config))
  143. (home-service? (dicod-configuration-home-service? config))
  144. (mappings `(,@(if home-service?
  145. '()
  146. (list (file-system-mapping
  147. (source "/dev/log")
  148. (target source))))
  149. ,(file-system-mapping
  150. (source dicod.conf)
  151. (target source))))
  152. (dicod (least-authority-wrapper
  153. (file-append (dicod-configuration-dico config)
  154. "/bin/dicod")
  155. #:name "dicod"
  156. #:mappings mappings
  157. #:namespaces (delq 'net %namespaces))))
  158. (list (shepherd-service
  159. (provision '(dicod))
  160. (requirement (if home-service?
  161. '()
  162. '(user-processes)))
  163. (documentation "Run the dicod daemon.")
  164. (start #~(make-inetd-constructor
  165. (list #$dicod "--inetd" "--foreground"
  166. (string-append "--config=" #$dicod.conf))
  167. (map (lambda (interface)
  168. (endpoint
  169. (addrinfo:addr
  170. (car (getaddrinfo interface "dict")))))
  171. '#$interfaces)
  172. #:requirements '#$requirement
  173. #:user #$(and (not home-service?) "dicod")
  174. #:group #$(and (not home-service?) "dicod")
  175. #:service-name-stem "dicod"))
  176. (stop #~(make-inetd-destructor))
  177. (actions (list (shepherd-configuration-action dicod.conf)))))))
  178. (define dicod-service-type
  179. (service-type
  180. (name 'dict)
  181. (extensions
  182. (list (service-extension account-service-type
  183. (const %dicod-accounts))
  184. (service-extension shepherd-root-service-type
  185. dicod-shepherd-service)))
  186. (default-value (dicod-configuration))
  187. (description
  188. "Run @command{dicod}, the dictionary server of
  189. @uref{https://www.gnu.org/software/dico, GNU Dico}. @command{dicod}
  190. implements the standard DICT protocol supported by clients such as
  191. @command{dico} and GNOME Dictionary.")))
  192. (define-deprecated (dicod-service #:key (config (dicod-configuration)))
  193. dicod-service-type
  194. "Return a service that runs the @command{dicod} daemon, an implementation
  195. of DICT server (@pxref{Dicod,,, dico, GNU Dico Manual}).
  196. The optional @var{config} argument specifies the configuration for
  197. @command{dicod}, which should be a @code{<dicod-configuration>} object, by
  198. default it serves the GNU Collaborative International Dictionary of English.
  199. You can add @command{open localhost} to your @file{~/.dico} file to make
  200. @code{localhost} the default server for @command{dico}
  201. client (@pxref{Initialization File,,, dico, GNU Dico Manual})."
  202. (service dicod-service-type config))