| 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> |
| 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) |
| 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 |
| 35 | dicod-service-type |
| 36 | dicod-configuration |
| 37 | dicod-handler |
| 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)) |
| 50 | (interfaces dicod-configuration-interfaces ;list of strings |
| 51 | (default '("localhost"))) |
| 52 | (handlers dicod-configuration-handlers ;list of <dicod-handler> |
| 53 | (default '())) |
| 54 | (databases dicod-configuration-databases ;list of <dicod-database> |
| 55 | (default (list %dicod-database:gcide)))) |
| 56 | |
| 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 | |
| 64 | (define-record-type* <dicod-database> |
| 65 | dicod-database make-dicod-database |
| 66 | 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 '()))) |
| 71 | |
| 72 | (define %dicod-database:gcide |
| 73 | (dicod-database |
| 74 | (name "gcide") |
| 75 | (handler "gcide") |
| 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") |
| 88 | (shell (file-append shadow "/sbin/nologin"))))) |
| 89 | |
| 90 | (define (dicod-configuration-file config) |
| 91 | (define handler->text |
| 92 | (match-lambda |
| 93 | (($ <dicod-handler> name #f '()) |
| 94 | `(" |
| 95 | load-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 | `(" |
| 103 | load-module " ,name " { |
| 104 | command \"" ,module (string-join (list ,@options) " " 'prefix) "\"; |
| 105 | }\n")))) |
| 106 | |
| 107 | (define database->text |
| 108 | (match-lambda |
| 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) |
| 119 | `(" |
| 120 | database { |
| 121 | name \"" ,name "\"; |
| 122 | handler \"" ,handler |
| 123 | (string-join (list ,@options) " " 'prefix) "\"; |
| 124 | }\n")))) |
| 125 | |
| 126 | (define configuration->text |
| 127 | (match-lambda |
| 128 | (($ <dicod-configuration> dico (interfaces ...) handlers databases) |
| 129 | (append `("listen (" |
| 130 | ,(string-join interfaces ", ") ");\n") |
| 131 | (append-map handler->text handlers) |
| 132 | (append-map database->text databases))))) |
| 133 | |
| 134 | (apply mixed-text-file "dicod.conf" (configuration->text config))) |
| 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) |
| 145 | (let ((dicod (file-append (dicod-configuration-dico config) |
| 146 | "/bin/dicod")) |
| 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 |
| 152 | (provision '(dicod)) |
| 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") |
| 163 | (target source) |
| 164 | (writable? #t))))) |
| 165 | (stop #~(make-kill-destructor))))))) |
| 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 |
| 176 | dicod-shepherd-service))) |
| 177 | (default-value (dicod-configuration)))) |
| 178 | |
| 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}). |
| 182 | |
| 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. |
| 186 | |
| 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)) |