summaryrefslogtreecommitdiff
path: root/guix/config.scm
blob: a525ab5c7e91c9c2148f2feacf614eb20f197e43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
;;; This file is part of GNU Taler.
;;; Copyright © 2018 GNUnet e.V.
;;;
;;; GNU Taler is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU Affero General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Taler is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public License
;;; along with GNU Taler.  If not, see <http://www.gnu.org/licenses/>.

;; Load modules relative to the script name.
(eval-when (load compile eval)
  (set! %load-path
        (cons ((@ (guix utils) current-source-directory)) %load-path)))

(use-modules
 (srfi srfi-1)
 (ice-9 match)
 (gnu)
 (guix)
 (guix utils)
 (guix gexp)
 (guix records)
 (guix modules)
 ((gnu packages admin) #:select (shadow shepherd))
 (taler-helpers))

(use-system-modules nss)
(use-service-modules networking ssh version-control cgit databases admin web shepherd)
(use-package-modules base bash shells web tls)

;;; Commentary:
;;;
;;; The GNU/Linux system that runs on gv.taler.net is defined here.


;;;
;;; Our definition of the fcgiwrap-service,
;;; this should eventually go upstream.
;;;


(define-record-type* <my-fcgiwrap-configuration> my-fcgiwrap-configuration
  make-my-fcgiwrap-configuration
  my-fcgiwrap-configuration?
  (package       my-fcgiwrap-configuration-package ;<package>
                 (default fcgiwrap))
  (socket        my-fcgiwrap-configuration-socket
                 (default "tcp:127.0.0.1:9000"))
  (user          my-fcgiwrap-configuration-user
                 (default "fcgiwrap"))
  (group         my-fcgiwrap-configuration-group
                 (default "fcgiwrap")))

(define (parse-fcgiwrap-socket s)
  (cond
   ((string-prefix? "unix:" s)
    (list 'unix (substring s 5)))
   ((string-prefix? "tcp:" s)
    (match (string-match "^tcp:([.0-9]+):([0-9]+)$" s)
      ((? regexp-match? m)
       (list
        'tcp
        (match:substring m 1)
        (string->number (match:substring m 2))))
      (_ (error "invalid tcp socket address"))))
   ((string-prefix? "tcp6:" s)
    (match (string-match "^tcp6:\\[(.*)\\]:([0-9]+)$" s)
      ((? regexp-match? m)
       (list
        'tcp6
        (match:substring m 1)
        (string->number (match:substring m 2))))
      (_ (error "invalid tcp6 socket address"))))
   (else (error "unrecognized socket protocol"))))


(define my-fcgiwrap-shepherd-service
  (match-lambda
    (($ <my-fcgiwrap-configuration> package socket user group)
     (let ((parsed-socket (parse-fcgiwrap-socket socket)))
       (list
        (shepherd-service
         (provision '(fcgiwrap))
         (documentation "Run the fcgiwrap daemon.")
         (requirement '(networking))
         (start (with-extensions
                 (list shepherd)
                 (with-imported-modules
                  `(((shepherd-with-sock) => ,(local-file "shepherd-with-sock.scm"))
                    ,@(source-module-closure '((shepherd service))))
                  #~(begin
                      (use-modules ((shepherd-with-sock) #:prefix my:))
                      (my:make-forkexec-constructor
                       '(#$(file-append package "/sbin/fcgiwrap"))
                       #:user #$user
                       #:group #$group
                       #:stdin-socket '#$parsed-socket)))))
                (stop #~(make-kill-destructor))))))))


(define my-fcgiwrap-accounts
  (match-lambda
    (($ <my-fcgiwrap-configuration> package socket user group)
     (filter identity
             (list
              (and (equal? group "fcgiwrap")
                   (user-group
                    (name "fcgiwrap")
                    (system? #t)))
              (and (equal? user "fcgiwrap")
                   (user-account
                    (name "fcgiwrap")
                    (group group)
                    (system? #t)
                    (comment "Fcgiwrap Daemon")
                    (home-directory "/var/empty")
                    (shell (file-append shadow "/sbin/nologin")))))))))


(define my-fcgiwrap-service-type
  (service-type (name 'fcgiwrap)
                (extensions
                 (list (service-extension shepherd-root-service-type
                                          my-fcgiwrap-shepherd-service)
		       (service-extension account-service-type
                                          my-fcgiwrap-accounts)))
                (default-value (fcgiwrap-configuration))))



;;; --- cron jobs start
(define %certbot-job
  ;; LE cert renewal 7d / 2
  #~(job (lambda (now)
           (next-day-from (next-hour-from now '(3))
                          '(2 5)))
         (string-append #$certbot "/bin/certbot renew")))
;;; --- cron jobs end

;;; --- nginx start
;; TODO: Translate nginx code to guix nginx-service without a file
;;       if possible wiht our config.
;; DOCUMENTATION: There are 2 ways to run nginx on GuixSD, we use
;; the way which allows us to work directly on nginx files instead
;; of generating them through Guix, for now. Every update of the
;; nginx config requires a reconfigure!
(define %nginx-deploy-hook
  (program-file
   "nginx-deploy-hook"
   #~(let ((pid (call-with-input-file "/var/run/nginx/pid" read)))
       (kill pid SIGHUP))))

(define %nginx-config
  (computed-file "nginx-config"
                 (with-imported-modules '((guix build utils))
                                        #~(begin
                                            (use-modules (guix build utils))
                                            (mkdir #$output)
                                            (chdir #$output)
                                            (symlink #$(local-file "etc/nginx/nginx.conf")
                                                     "nginx.conf")
                                            (mkdir "conf.d")
                                            (copy-file #$(local-file "etc/nginx/conf.d/favicon_robots")
                                                       "conf.d/favicon_robots")
                                            (copy-file #$(local-file "etc/nginx/conf.d/talerssl")
                                                       "conf.d/talerssl")
                                            (mkdir "sites-enabled")
                                            ;; (copy-file #$(local-file "etc/nginx/sites-enabled/git.site")
                                            ;;            "sites-enabled/git.site")
                                            (copy-file #$(local-file "etc/nginx/sites-enabled/git-ssl.site")
                                                       "sites-enabled/git-ssl.site")
                                            (copy-file #$(local-file "etc/nginx/sites-enabled/default.site")
                                                       "sites-enabled/default.site")))))

;; this includes defaults, so 'fastcgi' related files:
(define %nginx-mime-types
  (simple-service 'nginx-mime.types
                  etc-service-type
                  `(("nginx" ,(file-append nginx "/share/nginx/conf")))))

(define %nginx-cache-activation
  (simple-service 'nginx-/var/cache/nginx
                  activation-service-type
                  (with-imported-modules '((guix build utils))
                                         #~(begin
                                             (use-modules (guix build utils))
                                             (mkdir-p "/var/cache/nginx")))))
;;; --- nginx end

(operating-system
 (host-name "gv")
 (timezone "Europe/Paris")
 (locale "en_US.utf8")
 (initrd-modules (cons* "megaraid_sas" %base-initrd-modules))
 (kernel-arguments (list "console=ttyS0" "console=tty0"))

 (bootloader (bootloader-configuration
              (bootloader grub-bootloader)
              (target "/dev/sda")))

 (users
  (cons* (user-account
          (name "grothoff")
          (comment "Christian Grothoff")
          (group "users")
          (supplementary-groups '("wheel" "netdev" "kvm"))
          (home-directory "/home/grothoff"))
         (user-account
          (name "dold")
          (comment "Florian Dold")
          (group "users")
          (supplementary-groups '("wheel" "netdev" "kvm"))
          (home-directory "/home/dold"))
         (user-account
          (name "ng0")
          (comment "Nils Gillmann")
          (group "users")
          (supplementary-groups '("wheel" "netdev" "kvm"))
          (home-directory "/home/ng0"))
         (user-account
          (name "stanisci")
          (comment "Marcello Stanisci")
          (group "users")
          (supplementary-groups '("wheel" "netdev" "kvm"))
          (home-directory "/home/stanisci"))
         (user-account
          (name "git")
          (comment "gitolite")
          (group "git")
          (home-directory "/home/git"))
         %base-user-accounts))

 (groups (cons (user-group (name "git"))
               %base-groups))

 (file-systems
  (cons* (file-system
          (device (uuid "304189db-f9df-4222-810d-94c993598c3b"))
          (mount-point "/")
          (type "ext4"))
         %base-file-systems))

 (packages
  (append (map specification->package
               '("mg" "cryptsetup"
                 "screen" "tmux" "wget"
                 "vim" "openssh" "openssl"
                 "postgresql"
                 "nss-certs"
                 "curl" "gnutls-dane"
                 "gitolite"
                 "acme-client"
                 #| "buildbot" |#
                 "fcgiwrap"
                 "python-future"
                 "python" "python-jinja2"
                 "python-sphinx"))
          %base-packages))

 ;; TODO: cgit service?
 ;; TODO: gitolite service?

 (services
  (cons*
   (service static-networking-service-type
            (list
             (static-networking
              (interface "enp4s0f1")
              (ip "147.87.255.221")
              (netmask "255.255.255.240")
              (gateway "147.87.255.209")
              (name-servers '("8.8.8.8")))))

   (service special-files-service-type
            ;; Using 'canonical-package' as bash and coreutils
            ;; canonical packages are already a part of
            ;; '%base-packages'.
            `(("/bin/sh" ,(file-append (canonical-package bash)
                                       "/bin/sh"))
              ("/usr/bin/env" ,(file-append (canonical-package coreutils)
                                            "/bin/env"))
              ("/bin/ksh" ,(file-append (canonical-package loksh)
                                        "/bin/ksh"))))
   ;; TODO: Use deploy-hook
   ;; TODO: Add git.taler.net
   ;; (service certbot-service-type
   ;;          (certbot-configuration
   ;;           ;; FIXME: switch over to taler.net domain
   ;;           (email "cert-admin-taler@n0.is")
   ;;           (certificates
   ;;            (list
   ;;             (certificate-configuration
   ;;              (domains '("gv.taler.net")))))))
   ;; TODO: acme-client cronjob for:
   ;; taler.net www.taler.net api.taler.net lcov.taler.net
   ;; git.taler.net  gauger.taler.net buildbot.taler.net
   ;; test.taler.net playground.test.taler.net
   ;; auditor.test.taler.net auditor.demo.taler.net
   ;; demo.taler.net shop.test.taler.net
   ;; shop.demo.taler.net survey.test.taler.net
   ;; survey.demo.taler.net donations.demo.taler.net
   ;; backend.test.taler.net backend.demo.taler.net
   ;; bank.test.taler.net bank.demo.taler.net
   ;; www.git.taler.net exchange.demo.taler.net
   ;; exchange.test.taler.net env.taler.net
   ;; envs.taler.net blog.demo.taler.net
   ;; blog.test.taler.net donations.test.taler.net
   ;; docs.taler.net intranet.taler.net stage.taler.net
   (service openssh-service-type
            (openssh-configuration
             (x11-forwarding? #t)
             (port-number 22)
             (password-authentication? #f)
             (permit-root-login 'without-password)
             (authorized-keys
              `(("root" ,(concat-local-files
                          "root.pub"
                          '("keys/ssh/grothoff.pub"
                            "keys/ssh/ng0.pub"
                            "keys/ssh/dold.pub"
                            "keys/ssh/stanisci.pub")))
                ("stanisci" ,(local-file "keys/ssh/stanisci.pub"))
                ("dold" ,(local-file "keys/ssh/dold.pub"))
                ("ng0" ,(local-file "keys/ssh/ng0.pub"))
                ("grothoff" ,(local-file "keys/ssh/grothoff.pub"))))))

   ;; (service rottlog-service-type (rottlog-configuration))
   ;; (service mcron-service-type
   ;;          (mcron-configuration
   ;;           (jobs (list %gc-job %thing1))))
   (service postgresql-service-type)
   (git-daemon-service
    #:config (git-daemon-configuration
              (user-path "git")))
   (service openntpd-service-type
            (openntpd-configuration
             (listen-on '("127.0.0.1" "::1"))
             (sensor '("udcf0 correction 70000"))
             (constraint-from '("www.gnu.org"))
             (constraints-from '("https://www.google.com/"))
             (allow-large-adjustment? #t)))
   ;; FIXME: To be able to better test and replicate this,
   ;; we have to replicate what's done in nginx (copy into
   ;; store, use location in store).
   (service my-fcgiwrap-service-type
            (my-fcgiwrap-configuration
             (socket "unix:/var/run/fcgiwrap.socket")))
   ;(service cgit-service-type
   ;         (opaque-cgit-configuration
   ;          (cgitrc "/etc/deployment/guix/etc/cgitrc")))
   (service nginx-service-type
            (nginx-configuration
             (file (file-append %nginx-config
                                "/nginx.conf"))))
   %nginx-mime-types
   %nginx-cache-activation
   (modify-services %base-services
                    (guix-service-type
                     config =>
                     (guix-configuration
                      (inherit config)
                      (substitute-urls
                       (cons* "https://berlin.guixsd.org"
                              %default-substitute-urls)))))))

 ;; Allow resolution of '.local' host names with mDNS.
 (name-service-switch %mdns-host-lookup-nss))