sysctl.scm 2.9 KB

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