1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Sou Bunnbu <iyzsong@gmail.com>
3 ;;; Copyright © 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; Copyright © 2017 Huang Ying <huang.ying.caritas@gmail.com>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
21 (define-module (gnu services dict)
22 #:use-module (guix gexp)
23 #:use-module (guix records)
24 #:use-module (guix modules)
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 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-26)
33 #:use-module (ice-9 match)
34 #:export (dicod-service
39 %dicod-database:gcide))
46 (define-record-type* <dicod-configuration>
47 dicod-configuration make-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>
54 (databases dicod-configuration-databases ;list of <dicod-database>
55 (default (list %dicod-database:gcide))))
57 (define-record-type* <dicod-handler>
58 dicod-handler make-dicod-handler
60 (name dicod-handler-name)
61 (module dicod-handler-module (default #f))
62 (options dicod-handler-options (default '())))
64 (define-record-type* <dicod-database>
65 dicod-database make-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 '())))
72 (define %dicod-database:gcide
76 (options (list #~(string-append "dbdir=" #$gcide "/share/gcide")
77 "idxdir=/var/run/dicod"))))
79 (define %dicod-accounts
87 (home-directory "/var/empty")
88 (shell (file-append shadow "/sbin/nologin")))))
90 (define (dicod-configuration-file config)
93 (($ <dicod-handler> name #f '())
95 load-module " ,name ";"))
96 (($ <dicod-handler> name #f options)
97 (handler->text (dicod-handler
101 (($ <dicod-handler> name module options)
103 load-module " ,name " {
104 command \"" ,module (string-join (list ,@options) " " 'prefix) "\";
107 (define database->text
109 (($ <dicod-database> name handler #f options)
111 (handler->text (dicod-handler
113 (database->text (dicod-database
117 (options options)))))
118 (($ <dicod-database> name handler complex? options)
123 (string-join (list ,@options) " " 'prefix) "\";
126 (define configuration->text
128 (($ <dicod-configuration> dico (interfaces ...) handlers databases)
130 ,(string-join interfaces ", ") ");\n")
131 (append-map handler->text handlers)
132 (append-map database->text databases)))))
134 (apply mixed-text-file "dicod.conf" (configuration->text config)))
136 (define %dicod-activation
138 (use-modules (guix build utils))
139 (let ((user (getpwnam "dicod"))
140 (rundir "/var/run/dicod"))
142 (chown rundir (passwd:uid user) (passwd:gid user)))))
144 (define (dicod-shepherd-service config)
145 (let ((dicod (file-append (dicod-configuration-dico config)
147 (dicod.conf (dicod-configuration-file config)))
148 (with-imported-modules (source-module-closure
149 '((gnu build shepherd)
150 (gnu system file-systems)))
151 (list (shepherd-service
153 (requirement '(user-processes))
154 (documentation "Run the dicod daemon.")
155 (modules '((gnu build shepherd)
156 (gnu system file-systems)))
157 (start #~(make-forkexec-constructor/container
158 (list #$dicod "--foreground"
159 (string-append "--config=" #$dicod.conf))
160 #:user "dicod" #:group "dicod"
161 #:mappings (list (file-system-mapping
162 (source "/var/run/dicod")
165 (stop #~(make-kill-destructor)))))))
167 (define dicod-service-type
171 (list (service-extension account-service-type
172 (const %dicod-accounts))
173 (service-extension activation-service-type
174 (const %dicod-activation))
175 (service-extension shepherd-root-service-type
176 dicod-shepherd-service)))
177 (default-value (dicod-configuration))))
179 (define* (dicod-service #:key (config (dicod-configuration)))
180 "Return a service that runs the @command{dicod} daemon, an implementation
181 of DICT server (@pxref{Dicod,,, dico, GNU Dico Manual}).
183 The optional @var{config} argument specifies the configuration for
184 @command{dicod}, which should be a @code{<dicod-configuration>} object, by
185 default it serves the GNU Collaborative International Dictonary of English.
187 You can add @command{open localhost} to your @file{~/.dico} file to make
188 @code{localhost} the default server for @command{dico}
189 client (@pxref{Initialization File,,, dico, GNU Dico Manual})."
190 (service dicod-service-type config))