1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
4 ;;; Copyright © 2014, 2016 Alex Kost <alezost@gmail.com>
5 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
6 ;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
7 ;;; Copyright © 2016, 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
8 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
9 ;;; Copyright © 2017 Huang Ying <huang.ying.caritas@gmail.com>
10 ;;; Copyright © 2017 Maxim Cournoyer <maxim.cournoyer@gmail.com>
11 ;;; Copyright © 2019 Kyle Meyer <kyle@kyleam.com>
12 ;;; Copyright © 2019 Mathieu Othacehe <m.othacehe@gmail.com>
13 ;;; Copyright © 2020 Danny Milosavljevic <dannym@scratchpost.org>
15 ;;; This file is part of GNU Guix.
17 ;;; GNU Guix is free software; you can redistribute it and/or modify it
18 ;;; under the terms of the GNU General Public License as published by
19 ;;; the Free Software Foundation; either version 3 of the License, or (at
20 ;;; your option) any later version.
22 ;;; GNU Guix is distributed in the hope that it will be useful, but
23 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;;; GNU General Public License for more details.
27 ;;; You should have received a copy of the GNU General Public License
28 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
30 (define-module (guix profiles)
31 #:use-module ((guix config) #:select (%state-directory))
32 #:use-module ((guix utils) #:hide (package-name->name+version))
33 #:use-module ((guix build utils)
34 #:select (package-name->name+version mkdir-p))
35 #:use-module (guix i18n)
36 #:use-module (guix records)
37 #:use-module (guix packages)
38 #:use-module (guix derivations)
39 #:use-module (guix search-paths)
40 #:use-module (guix gexp)
41 #:use-module (guix modules)
42 #:use-module (guix monads)
43 #:use-module (guix store)
44 #:use-module (guix sets)
45 #:use-module (ice-9 vlist)
46 #:use-module (ice-9 match)
47 #:use-module (ice-9 regex)
48 #:use-module (ice-9 ftw)
49 #:use-module (ice-9 format)
50 #:use-module (srfi srfi-1)
51 #:use-module (srfi srfi-9)
52 #:use-module (srfi srfi-11)
53 #:use-module (srfi srfi-19)
54 #:use-module (srfi srfi-26)
55 #:use-module (srfi srfi-34)
56 #:use-module (srfi srfi-35)
57 #:export (&profile-error
60 &profile-not-found-error
61 profile-not-found-error?
62 &profile-collision-error
63 profile-collision-error?
64 profile-collision-error-entry
65 profile-collision-error-conflict
66 &missing-generation-error
67 missing-generation-error?
68 missing-generation-error-generation
69 &unmatched-pattern-error
70 unmatched-pattern-error?
71 unmatched-pattern-error-pattern
72 unmatched-pattern-error-manifest
74 manifest make-manifest
77 manifest-transitive-entries
79 <manifest-entry> ; FIXME: eventually make it internal
83 manifest-entry-version
86 manifest-entry-dependencies
87 manifest-entry-search-paths
89 manifest-entry-properties
94 manifest-pattern-version
95 manifest-pattern-output
103 manifest-matching-entries
104 manifest-search-paths
107 manifest-transaction?
108 manifest-transaction-install
109 manifest-transaction-remove
110 manifest-transaction-install-entry
111 manifest-transaction-remove-pattern
112 manifest-transaction-null?
113 manifest-transaction-removal-candidate?
114 manifest-perform-transaction
115 manifest-transaction-effects
118 package->manifest-entry
120 ca-certificate-bundle
121 %default-profile-hooks
129 relative-generation-spec->number
131 previous-generation-number
138 %user-profile-directory
141 ensure-profile-directory
143 user-friendly-profile
145 linux-module-database))
149 ;;; Tools to create and manipulate profiles---i.e., the representation of a
150 ;;; set of installed packages.
159 (define-condition-type &profile-error &error
161 (profile profile-error-profile))
163 (define-condition-type &profile-not-found-error &profile-error
164 profile-not-found-error?)
166 (define-condition-type &profile-collision-error &error
167 profile-collision-error?
168 (entry profile-collision-error-entry) ;<manifest-entry>
169 (conflict profile-collision-error-conflict)) ;<manifest-entry>
171 (define-condition-type &unmatched-pattern-error &error
172 unmatched-pattern-error?
173 (pattern unmatched-pattern-error-pattern) ;<manifest-pattern>
174 (manifest unmatched-pattern-error-manifest)) ;<manifest>
176 (define-condition-type &missing-generation-error &profile-error
177 missing-generation-error?
178 (generation missing-generation-error-generation))
185 (define-record-type <manifest>
188 (entries manifest-entries)) ; list of <manifest-entry>
190 ;; Convenient alias, to avoid name clashes.
191 (define make-manifest manifest)
193 (define-record-type* <manifest-entry> manifest-entry
196 (name manifest-entry-name) ; string
197 (version manifest-entry-version) ; string
198 (output manifest-entry-output ; string
200 (item manifest-entry-item) ; package | file-like | store path
201 (dependencies manifest-entry-dependencies ; <manifest-entry>*
203 (search-paths manifest-entry-search-paths ; search-path-specification*
205 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
206 (default (delay #f)))
207 (properties manifest-entry-properties ; list of symbol/value pairs
210 (define-record-type* <manifest-pattern> manifest-pattern
211 make-manifest-pattern
213 (name manifest-pattern-name) ; string
214 (version manifest-pattern-version ; string | #f
216 (output manifest-pattern-output ; string | #f
219 (define (manifest-transitive-entries manifest)
220 "Return the entries of MANIFEST along with their propagated inputs,
222 (let loop ((entries (manifest-entries manifest))
224 (visited (set))) ;compare with 'equal?'
229 (if (set-contains? visited head)
230 (loop tail result visited)
231 (loop (append (manifest-entry-dependencies head)
234 (set-insert head visited)))))))
236 (define (profile-manifest profile)
237 "Return the PROFILE's manifest."
238 (let ((file (string-append profile "/manifest")))
239 (if (file-exists? file)
240 (call-with-input-file file read-manifest)
243 (define (manifest-entry-lookup manifest)
244 "Return a lookup procedure for the entries of MANIFEST. The lookup
245 procedure takes two arguments: the entry name and output."
247 (let loop ((entries (manifest-entries manifest))
248 (mapping vlist-null))
249 (fold (lambda (entry result)
250 (vhash-cons (cons (manifest-entry-name entry)
251 (manifest-entry-output entry))
253 (loop (manifest-entry-dependencies entry)
258 (lambda (name output)
259 (match (vhash-assoc (cons name output) mapping)
263 (define* (lower-manifest-entry entry system #:key target)
264 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
266 (let ((item (manifest-entry-item entry)))
268 (with-monad %store-monad
270 (mlet %store-monad ((drv (lower-object item system
272 (output -> (manifest-entry-output entry)))
273 (return (manifest-entry
275 (item (derivation->output-path drv output))))))))
277 (define* (check-for-collisions manifest system #:key target)
278 "Check whether the entries of MANIFEST conflict with one another; raise a
279 '&profile-collision-error' when a conflict is encountered."
281 (manifest-entry-lookup manifest))
284 (filter-map (lambda (entry)
285 (let ((other (lookup (manifest-entry-name entry)
286 (manifest-entry-output entry))))
287 (and other (list entry other))))
288 (manifest-transitive-entries manifest)))
293 (mlet %store-monad ((first (lower-manifest-entry first system
295 (second (lower-manifest-entry second system
297 (return (list first second))))))
299 ;; Start by lowering CANDIDATES "in parallel".
300 (mlet %store-monad ((lst (mapm/accumulate-builds lower-pair candidates)))
302 (lambda (entries result)
305 (if (string=? (manifest-entry-item first)
306 (manifest-entry-item second))
309 (&profile-collision-error
311 (conflict second))))))))
315 (define* (package->manifest-entry package #:optional (output "out")
316 #:key (parent (delay #f))
318 "Return a manifest entry for the OUTPUT of package PACKAGE."
319 ;; For each dependency, keep a promise pointing to its "parent" entry.
320 (letrec* ((deps (map (match-lambda
322 (package->manifest-entry package
323 #:parent (delay entry)))
324 ((label package output)
325 (package->manifest-entry package output
326 #:parent (delay entry))))
327 (package-propagated-inputs package)))
328 (entry (manifest-entry
329 (name (package-name package))
330 (version (package-version package))
333 (dependencies (delete-duplicates deps))
335 (package-transitive-native-search-paths package))
337 (properties properties))))
340 (define (packages->manifest packages)
341 "Return a list of manifest entries, one for each item listed in PACKAGES.
342 Elements of PACKAGES can be either package objects or package/string tuples
343 denoting a specific output of a package."
344 (define inferiors-loaded?
345 ;; This hack allows us to provide seamless integration for inferior
346 ;; packages while not having a hard dependency on (guix inferior).
347 (resolve-module '(guix inferior) #f #f #:ensure #f))
349 (define (inferior->entry)
350 (module-ref (resolve-interface '(guix inferior))
351 'inferior-package->manifest-entry))
355 (((? package? package) output)
356 (package->manifest-entry package output))
357 ((? package? package)
358 (package->manifest-entry package))
360 (if inferiors-loaded?
361 ((inferior->entry) thing output)
362 (throw 'wrong-type-arg 'packages->manifest
363 "Wrong package object: ~S" (list thing) (list thing))))
365 (if inferiors-loaded?
366 ((inferior->entry) thing)
367 (throw 'wrong-type-arg 'packages->manifest
368 "Wrong package object: ~S" (list thing) (list thing)))))
371 (define (manifest->gexp manifest)
372 "Return a representation of MANIFEST as a gexp."
373 (define (entry->gexp entry)
375 (($ <manifest-entry> name version output (? string? path)
376 (deps ...) (search-paths ...) _ (properties ...))
377 #~(#$name #$version #$output #$path
378 (propagated-inputs #$(map entry->gexp deps))
379 (search-paths #$(map search-path-specification->sexp
381 (properties . #$properties)))
382 (($ <manifest-entry> name version output package
383 (deps ...) (search-paths ...) _ (properties ...))
384 #~(#$name #$version #$output
385 (ungexp package (or output "out"))
386 (propagated-inputs #$(map entry->gexp deps))
387 (search-paths #$(map search-path-specification->sexp
389 (properties . #$properties)))))
392 (($ <manifest> (entries ...))
393 #~(manifest (version 3)
394 (packages #$(map entry->gexp entries))))))
396 (define (find-package name version)
397 "Return a package from the distro matching NAME and possibly VERSION. This
398 procedure is here for backward-compatibility and will eventually vanish."
399 (define find-best-packages-by-name ;break abstractions
400 (module-ref (resolve-interface '(gnu packages))
401 'find-best-packages-by-name))
403 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
404 ;; former traverses the module tree only once and then allows for efficient
405 ;; access via a vhash.
406 (match (find-best-packages-by-name name version)
409 (match (find-best-packages-by-name name #f)
413 (define (sexp->manifest sexp)
414 "Parse SEXP as a manifest."
415 (define (infer-search-paths name version)
416 ;; Infer the search path specifications for NAME-VERSION by looking up a
417 ;; same-named package in the distro. Useful for the old manifest formats
418 ;; that did not store search path info.
419 (let ((package (find-package name version)))
421 (package-native-search-paths package)
424 (define (infer-dependency item parent)
425 ;; Return a <manifest-entry> for ITEM.
426 (let-values (((name version)
427 (package-name->name+version
428 (store-path-package-name item))))
435 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
437 ((name version output path
438 ('propagated-inputs deps)
439 ('search-paths search-paths)
441 ;; For each of DEPS, keep a promise pointing to ENTRY.
442 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
444 (entry (manifest-entry
450 (search-paths (map sexp->search-path-specification
453 (properties (or (assoc-ref extra-stuff 'properties)
458 (('manifest ('version 0)
459 ('packages ((name version output path) ...)))
461 (map (lambda (name version output path)
467 (search-paths (infer-search-paths name version))))
468 name version output path)))
470 ;; Version 1 adds a list of propagated inputs to the
471 ;; name/version/output/path tuples.
472 (('manifest ('version 1)
473 ('packages ((name version output path deps) ...)))
475 (map (lambda (name version output path deps)
476 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
477 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
479 (let ((deps (match deps
480 (((labels directories) ...)
484 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
486 (entry (manifest-entry
493 (infer-search-paths name version)))))
495 name version output path deps)))
497 ;; Version 2 adds search paths and is slightly more verbose.
498 (('manifest ('version 2 minor-version ...)
499 ('packages ((name version output path
500 ('propagated-inputs deps)
501 ('search-paths search-paths)
505 (map (lambda (name version output path deps search-paths)
506 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
508 (entry (manifest-entry
515 (map sexp->search-path-specification
518 name version output path deps search-paths)))
520 ;; Version 3 represents DEPS as full-blown manifest entries.
521 (('manifest ('version 3 minor-version ...)
522 ('packages (entries ...)))
523 (manifest (map sexp->manifest-entry entries)))
526 (&message (message "unsupported manifest format")))))))
528 (define (read-manifest port)
529 "Return the packages listed in MANIFEST."
530 (sexp->manifest (read port)))
532 (define (concatenate-manifests lst)
533 "Concatenate the manifests listed in LST and return the resulting manifest."
534 (manifest (append-map manifest-entries lst)))
536 (define (map-manifest-entries proc manifest)
537 "Apply PROC to all the entries of MANIFEST and return a new manifest."
539 (map proc (manifest-entries manifest))))
541 (define (entry-predicate pattern)
542 "Return a procedure that returns #t when passed a manifest entry that
543 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
546 (($ <manifest-pattern> name version output)
548 (($ <manifest-entry> entry-name entry-version entry-output)
549 (and (string=? entry-name name)
550 (or (not entry-output) (not output)
551 (string=? entry-output output))
553 (string=? entry-version version))))))))
555 (define (manifest-remove manifest patterns)
556 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
557 must be a manifest-pattern."
558 (define (remove-entry pattern lst)
559 (remove (entry-predicate pattern) lst))
561 (make-manifest (fold remove-entry
562 (manifest-entries manifest)
565 (define (manifest-add manifest entries)
566 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
567 Remove MANIFEST entries that have the same name and output as ENTRIES."
568 (define (same-entry? entry name output)
570 (($ <manifest-entry> entry-name _ entry-output _)
571 (and (equal? name entry-name)
572 (equal? output entry-output)))))
575 (fold (lambda (entry result) ;XXX: quadratic
577 (($ <manifest-entry> name _ out _)
579 (remove (cut same-entry? <> name out)
581 (manifest-entries manifest)
584 (define (manifest-lookup manifest pattern)
585 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
587 (find (entry-predicate pattern)
588 (manifest-entries manifest)))
590 (define (manifest-installed? manifest pattern)
591 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
593 (->bool (manifest-lookup manifest pattern)))
595 (define (manifest-matching-entries manifest patterns)
596 "Return all the entries of MANIFEST that match one of the PATTERNS. Raise
597 an '&unmatched-pattern-error' if none of the entries of MANIFEST matches one
599 (fold-right (lambda (pattern matches)
600 (match (filter (entry-predicate pattern)
601 (manifest-entries manifest))
604 (&unmatched-pattern-error
606 (manifest manifest)))))
608 (append lst matches))))
612 (define (manifest-search-paths manifest)
613 "Return the list of search path specifications that apply to MANIFEST,
614 including the search path specification for $PATH."
617 (append-map manifest-entry-search-paths
618 (manifest-entries manifest)))))
622 ;;; Manifest transactions.
625 (define-record-type* <manifest-transaction> manifest-transaction
626 make-manifest-transaction
627 manifest-transaction?
628 (install manifest-transaction-install ; list of <manifest-entry>
630 (remove manifest-transaction-remove ; list of <manifest-pattern>
633 (define (manifest-transaction-install-entry entry transaction)
634 "Augment TRANSACTION's set of installed packages with ENTRY, a
636 (manifest-transaction
637 (inherit transaction)
639 (cons entry (manifest-transaction-install transaction)))))
641 (define (manifest-transaction-remove-pattern pattern transaction)
642 "Add PATTERN to TRANSACTION's list of packages to remove."
643 (manifest-transaction
644 (inherit transaction)
646 (cons pattern (manifest-transaction-remove transaction)))))
648 (define (manifest-transaction-null? transaction)
649 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
652 (($ <manifest-transaction> () ()) #t)
653 (($ <manifest-transaction> _ _) #f)))
655 (define (manifest-transaction-removal-candidate? entry transaction)
656 "Return true if ENTRY is a candidate for removal in TRANSACTION."
657 (any (lambda (pattern)
658 ((entry-predicate pattern) entry))
659 (manifest-transaction-remove transaction)))
661 (define (manifest-transaction-effects manifest transaction)
662 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
663 the list of packages that would be removed, installed, upgraded, or downgraded
664 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
665 where the head is the entry being upgraded and the tail is the entry that will
667 (define (manifest-entry->pattern entry)
669 (name (manifest-entry-name entry))
670 (output (manifest-entry-output entry))))
672 (let loop ((input (manifest-transaction-install transaction))
678 (let ((remove (manifest-transaction-remove transaction)))
679 (values (manifest-matching-entries manifest remove)
680 (reverse install) (reverse upgrade) (reverse downgrade))))
682 ;; Check whether installing ENTRY corresponds to the installation of a
683 ;; new package or to an upgrade.
685 ;; XXX: When the exact same output directory is installed, we're not
686 ;; really upgrading anything. Add a check for that case.
687 (let* ((pattern (manifest-entry->pattern entry))
688 (previous (manifest-lookup manifest pattern))
689 (newer? (and previous
690 (version>=? (manifest-entry-version entry)
691 (manifest-entry-version previous)))))
693 (if previous install (cons entry install))
694 (if (and previous newer?)
695 (alist-cons previous entry upgrade)
697 (if (and previous (not newer?))
698 (alist-cons previous entry downgrade)
701 (define (manifest-perform-transaction manifest transaction)
702 "Perform TRANSACTION on MANIFEST and return the new manifest."
703 (let ((install (manifest-transaction-install transaction))
704 (remove (manifest-transaction-remove transaction)))
705 (manifest-add (manifest-remove manifest remove)
713 (define (manifest-inputs manifest)
714 "Return a list of <gexp-input> objects for MANIFEST."
717 (($ <manifest-entry> name version output thing deps)
718 ;; THING may be a package or a file name. In the latter case, assume
719 ;; it's already valid.
720 (cons (gexp-input thing output)
721 (append-map entry->input deps)))))
723 (append-map entry->input (manifest-entries manifest)))
725 (define* (manifest-lookup-package manifest name #:optional version)
726 "Return as a monadic value the first package or store path referenced by
727 MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
729 ;; Return as a monadic value the package or store path referenced by the
730 ;; manifest ENTRY, or #f if not referenced.
731 (define (entry-lookup-package entry)
732 (define (find-among-inputs inputs)
733 (find (lambda (input)
734 (and (package? input)
735 (equal? name (package-name input))
737 (string-prefix? version (package-version input))
740 (define (find-among-store-items items)
742 (let-values (((name* version*)
743 (package-name->name+version
744 (store-path-package-name item))))
745 (and (string=? name name*)
747 (string-prefix? version version*)
751 (with-monad %store-monad
752 (match (manifest-entry-item entry)
753 ((? package? package)
754 (match (cons (list (package-name package) package)
755 (package-transitive-inputs package))
756 (((labels inputs . _) ...)
757 (return (find-among-inputs inputs)))))
759 (mlet %store-monad ((refs (references* item)))
760 (return (find-among-store-items refs))))
762 ;; XXX: ITEM might be a 'computed-file' or anything like that, in
763 ;; which case we don't know what to do. The fix may be to check
764 ;; references once ITEM is compiled, as proposed at
765 ;; <https://bugs.gnu.org/29927>.
769 entry-lookup-package (manifest-entries manifest)))
771 (define (info-dir-file manifest)
772 "Return a derivation that builds the 'dir' file for all the entries of
774 (define texinfo ;lazy reference
775 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
776 (define gzip ;lazy reference
777 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
778 (define glibc-utf8-locales ;lazy reference
779 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
782 (with-imported-modules '((guix build utils))
784 (use-modules (guix build utils)
785 (srfi srfi-1) (srfi srfi-26)
788 (define (info-file? file)
789 (or (string-suffix? ".info" file)
790 (string-suffix? ".info.gz" file)))
792 (define (info-files top)
793 (let ((infodir (string-append top "/share/info")))
794 (map (cut string-append infodir "/" <>)
795 (or (scandir infodir info-file?) '()))))
797 (define (info-file-language file)
798 (let* ((base (if (string-suffix? ".gz" file)
799 (basename file ".info.gz")
800 (basename file ".info")))
801 (dot (string-rindex base #\.)))
803 (string-drop base (+ 1 dot))
806 (define (install-info info)
807 (let ((language (info-file-language info)))
808 ;; We need to choose a valid locale for $LANGUAGE to be honored.
809 (setenv "LC_ALL" "en_US.utf8")
810 (setenv "LANGUAGE" language)
812 (system* #+(file-append texinfo "/bin/install-info")
814 (apply string-append #$output "/share/info/dir"
815 (if (string=? "en" language)
817 `("." ,language)))))))
819 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
820 (setenv "GUIX_LOCPATH"
821 #+(file-append glibc-utf8-locales "/lib/locale"))
823 (mkdir-p (string-append #$output "/share/info"))
824 (exit (every install-info
825 (append-map info-files
826 '#$(manifest-inputs manifest)))))))
828 (gexp->derivation "info-dir" build
832 `((type . profile-hook)
835 (define (ghc-package-cache-file manifest)
836 "Return a derivation that builds the GHC 'package.cache' file for all the
837 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
838 (define ghc ;lazy reference
839 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
842 (with-imported-modules '((guix build utils))
844 (use-modules (guix build utils)
845 (srfi srfi-1) (srfi srfi-26)
848 (define ghc-name-version
849 (let* ((base (basename #+ghc)))
851 (+ 1 (string-index base #\-)))))
854 (string-append "lib/" ghc-name-version "/package.conf.d"))
857 (string-append #$output "/" db-subdir))
859 (define (conf-files top)
860 (let ((db (string-append top "/" db-subdir)))
861 (if (file-exists? db)
862 (find-files db "\\.conf$")
865 (define (copy-conf-file conf)
866 (let ((base (basename conf)))
867 (copy-file conf (string-append db-dir "/" base))))
869 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
870 (for-each copy-conf-file
871 (append-map conf-files
873 '#$(manifest-inputs manifest))))
876 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
877 (string-append "--package-db=" db-dir)))))
878 (for-each delete-file (find-files db-dir "\\.conf$"))
881 (with-monad %store-monad
882 ;; Don't depend on GHC when there's nothing to do.
883 (if (any (cut string-prefix? "ghc" <>)
884 (map manifest-entry-name (manifest-entries manifest)))
885 (gexp->derivation "ghc-package-cache" build
889 `((type . profile-hook)
890 (hook . ghc-package-cache)))
893 (define (ca-certificate-bundle manifest)
894 "Return a derivation that builds a single-file bundle containing the CA
895 certificates in the /etc/ssl/certs sub-directories of the packages in
896 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
897 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
900 (define glibc-utf8-locales ;lazy reference
901 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
904 (with-imported-modules '((guix build utils))
906 (use-modules (guix build utils)
913 (define (pem-file? file)
914 (string-suffix? ".pem" file))
916 (define (ca-files top)
917 (let ((cert-dir (string-append top "/etc/ssl/certs")))
918 (map (cut string-append cert-dir "/" <>)
919 (or (scandir cert-dir pem-file?) '()))))
921 (define (concatenate-files files result)
922 "Make RESULT the concatenation of all of FILES."
923 (define (dump file port)
924 (display (call-with-input-file file get-string-all)
926 (newline port)) ;required, see <https://bugs.debian.org/635570>
928 (call-with-output-file result
930 (for-each (cut dump <> port) files))))
932 ;; Some file names in the NSS certificates are UTF-8 encoded so
933 ;; install a UTF-8 locale.
935 (string-append #+glibc-utf8-locales "/lib/locale/"
936 #+(version-major+minor
937 (package-version glibc-utf8-locales))))
938 (setlocale LC_ALL "en_US.utf8")
940 (match (append-map ca-files '#$(manifest-inputs manifest))
942 ;; Since there are no CA files, just create an empty directory. Do
943 ;; not create the etc/ssl/certs sub-directory, since that would
944 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
949 (let ((result (string-append #$output "/etc/ssl/certs")))
951 (concatenate-files ca-files
952 (string-append result
953 "/ca-certificates.crt"))
956 (gexp->derivation "ca-certificate-bundle" build
960 `((type . profile-hook)
961 (hook . ca-certificate-bundle))))
963 (define (glib-schemas manifest)
964 "Return a derivation that unions all schemas from manifest entries and
965 creates the Glib 'gschemas.compiled' file."
966 (define glib ; lazy reference
967 (module-ref (resolve-interface '(gnu packages glib)) 'glib))
969 (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
970 ;; XXX: Can't use glib-compile-schemas corresponding
971 ;; to the glib referenced by 'manifest'. Because
972 ;; '%glib' can be either a package or store path, and
973 ;; there's no way to get the "bin" output for the later.
974 (glib-compile-schemas
975 -> #~(string-append #+glib:bin
976 "/bin/glib-compile-schemas")))
979 (with-imported-modules '((guix build utils)
981 (guix build profiles)
985 (use-modules (guix build utils)
987 (guix build profiles)
990 (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
991 (schemadirs (filter file-exists?
992 (map (cut string-append <> "/share/glib-2.0/schemas")
993 '#$(manifest-inputs manifest)))))
995 ;; Union all the schemas.
996 (mkdir-p (string-append #$output "/share/glib-2.0"))
997 (union-build destdir schemadirs
998 #:log-port (%make-void-port "w"))
1000 (let ((dir destdir))
1001 (when (file-is-directory? dir)
1002 (ensure-writable-directory dir)
1003 (invoke #+glib-compile-schemas
1004 (string-append "--targetdir=" dir)
1007 ;; Don't run the hook when there's nothing to do.
1009 (gexp->derivation "glib-schemas" build
1013 `((type . profile-hook)
1014 (hook . glib-schemas)))
1017 (define (gtk-icon-themes manifest)
1018 "Return a derivation that unions all icon themes from manifest entries and
1019 creates the GTK+ 'icon-theme.cache' file for each theme."
1020 (define gtk+ ; lazy reference
1021 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
1023 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
1024 ;; XXX: Can't use gtk-update-icon-cache corresponding
1025 ;; to the gtk+ referenced by 'manifest'. Because
1026 ;; '%gtk+' can be either a package or store path, and
1027 ;; there's no way to get the "bin" output for the later.
1028 (gtk-update-icon-cache
1029 -> #~(string-append #+gtk+:bin
1030 "/bin/gtk-update-icon-cache")))
1033 (with-imported-modules '((guix build utils)
1035 (guix build profiles)
1039 (use-modules (guix build utils)
1041 (guix build profiles)
1045 (let* ((destdir (string-append #$output "/share/icons"))
1046 (icondirs (filter file-exists?
1047 (map (cut string-append <> "/share/icons")
1048 '#$(manifest-inputs manifest)))))
1050 ;; Union all the icons.
1051 (mkdir-p (string-append #$output "/share"))
1052 (union-build destdir icondirs
1053 #:log-port (%make-void-port "w"))
1055 ;; Update the 'icon-theme.cache' file for each icon theme.
1058 (let ((dir (string-append destdir "/" theme)))
1059 ;; Occasionally DESTDIR contains plain files, such as
1060 ;; "abiword_48.png". Ignore these.
1061 (when (file-is-directory? dir)
1062 (ensure-writable-directory dir)
1063 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
1064 (scandir destdir (negate (cut member <> '("." "..")))))))))
1066 ;; Don't run the hook when there's nothing to do.
1068 (gexp->derivation "gtk-icon-themes" build
1072 `((type . profile-hook)
1073 (hook . gtk-icon-themes)))
1076 (define (gtk-im-modules manifest)
1077 "Return a derivation that builds the cache files for input method modules
1078 for both major versions of GTK+."
1080 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
1081 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
1083 (define (build gtk gtk-version query)
1084 (let ((major (string-take gtk-version 1)))
1085 (with-imported-modules '((guix build utils)
1087 (guix build profiles)
1091 (use-modules (guix build utils)
1093 (guix build profiles)
1098 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
1100 (destdir (string-append #$output prefix))
1101 (moddirs (cons (string-append #$gtk prefix "/immodules")
1102 (filter file-exists?
1103 (map (cut string-append <> prefix "/immodules")
1104 '#$(manifest-inputs manifest)))))
1105 (modules (append-map (cut find-files <> "\\.so$")
1108 ;; Generate a new immodules cache file.
1109 (mkdir-p (string-append #$output prefix))
1110 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
1111 (outfile (string-append #$output prefix
1112 "/immodules-gtk" #$major ".cache")))
1116 (call-with-output-file outfile
1118 (while (not (eof-object? (peek-char pipe)))
1119 (write-char (read-char pipe) out))))
1122 (close-pipe pipe)))))))))
1124 ;; Don't run the hook when there's nothing to do.
1125 (let* ((pkg-gtk+ (module-ref ; lazy reference
1126 (resolve-interface '(gnu packages gtk)) 'gtk+))
1131 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
1132 ;; output of latest gtk+ package.
1134 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
1140 #$gtk+-2 "/bin/gtk-query-immodules-2.0"))
1142 (if (or gtk+ gtk+-2)
1143 (gexp->derivation "gtk-im-modules" gexp
1147 `((type . profile-hook)
1148 (hook . gtk-im-modules)))
1151 (define (linux-module-database manifest)
1152 "Return a derivation that unites all the kernel modules of the manifest
1153 and creates the dependency graph of all these kernel modules.
1155 This is meant to be used as a profile hook."
1156 (define kmod ; lazy reference
1157 (module-ref (resolve-interface '(gnu packages linux)) 'kmod))
1159 (with-imported-modules
1160 (source-module-closure '((guix build utils)
1161 (gnu build linux-modules)))
1163 (use-modules (ice-9 ftw)
1165 (srfi srfi-1) ; append-map
1166 (gnu build linux-modules))
1167 (let* ((inputs '#$(manifest-inputs manifest))
1169 (map (lambda (directory)
1170 (string-append directory "/lib/modules"))
1174 (scandir directory (lambda (basename)
1176 (string-prefix? "." basename))))))
1177 ;; Note: Should usually result in one entry.
1178 (versions (delete-duplicates
1179 (append-map directory-entries
1180 module-directories))))
1183 (let ((old-path (getenv "PATH")))
1184 (setenv "PATH" #+(file-append kmod "/bin"))
1185 (make-linux-module-directory inputs version #$output)
1186 (setenv "PATH" old-path)))
1187 (_ (error "Specified Linux kernel and Linux kernel modules
1188 are not all of the same version")))))))
1189 (gexp->derivation "linux-module-database" build
1193 `((type . profile-hook)
1194 (hook . linux-module-database))))
1196 (define (xdg-desktop-database manifest)
1197 "Return a derivation that builds the @file{mimeinfo.cache} database from
1198 desktop files. It's used to query what applications can handle a given
1200 (define desktop-file-utils ; lazy reference
1201 (module-ref (resolve-interface '(gnu packages freedesktop))
1202 'desktop-file-utils))
1204 (mlet %store-monad ((glib
1205 (manifest-lookup-package
1208 (with-imported-modules '((guix build utils)
1211 (use-modules (srfi srfi-26)
1214 (let* ((destdir (string-append #$output "/share/applications"))
1215 (appdirs (filter file-exists?
1216 (map (cut string-append <>
1217 "/share/applications")
1218 '#$(manifest-inputs manifest))))
1219 (update-desktop-database (string-append
1220 #+desktop-file-utils
1221 "/bin/update-desktop-database")))
1222 (mkdir-p (string-append #$output "/share"))
1223 (union-build destdir appdirs
1224 #:log-port (%make-void-port "w"))
1225 (exit (zero? (system* update-desktop-database destdir)))))))
1227 ;; Don't run the hook when 'glib' is not referenced.
1229 (gexp->derivation "xdg-desktop-database" build
1233 `((type . profile-hook)
1234 (hook . xdg-desktop-database)))
1237 (define (xdg-mime-database manifest)
1238 "Return a derivation that builds the @file{mime.cache} database from manifest
1239 entries. It's used to query the MIME type of a given file."
1240 (define shared-mime-info ; lazy reference
1241 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1243 (mlet %store-monad ((glib
1244 (manifest-lookup-package
1247 (with-imported-modules '((guix build utils)
1250 (use-modules (srfi srfi-26)
1253 (let* ((datadir (string-append #$output "/share"))
1254 (destdir (string-append datadir "/mime"))
1255 (pkgdirs (filter file-exists?
1256 (map (cut string-append <>
1257 "/share/mime/packages")
1258 (cons #+shared-mime-info
1259 '#$(manifest-inputs manifest)))))
1260 (update-mime-database (string-append
1262 "/bin/update-mime-database")))
1264 (union-build (string-append destdir "/packages") pkgdirs
1265 #:log-port (%make-void-port "w"))
1266 (setenv "XDG_DATA_HOME" datadir)
1267 (exit (zero? (system* update-mime-database destdir)))))))
1269 ;; Don't run the hook when there are no GLib based applications.
1271 (gexp->derivation "xdg-mime-database" build
1275 `((type . profile-hook)
1276 (hook . xdg-mime-database)))
1279 ;; Several font packages may install font files into same directory, so
1280 ;; fonts.dir and fonts.scale file should be generated here, instead of in
1282 (define (fonts-dir-file manifest)
1283 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
1284 files for the fonts of the @var{manifest} entries."
1286 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1289 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1293 (use-modules (srfi srfi-26)
1296 (let ((fonts-dirs (filter file-exists?
1297 (map (cut string-append <>
1299 '#$(manifest-inputs manifest)))))
1301 (if (null? fonts-dirs)
1303 (let* ((share-dir (string-append #$output "/share"))
1304 (fonts-dir (string-append share-dir "/fonts"))
1305 (mkfontscale (string-append #+mkfontscale
1306 "/bin/mkfontscale"))
1307 (mkfontdir (string-append #+mkfontdir
1309 (empty-file? (lambda (filename)
1310 (call-with-ascii-input-file filename
1312 (eqv? #\0 (read-char p))))))
1313 (fonts-dir-file "fonts.dir")
1314 (fonts-scale-file "fonts.scale"))
1316 ;; Create all sub-directories, because we may create fonts.dir
1317 ;; and fonts.scale files in the sub-directories.
1318 (union-build fonts-dir fonts-dirs
1319 #:log-port (%make-void-port "w")
1320 #:create-all-directories? #t)
1321 (let ((directories (find-files fonts-dir
1323 (eq? 'directory (stat:type stat)))
1324 #:directories? #t)))
1325 (for-each (lambda (dir)
1326 (with-directory-excursion dir
1327 (when (file-exists? fonts-scale-file)
1328 (delete-file fonts-scale-file))
1329 (when (file-exists? fonts-dir-file)
1330 (delete-file fonts-dir-file))
1331 (unless (and (zero? (system* mkfontscale))
1332 (zero? (system* mkfontdir)))
1334 (when (and (file-exists? fonts-scale-file)
1335 (empty-file? fonts-scale-file))
1336 (delete-file fonts-scale-file))
1337 (when (and (file-exists? fonts-dir-file)
1338 (empty-file? fonts-dir-file))
1339 (delete-file fonts-dir-file))))
1342 (gexp->derivation "fonts-dir" build
1343 #:modules '((guix build utils)
1349 `((type . profile-hook)
1350 (hook . fonts-dir))))
1352 (define (manual-database manifest)
1353 "Return a derivation that builds the manual page database (\"mandb\") for
1354 the entries in MANIFEST."
1356 (module-ref (resolve-interface '(gnu packages guile))
1360 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
1363 (scheme-file "config.scm"
1365 (define-module #$'(guix config) ;placate Geiser
1369 #+(file-append zlib "/lib/libz")))))
1372 (cons `((guix config) => ,config.scm)
1373 (delete '(guix config)
1374 (source-module-closure `((guix build utils)
1378 (with-imported-modules modules
1379 (with-extensions (list gdbm-ffi) ;for (guix man-db)
1381 (use-modules (guix man-db)
1386 (define (compute-entries)
1387 ;; This is the most expensive part (I/O and CPU, due to
1388 ;; decompression), so report progress as we traverse INPUTS.
1389 (let* ((inputs '#$(manifest-inputs manifest))
1390 (total (length inputs)))
1391 (append-map (lambda (directory count)
1392 (format #t "\r[~3d/~3d] building list of \
1396 (let ((man (string-append directory
1398 (if (directory-exists? man)
1404 (define man-directory
1405 (string-append #$output "/share/man"))
1407 (mkdir-p man-directory)
1409 (format #t "Creating manual page database...~%")
1411 (let* ((start (current-time))
1412 (entries (compute-entries))
1413 (_ (write-mandb-database (string-append man-directory
1416 (duration (time-difference (current-time) start)))
1418 (format #t "~a entries processed in ~,1f s~%"
1420 (+ (time-second duration)
1421 (* (time-nanosecond duration) (expt 10 -9))))
1424 (gexp->derivation "manual-database" build
1426 ;; Work around GDBM 1.13 issue whereby uninitialized bytes
1427 ;; get written to disk:
1428 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
1429 #:env-vars `(("MALLOC_PERTURB_" . "1"))
1433 `((type . profile-hook)
1434 (hook . manual-database))))
1436 (define (texlive-configuration manifest)
1437 "Return a derivation that builds a TeXlive configuration for the entries in
1439 (define entry->texlive-input
1441 (($ <manifest-entry> name version output thing deps)
1442 (if (string-prefix? "texlive-" name)
1443 (cons (gexp-input thing output)
1444 (append-map entry->texlive-input deps))
1447 (with-imported-modules '((guix build utils)
1450 (use-modules (guix build utils)
1453 ;; Build a modifiable union of all texlive inputs. We do this so
1454 ;; that TeX live can resolve the parent and grandparent directories
1455 ;; correctly. There might be a more elegant way to accomplish this.
1456 (union-build #$output
1457 '#$(append-map entry->texlive-input
1458 (manifest-entries manifest))
1459 #:create-all-directories? #t
1460 #:log-port (%make-void-port "w"))
1461 (let ((texmf.cnf (string-append
1463 "/share/texmf-dist/web2c/texmf.cnf")))
1464 (when (file-exists? texmf.cnf)
1465 (substitute* texmf.cnf
1466 (("^TEXMFROOT = .*")
1467 (string-append "TEXMFROOT = " #$output "/share\n"))
1469 "TEXMF = $TEXMFROOT/share/texmf-dist\n"))))
1472 (with-monad %store-monad
1473 (if (any (cut string-prefix? "texlive-" <>)
1474 (map manifest-entry-name (manifest-entries manifest)))
1475 (gexp->derivation "texlive-configuration" build
1479 `((type . profile-hook)
1480 (hook . texlive-configuration)))
1483 (define %default-profile-hooks
1484 ;; This is the list of derivation-returning procedures that are called by
1485 ;; default when making a non-empty profile.
1489 ghc-package-cache-file
1490 ca-certificate-bundle
1494 texlive-configuration
1495 xdg-desktop-database
1498 (define* (profile-derivation manifest
1500 (hooks %default-profile-hooks)
1502 (allow-collisions? #f)
1503 (relative-symlinks? #f)
1505 "Return a derivation that builds a profile (aka. 'user environment') with
1506 the given MANIFEST. The profile includes additional derivations returned by
1507 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
1508 Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1509 entries in MANIFEST collide (for instance if there are two same-name packages
1510 with a different version number.)
1512 When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
1513 a dependency on the 'glibc-utf8-locales' package.
1515 When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
1516 This is one of the things to do for the result to be relocatable.
1518 When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1519 are cross-built for TARGET."
1520 (mlet* %store-monad ((system (if system
1525 (current-target-system)))
1526 (ok? (if allow-collisions?
1528 (check-for-collisions manifest system
1530 (extras (if (null? (manifest-entries manifest))
1532 (mapm/accumulate-builds (lambda (hook)
1536 (append (filter-map (lambda (drv)
1537 (and (derivation? drv)
1540 (manifest-inputs manifest)))
1542 (define glibc-utf8-locales ;lazy reference
1543 (module-ref (resolve-interface '(gnu packages base))
1544 'glibc-utf8-locales))
1546 (define set-utf8-locale
1547 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1548 ;; install a UTF-8 locale.
1551 #$(file-append glibc-utf8-locales "/lib/locale/"
1552 (version-major+minor
1553 (package-version glibc-utf8-locales))))
1554 (setlocale LC_ALL "en_US.utf8")))
1557 (with-imported-modules '((guix build profiles)
1563 (use-modules (guix build profiles)
1567 (setvbuf (current-output-port) _IOLBF)
1568 (setvbuf (current-error-port) _IOLBF)
1570 #+(if locales? set-utf8-locale #t)
1572 (define search-paths
1573 ;; Search paths of MANIFEST's packages, converted back to their
1575 (map sexp->search-path-specification
1577 '#$(map search-path-specification->sexp
1578 (manifest-search-paths manifest)))))
1580 (build-profile #$output '#$inputs
1581 #:symlink #$(if relative-symlinks?
1584 #:manifest '#$(manifest->gexp manifest)
1585 #:search-paths search-paths))))
1587 (gexp->derivation "profile" builder
1591 ;; Don't complain about _IO* on Guile 2.2.
1592 #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
1594 ;; Not worth offloading.
1597 ;; Disable substitution because it would trigger a
1598 ;; connection to the substitute server, which is likely
1599 ;; to have no substitute to offer.
1602 #:properties `((type . profile)
1606 (manifest-entries manifest))))))))
1608 (define* (profile-search-paths profile
1609 #:optional (manifest (profile-manifest profile))
1610 #:key (getenv (const #f)))
1611 "Read the manifest of PROFILE and evaluate the values of search path
1612 environment variables required by PROFILE; return a list of
1613 specification/value pairs. If MANIFEST is not #f, it is assumed to be the
1614 manifest of PROFILE, which avoids rereading it.
1616 Use GETENV to determine the current settings and report only settings not
1618 (evaluate-search-paths (manifest-search-paths manifest)
1619 (list profile) getenv))
1621 (define (profile-regexp profile)
1622 "Return a regular expression that matches PROFILE's name and number."
1623 (make-regexp (string-append "^" (regexp-quote (basename profile))
1626 (define (generation-number profile)
1627 "Return PROFILE's number or 0. An absolute file name must be used."
1628 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
1629 (basename (readlink profile))))
1630 (compose string->number (cut match:substring <> 1)))
1633 (define %profile-generation-rx
1634 ;; Regexp that matches profile generation.
1635 (make-regexp "(.*)-([0-9]+)-link$"))
1637 (define (generation-profile file)
1638 "If FILE is a profile generation GC root such as \"guix-profile-42-link\",
1639 return its corresponding profile---e.g., \"guix-profile\". Otherwise return
1641 (match (regexp-exec %profile-generation-rx file)
1643 (m (let ((profile (match:substring m 1)))
1644 (and (file-exists? (string-append profile "/manifest"))
1647 (define (generation-numbers profile)
1648 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
1649 former profiles were found."
1650 (match (scandir (dirname profile)
1651 (cute regexp-exec (profile-regexp profile) <>))
1652 (#f ; no profile directory
1656 ((profiles ...) ; former profiles around
1657 (sort (map (compose string->number
1658 (cut match:substring <> 1)
1659 (cute regexp-exec (profile-regexp profile) <>))
1663 (define (profile-generations profile)
1664 "Return a list of PROFILE's generations."
1665 (let ((generations (generation-numbers profile)))
1666 (if (equal? generations '(0))
1670 (define (relative-generation-spec->number profile spec)
1671 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
1672 may be a N, -N, or +N, where N is a number. If the spec is N, then the number
1673 returned is N. If it is -N, then the number returned is the profile's current
1674 generation number minus N. If it is +N, then the number returned is the
1675 profile's current generation number plus N. Return #f if there is no such
1677 (let ((number (string->number spec)))
1679 (case (string-ref spec 0)
1681 (relative-generation profile number))
1682 (else (if (memv number (profile-generations profile))
1687 (define* (relative-generation profile shift #:optional
1688 (current (generation-number profile)))
1689 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
1690 SHIFT is a positive or negative number.
1691 Return #f if there is no such generation."
1692 (let* ((abs-shift (abs shift))
1693 (numbers (profile-generations profile))
1694 (from-current (memq current
1695 (if (negative? shift)
1699 (< abs-shift (length from-current))
1700 (list-ref from-current abs-shift))))
1702 (define* (previous-generation-number profile #:optional
1703 (number (generation-number profile)))
1704 "Return the number of the generation before generation NUMBER of
1705 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
1706 case when generations have been deleted (there are \"holes\")."
1707 (or (relative-generation profile -1 number)
1710 (define (generation-file-name profile generation)
1711 "Return the file name for PROFILE's GENERATION."
1712 (format #f "~a-~a-link" profile generation))
1714 (define (generation-time profile number)
1715 "Return the creation time of a generation in the UTC format."
1716 (make-time time-utc 0
1717 (stat:ctime (stat (generation-file-name profile number)))))
1719 (define (link-to-empty-profile store generation)
1720 "Link GENERATION, a string, to the empty profile. An error is raised if
1722 (let* ((drv (run-with-store store
1723 (profile-derivation (manifest '())
1725 (prof (derivation->output-path drv "out")))
1726 (build-derivations store (list drv))
1727 (switch-symlinks generation prof)))
1729 (define (switch-to-generation profile number)
1730 "Atomically switch PROFILE to the generation NUMBER. Return the number of
1731 the generation that was current before switching."
1732 (let ((current (generation-number profile))
1733 (generation (generation-file-name profile number)))
1734 (cond ((not (file-exists? profile))
1735 (raise (condition (&profile-not-found-error
1736 (profile profile)))))
1737 ((not (file-exists? generation))
1738 (raise (condition (&missing-generation-error
1740 (generation number)))))
1742 (switch-symlinks profile (basename generation))
1745 (define (switch-to-previous-generation profile)
1746 "Atomically switch PROFILE to the previous generation. Return the former
1747 generation number and the current one."
1748 (let ((previous (previous-generation-number profile)))
1749 (values (switch-to-generation profile previous)
1752 (define (roll-back store profile)
1753 "Roll back to the previous generation of PROFILE. Return the number of the
1754 generation that was current before switching and the new generation number."
1755 (let* ((number (generation-number profile))
1756 (previous-number (previous-generation-number profile number))
1757 (previous-generation (generation-file-name profile previous-number)))
1758 (cond ((not (file-exists? profile)) ;invalid profile
1759 (raise (condition (&profile-not-found-error
1760 (profile profile)))))
1761 ((zero? number) ;empty profile
1762 (values number number))
1763 ((or (zero? previous-number) ;going to emptiness
1764 (not (file-exists? previous-generation)))
1765 (link-to-empty-profile store previous-generation)
1766 (switch-to-previous-generation profile))
1767 (else ;anything else
1768 (switch-to-previous-generation profile)))))
1770 (define (delete-generation store profile number)
1771 "Delete generation with NUMBER from PROFILE. Return the file name of the
1772 generation that has been deleted, or #f if nothing was done (for instance
1773 because the NUMBER is zero.)"
1774 (define (delete-and-return)
1775 (let ((generation (generation-file-name profile number)))
1776 (delete-file generation)
1779 (let* ((current-number (generation-number profile))
1780 (previous-number (previous-generation-number profile number))
1781 (previous-generation (generation-file-name profile previous-number)))
1782 (cond ((zero? number) #f) ;do not delete generation 0
1783 ((and (= number current-number)
1784 (not (file-exists? previous-generation)))
1785 (link-to-empty-profile store previous-generation)
1786 (switch-to-previous-generation profile)
1787 (delete-and-return))
1788 ((= number current-number)
1789 (roll-back store profile)
1790 (delete-and-return))
1792 (delete-and-return)))))
1794 (define %user-profile-directory
1795 (and=> (getenv "HOME")
1796 (cut string-append <> "/.guix-profile")))
1798 (define %profile-directory
1799 (string-append %state-directory "/profiles/"
1800 (or (and=> (or (getenv "USER")
1803 (passwd:name (getpwuid (getuid)))))
1804 (cut string-append "per-user/" <>))
1807 (define %current-profile
1808 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
1809 ;; coexist with Nix profiles.
1810 (string-append %profile-directory "/guix-profile"))
1812 (define (ensure-profile-directory)
1813 "Attempt to create /…/profiles/per-user/$USER if needed. Nowadays this is
1814 taken care of by the daemon."
1815 (let ((s (stat %profile-directory #f)))
1816 (unless (and s (eq? 'directory (stat:type s)))
1817 (catch 'system-error
1819 (mkdir-p %profile-directory))
1821 ;; Often, we cannot create %PROFILE-DIRECTORY because its
1822 ;; parent directory is root-owned and we're running
1828 (G_ "while creating directory `~a': ~a")
1830 (strerror (system-error-errno args)))))
1833 (format #f (G_ "Please create the @file{~a} directory, \
1834 with you as the owner.")
1835 %profile-directory))))))))
1837 ;; Bail out if it's not owned by the user.
1838 (unless (or (not s) (= (stat:uid s) (getuid)))
1842 (format #f (G_ "directory `~a' is not owned by you")
1843 %profile-directory)))
1846 (format #f (G_ "Please change the owner of @file{~a} \
1848 %profile-directory (or (getenv "USER")
1852 (define (canonicalize-profile profile)
1853 "If PROFILE points to a profile in %PROFILE-DIRECTORY, return that.
1854 Otherwise return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile'
1855 as if '-p' was omitted." ; see <http://bugs.gnu.org/17939>
1856 ;; Trim trailing slashes so 'readlink' can do its job.
1857 (let ((profile (string-trim-right profile #\/)))
1858 (catch 'system-error
1860 (let ((target (readlink profile)))
1861 (if (string=? (dirname target) %profile-directory)
1866 (define %known-shorthand-profiles
1867 ;; Known shorthand forms for profiles that the user manipulates.
1868 (list (string-append (config-directory #:ensure? #f) "/current")
1869 %user-profile-directory))
1871 (define (user-friendly-profile profile)
1872 "Return either ~/.guix-profile or ~/.config/guix/current if that's what
1873 PROFILE refers to, directly or indirectly, or PROFILE."
1874 (or (find (lambda (shorthand)
1876 (let ((target (false-if-exception
1877 (readlink shorthand))))
1878 (and target (string=? target profile)))))
1879 %known-shorthand-profiles)
1882 ;;; profiles.scm ends here