sysctl.scm 3.0 KB

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