shepherd.scm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. shepherd-configuration-action))
  47. (define-record-type* <home-shepherd-configuration>
  48. home-shepherd-configuration make-home-shepherd-configuration
  49. home-shepherd-configuration?
  50. (shepherd home-shepherd-configuration-shepherd
  51. (default shepherd-0.10)) ; package
  52. (auto-start? home-shepherd-configuration-auto-start?
  53. (default #t))
  54. (daemonize? home-shepherd-configuration-daemonize?
  55. (default #t))
  56. (services home-shepherd-configuration-services
  57. (default '())))
  58. (define (home-shepherd-configuration-file config)
  59. "Return the shepherd configuration file for SERVICES. SHEPHERD is used
  60. as shepherd package."
  61. (let* ((daemonize? (home-shepherd-configuration-daemonize? config))
  62. (services (home-shepherd-configuration-services config))
  63. (_ (assert-valid-graph services))
  64. (files (map shepherd-service-file services))
  65. ;; TODO: Add compilation of services, it can improve start
  66. ;; time.
  67. ;; (scm->go (cute scm->go <> shepherd))
  68. )
  69. (define config
  70. #~(begin
  71. (use-modules (srfi srfi-34)
  72. (system repl error-handling))
  73. (apply
  74. register-services
  75. (map
  76. (lambda (file) (load file))
  77. '#$files))
  78. #$@(if daemonize?
  79. `((action 'root 'daemonize))
  80. '())
  81. (format #t "Starting services...~%")
  82. (let ((services-to-start
  83. '#$(append-map shepherd-service-provision
  84. (filter shepherd-service-auto-start?
  85. services))))
  86. (if (defined? 'start-in-the-background)
  87. (start-in-the-background services-to-start)
  88. (for-each start services-to-start))
  89. (redirect-port (open-input-file "/dev/null")
  90. (current-input-port)))))
  91. (scheme-file "shepherd.conf" config)))
  92. (define (launch-shepherd-gexp config)
  93. (let* ((shepherd (home-shepherd-configuration-shepherd config)))
  94. (if (home-shepherd-configuration-auto-start? config)
  95. (with-imported-modules '((guix build utils))
  96. #~(unless (file-exists?
  97. (string-append
  98. (or (getenv "XDG_RUNTIME_DIR")
  99. (format #f "/run/user/~a" (getuid)))
  100. "/shepherd/socket"))
  101. (let ((log-dir (or (getenv "XDG_LOG_HOME")
  102. (format #f "~a/.local/var/log"
  103. (getenv "HOME")))))
  104. ;; TODO: Remove it, 0.9.2 creates it automatically?
  105. ((@ (guix build utils) mkdir-p) log-dir)
  106. (system*
  107. #$(file-append shepherd "/bin/shepherd")
  108. "--logfile"
  109. (string-append log-dir "/shepherd.log")
  110. "--config"
  111. #$(home-shepherd-configuration-file config)))))
  112. #~"")))
  113. (define (reload-configuration-gexp config)
  114. (let* ((shepherd (home-shepherd-configuration-shepherd config)))
  115. #~(system*
  116. #$(file-append shepherd "/bin/herd")
  117. "load" "root"
  118. #$(home-shepherd-configuration-file config))))
  119. (define (ensure-shepherd-gexp config)
  120. #~(if (file-exists?
  121. (string-append
  122. (or (getenv "XDG_RUNTIME_DIR")
  123. (format #f "/run/user/~a" (getuid)))
  124. "/shepherd/socket"))
  125. #$(reload-configuration-gexp config)
  126. #$(launch-shepherd-gexp config)))
  127. (define (shepherd-xdg-configuration-files config)
  128. `(("shepherd/init.scm" ,(home-shepherd-configuration-file config))))
  129. (define-public home-shepherd-service-type
  130. (service-type (name 'home-shepherd)
  131. (extensions
  132. (list (service-extension
  133. home-run-on-first-login-service-type
  134. launch-shepherd-gexp)
  135. (service-extension
  136. home-xdg-configuration-files-service-type
  137. shepherd-xdg-configuration-files)
  138. (service-extension
  139. home-activation-service-type
  140. ensure-shepherd-gexp)
  141. (service-extension
  142. home-profile-service-type
  143. (lambda (config)
  144. `(,(home-shepherd-configuration-shepherd config))))))
  145. (compose concatenate)
  146. (extend
  147. (lambda (config extra-services)
  148. (home-shepherd-configuration
  149. (inherit config)
  150. (services
  151. (append (home-shepherd-configuration-services config)
  152. extra-services)))))
  153. (default-value (home-shepherd-configuration))
  154. (description "Configure and install userland Shepherd.")))