projects/guix·blob java-team · refs

811 lines · 30.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 © 2022-2023, 2025 Ludovic Courtès <[email protected]>
5;;; Copyright © 2023 Carlo Zancanaro <[email protected]>
6;;; Copyright © 2025 Giacomo Leidi <[email protected]>
7;;;
8;;; This file is part of GNU Guix.
9;;;
10;;; GNU Guix is free software; you can redistribute it and/or modify it
11;;; under the terms of the GNU General Public License as published by
12;;; the Free Software Foundation; either version 3 of the License, or (at
13;;; your option) any later version.
14;;;
15;;; GNU Guix is distributed in the hope that it will be useful, but
16;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;;; GNU General Public License for more details.
19;;;
20;;; You should have received a copy of the GNU General Public License
21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
23(define-module (gnu home services)
24 #:use-module (gnu services)
25 #:use-module ((gnu packages package-management) #:select (guix))
26 #:use-module ((gnu packages base) #:select (coreutils))
27 #:use-module (guix channels)
28 #:use-module (guix monads)
29 #:use-module (guix store)
30 #:use-module (guix gexp)
31 #:use-module (guix profiles)
32 #:use-module (guix sets)
33 #:use-module (guix ui)
34 #:use-module (guix discovery)
35 #:use-module (guix diagnostics)
36 #:use-module (guix i18n)
37 #:use-module (guix modules)
38 #:use-module (guix memoization)
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-9)
41 #:use-module (srfi srfi-34)
42 #:use-module (ice-9 match)
43 #:use-module (ice-9 vlist)
44
45 #:export (home-service-type
46 home-profile-service-type
47 home-environment-variables-service-type
48 home-files-service-type
49 home-xdg-configuration-files-service-type
50 home-xdg-data-files-service-type
51 home-run-on-first-login-service-type
52 home-activation-service-type
53 home-run-on-change-service-type
54 home-provenance-service-type
55 home-gc-root-service-type
56
57 literal-string
58 literal-string?
59 literal-string-value
60
61 with-shell-quotation-bindings
62 environment-variable-shell-definitions
63 shell-variable-definitions
64 home-files-directory
65 xdg-configuration-files-directory
66 xdg-data-files-directory
67
68 fold-home-service-types
69 lookup-home-service-types
70 home-provenance
71
72 define-service-type-mapping
73 system->home-service-type
74
75 %initialize-gettext)
76
77 #:re-export (service
78 service-type
79 service-extension
80 simple-service
81 for-home
82 for-home?))
83
84;;; Comment:
85;;;
86;;; This module is similar to (gnu system services) module, but
87;;; provides Home Services, which are supposed to be used for building
88;;; home-environment.
89;;;
90;;; Home Services use the same extension as System Services. Consult
91;;; (gnu system services) module or manual for more information.
92;;;
93;;; home-service-type is a root of home services DAG.
94;;;
95;;; home-profile-service-type is almost the same as profile-service-type, at least
96;;; for now.
97;;;
98;;; home-environment-variables-service-type generates a @file{setup-environment}
99;;; shell script, which is expected to be sourced by login shell or other program,
100;;; which starts early and spawns all other processes. Home services for shells
101;;; automatically add code for sourcing this file, if person do not use those home
102;;; services they have to source this script manually in their's shell *profile
103;;; file (details described in the manual).
104;;;
105;;; home-files-service-type is similar to etc-service-type, but doesn't extend
106;;; home-activation, because deploy mechanism for config files is pluggable
107;;; and can be different for different home environments: The default one is
108;;; called symlink-manager, which creates links for various dotfiles and xdg
109;;; configuration files to store, but is possible to implement alternative
110;;; approaches like read-only home from Julien's guix-home-manager.
111;;;
112;;; home-run-on-first-login-service-type provides an @file{on-first-login} guile
113;;; script, which runs provided gexps once, when user makes first login. It can
114;;; be used to start user's Shepherd and maybe some other process. It relies on
115;;; assumption that /run/user/$UID will be created on login by some login
116;;; manager (elogind for example).
117;;;
118;;; home-activation-service-type provides an @file{activate} guile script, which
119;;; do three main things:
120;;;
121;;; - Sets environment variables to the values declared in
122;;; @file{setup-environment} shell script. It's necessary, because user can set
123;;; for example XDG_CONFIG_HOME and it should be respected by activation gexp of
124;;; symlink-manager.
125;;;
126;;; - Sets GUIX_NEW_HOME and possibly GUIX_OLD_HOME vars to paths in the store.
127;;; Later those variables can be used by activation gexps, for example by
128;;; symlink-manager or run-on-change services.
129;;;
130;;; - Run all activation gexps provided by other home services.
131;;;
132;;; home-run-on-change-service-type allows to trigger actions during
133;;; activation if file or directory specified by pattern is changed.
134;;;
135;;; Code:
136
137
138(define (home-derivation entries mextensions)
139 "Return as a monadic value the derivation of the 'home'
140directory containing the given entries."
141 (mlet %store-monad ((extensions (mapm/accumulate-builds identity
142 mextensions)))
143 (lower-object
144 (file-union "home" (append entries (concatenate extensions))))))
145
146(define home-service-type
147 ;; This is the ultimate service type, the root of the home service
148 ;; DAG. The service of this type is extended by monadic name/item
149 ;; pairs. These items end up in the "home-environment directory" as
150 ;; returned by 'home-environment-derivation'.
151 (service-type (name 'home)
152 (extensions '())
153 (compose identity)
154 (extend home-derivation)
155 (default-value '())
156 (description
157 "Build the home environment top-level directory,
158which in turn refers to everything the home environment needs: its
159packages, configuration files, activation script, and so on.")))
160
161(define (packages->profile-entry packages)
162 "Return a system entry for the profile containing PACKAGES."
163 ;; XXX: 'mlet' is needed here for one reason: to get the proper
164 ;; '%current-target' and '%current-target-system' bindings when
165 ;; 'packages->manifest' is called, and thus when the 'package-inputs'
166 ;; etc. procedures are called on PACKAGES. That way, conditionals in those
167 ;; inputs see the "correct" value of these two parameters. See
168 ;; <https://issues.guix.gnu.org/44952>.
169 (mlet %store-monad ((_ (current-target-system)))
170 (return `(("profile" ,(profile
171 (content (packages->manifest
172 (map identity
173 ;;(options->transformation transformations)
174 (delete-duplicates packages eq?))))))))))
175
176;; MAYBE: Add a list of transformations for packages. It's better to
177;; place it in home-profile-service-type to affect all profile
178;; packages and prevent conflicts, when other packages relies on
179;; non-transformed version of package.
180(define home-profile-service-type
181 (service-type (name 'home-profile)
182 (extensions
183 (list (service-extension home-service-type
184 packages->profile-entry)))
185 (compose concatenate)
186 (extend append)
187 (description
188 "This is the @dfn{home profile} and can be found in
189@file{~/.guix-home/profile}. It contains packages and
190configuration files that the user has declared in their
191@code{home-environment} record.")))
192
193;; Representation of a literal string.
194(define-record-type <literal-string>
195 (literal-string str)
196 literal-string?
197 (str literal-string-value))
198
199(define (with-shell-quotation-bindings exp)
200 "Insert EXP, a gexp, in a lexical environment providing the
201'shell-single-quote' and 'shell-double-quote' bindings."
202#~(let* ((quote-string
203 (lambda (value quoted-chars)
204 (list->string (string-fold-right
205 (lambda (chr lst)
206 (if (memq chr quoted-chars)
207 (append (list #\\ chr) lst)
208 (cons chr lst)))
209 '()
210 value))))
211 (shell-double-quote
212 (lambda (value)
213 ;; Double-quote VALUE, leaving dollar sign as is.
214 (string-append "\"" (quote-string value '(#\" #\\))
215 "\"")))
216 (shell-single-quote
217 (lambda (value)
218 ;; Single-quote VALUE to enter a literal string.
219 (string-append "'" (quote-string value '(#\'))
220 "'"))))
221 #$exp))
222
223(define (environment-variable-shell-definitions variables)
224 "Return a gexp that evaluates to a list of POSIX shell statements defining
225VARIABLES, a list of environment variable name/value pairs. The returned code
226ensures variable values are properly quoted."
227 (with-shell-quotation-bindings
228 #~(string-append
229 #$@(map (match-lambda
230 ((key . #f)
231 "")
232 ((key . #t)
233 #~(string-append "export " #$key "\n"))
234 ((key . (or (? string? value)
235 (? file-like? value)
236 (? gexp? value)))
237 #~(string-append "export " #$key "="
238 (shell-double-quote #$value)
239 "\n"))
240 ((key . (? literal-string? value))
241 #~(string-append "export " #$key "="
242 (shell-single-quote
243 #$(literal-string-value value))
244 "\n")))
245 variables))))
246
247(define (shell-variable-definitions variables)
248 "Return a gexp that evaluates to a list of POSIX shell statements defining
249VARIABLES, a list of variable name/value pairs, as shell variables (not
250environment variables). The returned code ensures variable values are
251properly quoted."
252 (with-shell-quotation-bindings
253 #~(string-append
254 #$@(map (match-lambda
255 ((key . (or (? string? value)
256 (? file-like? value)
257 (? gexp? value)))
258 #~(string-append #$key "="
259 (shell-double-quote #$value)
260 "\n"))
261 ((key . (? literal-string? value))
262 #~(string-append #$key "="
263 (shell-single-quote
264 #$(literal-string-value value))
265 "\n")))
266 variables))))
267
268(define (environment-variables->setup-environment-script vars)
269 "Return a file that can be sourced by a POSIX compliant shell which
270initializes the environment. The file will source the home
271environment profile, set some default environment variables, and set
272environment variables provided in @code{vars}. @code{vars} is a list
273of pairs (@code{(key . value)}), @code{key} is a string and
274@code{value} is a string or gexp.
275
276If value is @code{#f} variable will be omitted.
277If value is @code{#t} variable will be just exported.
278For any other, value variable will be set to the @code{value} and
279exported."
280 (define (warn-about-duplicate-definitions)
281 (fold
282 (lambda (x acc)
283 (when (equal? (car x) (car acc))
284 (warning
285 (G_ "duplicate definition for `~a' environment variable ~%") (car x)))
286 x)
287 (cons "" "")
288 (sort vars (lambda (a b)
289 (string<? (car a) (car b))))))
290
291 (warn-about-duplicate-definitions)
292 (with-monad
293 %store-monad
294 (return
295 `(("setup-environment"
296 ;; TODO: It's necessary to source ~/.guix-profile too
297 ;; on foreign distros
298 ,(computed-file "setup-environment"
299 #~(call-with-output-file #$output
300 (lambda (port)
301 (set-port-encoding! port "UTF-8")
302 (display "\
303# NOTE: Set HOME_ENVIRONMENT before sourcing (home-shell-profile-service-type ensures
304# ~/.profile does)
305GUIX_PROFILE=\"$HOME_ENVIRONMENT/profile\"
306PROFILE_FILE=\"$GUIX_PROFILE/etc/profile\"
307[ -f $PROFILE_FILE ] && . $PROFILE_FILE
308
309case $GUIX_LOCPATH in
310 *$GUIX_PROFILE/lib/locale*) ;;
311 *) export GUIX_LOCPATH=$GUIX_PROFILE/lib/locale:$GUIX_LOCPATH ;;
312esac
313case $XDG_DATA_DIRS in
314 *$GUIX_PROFILE/share*) ;;
315 *) export XDG_DATA_DIRS=$GUIX_PROFILE/share:$XDG_DATA_DIRS ;;
316esac
317case $MANPATH in
318 *$GUIX_PROFILE/share/man*) ;;
319 *) export MANPATH=$GUIX_PROFILE/share/man:$MANPATH
320esac
321case $INFOPATH in
322 *$GUIX_PROFILE/share/info*) ;;
323 *) export INFOPATH=$GUIX_PROFILE/share/info:$INFOPATH ;;
324esac
325case $XDG_CONFIG_DIRS in
326 *$GUIX_PROFILE/etc/xdg*) ;;
327 *) export XDG_CONFIG_DIRS=$GUIX_PROFILE/etc/xdg:$XDG_CONFIG_DIRS ;;
328esac
329case $XCURSOR_PATH in
330 *$GUIX_PROFILE/share/icons*) ;;
331 *) export XCURSOR_PATH=$GUIX_PROFILE/share/icons:$XCURSOR_PATH ;;
332esac
333case $GUIX_EXTENSIONS_PATH in
334 *$GUIX_PROFILE/share/guix/extensions*) ;;
335 *) export GUIX_EXTENSIONS_PATH=$GUIX_PROFILE/share/guix/extensions:$GUIX_EXTENSIONS_PATH ;;
336esac
337
338# Keep the shell environment clean.
339unset GUIX_PROFILE PROFILE_FILE
340
341" port)
342 (display
343 #$(environment-variable-shell-definitions vars)
344 port)))))))))
345
346(define home-environment-variables-service-type
347 (service-type (name 'home-environment-variables)
348 (extensions
349 (list (service-extension
350 home-service-type
351 environment-variables->setup-environment-script)))
352 (compose concatenate)
353 (extend append)
354 (default-value '())
355 (description "Set the environment variables.")))
356
357(define (files->files-directory files)
358 "Return a @code{files} directory that contains FILES."
359 (define (assert-no-duplicates files)
360 (let loop ((files files)
361 (seen (set)))
362 (match files
363 (() #t)
364 (((file _) rest ...)
365 (when (set-contains? seen file)
366 (raise (formatted-message (G_ "duplicate '~a' entry for files/")
367 file)))
368 (loop rest (set-insert file seen))))))
369
370 ;; Detect duplicates early instead of letting them through, eventually
371 ;; leading to a build failure of "files.drv".
372 (assert-no-duplicates files)
373
374 ;; Allow symlinks to locations outside the store.
375 (file-union "files" files #:dangling-symlinks? #t))
376
377;; Used by symlink-manager
378(define home-files-directory "files")
379
380(define (files-entry files)
381 "Return an entry for the @file{~/.guix-home/files}
382directory containing FILES."
383 (with-monad %store-monad
384 (return `((,home-files-directory ,(files->files-directory files))))))
385
386(define home-files-service-type
387 (service-type (name 'home-files)
388 (extensions
389 (list (service-extension home-service-type
390 files-entry)))
391 (compose concatenate)
392 (extend append)
393 (default-value '())
394 (description "Files that will be put in
395@file{~/.guix-home/files}, and further processed during activation.")))
396
397(define xdg-configuration-files-directory ".config")
398
399(define (xdg-configuration-files files)
400 "Add .config/ prefix to each file-path in FILES."
401 (map (match-lambda
402 ((file-path . rest)
403 (cons (string-append xdg-configuration-files-directory "/" file-path)
404 rest)))
405 files))
406
407(define home-xdg-configuration-files-service-type
408 (service-type (name 'home-xdg-configuration)
409 (extensions
410 (list (service-extension home-files-service-type
411 xdg-configuration-files)))
412 (compose concatenate)
413 (extend append)
414 (default-value '())
415 (description "Files that will be put in
416@file{~/.guix-home/files/.config}, and further processed during activation.")))
417
418(define xdg-data-files-directory ".local/share")
419
420(define (xdg-data-files files)
421 "Add .local/share prefix to each file-path in FILES."
422 (map (match-lambda
423 ((file-path . rest)
424 (cons (string-append xdg-data-files-directory "/" file-path)
425 rest)))
426 files))
427
428(define home-xdg-data-files-service-type
429 (service-type (name 'home-xdg-data)
430 (extensions
431 (list (service-extension home-files-service-type
432 xdg-data-files)))
433 (compose concatenate)
434 (extend append)
435 (default-value '())
436 (description "Files that will be put in
437@file{~/.guix-home/files/.local/share}, and further processed during
438activation.")))
439
440
441(define %initialize-gettext
442 #~(begin
443 (bindtextdomain %gettext-domain
444 (string-append #$guix "/share/locale"))
445 (textdomain %gettext-domain)))
446
447(define (compute-on-first-login-script _ gexps)
448 (program-file
449 "on-first-login"
450 (with-imported-modules (source-module-closure '((guix i18n)
451 (guix diagnostics)))
452 #~(begin
453 (use-modules (guix i18n)
454 (guix diagnostics))
455
456 (define (claim-first-run file)
457 (catch #t
458 (lambda ()
459 ;; This incantation raises an error if FILE already exists, and
460 ;; creates it otherwise.
461 (close-fdes
462 (open-fdes file (logior O_CREAT O_EXCL O_CLOEXEC)))
463 #t)
464 (const #f)))
465
466 #$%initialize-gettext
467
468 (let* ((xdg-runtime-dir (or (getenv "XDG_RUNTIME_DIR")
469 (format #f "/run/user/~a" (getuid))))
470 (flag-file-path (string-append
471 xdg-runtime-dir "/on-first-login-executed")))
472 ;; XDG_RUNTIME_DIR disappears on logout, that means such trick
473 ;; allows to launch on-first-login script on first login only
474 ;; after complete logout/reboot.
475 (if (file-exists? xdg-runtime-dir)
476 (when (claim-first-run flag-file-path)
477 ;; GEXPS can be empty, hence 'begin *unspecified*'. Having just
478 ;; 'begin' without '*unspecified*' leads to
479 ;; "Syntax error: ... sequence of zero expressions in form (begin)"
480 (begin *unspecified* #$@gexps))
481 ;; TRANSLATORS: 'on-first-login' is the name of a service and
482 ;; shouldn't be translated
483 (warning (G_ "XDG_RUNTIME_DIR doesn't exists, on-first-login script
484won't execute anything. You can check if xdg runtime directory exists,
485XDG_RUNTIME_DIR variable is set to appropriate value and manually execute the
486script by running '$HOME/.guix-home/on-first-login'~%"))))))))
487
488(define (on-first-login-script-entry on-first-login)
489 "Return, as a monadic value, an entry for the on-first-login script
490in the home environment directory."
491 (with-monad %store-monad
492 (return `(("on-first-login" ,on-first-login)))))
493
494(define home-run-on-first-login-service-type
495 (service-type (name 'home-run-on-first-login)
496 (extensions
497 (list (service-extension
498 home-service-type
499 on-first-login-script-entry)))
500 (compose identity)
501 (extend compute-on-first-login-script)
502 (default-value #f)
503 (description "Run gexps on first user login. Can be
504extended with one gexp.")))
505
506
507(define (compute-activation-script init-gexp gexps)
508 (gexp->script
509 "activate"
510 #~(let* ((he-init-file (lambda (he) (string-append he "/setup-environment")))
511 (he-path (string-append (getenv "HOME") "/.guix-home"))
512 (new-home-env (getenv "GUIX_NEW_HOME"))
513 (new-home (or new-home-env
514 ;; Absolute path of the directory of the activation
515 ;; file if called interactively.
516 (canonicalize-path (dirname (car (command-line))))))
517 (old-home-env (getenv "GUIX_OLD_HOME"))
518 (old-home (or old-home-env
519 (if (file-exists? (he-init-file he-path))
520 (readlink he-path)
521 #f))))
522 (if (file-exists? (he-init-file new-home))
523 (let* ((port ((@ (ice-9 popen) open-input-pipe)
524 (format #f "source ~a && ~a -0"
525 (he-init-file new-home)
526 #$(file-append coreutils "/bin/env"))))
527 (result ((@ (ice-9 rdelim) read-delimited) "" port))
528 (vars (map (lambda (x)
529 (let ((si (string-index x #\=)))
530 (cons (string-take x si)
531 (string-drop x (1+ si)))))
532 ((@ (srfi srfi-1) remove)
533 string-null?
534 (string-split result #\nul)))))
535 (close-port port)
536 (map (lambda (x) (setenv (car x) (cdr x))) vars)
537
538 (setenv "GUIX_NEW_HOME" new-home)
539 (setenv "GUIX_OLD_HOME" old-home)
540
541 #$@gexps
542
543 ;; Do not unset env variable if it was set outside.
544 (unless new-home-env (setenv "GUIX_NEW_HOME" #f))
545 (unless old-home-env (setenv "GUIX_OLD_HOME" #f)))
546 (format #t "\
547Activation script was either called or loaded by file from this directory:
548~a
549It doesn't seem that home environment is somewhere around.
550Make sure that you call ./activate by symlink from -home store item.\n"
551 new-home)))))
552
553(define (activation-script-entry m-activation)
554 "Return, as a monadic value, an entry for the activation script
555in the home environment directory."
556 (mlet %store-monad ((activation m-activation))
557 (return `(("activate" ,activation)))))
558
559(define home-activation-service-type
560 (service-type (name 'home-activation)
561 (extensions
562 (list (service-extension
563 home-service-type
564 activation-script-entry)))
565 (compose identity)
566 (extend compute-activation-script)
567 (default-value #f)
568 (description "Run gexps to activate the current
569generation of home environment and update the state of the home
570directory. @command{activate} script automatically called during
571reconfiguration or generation switching. This service can be extended
572with one gexp, but many times, and all gexps must be idempotent.")))
573
574
575;;;
576;;; Service type graph rewriting.
577;;;
578
579(define (service-type-mapping proc)
580 "Return a procedure that applies PROC to map a service type graph to another
581one."
582 (define (rewrite extension)
583 (match (proc (service-extension-target extension))
584 (#f #f)
585 (target
586 (service-extension target
587 (service-extension-compute extension)))))
588
589 (define replace
590 (mlambdaq (type)
591 (service-type
592 (inherit type)
593 (name (symbol-append 'home- (service-type-name type)))
594 (location (service-type-location type))
595 (extensions (filter-map rewrite (service-type-extensions type))))))
596
597 replace)
598
599(define %system/home-service-type-mapping
600 ;; Mapping of System to Home services.
601 (make-hash-table))
602
603(define system->home-service-type
604 ;; Map the given System service type to the corresponding Home service type.
605 (let ()
606 (define (replace type)
607 (define replacement
608 (hashq-ref %system/home-service-type-mapping type
609 *unspecified*))
610
611 (if (eq? replacement *unspecified*)
612 type
613 replacement))
614
615 (service-type-mapping replace)))
616
617(define-syntax define-service-type-mapping
618 (syntax-rules (=>)
619 ((_ system-type => home-type)
620 (hashq-set! %system/home-service-type-mapping
621 system-type home-type))))
622
623(define-syntax define-service-type-mappings
624 (syntax-rules (=>)
625 ((_ (system-type => home-type) ...)
626 (begin
627 (define-service-type-mapping system-type => home-type)
628 ...))))
629
630(define-service-type-mappings
631 (system-service-type => home-service-type)
632 (activation-service-type => home-activation-service-type)
633 (profile-service-type => home-profile-service-type))
634
635
636;;;
637;;; On-change.
638;;;
639
640(define (compute-on-change-gexp eval-gexps? pattern-gexp-tuples)
641 (with-imported-modules (source-module-closure '((guix i18n)))
642 #~(begin
643 (use-modules (guix i18n))
644
645 #$%initialize-gettext
646
647 (define (equal-regulars? file1 file2)
648 "Check if FILE1 and FILE2 are bit for bit identical."
649 (let* ((cmp-binary #$(file-append
650 (@ (gnu packages base) diffutils) "/bin/cmp"))
651 (stats1 (lstat file1))
652 (stats2 (lstat file2)))
653 (cond
654 ((= (stat:ino stats1) (stat:ino stats2)) #t)
655 ((not (= (stat:size stats1) (stat:size stats2))) #f)
656
657 (else (= (system* cmp-binary file1 file2) 0)))))
658
659 (define (equal-symlinks? symlink1 symlink2)
660 "Check if SYMLINK1 and SYMLINK2 are pointing to the same target."
661 (string=? (readlink symlink1) (readlink symlink2)))
662
663 (define (equal-directories? dir1 dir2)
664 "Check if DIR1 and DIR2 have the same content."
665 (define (ordinary-file file)
666 (not (or (string=? file ".")
667 (string=? file ".."))))
668 (let* ((files1 (scandir dir1 ordinary-file))
669 (files2 (scandir dir2 ordinary-file)))
670 (if (equal? files1 files2)
671 (map (lambda (file)
672 (equal-files?
673 (string-append dir1 "/" file)
674 (string-append dir2 "/" file)))
675 files1)
676 #f)))
677
678 (define (equal-files? file1 file2)
679 "Compares files, symlinks or directories of the same type."
680 (case (file-type file1)
681 ((directory) (equal-directories? file1 file2))
682 ((symlink) (equal-symlinks? file1 file2))
683 ((regular) (equal-regulars? file1 file2))
684 (else
685 (display "The file type is unsupported by on-change service.\n")
686 #f)))
687
688 (define (file-type file)
689 (stat:type (lstat file)))
690
691 (define (something-changed? file1 file2)
692 (cond
693 ((and (not (file-exists? file1))
694 (not (file-exists? file2))) #f)
695 ((or (not (file-exists? file1))
696 (not (file-exists? file2))) #t)
697
698 ((not (eq? (file-type file1) (file-type file2))) #t)
699
700 (else
701 (not (equal-files? file1 file2)))))
702
703 (define expressions-to-eval
704 (map
705 (lambda (x)
706 (let* ((file1 (string-append
707 (or (getenv "GUIX_OLD_HOME")
708 "/gnu/store/non-existing-generation")
709 "/" (car x)))
710 (file2 (string-append (getenv "GUIX_NEW_HOME") "/" (car x)))
711 (_ (format #t (G_ "Comparing ~a and\n~10t~a...") file1 file2))
712 (any-changes? (something-changed? file1 file2))
713 (_ (format #t (G_ " done (~a)\n")
714 (if any-changes? "changed" "same"))))
715 (if any-changes? (cadr x) "")))
716 '#$pattern-gexp-tuples))
717
718 (if #$eval-gexps?
719 (begin
720 ;;; TRANSLATORS: 'on-change' is the name of a service type, it
721 ;;; probably shouldn't be translated.
722 (display (G_ "Evaluating on-change gexps.\n\n"))
723 (for-each primitive-eval expressions-to-eval)
724 (display (G_ "On-change gexps evaluation finished.\n\n")))
725 (display "\
726On-change gexps won't be evaluated; evaluation has been disabled in the
727service configuration")))))
728
729(define home-run-on-change-service-type
730 (service-type (name 'home-run-on-change)
731 (extensions
732 (list (service-extension
733 home-activation-service-type
734 identity)))
735 (compose concatenate)
736 (extend compute-on-change-gexp)
737 (default-value #t)
738 (description "\
739G-expressions to run if the specified files have changed since the
740last generation. The extension should be a list of lists where the
741first element is the pattern for file or directory that expected to be
742changed, and the second element is the G-expression to be evaluated.")))
743
744
745;;;
746;;; Provenance tracking.
747;;;
748
749(define home-provenance-service-type
750 (service-type
751 (name 'home-provenance)
752 (extensions
753 (list (service-extension
754 home-service-type
755 (service-extension-compute
756 (first (service-type-extensions provenance-service-type))))))
757 (default-value #f) ;the HE config file
758 (description "\
759Store provenance information about the home environment in the home
760environment itself: the channels used when building the home
761environment, and its configuration file, when available.")))
762
763(define sexp->home-provenance sexp->system-provenance)
764(define home-provenance system-provenance)
765
766
767;;;
768;;; Garbage collector roots.
769;;;
770
771(define home-gc-root-service-type
772 (service-type
773 (inherit (system->home-service-type gc-root-service-type))
774 (default-value '())))
775
776
777;;;
778;;; Searching
779;;;
780
781(define (parent-directory directory)
782 "Get the parent directory of DIRECTORY"
783 (string-join (drop-right (string-split directory #\/) 1) "/"))
784
785(define %guix-home-root-directory
786 ;; Absolute file name of the module hierarchy.
787 (parent-directory
788 (dirname (dirname (search-path %load-path "gnu/home/services.scm")))))
789
790(define %service-type-path
791 ;; Search path for service types.
792 (make-parameter `((,%guix-home-root-directory . "gnu/home/services"))))
793
794(define (all-home-service-modules)
795 "Return the default set of `home service' modules."
796 (cons (resolve-interface '(gnu home services))
797 (all-modules (%service-type-path)
798 #:warn warn-about-load-error)))
799
800(define* (fold-home-service-types proc seed)
801 (fold-service-types proc seed (all-home-service-modules)))
802
803(define lookup-home-service-types
804 (let ((table
805 (delay (fold-home-service-types (lambda (type result)
806 (vhash-consq (service-type-name type)
807 type result))
808 vlist-null))))
809 (lambda (name)
810 "Return the list of services with the given NAME (a symbol)."
811 (vhash-foldq* cons '() name (force table)))))