services: bitlbee: Use 'make-inetd-constructor'.
[jackhill/guix/guix.git] / gnu / services / linux.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
3 ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
4 ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
5 ;;; Copyright © 2021 raid5atemyhomework <raid5atemyhomework@protonmail.com>
6 ;;; Copyright © 2021 B. Wilson <elaexuotee@wilsonb.com>
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 linux)
24 #:use-module (guix gexp)
25 #:use-module (guix records)
26 #:use-module (guix modules)
27 #:use-module (gnu services)
28 #:use-module (gnu services base)
29 #:use-module (gnu services shepherd)
30 #:use-module (gnu packages linux)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
35 #:use-module (ice-9 match)
36 #:export (earlyoom-configuration
37 earlyoom-configuration?
38 earlyoom-configuration-earlyoom
39 earlyoom-configuration-minimum-available-memory
40 earlyoom-configuration-minimum-free-swap
41 earlyoom-configuration-prefer-regexp
42 earlyoom-configuration-avoid-regexp
43 earlyoom-configuration-memory-report-interval
44 earlyoom-configuration-ignore-positive-oom-score-adj?
45 earlyoom-configuration-show-debug-messages?
46 earlyoom-configuration-send-notification-command
47 earlyoom-service-type
48
49 kernel-module-loader-service-type
50
51 rasdaemon-configuration
52 rasdaemon-configuration?
53 rasdaemon-configuration-record?
54 rasdaemon-service-type
55
56 zram-device-configuration
57 zram-device-configuration?
58 zram-device-configuration-size
59 zram-device-configuration-compression-algorithm
60 zram-device-configuration-memory-limit
61 zram-device-configuration-priority
62 zram-device-service-type))
63
64 \f
65 ;;;
66 ;;; Early OOM daemon.
67 ;;;
68
69 (define-record-type* <earlyoom-configuration>
70 earlyoom-configuration make-earlyoom-configuration
71 earlyoom-configuration?
72 (earlyoom earlyoom-configuration-earlyoom
73 (default earlyoom))
74 (minimum-available-memory earlyoom-configuration-minimum-available-memory
75 (default 10)) ; in percent
76 (minimum-free-swap earlyoom-configuration-minimum-free-swap
77 (default 10)) ; in percent
78 (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
79 (default #f))
80 (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
81 (default #f))
82 (memory-report-interval earlyoom-configuration-memory-report-interval
83 (default 0)) ; in seconds; 0 means disabled
84 (ignore-positive-oom-score-adj?
85 earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
86 (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
87 (default #f))
88 (show-debug-messages? earlyoom-configuration-show-debug-messages?
89 (default #f))
90 (send-notification-command
91 earlyoom-configuration-send-notification-command ; <string>
92 (default #f)))
93
94 (define (earlyoom-configuration->command-line-args config)
95 "Translate a <earlyoom-configuration> object to its command line arguments
96 representation."
97 (match config
98 (($ <earlyoom-configuration> earlyoom minimum-available-memory
99 minimum-free-swap prefer-regexp avoid-regexp
100 memory-report-interval
101 ignore-positive-oom-score-adj?
102 run-with-higher-priority? show-debug-messages?
103 send-notification-command)
104 `(,(file-append earlyoom "/bin/earlyoom")
105 ,@(if minimum-available-memory
106 (list "-m" (format #f "~s" minimum-available-memory))
107 '())
108 ,@(if minimum-free-swap
109 (list "-s" (format #f "~s" minimum-free-swap))
110 '())
111 ,@(if prefer-regexp
112 (list "--prefer" prefer-regexp)
113 '())
114 ,@(if avoid-regexp
115 (list "--avoid" avoid-regexp)
116 '())
117 "-r" ,(format #f "~s" memory-report-interval)
118 ,@(if ignore-positive-oom-score-adj?
119 (list "-i")
120 '())
121 ,@(if run-with-higher-priority?
122 (list "-p")
123 '())
124 ,@(if show-debug-messages?
125 (list "-d")
126 '())
127 ,@(if send-notification-command
128 (list "-N" send-notification-command)
129 '())))))
130
131 (define (earlyoom-shepherd-service config)
132 (shepherd-service
133 (documentation "Run the Early OOM daemon.")
134 (provision '(earlyoom))
135 (start #~(make-forkexec-constructor
136 '#$(earlyoom-configuration->command-line-args config)
137 #:log-file "/var/log/earlyoom.log"))
138 (stop #~(make-kill-destructor))))
139
140 (define earlyoom-service-type
141 (service-type
142 (name 'earlyoom)
143 (default-value (earlyoom-configuration))
144 (extensions
145 (list (service-extension shepherd-root-service-type
146 (compose list earlyoom-shepherd-service))))
147 (description "Run @command{earlyoom}, the Early OOM daemon.")))
148
149 \f
150 ;;;
151 ;;; Kernel module loader.
152 ;;;
153
154 (define kernel-module-loader-shepherd-service
155 (match-lambda
156 ((and (? list? kernel-modules) ((? string?) ...))
157 (shepherd-service
158 (documentation "Load kernel modules.")
159 (provision '(kernel-module-loader))
160 (requirement '())
161 (one-shot? #t)
162 (modules `((srfi srfi-1)
163 (srfi srfi-34)
164 (srfi srfi-35)
165 (rnrs io ports)
166 ,@%default-modules))
167 (start
168 #~(lambda _
169 (cond
170 ((null? '#$kernel-modules) #t)
171 ((file-exists? "/proc/sys/kernel/modprobe")
172 (let ((modprobe (call-with-input-file
173 "/proc/sys/kernel/modprobe" get-line)))
174 (guard (c ((message-condition? c)
175 (format (current-error-port) "~a~%"
176 (condition-message c))
177 #f))
178 (every (lambda (module)
179 (invoke/quiet modprobe "--" module))
180 '#$kernel-modules))))
181 (else
182 (format (current-error-port) "error: ~a~%"
183 "Kernel is missing loadable module support.")
184 #f))))))))
185
186 (define kernel-module-loader-service-type
187 (service-type
188 (name 'kernel-module-loader)
189 (description "Load kernel modules.")
190 (extensions
191 (list (service-extension shepherd-root-service-type
192 (compose list kernel-module-loader-shepherd-service))))
193 (compose concatenate)
194 (extend append)
195 (default-value '())))
196
197 \f
198 ;;;
199 ;;; Reliability, Availability, and Serviceability (RAS) daemon
200 ;;;
201
202 (define-record-type* <rasdaemon-configuration>
203 rasdaemon-configuration make-rasdaemon-configuration
204 rasdaemon-configuration?
205 (record? rasdaemon-configuration-record? (default #f)))
206
207 (define (rasdaemon-configuration->command-line-args config)
208 "Translate <rasdaemon-configuration> to its command line arguments
209 representation"
210 (let ((record? (rasdaemon-configuration-record? config)))
211 `(,(file-append rasdaemon "/sbin/rasdaemon")
212 "--foreground" ,@(if record? '("--record") '()))))
213
214 (define (rasdaemon-activation config)
215 (let ((record? (rasdaemon-configuration-record? config))
216 (rasdaemon-dir "/var/lib/rasdaemon"))
217 (with-imported-modules '((guix build utils))
218 #~(if #$record? (mkdir-p #$rasdaemon-dir)))))
219
220 (define (rasdaemon-shepherd-service config)
221 (shepherd-service
222 (documentation "Run rasdaemon")
223 (provision '(rasdaemon))
224 (requirement '(syslogd))
225 (start #~(make-forkexec-constructor
226 '#$(rasdaemon-configuration->command-line-args config)))
227 (stop #~(make-kill-destructor))))
228
229 (define rasdaemon-service-type
230 (service-type
231 (name 'rasdaemon)
232 (default-value (rasdaemon-configuration))
233 (extensions
234 (list (service-extension shepherd-root-service-type
235 (compose list rasdaemon-shepherd-service))
236 (service-extension activation-service-type rasdaemon-activation)))
237 (compose concatenate)
238 (description "Run @command{rasdaemon}, the RAS monitor")))
239
240 \f
241 ;;;
242 ;;; Kernel module loader.
243 ;;;
244
245 (define-record-type* <zram-device-configuration>
246 zram-device-configuration make-zram-device-configuration
247 zram-device-configuration?
248 (size zram-device-configuration-size
249 (default "1G")) ; string or integer
250 (compression-algorithm zram-device-configuration-compression-algorithm
251 (default 'lzo)) ; symbol
252 (memory-limit zram-device-configuration-memory-limit
253 (default 0)) ; string or integer
254 (priority zram-device-configuration-priority
255 (default -1))) ; integer
256
257 (define (zram-device-configuration->udev-string config)
258 "Translate a <zram-device-configuration> into a string which can be
259 placed in a udev rules file."
260 (match config
261 (($ <zram-device-configuration> size compression-algorithm memory-limit priority)
262 (string-append
263 "KERNEL==\"zram0\", "
264 "ATTR{comp_algorithm}=\"" (symbol->string compression-algorithm) "\" "
265 (if (not (or (equal? "0" size)
266 (equal? 0 size)))
267 (string-append "ATTR{disksize}=\"" (if (number? size)
268 (number->string size)
269 size)
270 "\" ")
271 "")
272 (if (not (or (equal? "0" memory-limit)
273 (equal? 0 memory-limit)))
274 (string-append "ATTR{mem_limit}=\"" (if (number? memory-limit)
275 (number->string memory-limit)
276 memory-limit)
277 "\" ")
278 "")
279 "RUN+=\"/run/current-system/profile/sbin/mkswap /dev/zram0\" "
280 "RUN+=\"/run/current-system/profile/sbin/swapon "
281 (if (not (equal? -1 priority))
282 (string-append "--priority " (number->string priority) " ")
283 "")
284 "/dev/zram0\"\n"))))
285
286 (define %zram-device-config
287 `("modprobe.d/zram.conf"
288 ,(plain-file "zram.conf"
289 "options zram num_devices=1")))
290
291 (define (zram-device-udev-rule config)
292 (file->udev-rule "99-zram.rules"
293 (plain-file "99-zram.rules"
294 (zram-device-configuration->udev-string config))))
295
296 (define zram-device-service-type
297 (service-type
298 (name 'zram)
299 (default-value (zram-device-configuration))
300 (extensions
301 (list (service-extension kernel-module-loader-service-type
302 (const (list "zram")))
303 (service-extension etc-service-type
304 (const (list %zram-device-config)))
305 (service-extension udev-service-type
306 (compose list zram-device-udev-rule))))
307 (description "Creates a zram swap device.")))