| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2019 Christopher Baines <mail@cbaines.net> |
| 3 | ;;; |
| 4 | ;;; This file is part of GNU Guix. |
| 5 | ;;; |
| 6 | ;;; GNU Guix is free software; you can redistribute it and/or modify |
| 7 | ;;; it under the terms of the GNU General Public License as published by |
| 8 | ;;; the Free Software Foundation, either version 3 of the License, or |
| 9 | ;;; (at your option) any later version. |
| 10 | ;;; |
| 11 | ;;; GNU Guix is distributed in the hope that it will be useful, |
| 12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | ;;; GNU General Public License for more details. |
| 15 | ;;; |
| 16 | ;;; You should have received a copy of the GNU General Public License |
| 17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 18 | |
| 19 | (define-module (gnu services guix) |
| 20 | #:use-module (ice-9 match) |
| 21 | #:use-module (guix gexp) |
| 22 | #:use-module (guix records) |
| 23 | #:use-module ((gnu packages base) |
| 24 | #:select (glibc-utf8-locales)) |
| 25 | #:use-module (gnu packages admin) |
| 26 | #:use-module (gnu packages web) |
| 27 | #:use-module (gnu services) |
| 28 | #:use-module (gnu services base) |
| 29 | #:use-module (gnu services admin) |
| 30 | #:use-module (gnu services shepherd) |
| 31 | #:use-module (gnu services getmail) |
| 32 | #:use-module (gnu system shadow) |
| 33 | #:export (<guix-data-service-configuration> |
| 34 | guix-data-service-configuration |
| 35 | guix-data-service-configuration? |
| 36 | guix-data-service-package |
| 37 | guix-data-service-user |
| 38 | guix-data-service-group |
| 39 | guix-data-service-port |
| 40 | guix-data-service-host |
| 41 | guix-data-service-getmail-idle-mailboxes |
| 42 | guix-data-service-commits-getmail-retriever-configuration |
| 43 | |
| 44 | guix-data-service-type)) |
| 45 | |
| 46 | ;;;; Commentary: |
| 47 | ;;; |
| 48 | ;;; This module implements a service that to run instances of the Guix Data |
| 49 | ;;; Service, which provides data about Guix over time. |
| 50 | ;;; |
| 51 | ;;;; Code: |
| 52 | |
| 53 | (define-record-type* <guix-data-service-configuration> |
| 54 | guix-data-service-configuration make-guix-data-service-configuration |
| 55 | guix-data-service-configuration? |
| 56 | (package guix-data-service-package |
| 57 | (default guix-data-service)) |
| 58 | (user guix-data-service-configuration-user |
| 59 | (default "guix-data-service")) |
| 60 | (group guix-data-service-configuration-group |
| 61 | (default "guix-data-service")) |
| 62 | (port guix-data-service-port |
| 63 | (default 8765)) |
| 64 | (host guix-data-service-host |
| 65 | (default "127.0.0.1")) |
| 66 | (getmail-idle-mailboxes |
| 67 | guix-data-service-getmail-idle-mailboxes |
| 68 | (default #f)) |
| 69 | (commits-getmail-retriever-configuration |
| 70 | guix-data-service-commits-getmail-retriever-configuration |
| 71 | (default #f)) |
| 72 | (extra-options guix-data-service-extra-options |
| 73 | (default '())) |
| 74 | (extra-process-jobs-options |
| 75 | guix-data-service-extra-process-jobs-options |
| 76 | (default '()))) |
| 77 | |
| 78 | (define (guix-data-service-profile-packages config) |
| 79 | "Return the guix-data-service package, this will populate the |
| 80 | ca-certificates.crt file in the system profile." |
| 81 | (list |
| 82 | (guix-data-service-package config))) |
| 83 | |
| 84 | (define (guix-data-service-shepherd-services config) |
| 85 | (match-record config <guix-data-service-configuration> |
| 86 | (package user group port host extra-options extra-process-jobs-options) |
| 87 | (list |
| 88 | (shepherd-service |
| 89 | (documentation "Guix Data Service web server") |
| 90 | (provision '(guix-data-service)) |
| 91 | (requirement '(postgres networking)) |
| 92 | (start #~(make-forkexec-constructor |
| 93 | (list #$(file-append package |
| 94 | "/bin/guix-data-service") |
| 95 | "--pid-file=/var/run/guix-data-service/pid" |
| 96 | #$(string-append "--port=" (number->string port)) |
| 97 | #$(string-append "--host=" host) |
| 98 | ;; Perform any database migrations when the |
| 99 | ;; service is started |
| 100 | "--update-database" |
| 101 | #$@extra-options) |
| 102 | |
| 103 | #:user #$user |
| 104 | #:group #$group |
| 105 | #:pid-file "/var/run/guix-data-service/pid" |
| 106 | ;; Allow time for migrations to run |
| 107 | #:pid-file-timeout 60 |
| 108 | #:environment-variables |
| 109 | `(,(string-append |
| 110 | "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale") |
| 111 | "LC_ALL=en_US.utf8") |
| 112 | #:log-file "/var/log/guix-data-service/web.log")) |
| 113 | (stop #~(make-kill-destructor))) |
| 114 | |
| 115 | (shepherd-service |
| 116 | (documentation "Guix Data Service process jobs") |
| 117 | (provision '(guix-data-service-process-jobs)) |
| 118 | (requirement '(postgres |
| 119 | networking |
| 120 | ;; Require guix-data-service, as that the database |
| 121 | ;; migrations are handled through this service |
| 122 | guix-data-service)) |
| 123 | (start #~(make-forkexec-constructor |
| 124 | (list |
| 125 | #$(file-append package |
| 126 | "/bin/guix-data-service-process-jobs") |
| 127 | #$@extra-process-jobs-options) |
| 128 | #:user #$user |
| 129 | #:group #$group |
| 130 | #:environment-variables |
| 131 | `("HOME=/var/lib/guix-data-service" |
| 132 | "GIT_SSL_CAINFO=/etc/ssl/certs/ca-certificates.crt" |
| 133 | ,(string-append |
| 134 | "GUIX_LOCPATH=" #$glibc-utf8-locales "/lib/locale") |
| 135 | "LC_ALL=en_US.utf8") |
| 136 | #:log-file "/var/log/guix-data-service/process-jobs.log")) |
| 137 | (stop #~(make-kill-destructor)))))) |
| 138 | |
| 139 | (define (guix-data-service-activation config) |
| 140 | #~(begin |
| 141 | (use-modules (guix build utils)) |
| 142 | |
| 143 | (define %user (getpw "guix-data-service")) |
| 144 | |
| 145 | (chmod "/var/lib/guix-data-service" #o755) |
| 146 | |
| 147 | (mkdir-p "/var/log/guix-data-service") |
| 148 | |
| 149 | ;; Allow writing the PID file |
| 150 | (mkdir-p "/var/run/guix-data-service") |
| 151 | (chown "/var/run/guix-data-service" |
| 152 | (passwd:uid %user) |
| 153 | (passwd:gid %user)))) |
| 154 | |
| 155 | (define (guix-data-service-account config) |
| 156 | (match-record config <guix-data-service-configuration> |
| 157 | (user group) |
| 158 | (list (user-group |
| 159 | (name group) |
| 160 | (system? #t)) |
| 161 | (user-account |
| 162 | (name user) |
| 163 | (group group) |
| 164 | (system? #t) |
| 165 | (comment "Guix Data Service user") |
| 166 | (home-directory "/var/lib/guix-data-service") |
| 167 | (shell (file-append shadow "/sbin/nologin")))))) |
| 168 | |
| 169 | (define (guix-data-service-getmail-configuration config) |
| 170 | (match config |
| 171 | (($ <guix-data-service-configuration> package user group |
| 172 | port host |
| 173 | #f #f) |
| 174 | '()) |
| 175 | (($ <guix-data-service-configuration> package user group |
| 176 | port host |
| 177 | getmail-idle-mailboxes |
| 178 | commits-getmail-retriever-configuration) |
| 179 | (list |
| 180 | (getmail-configuration |
| 181 | (name 'guix-data-service) |
| 182 | (user user) |
| 183 | (group group) |
| 184 | (directory "/var/lib/getmail/guix-data-service") |
| 185 | (rcfile |
| 186 | (getmail-configuration-file |
| 187 | (retriever commits-getmail-retriever-configuration) |
| 188 | (destination |
| 189 | (getmail-destination-configuration |
| 190 | (type "MDA_external") |
| 191 | (path (file-append |
| 192 | package |
| 193 | "/bin/guix-data-service-process-branch-updated-email")))) |
| 194 | (options |
| 195 | (getmail-options-configuration |
| 196 | (read-all #f) |
| 197 | (delivered-to #f) |
| 198 | (received #f))))) |
| 199 | (idle getmail-idle-mailboxes)))))) |
| 200 | |
| 201 | (define guix-data-service-type |
| 202 | (service-type |
| 203 | (name 'guix-data-service) |
| 204 | (extensions |
| 205 | (list |
| 206 | (service-extension profile-service-type |
| 207 | guix-data-service-profile-packages) |
| 208 | (service-extension shepherd-root-service-type |
| 209 | guix-data-service-shepherd-services) |
| 210 | (service-extension activation-service-type |
| 211 | guix-data-service-activation) |
| 212 | (service-extension account-service-type |
| 213 | guix-data-service-account) |
| 214 | (service-extension getmail-service-type |
| 215 | guix-data-service-getmail-configuration))) |
| 216 | (default-value |
| 217 | (guix-data-service-configuration)) |
| 218 | (description |
| 219 | "Run an instance of the Guix Data Service."))) |