gnu: csound: Update to 6.16.2.
[jackhill/guix/guix.git] / gnu / home / services / shepherd.scm
CommitLineData
6b5ff71b
AT
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
ba8ddb34 20(define-module (gnu home services shepherd)
0e8d2df0 21 #:use-module (gnu home services)
6b5ff71b
AT
22 #:use-module (gnu packages admin)
23 #:use-module (gnu services shepherd)
24 #:use-module (guix sets)
25 #:use-module (guix gexp)
26 #:use-module (guix records)
6b5ff71b 27 #:use-module (srfi srfi-1)
44ed9ebc 28 #:export (home-shepherd-service-type
e607c377
LC
29
30 home-shepherd-configuration
31 home-shepherd-configuration?
32 home-shepherd-configuration-shepherd
33 home-shepherd-configuration-auto-start?
34 home-shepherd-configuration-services)
6b5ff71b 35 #:re-export (shepherd-service
e607c377
LC
36 shepherd-service?
37 shepherd-service-documentation
38 shepherd-service-provision
39 shepherd-service-canonical-name
40 shepherd-service-requirement
41 shepherd-service-one-shot?
42 shepherd-service-respawn?
43 shepherd-service-start
44 shepherd-service-stop
45 shepherd-service-auto-start?
46 shepherd-service-modules
47
6b5ff71b
AT
48 shepherd-action))
49
50(define-record-type* <home-shepherd-configuration>
51 home-shepherd-configuration make-home-shepherd-configuration
52 home-shepherd-configuration?
53 (shepherd home-shepherd-configuration-shepherd
22ab9014 54 (default shepherd-0.9)) ; package
6b5ff71b
AT
55 (auto-start? home-shepherd-configuration-auto-start?
56 (default #t))
6cdde65b
AT
57 (daemonize? home-shepherd-configuration-daemonize?
58 (default #t))
6b5ff71b
AT
59 (services home-shepherd-configuration-services
60 (default '())))
61
6cdde65b 62(define (home-shepherd-configuration-file config)
6b5ff71b
AT
63 "Return the shepherd configuration file for SERVICES. SHEPHERD is used
64as shepherd package."
6cdde65b
AT
65 (let* ((daemonize? (home-shepherd-configuration-daemonize? config))
66 (services (home-shepherd-configuration-services config))
67 (_ (assert-valid-graph services))
68 (files (map shepherd-service-file services))
69 ;; TODO: Add compilation of services, it can improve start
70 ;; time.
71 ;; (scm->go (cute scm->go <> shepherd))
72 )
6b5ff71b
AT
73 (define config
74 #~(begin
75 (use-modules (srfi srfi-34)
76 (system repl error-handling))
77 (apply
78 register-services
79 (map
80 (lambda (file) (load file))
81 '#$files))
6cdde65b
AT
82
83 #$@(if daemonize?
84 `((action 'root 'daemonize))
85 '())
86
6b5ff71b 87 (format #t "Starting services...~%")
22ab9014
LC
88 (let ((services-to-start
89 '#$(append-map shepherd-service-provision
90 (filter shepherd-service-auto-start?
91 services))))
92 (if (defined? 'start-in-the-background)
93 (start-in-the-background services-to-start)
bc867b2a
LC
94 (for-each start services-to-start))
95
dd2ab92d
LC
96 (redirect-port (open-input-file "/dev/null")
97 (current-input-port)))))
6b5ff71b
AT
98
99 (scheme-file "shepherd.conf" config)))
100
101(define (launch-shepherd-gexp config)
6cdde65b 102 (let* ((shepherd (home-shepherd-configuration-shepherd config)))
6b5ff71b
AT
103 (if (home-shepherd-configuration-auto-start? config)
104 (with-imported-modules '((guix build utils))
8805c7ea
AT
105 #~(unless (file-exists?
106 (string-append
107 (or (getenv "XDG_RUNTIME_DIR")
108 (format #f "/run/user/~a" (getuid)))
109 "/shepherd/socket"))
110 (let ((log-dir (or (getenv "XDG_LOG_HOME")
111 (format #f "~a/.local/var/log"
112 (getenv "HOME")))))
6cdde65b 113 ;; TODO: Remove it, 0.9.2 creates it automatically?
8805c7ea
AT
114 ((@ (guix build utils) mkdir-p) log-dir)
115 (system*
116 #$(file-append shepherd "/bin/shepherd")
117 "--logfile"
118 (string-append log-dir "/shepherd.log")
119 "--config"
6cdde65b 120 #$(home-shepherd-configuration-file config)))))
6b5ff71b
AT
121 #~"")))
122
123(define (reload-configuration-gexp config)
6cdde65b 124 (let* ((shepherd (home-shepherd-configuration-shepherd config)))
6b5ff71b
AT
125 #~(system*
126 #$(file-append shepherd "/bin/herd")
127 "load" "root"
6cdde65b 128 #$(home-shepherd-configuration-file config))))
6b5ff71b
AT
129
130(define (ensure-shepherd-gexp config)
131 #~(if (file-exists?
132 (string-append
133 (or (getenv "XDG_RUNTIME_DIR")
134 (format #f "/run/user/~a" (getuid)))
135 "/shepherd/socket"))
136 #$(reload-configuration-gexp config)
137 #$(launch-shepherd-gexp config)))
138
70d58d15 139(define (shepherd-xdg-configuration-files config)
6cdde65b 140 `(("shepherd/init.scm" ,(home-shepherd-configuration-file config))))
70d58d15 141
6b5ff71b
AT
142(define-public home-shepherd-service-type
143 (service-type (name 'home-shepherd)
144 (extensions
145 (list (service-extension
146 home-run-on-first-login-service-type
147 launch-shepherd-gexp)
70d58d15
AT
148 (service-extension
149 home-xdg-configuration-files-service-type
150 shepherd-xdg-configuration-files)
6b5ff71b
AT
151 (service-extension
152 home-activation-service-type
153 ensure-shepherd-gexp)
154 (service-extension
155 home-profile-service-type
156 (lambda (config)
157 `(,(home-shepherd-configuration-shepherd config))))))
158 (compose concatenate)
159 (extend
160 (lambda (config extra-services)
161 (home-shepherd-configuration
162 (inherit config)
163 (services
164 (append (home-shepherd-configuration-services config)
165 extra-services)))))
166 (default-value (home-shepherd-configuration))
167 (description "Configure and install userland Shepherd.")))
168
169