PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/gnu/home/services/shepherd.scm

https://gitlab.com/janneke/guix
Scheme | 157 lines | 122 code | 14 blank | 21 comment | 1 complexity | e4a5573d03681a0cf519e2cad9a91c5d MD5 | raw file
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
  3. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu home services shepherd)
  20. #:use-module (gnu home services)
  21. #:use-module (gnu packages admin)
  22. #:use-module (gnu services shepherd)
  23. #:use-module (guix sets)
  24. #:use-module (guix gexp)
  25. #:use-module (guix records)
  26. #:use-module (srfi srfi-1)
  27. #:export (home-shepherd-service-type
  28. home-shepherd-configuration
  29. home-shepherd-configuration?
  30. home-shepherd-configuration-shepherd
  31. home-shepherd-configuration-auto-start?
  32. home-shepherd-configuration-services)
  33. #:re-export (shepherd-service
  34. shepherd-service?
  35. shepherd-service-documentation
  36. shepherd-service-provision
  37. shepherd-service-canonical-name
  38. shepherd-service-requirement
  39. shepherd-service-one-shot?
  40. shepherd-service-respawn?
  41. shepherd-service-start
  42. shepherd-service-stop
  43. shepherd-service-auto-start?
  44. shepherd-service-modules
  45. shepherd-action))
  46. (define-record-type* <home-shepherd-configuration>
  47. home-shepherd-configuration make-home-shepherd-configuration
  48. home-shepherd-configuration?
  49. (shepherd home-shepherd-configuration-shepherd
  50. (default shepherd-0.9)) ; package
  51. (auto-start? home-shepherd-configuration-auto-start?
  52. (default #t))
  53. (services home-shepherd-configuration-services
  54. (default '())))
  55. (define (home-shepherd-configuration-file services shepherd)
  56. "Return the shepherd configuration file for SERVICES. SHEPHERD is used
  57. as shepherd package."
  58. (assert-valid-graph services)
  59. (let ((files (map shepherd-service-file services))
  60. ;; TODO: Add compilation of services, it can improve start
  61. ;; time.
  62. ;; (scm->go (cute scm->go <> shepherd))
  63. )
  64. (define config
  65. #~(begin
  66. (use-modules (srfi srfi-34)
  67. (system repl error-handling))
  68. (apply
  69. register-services
  70. (map
  71. (lambda (file) (load file))
  72. '#$files))
  73. (action 'root 'daemonize)
  74. (format #t "Starting services...~%")
  75. (let ((services-to-start
  76. '#$(append-map shepherd-service-provision
  77. (filter shepherd-service-auto-start?
  78. services))))
  79. (if (defined? 'start-in-the-background)
  80. (start-in-the-background services-to-start)
  81. (for-each start services-to-start))
  82. (redirect-port (open-input-file "/dev/null")
  83. (current-input-port)))))
  84. (scheme-file "shepherd.conf" config)))
  85. (define (launch-shepherd-gexp config)
  86. (let* ((shepherd (home-shepherd-configuration-shepherd config))
  87. (services (home-shepherd-configuration-services config)))
  88. (if (home-shepherd-configuration-auto-start? config)
  89. (with-imported-modules '((guix build utils))
  90. #~(unless (file-exists?
  91. (string-append
  92. (or (getenv "XDG_RUNTIME_DIR")
  93. (format #f "/run/user/~a" (getuid)))
  94. "/shepherd/socket"))
  95. (let ((log-dir (or (getenv "XDG_LOG_HOME")
  96. (format #f "~a/.local/var/log"
  97. (getenv "HOME")))))
  98. ((@ (guix build utils) mkdir-p) log-dir)
  99. (system*
  100. #$(file-append shepherd "/bin/shepherd")
  101. "--logfile"
  102. (string-append log-dir "/shepherd.log")
  103. "--config"
  104. #$(home-shepherd-configuration-file services shepherd)))))
  105. #~"")))
  106. (define (reload-configuration-gexp config)
  107. (let* ((shepherd (home-shepherd-configuration-shepherd config))
  108. (services (home-shepherd-configuration-services config)))
  109. #~(system*
  110. #$(file-append shepherd "/bin/herd")
  111. "load" "root"
  112. #$(home-shepherd-configuration-file services shepherd))))
  113. (define (ensure-shepherd-gexp config)
  114. #~(if (file-exists?
  115. (string-append
  116. (or (getenv "XDG_RUNTIME_DIR")
  117. (format #f "/run/user/~a" (getuid)))
  118. "/shepherd/socket"))
  119. #$(reload-configuration-gexp config)
  120. #$(launch-shepherd-gexp config)))
  121. (define-public home-shepherd-service-type
  122. (service-type (name 'home-shepherd)
  123. (extensions
  124. (list (service-extension
  125. home-run-on-first-login-service-type
  126. launch-shepherd-gexp)
  127. (service-extension
  128. home-activation-service-type
  129. ensure-shepherd-gexp)
  130. (service-extension
  131. home-profile-service-type
  132. (lambda (config)
  133. `(,(home-shepherd-configuration-shepherd config))))))
  134. (compose concatenate)
  135. (extend
  136. (lambda (config extra-services)
  137. (home-shepherd-configuration
  138. (inherit config)
  139. (services
  140. (append (home-shepherd-configuration-services config)
  141. extra-services)))))
  142. (default-value (home-shepherd-configuration))
  143. (description "Configure and install userland Shepherd.")))