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