aboutsummaryrefslogtreecommitdiff
path: root/guix-system.scm
blob: c154d01de7f9295c9329db8002cdfcfd694885f6 (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
(use-modules (gnu)
             (gn services databases)
             (gnu packages admin)
             (gnu services shepherd)
             (guix derivations)
             (guix monads)
             (guix profiles)
             (guix search-paths)
             (guix records)
             (guix store)
             (ice-9 match))

(define genenetwork3
  (load "guix.scm"))

(define (packages->profile packages)
  "Return profile with PACKAGES."
  (with-store store
    (run-with-store store
      (mlet* %store-monad ((prof-drv (profile-derivation
                                      (packages->manifest packages)))
                           (profile -> (derivation->output-path prof-drv)))
        (mbegin %store-monad
          (built-derivations (list prof-drv))
          (return profile))))))

(define (packages->environment-variables packages)
  "Return environment variables of a profile with PACKAGES. Return value is an
association list mapping the names of environment variables to their values."
  (map (match-lambda
         ((search-path . value)
          (cons (search-path-specification-variable search-path)
                value)))
       (profile-search-paths (packages->profile packages))))

(define (packages->profile-environment packages)
  "Return environment of a profile with PACKAGES. Return value is a
list of environment variables suitable as input to the environ
function."
  (map (match-lambda
         ((search-path . value)
          (string-append (search-path-specification-variable search-path)
                         "=" value)))
       (profile-search-paths (packages->profile packages))))

(define-record-type* <genenetwork3-configuration>
  genenetwork3-configuration make-genenetwork3-configuration
  genenetwork3-configuration?
  (package genenetwork3-configuration-package
           (default genenetwork3))
  (port genenetwork3-configuration-port
        (default 5000)))

(define %genenetwork3-accounts
  (list (user-group (name "genenetwork3")
                    (system? #t))
        (user-account
         (name "genenetwork3")
         (group "genenetwork3")
         (system? #t)
         (comment "GeneNetwork 3 user")
         (home-directory "/var/empty")
         (shell (file-append shadow "/sbin/nologin")))))

;; FIXME: Factorize this service into two. We should have a gunicorn
;; service that is extended by the genenetwork service. This way, the
;; app is better decoupled from the deployment.
(define genenetwork3-shepherd-service
  (match-lambda
    (($ <genenetwork3-configuration> package port)
     (shepherd-service
      (documentation "Run GeneNetwork 3.")
      (provision '(genenetwork3))
      (requirement '(networking virtuoso))
      (start #~(begin
                 ;; Reference the profile.
                 #$(packages->profile (list package))
                 ;; Start the gunicorn process.
                 (make-forkexec-constructor
                  (list #$(file-append gunicorn "/bin/gunicorn")
                        "-b" #$(string-append "127.0.0.1:" (number->string port))
                        "gn3.app:create_app()")
                  #:user "genenetwork3"
                  #:group "genenetwork3"
                  #:environment-variables
                  '#$(packages->profile-environment (list package)))))
      (stop #~(make-kill-destructor))))))

(define genenetwork3-service-type
  (service-type
   (name 'genenetwork3)
   (description "Run GeneNetwork 3.")
   (extensions
    (list (service-extension account-service-type
                             (const %genenetwork3-accounts))
          (service-extension shepherd-root-service-type
                             (compose list genenetwork3-shepherd-service))))
   (default-value (genenetwork3-configuration))))

(operating-system
  (host-name "genenetwork3")
  (timezone "UTC")
  (locale "en_US.utf8")
  (bootloader (bootloader-configuration
               (bootloader grub-bootloader)
               (targets (list "/dev/sdX"))))
  (file-systems (cons (file-system
                        (device "root")
                        (mount-point "/")
                        (type "ext4"))
                      %base-file-systems))
  (users %base-user-accounts)
  (packages %base-packages)
  (services (cons* (service virtuoso-service-type
                            (virtuoso-configuration
                             (http-server-port 8891)))
                   (service genenetwork3-service-type
                            (genenetwork3-configuration
                             (port 5000)))
                   %base-services)))