dict.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Sou Bunnbu <iyzsong@gmail.com>
  3. ;;; Copyright © 2016, 2017, 2018, 2020 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 (gnu services)
  25. #:use-module (gnu services shepherd)
  26. #:use-module (gnu system shadow)
  27. #:use-module ((gnu packages admin) #:select (shadow))
  28. #:use-module (gnu packages dico)
  29. #:use-module (gnu packages dictionaries)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-26)
  32. #:use-module (ice-9 match)
  33. #:export (dicod-service
  34. dicod-service-type
  35. dicod-configuration
  36. dicod-handler
  37. dicod-database
  38. %dicod-database:gcide))
  39. ;;;
  40. ;;; GNU Dico.
  41. ;;;
  42. (define-record-type* <dicod-configuration>
  43. dicod-configuration make-dicod-configuration
  44. dicod-configuration?
  45. (dico dicod-configuration-dico (default dico))
  46. (interfaces dicod-configuration-interfaces ;list of strings
  47. (default '("localhost")))
  48. (handlers dicod-configuration-handlers ;list of <dicod-handler>
  49. (default '()))
  50. (databases dicod-configuration-databases ;list of <dicod-database>
  51. (default (list %dicod-database:gcide))))
  52. (define-record-type* <dicod-handler>
  53. dicod-handler make-dicod-handler
  54. dicod-handler?
  55. (name dicod-handler-name)
  56. (module dicod-handler-module (default #f))
  57. (options dicod-handler-options (default '())))
  58. (define-record-type* <dicod-database>
  59. dicod-database make-dicod-database
  60. dicod-database?
  61. (name dicod-database-name)
  62. (handler dicod-database-handler)
  63. (complex? dicod-database-complex? (default #f))
  64. (options dicod-database-options (default '())))
  65. (define %dicod-database:gcide
  66. (dicod-database
  67. (name "gcide")
  68. (handler "gcide")
  69. (options (list #~(string-append "dbdir=" #$gcide "/share/gcide")
  70. "idxdir=/var/run/dicod"))))
  71. (define %dicod-accounts
  72. (list (user-group
  73. (name "dicod")
  74. (system? #t))
  75. (user-account
  76. (name "dicod")
  77. (group "dicod")
  78. (system? #t)
  79. (home-directory "/var/empty")
  80. (shell (file-append shadow "/sbin/nologin")))))
  81. (define (dicod-configuration-file config)
  82. (define handler->text
  83. (match-lambda
  84. (($ <dicod-handler> name #f '())
  85. `("
  86. load-module " ,name ";"))
  87. (($ <dicod-handler> name #f options)
  88. (handler->text (dicod-handler
  89. (name name)
  90. (module name)
  91. (options options))))
  92. (($ <dicod-handler> name module options)
  93. `("
  94. load-module " ,name " {
  95. command \"" ,module (string-join (list ,@options) " " 'prefix) "\";
  96. }\n"))))
  97. (define database->text
  98. (match-lambda
  99. (($ <dicod-database> name handler #f options)
  100. (append
  101. (handler->text (dicod-handler
  102. (name handler)))
  103. (database->text (dicod-database
  104. (name name)
  105. (handler handler)
  106. (complex? #t)
  107. (options options)))))
  108. (($ <dicod-database> name handler complex? options)
  109. `("
  110. database {
  111. name \"" ,name "\";
  112. handler \"" ,handler
  113. (string-join (list ,@options) " " 'prefix) "\";
  114. }\n"))))
  115. (define configuration->text
  116. (match-lambda
  117. (($ <dicod-configuration> dico (interfaces ...) handlers databases)
  118. (append `("listen ("
  119. ,(string-join interfaces ", ") ");\n")
  120. (append-map handler->text handlers)
  121. (append-map database->text databases)))))
  122. (apply mixed-text-file "dicod.conf" (configuration->text config)))
  123. (define %dicod-activation
  124. #~(begin
  125. (use-modules (guix build utils))
  126. (let ((user (getpwnam "dicod"))
  127. (rundir "/var/run/dicod"))
  128. (mkdir-p rundir)
  129. (chown rundir (passwd:uid user) (passwd:gid user)))))
  130. (define (dicod-shepherd-service config)
  131. (let ((dicod (file-append (dicod-configuration-dico config)
  132. "/bin/dicod"))
  133. (dicod.conf (dicod-configuration-file config)))
  134. (with-imported-modules (source-module-closure
  135. '((gnu build shepherd)
  136. (gnu system file-systems)))
  137. (list (shepherd-service
  138. (provision '(dicod))
  139. (requirement '(user-processes))
  140. (documentation "Run the dicod daemon.")
  141. (modules '((gnu build shepherd)
  142. (gnu system file-systems)))
  143. (start #~(make-forkexec-constructor/container
  144. (list #$dicod "--foreground"
  145. (string-append "--config=" #$dicod.conf))
  146. #:user "dicod" #:group "dicod"
  147. #:mappings (list (file-system-mapping
  148. (source "/var/run/dicod")
  149. (target source)
  150. (writable? #t)))))
  151. (stop #~(make-kill-destructor)))))))
  152. (define dicod-service-type
  153. (service-type
  154. (name 'dict)
  155. (extensions
  156. (list (service-extension account-service-type
  157. (const %dicod-accounts))
  158. (service-extension activation-service-type
  159. (const %dicod-activation))
  160. (service-extension shepherd-root-service-type
  161. dicod-shepherd-service)))
  162. (default-value (dicod-configuration))
  163. (description
  164. "Run @command{dicod}, the dictionary server of
  165. @uref{https://www.gnu.org/software/dico, GNU Dico}. @command{dicod}
  166. implements the standard DICT protocol supported by clients such as
  167. @command{dico} and GNOME Dictionary.")))
  168. (define* (dicod-service #:key (config (dicod-configuration)))
  169. "Return a service that runs the @command{dicod} daemon, an implementation
  170. of DICT server (@pxref{Dicod,,, dico, GNU Dico Manual}).
  171. The optional @var{config} argument specifies the configuration for
  172. @command{dicod}, which should be a @code{<dicod-configuration>} object, by
  173. default it serves the GNU Collaborative International Dictionary of English.
  174. You can add @command{open localhost} to your @file{~/.dico} file to make
  175. @code{localhost} the default server for @command{dico}
  176. client (@pxref{Initialization File,,, dico, GNU Dico Manual})."
  177. (service dicod-service-type config))