sysctl.scm 2.8 KB

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