projects/guix·blob python-team · refs

202 lines · 7.9 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2021, 2023 Andrew Tropin <[email protected]>
3;;; Copyright © 2021 Xinglu Chen <[email protected]>
4;;; Copyright © 2024-2025 Ludovic Courtès <[email protected]>
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (gnu home services shepherd)
22 #:use-module (gnu home services)
23 #:use-module (gnu packages admin)
24 #:use-module (gnu services shepherd)
25 #:use-module (guix sets)
26 #:use-module (guix gexp)
27 #:use-module (guix records)
28 #:use-module (srfi srfi-1)
29 #:export (home-shepherd-service-type
30
31 home-shepherd-configuration
32 home-shepherd-configuration?
33 home-shepherd-configuration-shepherd
34 home-shepherd-configuration-auto-start?
35 home-shepherd-configuration-daemonize?
36 home-shepherd-configuration-silent?
37 home-shepherd-configuration-services
38
39 home-shepherd-transient-service-type
40 home-shepherd-timer-service-type)
41 #:re-export (shepherd-service
42 shepherd-service?
43 shepherd-service-documentation
44 shepherd-service-provision
45 shepherd-service-canonical-name
46 shepherd-service-requirement
47 shepherd-service-one-shot?
48 shepherd-service-respawn?
49 shepherd-service-start
50 shepherd-service-stop
51 shepherd-service-auto-start?
52 shepherd-service-modules
53
54 shepherd-action
55 shepherd-configuration-action
56 shepherd-trigger-action
57 shepherd-timer))
58
59(define-record-type* <home-shepherd-configuration>
60 home-shepherd-configuration make-home-shepherd-configuration
61 home-shepherd-configuration?
62 (shepherd home-shepherd-configuration-shepherd
63 (default shepherd-for-home)) ;package
64 (auto-start? home-shepherd-configuration-auto-start?
65 (default #t))
66 (daemonize? home-shepherd-configuration-daemonize?
67 (default #t))
68 (silent? home-shepherd-configuration-silent?
69 (default #t))
70 (services home-shepherd-configuration-services
71 (default '())))
72
73(define (home-shepherd-configuration-file config)
74 "Return the shepherd configuration file for SERVICES. SHEPHERD is used
75as shepherd package."
76 (let* ((daemonize? (home-shepherd-configuration-daemonize? config))
77 (services (home-shepherd-configuration-services config))
78 (_ (assert-valid-graph services))
79 (files (map shepherd-service-file services))
80 ;; TODO: Add compilation of services, it can improve start
81 ;; time.
82 ;; (scm->go (cute scm->go <> shepherd))
83 )
84 (define config
85 #~(begin
86 (use-modules (srfi srfi-34)
87 (system repl error-handling))
88
89 (define (make-user-module)
90 ;; Copied from (shepherd support), where it's private.
91 (let ((m (make-fresh-user-module)))
92 (module-use! m (resolve-interface '(shepherd service)))
93 m))
94
95 (register-services
96 (map (lambda (file)
97 (save-module-excursion
98 (lambda ()
99 (set-current-module (make-user-module))
100 (load file))))
101 '#$files))
102
103 #$(and daemonize?
104 #~(perform-service-action root-service 'daemonize))
105
106 (format #t "Starting services...~%")
107 (let ((services-to-start
108 '#$(append-map shepherd-service-provision
109 (filter shepherd-service-auto-start?
110 services))))
111 (start-in-the-background services-to-start)
112
113 (redirect-port (open-input-file "/dev/null")
114 (current-input-port)))))
115
116 (scheme-file "shepherd.conf" config)))
117
118(define (launch-shepherd-gexp config)
119 (let* ((shepherd (home-shepherd-configuration-shepherd config))
120 (silent? (home-shepherd-configuration-silent? config)))
121 (if (home-shepherd-configuration-auto-start? config)
122 (with-imported-modules '((guix build utils))
123 #~(unless (file-exists?
124 (string-append
125 (or (getenv "XDG_RUNTIME_DIR")
126 (format #f "/run/user/~a" (getuid)))
127 "/shepherd/socket"))
128 (system*
129 #$(file-append shepherd "/bin/shepherd")
130 #$@(if silent? '("--silent") '())
131 "--config"
132 #$(home-shepherd-configuration-file config))))
133 #~"")))
134
135(define (reload-configuration-gexp config)
136 (let* ((shepherd (home-shepherd-configuration-shepherd config)))
137 #~(system*
138 #$(file-append shepherd "/bin/herd")
139 "load" "root"
140 #$(home-shepherd-configuration-file config))))
141
142(define (ensure-shepherd-gexp config)
143 #~(if (file-exists?
144 (string-append
145 (or (getenv "XDG_RUNTIME_DIR")
146 (format #f "/run/user/~a" (getuid)))
147 "/shepherd/socket"))
148 #$(reload-configuration-gexp config)
149 ;; Don't attempt to start user shepherd if the system is running the
150 ;; activation script. /run/user/<uid> may not have been created
151 ;; yet. But do otherwise so if the runtime dir does not exist an error
152 ;; is logged.
153 (unless (getenv "GUIX_SYSTEM_IS_RUNNING_HOME_ACTIVATE")
154 #$(launch-shepherd-gexp config))))
155
156(define (shepherd-xdg-configuration-files config)
157 `(("shepherd/init.scm" ,(home-shepherd-configuration-file config))))
158
159(define home-shepherd-service-type
160 (service-type (name 'home-shepherd)
161 (extensions
162 (list (service-extension
163 home-run-on-first-login-service-type
164 launch-shepherd-gexp)
165 (service-extension
166 home-xdg-configuration-files-service-type
167 shepherd-xdg-configuration-files)
168 (service-extension
169 home-activation-service-type
170 ensure-shepherd-gexp)
171 (service-extension
172 home-profile-service-type
173 (lambda (config)
174 `(,(home-shepherd-configuration-shepherd config))))))
175 (compose concatenate)
176 (extend
177 (lambda (config extra-services)
178 (home-shepherd-configuration
179 (inherit config)
180 (services
181 (append (home-shepherd-configuration-services config)
182 extra-services)))))
183 (default-value (home-shepherd-configuration))
184 (description "Configure and install userland Shepherd.")))
185
186(define-service-type-mapping
187 shepherd-root-service-type => home-shepherd-service-type)
188
189
190;;;
191;;; Timer and transient service maker.
192;;;
193
194(define home-shepherd-timer-service-type
195 (service-type
196 (inherit (system->home-service-type shepherd-timer-service-type))
197 (default-value '()))) ;requirement
198
199(define home-shepherd-transient-service-type
200 (service-type
201 (inherit (system->home-service-type shepherd-transient-service-type))
202 (default-value '()))) ;requirement