dict.scm 6.3 KB

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