services: 'references-file' depends on Guile-Gcrypt.
[jackhill/guix/guix.git] / gnu / services / dict.scm
CommitLineData
c3d38b2b
SB
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2016 Sou Bunnbu <iyzsong@gmail.com>
dd0804c6 3;;; Copyright © 2016, 2017, 2018, 2020 Ludovic Courtès <ludo@gnu.org>
9af7ecd9 4;;; Copyright © 2017 Huang Ying <huang.ying.caritas@gmail.com>
c3d38b2b
SB
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
21(define-module (gnu services dict)
22 #:use-module (guix gexp)
23 #:use-module (guix records)
9e549ad1 24 #:use-module (guix modules)
c3d38b2b
SB
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
24e96431 35 dicod-service-type
c3d38b2b 36 dicod-configuration
9af7ecd9 37 dicod-handler
c3d38b2b
SB
38 dicod-database
39 %dicod-database:gcide))
40
41\f
42;;;
43;;; GNU Dico.
44;;;
45
46(define-record-type* <dicod-configuration>
47 dicod-configuration make-dicod-configuration
48 dicod-configuration?
49 (dico dicod-configuration-dico (default dico))
a1b48465
LC
50 (interfaces dicod-configuration-interfaces ;list of strings
51 (default '("localhost")))
9af7ecd9
HY
52 (handlers dicod-configuration-handlers ;list of <dicod-handler>
53 (default '()))
54 (databases dicod-configuration-databases ;list of <dicod-database>
c3d38b2b
SB
55 (default (list %dicod-database:gcide))))
56
9af7ecd9
HY
57(define-record-type* <dicod-handler>
58 dicod-handler make-dicod-handler
59 dicod-handler?
60 (name dicod-handler-name)
61 (module dicod-handler-module (default #f))
62 (options dicod-handler-options (default '())))
63
c3d38b2b
SB
64(define-record-type* <dicod-database>
65 dicod-database make-dicod-database
66 dicod-database?
67 (name dicod-database-name)
9af7ecd9
HY
68 (handler dicod-database-handler)
69 (complex? dicod-database-complex? (default #f))
c3d38b2b
SB
70 (options dicod-database-options (default '())))
71
72(define %dicod-database:gcide
73 (dicod-database
74 (name "gcide")
9af7ecd9 75 (handler "gcide")
c3d38b2b
SB
76 (options (list #~(string-append "dbdir=" #$gcide "/share/gcide")
77 "idxdir=/var/run/dicod"))))
78
79(define %dicod-accounts
80 (list (user-group
81 (name "dicod")
82 (system? #t))
83 (user-account
84 (name "dicod")
85 (group "dicod")
86 (system? #t)
87 (home-directory "/var/empty")
9e41130b 88 (shell (file-append shadow "/sbin/nologin")))))
c3d38b2b
SB
89
90(define (dicod-configuration-file config)
9af7ecd9
HY
91 (define handler->text
92 (match-lambda
93 (($ <dicod-handler> name #f '())
94 `("
95load-module " ,name ";"))
96 (($ <dicod-handler> name #f options)
97 (handler->text (dicod-handler
98 (name name)
99 (module name)
100 (options options))))
101 (($ <dicod-handler> name module options)
102 `("
103load-module " ,name " {
104 command \"" ,module (string-join (list ,@options) " " 'prefix) "\";
105}\n"))))
106
a1b48465 107 (define database->text
c3d38b2b 108 (match-lambda
9af7ecd9
HY
109 (($ <dicod-database> name handler #f options)
110 (append
111 (handler->text (dicod-handler
112 (name handler)))
113 (database->text (dicod-database
114 (name name)
115 (handler handler)
116 (complex? #t)
117 (options options)))))
118 (($ <dicod-database> name handler complex? options)
a1b48465 119 `("
c3d38b2b
SB
120database {
121 name \"" ,name "\";
9af7ecd9 122 handler \"" ,handler
c3d38b2b 123 (string-join (list ,@options) " " 'prefix) "\";
a1b48465
LC
124}\n"))))
125
126 (define configuration->text
127 (match-lambda
9af7ecd9 128 (($ <dicod-configuration> dico (interfaces ...) handlers databases)
a1b48465
LC
129 (append `("listen ("
130 ,(string-join interfaces ", ") ");\n")
9af7ecd9 131 (append-map handler->text handlers)
a1b48465
LC
132 (append-map database->text databases)))))
133
134 (apply mixed-text-file "dicod.conf" (configuration->text config)))
c3d38b2b
SB
135
136(define %dicod-activation
137 #~(begin
138 (use-modules (guix build utils))
139 (let ((user (getpwnam "dicod"))
140 (rundir "/var/run/dicod"))
141 (mkdir-p rundir)
142 (chown rundir (passwd:uid user) (passwd:gid user)))))
143
144(define (dicod-shepherd-service config)
e5aade79
SB
145 (let ((dicod (file-append (dicod-configuration-dico config)
146 "/bin/dicod"))
147 (dicod.conf (dicod-configuration-file config)))
9e549ad1
LC
148 (with-imported-modules (source-module-closure
149 '((gnu build shepherd)
150 (gnu system file-systems)))
151 (list (shepherd-service
152 (provision '(dicod))
366ddc1a 153 (requirement '(user-processes))
9e549ad1
LC
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")
163 (target source)
164 (writable? #t)))))
165 (stop #~(make-kill-destructor)))))))
c3d38b2b
SB
166
167(define dicod-service-type
168 (service-type
169 (name 'dict)
170 (extensions
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
3d3c5650 176 dicod-shepherd-service)))
dd0804c6
LC
177 (default-value (dicod-configuration))
178 (description
179 "Run @command{dicod}, the dictionary server of
180@uref{https://www.gnu.org/software/dico, GNU Dico}. @command{dicod}
181implements the standard DICT protocol supported by clients such as
182@command{dico} and GNOME Dictionary.")))
c3d38b2b
SB
183
184(define* (dicod-service #:key (config (dicod-configuration)))
185 "Return a service that runs the @command{dicod} daemon, an implementation
186of DICT server (@pxref{Dicod,,, dico, GNU Dico Manual}).
187
188The optional @var{config} argument specifies the configuration for
189@command{dicod}, which should be a @code{<dicod-configuration>} object, by
b864ddb6 190default it serves the GNU Collaborative International Dictionary of English.
c3d38b2b
SB
191
192You can add @command{open localhost} to your @file{~/.dico} file to make
193@code{localhost} the default server for @command{dico}
194client (@pxref{Initialization File,,, dico, GNU Dico Manual})."
195 (service dicod-service-type config))