services: rottlog: Add Rottlog to the global profile.
[jackhill/guix/guix.git] / gnu / services / dict.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Sou Bunnbu <iyzsong@gmail.com>
3 ;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
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-database
36 %dicod-database:gcide))
37
38 \f
39 ;;;
40 ;;; GNU Dico.
41 ;;;
42
43 (define-record-type* <dicod-configuration>
44 dicod-configuration make-dicod-configuration
45 dicod-configuration?
46 (dico dicod-configuration-dico (default dico))
47 (interfaces dicod-configuration-interfaces ;list of strings
48 (default '("localhost")))
49 (databases dicod-configuration-databases
50 ;; list of <dicod-database>
51 (default (list %dicod-database:gcide))))
52
53 (define-record-type* <dicod-database>
54 dicod-database make-dicod-database
55 dicod-database?
56 (name dicod-database-name)
57 (module dicod-database-module)
58 (options dicod-database-options (default '())))
59
60 (define %dicod-database:gcide
61 (dicod-database
62 (name "gcide")
63 (module "gcide")
64 (options (list #~(string-append "dbdir=" #$gcide "/share/gcide")
65 "idxdir=/var/run/dicod"))))
66
67 (define %dicod-accounts
68 (list (user-group
69 (name "dicod")
70 (system? #t))
71 (user-account
72 (name "dicod")
73 (group "dicod")
74 (system? #t)
75 (home-directory "/var/empty")
76 (shell (file-append shadow "/sbin/nologin")))))
77
78 (define (dicod-configuration-file config)
79 (define database->text
80 (match-lambda
81 (($ <dicod-database> name module options)
82 `("
83 load-module " ,module ";
84 database {
85 name \"" ,name "\";
86 handler \"" ,module
87 (string-join (list ,@options) " " 'prefix) "\";
88 }\n"))))
89
90 (define configuration->text
91 (match-lambda
92 (($ <dicod-configuration> dico (interfaces ...) databases)
93 (append `("listen ("
94 ,(string-join interfaces ", ") ");\n")
95 (append-map database->text databases)))))
96
97 (apply mixed-text-file "dicod.conf" (configuration->text config)))
98
99 (define %dicod-activation
100 #~(begin
101 (use-modules (guix build utils))
102 (let ((user (getpwnam "dicod"))
103 (rundir "/var/run/dicod"))
104 (mkdir-p rundir)
105 (chown rundir (passwd:uid user) (passwd:gid user)))))
106
107 (define (dicod-shepherd-service config)
108 (list (shepherd-service
109 (provision '(dicod))
110 (documentation "Run the dicod daemon.")
111 (start #~(make-forkexec-constructor
112 (list (string-append #$dico "/bin/dicod") "--foreground"
113 (string-append
114 "--config=" #$(dicod-configuration-file config)))
115 #:user "dicod" #:group "dicod"))
116 (stop #~(make-kill-destructor)))))
117
118 (define dicod-service-type
119 (service-type
120 (name 'dict)
121 (extensions
122 (list (service-extension account-service-type
123 (const %dicod-accounts))
124 (service-extension activation-service-type
125 (const %dicod-activation))
126 (service-extension shepherd-root-service-type
127 dicod-shepherd-service)))))
128
129 (define* (dicod-service #:key (config (dicod-configuration)))
130 "Return a service that runs the @command{dicod} daemon, an implementation
131 of DICT server (@pxref{Dicod,,, dico, GNU Dico Manual}).
132
133 The optional @var{config} argument specifies the configuration for
134 @command{dicod}, which should be a @code{<dicod-configuration>} object, by
135 default it serves the GNU Collaborative International Dictonary of English.
136
137 You can add @command{open localhost} to your @file{~/.dico} file to make
138 @code{localhost} the default server for @command{dico}
139 client (@pxref{Initialization File,,, dico, GNU Dico Manual})."
140 (service dicod-service-type config))