Commit | Line | Data |
---|---|---|
8af4c335 DM |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2018 Danny Milosavljevic <dannym@scratchpost.org> | |
2ef4d273 | 3 | ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net> |
7c9be7b7 | 4 | ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com> |
8af4c335 DM |
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 docker) | |
22 | #:use-module (gnu services) | |
23 | #:use-module (gnu services configuration) | |
24 | #:use-module (gnu services base) | |
25 | #:use-module (gnu services dbus) | |
26 | #:use-module (gnu services shepherd) | |
27 | #:use-module (gnu system shadow) | |
28 | #:use-module (gnu packages docker) | |
08814aec | 29 | #:use-module (gnu packages linux) ;singularity |
8af4c335 DM |
30 | #:use-module (guix records) |
31 | #:use-module (guix gexp) | |
32 | #:use-module (guix packages) | |
33 | ||
34 | #:export (docker-configuration | |
08814aec LC |
35 | docker-service-type |
36 | singularity-service-type)) | |
8af4c335 | 37 | |
7a31d93a MC |
38 | ;;; We're not using serialize-configuration, but we must define this because |
39 | ;;; the define-configuration macro validates it exists. | |
40 | (define (serialize-boolean field-name val) | |
41 | "") | |
42 | ||
8af4c335 DM |
43 | (define-configuration docker-configuration |
44 | (docker | |
45 | (package docker) | |
46 | "Docker daemon package.") | |
47 | (containerd | |
48 | (package containerd) | |
7a31d93a MC |
49 | "containerd package.") |
50 | (proxy | |
51 | (package docker-libnetwork-cmd-proxy) | |
52 | "The proxy package to support inter-container and outside-container | |
53 | loop-back communications.") | |
54 | (enable-proxy? | |
55 | (boolean #t) | |
7c9be7b7 MC |
56 | "Enable or disable the user-land proxy (enabled by default).") |
57 | (debug? | |
58 | (boolean #f) | |
59 | "Enable or disable debug output.")) | |
8af4c335 DM |
60 | |
61 | (define %docker-accounts | |
62 | (list (user-group (name "docker") (system? #t)))) | |
63 | ||
64 | (define (%containerd-activation config) | |
65 | (let ((state-dir "/var/lib/containerd")) | |
66 | #~(begin | |
67 | (use-modules (guix build utils)) | |
68 | (mkdir-p #$state-dir)))) | |
69 | ||
70 | (define (%docker-activation config) | |
71 | (%containerd-activation config) | |
72 | (let ((state-dir "/var/lib/docker")) | |
73 | #~(begin | |
74 | (use-modules (guix build utils)) | |
75 | (mkdir-p #$state-dir)))) | |
76 | ||
77 | (define (containerd-shepherd-service config) | |
7c9be7b7 MC |
78 | (let* ((package (docker-configuration-containerd config)) |
79 | (debug? (docker-configuration-debug? config))) | |
8af4c335 DM |
80 | (shepherd-service |
81 | (documentation "containerd daemon.") | |
82 | (provision '(containerd)) | |
83 | (start #~(make-forkexec-constructor | |
7c9be7b7 MC |
84 | (list (string-append #$package "/bin/containerd") |
85 | #$@(if debug? | |
86 | '("--log-level=debug") | |
87 | '())) | |
f0bfd0fc | 88 | #:log-file "/var/log/containerd.log")) |
8af4c335 DM |
89 | (stop #~(make-kill-destructor))))) |
90 | ||
91 | (define (docker-shepherd-service config) | |
7a31d93a MC |
92 | (let* ((docker (docker-configuration-docker config)) |
93 | (enable-proxy? (docker-configuration-enable-proxy? config)) | |
7c9be7b7 MC |
94 | (proxy (docker-configuration-proxy config)) |
95 | (debug? (docker-configuration-debug? config))) | |
8af4c335 DM |
96 | (shepherd-service |
97 | (documentation "Docker daemon.") | |
98 | (provision '(dockerd)) | |
8b0c1744 | 99 | (requirement '(containerd |
1c84e68b DM |
100 | dbus-system |
101 | elogind | |
8b0c1744 DM |
102 | file-system-/sys/fs/cgroup/blkio |
103 | file-system-/sys/fs/cgroup/cpu | |
104 | file-system-/sys/fs/cgroup/cpuset | |
105 | file-system-/sys/fs/cgroup/devices | |
106 | file-system-/sys/fs/cgroup/memory | |
2ef4d273 | 107 | file-system-/sys/fs/cgroup/pids |
1c84e68b DM |
108 | networking |
109 | udev)) | |
8af4c335 DM |
110 | (start #~(make-forkexec-constructor |
111 | (list (string-append #$docker "/bin/dockerd") | |
7a31d93a | 112 | "-p" "/var/run/docker.pid" |
7c9be7b7 MC |
113 | #$@(if debug? |
114 | '("--debug" "--log-level=debug") | |
115 | '()) | |
7a31d93a MC |
116 | (if #$enable-proxy? "--userland-proxy" "") |
117 | "--userland-proxy-path" (string-append #$proxy | |
118 | "/bin/proxy")) | |
8af4c335 DM |
119 | #:pid-file "/var/run/docker.pid" |
120 | #:log-file "/var/log/docker.log")) | |
121 | (stop #~(make-kill-destructor))))) | |
122 | ||
123 | (define docker-service-type | |
124 | (service-type (name 'docker) | |
125 | (description "Provide capability to run Docker application | |
126 | bundles in Docker containers.") | |
127 | (extensions | |
128 | (list | |
129 | (service-extension activation-service-type | |
130 | %docker-activation) | |
131 | (service-extension shepherd-root-service-type | |
d3a0e74d DM |
132 | (lambda (config) |
133 | (list (containerd-shepherd-service config) | |
134 | (docker-shepherd-service config)))) | |
8af4c335 DM |
135 | (service-extension account-service-type |
136 | (const %docker-accounts)))) | |
137 | (default-value (docker-configuration)))) | |
08814aec LC |
138 | |
139 | \f | |
140 | ;;; | |
141 | ;;; Singularity. | |
142 | ;;; | |
143 | ||
144 | (define %singularity-activation | |
145 | (with-imported-modules '((guix build utils)) | |
146 | #~(begin | |
147 | (use-modules (guix build utils)) | |
148 | ||
149 | (define %mount-directory | |
150 | "/var/singularity/mnt/") | |
151 | ||
152 | ;; Create the directories that Singularity 2.6 expects to find. Make | |
153 | ;; them #o755 like the 'install-data-hook' rule in 'Makefile.am' of | |
154 | ;; Singularity 2.6.1. | |
155 | (for-each (lambda (directory) | |
156 | (let ((directory (string-append %mount-directory | |
157 | directory))) | |
158 | (mkdir-p directory) | |
159 | (chmod directory #o755))) | |
160 | '("container" "final" "overlay" "session")) | |
161 | (chmod %mount-directory #o755)))) | |
162 | ||
163 | (define (singularity-setuid-programs singularity) | |
164 | "Return the setuid-root programs that SINGULARITY needs." | |
165 | (define helpers | |
166 | ;; The helpers, under a meaningful name. | |
167 | (computed-file "singularity-setuid-helpers" | |
168 | #~(begin | |
169 | (mkdir #$output) | |
170 | (for-each (lambda (program) | |
171 | (symlink (string-append #$singularity | |
172 | "/libexec/singularity" | |
173 | "/bin/" | |
174 | program "-suid") | |
175 | (string-append #$output | |
176 | "/singularity-" | |
177 | program | |
178 | "-helper"))) | |
179 | '("action" "mount" "start"))))) | |
180 | ||
181 | (list (file-append helpers "/singularity-action-helper") | |
182 | (file-append helpers "/singularity-mount-helper") | |
183 | (file-append helpers "/singularity-start-helper"))) | |
184 | ||
185 | (define singularity-service-type | |
186 | (service-type (name 'singularity) | |
187 | (description | |
188 | "Install the Singularity application bundle tool.") | |
189 | (extensions | |
190 | (list (service-extension setuid-program-service-type | |
191 | singularity-setuid-programs) | |
192 | (service-extension activation-service-type | |
193 | (const %singularity-activation)))) | |
194 | (default-value singularity))) |