Merge branch 'master' into staging
[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 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu services linux)
20 #:use-module (guix gexp)
21 #:use-module (guix records)
22 #:use-module (guix modules)
23 #:use-module (gnu services)
24 #:use-module (gnu services shepherd)
25 #:use-module (gnu packages linux)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-26)
28 #:use-module (ice-9 match)
29 #:export (earlyoom-configuration
30 earlyoom-configuration?
31 earlyoom-configuration-earlyoom
32 earlyoom-configuration-minimum-available-memory
33 earlyoom-configuration-minimum-free-swap
34 earlyoom-configuration-prefer-regexp
35 earlyoom-configuration-avoid-regexp
36 earlyoom-configuration-memory-report-interval
37 earlyoom-configuration-ignore-positive-oom-score-adj?
38 earlyoom-configuration-show-debug-messages?
39 earlyoom-configuration-send-notification-command
40 earlyoom-service-type))
41
42 \f
43 ;;;
44 ;;; Early OOM daemon.
45 ;;;
46
47 (define-record-type* <earlyoom-configuration>
48 earlyoom-configuration make-earlyoom-configuration
49 earlyoom-configuration?
50 (earlyoom earlyoom-configuration-earlyoom
51 (default earlyoom))
52 (minimum-available-memory earlyoom-configuration-minimum-available-memory
53 (default 10)) ; in percent
54 (minimum-free-swap earlyoom-configuration-minimum-free-swap
55 (default 10)) ; in percent
56 (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
57 (default #f))
58 (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
59 (default #f))
60 (memory-report-interval earlyoom-configuration-memory-report-interval
61 (default 0)) ; in seconds; 0 means disabled
62 (ignore-positive-oom-score-adj?
63 earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
64 (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
65 (default #f))
66 (show-debug-messages? earlyoom-configuration-show-debug-messages?
67 (default #f))
68 (send-notification-command
69 earlyoom-configuration-send-notification-command ; <string>
70 (default #f)))
71
72 (define (earlyoom-configuration->command-line-args config)
73 "Translate a <earlyoom-configuration> object to its command line arguments
74 representation."
75 (match config
76 (($ <earlyoom-configuration> earlyoom minimum-available-memory
77 minimum-free-swap prefer-regexp avoid-regexp
78 memory-report-interval
79 ignore-positive-oom-score-adj?
80 run-with-higher-priority? show-debug-messages?
81 send-notification-command)
82 `(,(file-append earlyoom "/bin/earlyoom")
83 ,@(if minimum-available-memory
84 (list "-m" (format #f "~s" minimum-available-memory))
85 '())
86 ,@(if minimum-free-swap
87 (list "-s" (format #f "~s" minimum-free-swap))
88 '())
89 ,@(if prefer-regexp
90 (list "--prefer" prefer-regexp)
91 '())
92 ,@(if avoid-regexp
93 (list "--avoid" avoid-regexp)
94 '())
95 "-r" ,(format #f "~s" memory-report-interval)
96 ,@(if ignore-positive-oom-score-adj?
97 (list "-i")
98 '())
99 ,@(if run-with-higher-priority?
100 (list "-p")
101 '())
102 ,@(if show-debug-messages?
103 (list "-d")
104 '())
105 ,@(if send-notification-command
106 (list "-N" send-notification-command)
107 '())))))
108
109 (define (earlyoom-shepherd-service config)
110 (shepherd-service
111 (documentation "Run the Early OOM daemon.")
112 (provision '(earlyoom))
113 (start #~(make-forkexec-constructor
114 '#$(earlyoom-configuration->command-line-args config)
115 #:log-file "/var/log/earlyoom.log"))
116 (stop #~(make-kill-destructor))))
117
118 (define earlyoom-service-type
119 (service-type
120 (name 'earlyoom)
121 (default-value (earlyoom-configuration))
122 (extensions
123 (list (service-extension shepherd-root-service-type
124 (compose list earlyoom-shepherd-service))))
125 (description "Run @command{earlyoom}, the Early OOM daemon.")))