services: guix-publish: Add zstd compression by default.
[jackhill/guix/guix.git] / gnu / services / sysctl.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Sou Bunnbu <iyzsong@member.fsf.org>
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 sysctl)
20 #:use-module (gnu services)
21 #:use-module (gnu services shepherd)
22 #:use-module (gnu packages linux)
23 #:use-module (guix gexp)
24 #:use-module (guix records)
25 #:use-module (srfi srfi-1)
26 #:use-module (ice-9 match)
27 #:export (sysctl-configuration
28 sysctl-configuration-sysctl
29 sysctl-configuration-settings
30 sysctl-service-type
31 %default-sysctl-settings))
32
33 \f
34 ;;;
35 ;;; System Control Service.
36 ;;;
37
38 (define %default-sysctl-settings
39 ;; Default kernel parameters enabled with sysctl.
40 '(("fs.protected_hardlinks" . "1")
41 ("fs.protected_symlinks" . "1")))
42
43 (define-record-type* <sysctl-configuration>
44 sysctl-configuration make-sysctl-configuration
45 sysctl-configuration?
46 (sysctl sysctl-configuration-sysctl ; path of the 'sysctl' command
47 (default (file-append procps "/sbin/sysctl")))
48 (settings sysctl-configuration-settings ; alist of string pairs
49 (default %default-sysctl-settings)))
50
51 (define (sysctl-configuration-settings->sysctl.conf settings)
52 "Return a file for @command{sysctl} to set kernel parameters as specified by
53 @var{settings}."
54 (apply mixed-text-file "sysctl.conf"
55 (append-map (match-lambda
56 ((key . value)
57 (list key "=" value "\n")))
58 settings)))
59
60 (define sysctl-shepherd-service
61 (match-lambda
62 (($ <sysctl-configuration> sysctl settings)
63 (let ((sysctl.conf
64 (sysctl-configuration-settings->sysctl.conf settings)))
65 (shepherd-service
66 (documentation "Configure kernel parameters at boot.")
67 (provision '(sysctl))
68 (start #~(lambda _
69 (zero? (system* #$sysctl "--load" #$sysctl.conf))))
70 (one-shot? #t))))))
71
72 (define sysctl-service-type
73 (service-type
74 (name 'sysctl)
75 (extensions
76 (list (service-extension shepherd-root-service-type
77 (compose list sysctl-shepherd-service))))
78 (compose concatenate)
79 (extend (lambda (config settings)
80 (sysctl-configuration
81 (inherit config)
82 (settings (append (sysctl-configuration-settings config)
83 settings)))))
84 (default-value (sysctl-configuration))))