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> |
8422a67d | 5 | ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il> |
f0a09310 | 6 | ;;; Copyright © 2020 Jesse Dowell <jessedowell@gmail.com> |
8af4c335 DM |
7 | ;;; |
8 | ;;; This file is part of GNU Guix. | |
9 | ;;; | |
10 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
11 | ;;; under the terms of the GNU General Public License as published by | |
12 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
13 | ;;; your option) any later version. | |
14 | ;;; | |
15 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
16 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;;; GNU General Public License for more details. | |
19 | ;;; | |
20 | ;;; You should have received a copy of the GNU General Public License | |
21 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
22 | ||
23 | (define-module (gnu services docker) | |
24 | #:use-module (gnu services) | |
25 | #:use-module (gnu services configuration) | |
26 | #:use-module (gnu services base) | |
27 | #:use-module (gnu services dbus) | |
28 | #:use-module (gnu services shepherd) | |
29 | #:use-module (gnu system shadow) | |
30 | #:use-module (gnu packages docker) | |
08814aec | 31 | #:use-module (gnu packages linux) ;singularity |
8af4c335 DM |
32 | #:use-module (guix records) |
33 | #:use-module (guix gexp) | |
34 | #:use-module (guix packages) | |
35 | ||
36 | #:export (docker-configuration | |
08814aec LC |
37 | docker-service-type |
38 | singularity-service-type)) | |
8af4c335 | 39 | |
7a31d93a MC |
40 | ;;; We're not using serialize-configuration, but we must define this because |
41 | ;;; the define-configuration macro validates it exists. | |
42 | (define (serialize-boolean field-name val) | |
43 | "") | |
44 | ||
8af4c335 DM |
45 | (define-configuration docker-configuration |
46 | (docker | |
47 | (package docker) | |
48 | "Docker daemon package.") | |
f6dfe421 OP |
49 | (docker-cli |
50 | (package docker-cli) | |
51 | "Docker client package.") | |
8af4c335 DM |
52 | (containerd |
53 | (package containerd) | |
7a31d93a MC |
54 | "containerd package.") |
55 | (proxy | |
56 | (package docker-libnetwork-cmd-proxy) | |
57 | "The proxy package to support inter-container and outside-container | |
58 | loop-back communications.") | |
59 | (enable-proxy? | |
60 | (boolean #t) | |
7c9be7b7 MC |
61 | "Enable or disable the user-land proxy (enabled by default).") |
62 | (debug? | |
63 | (boolean #f) | |
2b68a964 AA |
64 | "Enable or disable debug output.") |
65 | (enable-iptables? | |
66 | (boolean #t) | |
67 | "Enable addition of iptables rules (enabled by default).")) | |
8af4c335 DM |
68 | |
69 | (define %docker-accounts | |
70 | (list (user-group (name "docker") (system? #t)))) | |
71 | ||
72 | (define (%containerd-activation config) | |
73 | (let ((state-dir "/var/lib/containerd")) | |
74 | #~(begin | |
75 | (use-modules (guix build utils)) | |
76 | (mkdir-p #$state-dir)))) | |
77 | ||
78 | (define (%docker-activation config) | |
79 | (%containerd-activation config) | |
80 | (let ((state-dir "/var/lib/docker")) | |
81 | #~(begin | |
82 | (use-modules (guix build utils)) | |
83 | (mkdir-p #$state-dir)))) | |
84 | ||
85 | (define (containerd-shepherd-service config) | |
7c9be7b7 MC |
86 | (let* ((package (docker-configuration-containerd config)) |
87 | (debug? (docker-configuration-debug? config))) | |
8af4c335 DM |
88 | (shepherd-service |
89 | (documentation "containerd daemon.") | |
90 | (provision '(containerd)) | |
91 | (start #~(make-forkexec-constructor | |
7c9be7b7 MC |
92 | (list (string-append #$package "/bin/containerd") |
93 | #$@(if debug? | |
94 | '("--log-level=debug") | |
95 | '())) | |
f0bfd0fc | 96 | #:log-file "/var/log/containerd.log")) |
8af4c335 DM |
97 | (stop #~(make-kill-destructor))))) |
98 | ||
99 | (define (docker-shepherd-service config) | |
7a31d93a MC |
100 | (let* ((docker (docker-configuration-docker config)) |
101 | (enable-proxy? (docker-configuration-enable-proxy? config)) | |
2b68a964 | 102 | (enable-iptables? (docker-configuration-enable-iptables? config)) |
7c9be7b7 MC |
103 | (proxy (docker-configuration-proxy config)) |
104 | (debug? (docker-configuration-debug? config))) | |
8af4c335 DM |
105 | (shepherd-service |
106 | (documentation "Docker daemon.") | |
107 | (provision '(dockerd)) | |
8b0c1744 | 108 | (requirement '(containerd |
1c84e68b DM |
109 | dbus-system |
110 | elogind | |
8b0c1744 DM |
111 | file-system-/sys/fs/cgroup/blkio |
112 | file-system-/sys/fs/cgroup/cpu | |
113 | file-system-/sys/fs/cgroup/cpuset | |
114 | file-system-/sys/fs/cgroup/devices | |
115 | file-system-/sys/fs/cgroup/memory | |
2ef4d273 | 116 | file-system-/sys/fs/cgroup/pids |
1c84e68b DM |
117 | networking |
118 | udev)) | |
8af4c335 DM |
119 | (start #~(make-forkexec-constructor |
120 | (list (string-append #$docker "/bin/dockerd") | |
7a31d93a | 121 | "-p" "/var/run/docker.pid" |
7c9be7b7 MC |
122 | #$@(if debug? |
123 | '("--debug" "--log-level=debug") | |
124 | '()) | |
cd4bb173 OP |
125 | #$@(if enable-proxy? |
126 | (list "--userland-proxy=true" | |
127 | #~(string-append | |
128 | "--userland-proxy-path=" #$proxy "/bin/proxy")) | |
129 | '("--userland-proxy=false")) | |
2b68a964 AA |
130 | (if #$enable-iptables? |
131 | "--iptables" | |
132 | "--iptables=false")) | |
8af4c335 DM |
133 | #:pid-file "/var/run/docker.pid" |
134 | #:log-file "/var/log/docker.log")) | |
135 | (stop #~(make-kill-destructor))))) | |
136 | ||
137 | (define docker-service-type | |
138 | (service-type (name 'docker) | |
139 | (description "Provide capability to run Docker application | |
140 | bundles in Docker containers.") | |
141 | (extensions | |
142 | (list | |
8422a67d EF |
143 | ;; Make sure the 'docker' command is available. |
144 | (service-extension profile-service-type | |
f6dfe421 | 145 | (compose list docker-configuration-docker-cli)) |
8af4c335 DM |
146 | (service-extension activation-service-type |
147 | %docker-activation) | |
148 | (service-extension shepherd-root-service-type | |
d3a0e74d DM |
149 | (lambda (config) |
150 | (list (containerd-shepherd-service config) | |
151 | (docker-shepherd-service config)))) | |
8af4c335 DM |
152 | (service-extension account-service-type |
153 | (const %docker-accounts)))) | |
154 | (default-value (docker-configuration)))) | |
08814aec LC |
155 | |
156 | \f | |
157 | ;;; | |
158 | ;;; Singularity. | |
159 | ;;; | |
160 | ||
161 | (define %singularity-activation | |
162 | (with-imported-modules '((guix build utils)) | |
163 | #~(begin | |
164 | (use-modules (guix build utils)) | |
165 | ||
166 | (define %mount-directory | |
167 | "/var/singularity/mnt/") | |
168 | ||
169 | ;; Create the directories that Singularity 2.6 expects to find. Make | |
170 | ;; them #o755 like the 'install-data-hook' rule in 'Makefile.am' of | |
171 | ;; Singularity 2.6.1. | |
172 | (for-each (lambda (directory) | |
173 | (let ((directory (string-append %mount-directory | |
174 | directory))) | |
175 | (mkdir-p directory) | |
176 | (chmod directory #o755))) | |
177 | '("container" "final" "overlay" "session")) | |
178 | (chmod %mount-directory #o755)))) | |
179 | ||
180 | (define (singularity-setuid-programs singularity) | |
181 | "Return the setuid-root programs that SINGULARITY needs." | |
182 | (define helpers | |
183 | ;; The helpers, under a meaningful name. | |
184 | (computed-file "singularity-setuid-helpers" | |
185 | #~(begin | |
186 | (mkdir #$output) | |
187 | (for-each (lambda (program) | |
188 | (symlink (string-append #$singularity | |
189 | "/libexec/singularity" | |
190 | "/bin/" | |
191 | program "-suid") | |
192 | (string-append #$output | |
193 | "/singularity-" | |
194 | program | |
195 | "-helper"))) | |
196 | '("action" "mount" "start"))))) | |
197 | ||
198 | (list (file-append helpers "/singularity-action-helper") | |
199 | (file-append helpers "/singularity-mount-helper") | |
200 | (file-append helpers "/singularity-start-helper"))) | |
201 | ||
202 | (define singularity-service-type | |
203 | (service-type (name 'singularity) | |
204 | (description | |
205 | "Install the Singularity application bundle tool.") | |
206 | (extensions | |
207 | (list (service-extension setuid-program-service-type | |
208 | singularity-setuid-programs) | |
209 | (service-extension activation-service-type | |
210 | (const %singularity-activation)))) | |
211 | (default-value singularity))) |