1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 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>
14 ;;; This file is part of GNU Guix.
16 ;;; GNU Guix is free software; you can redistribute it and/or modify it
17 ;;; under the terms of the GNU General Public License as published by
18 ;;; the Free Software Foundation; either version 3 of the License, or (at
19 ;;; your option) any later version.
21 ;;; GNU Guix is distributed in the hope that it will be useful, but
22 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;;; GNU General Public License for more details.
26 ;;; You should have received a copy of the GNU General Public License
27 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
29 (define-module (guix profiles)
30 #:use-module ((guix config) #:select (%state-directory))
31 #:use-module ((guix utils) #:hide (package-name->name+version))
32 #:use-module ((guix build utils)
33 #:select (package-name->name+version mkdir-p))
34 #:use-module (guix i18n)
35 #:use-module (guix records)
36 #:use-module (guix packages)
37 #:use-module (guix derivations)
38 #:use-module (guix search-paths)
39 #:use-module (guix gexp)
40 #:use-module (guix modules)
41 #:use-module (guix monads)
42 #:use-module (guix store)
43 #:use-module (guix sets)
44 #:use-module (ice-9 vlist)
45 #:use-module (ice-9 match)
46 #:use-module (ice-9 regex)
47 #:use-module (ice-9 ftw)
48 #:use-module (ice-9 format)
49 #:use-module (srfi srfi-1)
50 #:use-module (srfi srfi-9)
51 #:use-module (srfi srfi-11)
52 #:use-module (srfi srfi-19)
53 #:use-module (srfi srfi-26)
54 #:use-module (srfi srfi-34)
55 #:use-module (srfi srfi-35)
56 #:export (&profile-error
59 &profile-not-found-error
60 profile-not-found-error?
61 &profile-collision-error
62 profile-collision-error?
63 profile-collision-error-entry
64 profile-collision-error-conflict
65 &missing-generation-error
66 missing-generation-error?
67 missing-generation-error-generation
68 &unmatched-pattern-error
69 unmatched-pattern-error?
70 unmatched-pattern-error-pattern
71 unmatched-pattern-error-manifest
73 manifest make-manifest
76 manifest-transitive-entries
78 <manifest-entry> ; FIXME: eventually make it internal
82 manifest-entry-version
85 manifest-entry-dependencies
86 manifest-entry-search-paths
88 manifest-entry-properties
93 manifest-pattern-version
94 manifest-pattern-output
102 manifest-matching-entries
103 manifest-search-paths
106 manifest-transaction?
107 manifest-transaction-install
108 manifest-transaction-remove
109 manifest-transaction-install-entry
110 manifest-transaction-remove-pattern
111 manifest-transaction-null?
112 manifest-transaction-removal-candidate?
113 manifest-perform-transaction
114 manifest-transaction-effects
117 package->manifest-entry
119 ca-certificate-bundle
120 %default-profile-hooks
128 relative-generation-spec->number
130 previous-generation-number
137 %user-profile-directory
140 ensure-profile-directory
142 user-friendly-profile))
146 ;;; Tools to create and manipulate profiles---i.e., the representation of a
147 ;;; set of installed packages.
156 (define-condition-type &profile-error &error
158 (profile profile-error-profile))
160 (define-condition-type &profile-not-found-error &profile-error
161 profile-not-found-error?)
163 (define-condition-type &profile-collision-error &error
164 profile-collision-error?
165 (entry profile-collision-error-entry) ;<manifest-entry>
166 (conflict profile-collision-error-conflict)) ;<manifest-entry>
168 (define-condition-type &unmatched-pattern-error &error
169 unmatched-pattern-error?
170 (pattern unmatched-pattern-error-pattern) ;<manifest-pattern>
171 (manifest unmatched-pattern-error-manifest)) ;<manifest>
173 (define-condition-type &missing-generation-error &profile-error
174 missing-generation-error?
175 (generation missing-generation-error-generation))
182 (define-record-type <manifest>
185 (entries manifest-entries)) ; list of <manifest-entry>
187 ;; Convenient alias, to avoid name clashes.
188 (define make-manifest manifest)
190 (define-record-type* <manifest-entry> manifest-entry
193 (name manifest-entry-name) ; string
194 (version manifest-entry-version) ; string
195 (output manifest-entry-output ; string
197 (item manifest-entry-item) ; package | file-like | store path
198 (dependencies manifest-entry-dependencies ; <manifest-entry>*
200 (search-paths manifest-entry-search-paths ; search-path-specification*
202 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
203 (default (delay #f)))
204 (properties manifest-entry-properties ; list of symbol/value pairs
207 (define-record-type* <manifest-pattern> manifest-pattern
208 make-manifest-pattern
210 (name manifest-pattern-name) ; string
211 (version manifest-pattern-version ; string | #f
213 (output manifest-pattern-output ; string | #f
216 (define (manifest-transitive-entries manifest)
217 "Return the entries of MANIFEST along with their propagated inputs,
219 (let loop ((entries (manifest-entries manifest))
221 (visited (set))) ;compare with 'equal?'
226 (if (set-contains? visited head)
227 (loop tail result visited)
228 (loop (append (manifest-entry-dependencies head)
231 (set-insert head visited)))))))
233 (define (profile-manifest profile)
234 "Return the PROFILE's manifest."
235 (let ((file (string-append profile "/manifest")))
236 (if (file-exists? file)
237 (call-with-input-file file read-manifest)
240 (define (manifest-entry-lookup manifest)
241 "Return a lookup procedure for the entries of MANIFEST. The lookup
242 procedure takes two arguments: the entry name and output."
244 (let loop ((entries (manifest-entries manifest))
245 (mapping vlist-null))
246 (fold (lambda (entry result)
247 (vhash-cons (cons (manifest-entry-name entry)
248 (manifest-entry-output entry))
250 (loop (manifest-entry-dependencies entry)
255 (lambda (name output)
256 (match (vhash-assoc (cons name output) mapping)
260 (define* (lower-manifest-entry entry system #:key target)
261 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
263 (let ((item (manifest-entry-item entry)))
265 (with-monad %store-monad
267 (mlet %store-monad ((drv (lower-object item system
269 (output -> (manifest-entry-output entry)))
270 (return (manifest-entry
272 (item (derivation->output-path drv output))))))))
274 (define* (check-for-collisions manifest system #:key target)
275 "Check whether the entries of MANIFEST conflict with one another; raise a
276 '&profile-collision-error' when a conflict is encountered."
278 (manifest-entry-lookup manifest))
280 (with-monad %store-monad
282 (lambda (entry result)
283 (match (lookup (manifest-entry-name entry)
284 (manifest-entry-output entry))
285 ((? manifest-entry? second) ;potential conflict
286 (mlet %store-monad ((first (lower-manifest-entry entry system
289 (second (lower-manifest-entry second system
292 (if (string=? (manifest-entry-item first)
293 (manifest-entry-item second))
296 (&profile-collision-error
298 (conflict second)))))))
302 (manifest-transitive-entries manifest))))
304 (define* (package->manifest-entry package #:optional (output "out")
305 #:key (parent (delay #f))
307 "Return a manifest entry for the OUTPUT of package PACKAGE."
308 ;; For each dependency, keep a promise pointing to its "parent" entry.
309 (letrec* ((deps (map (match-lambda
311 (package->manifest-entry package
312 #:parent (delay entry)))
313 ((label package output)
314 (package->manifest-entry package output
315 #:parent (delay entry))))
316 (package-propagated-inputs package)))
317 (entry (manifest-entry
318 (name (package-name package))
319 (version (package-version package))
322 (dependencies (delete-duplicates deps))
324 (package-transitive-native-search-paths package))
326 (properties properties))))
329 (define (packages->manifest packages)
330 "Return a list of manifest entries, one for each item listed in PACKAGES.
331 Elements of PACKAGES can be either package objects or package/string tuples
332 denoting a specific output of a package."
333 (define inferiors-loaded?
334 ;; This hack allows us to provide seamless integration for inferior
335 ;; packages while not having a hard dependency on (guix inferior).
336 (resolve-module '(guix inferior) #f #f #:ensure #f))
338 (define (inferior->entry)
339 (module-ref (resolve-interface '(guix inferior))
340 'inferior-package->manifest-entry))
344 (((? package? package) output)
345 (package->manifest-entry package output))
346 ((? package? package)
347 (package->manifest-entry package))
349 (if inferiors-loaded?
350 ((inferior->entry) thing output)
351 (throw 'wrong-type-arg 'packages->manifest
352 "Wrong package object: ~S" (list thing) (list thing))))
354 (if inferiors-loaded?
355 ((inferior->entry) thing)
356 (throw 'wrong-type-arg 'packages->manifest
357 "Wrong package object: ~S" (list thing) (list thing)))))
360 (define (manifest->gexp manifest)
361 "Return a representation of MANIFEST as a gexp."
362 (define (entry->gexp entry)
364 (($ <manifest-entry> name version output (? string? path)
365 (deps ...) (search-paths ...) _ (properties ...))
366 #~(#$name #$version #$output #$path
367 (propagated-inputs #$(map entry->gexp deps))
368 (search-paths #$(map search-path-specification->sexp
370 (properties . #$properties)))
371 (($ <manifest-entry> name version output package
372 (deps ...) (search-paths ...) _ (properties ...))
373 #~(#$name #$version #$output
374 (ungexp package (or output "out"))
375 (propagated-inputs #$(map entry->gexp deps))
376 (search-paths #$(map search-path-specification->sexp
378 (properties . #$properties)))))
381 (($ <manifest> (entries ...))
382 #~(manifest (version 3)
383 (packages #$(map entry->gexp entries))))))
385 (define (find-package name version)
386 "Return a package from the distro matching NAME and possibly VERSION. This
387 procedure is here for backward-compatibility and will eventually vanish."
388 (define find-best-packages-by-name ;break abstractions
389 (module-ref (resolve-interface '(gnu packages))
390 'find-best-packages-by-name))
392 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
393 ;; former traverses the module tree only once and then allows for efficient
394 ;; access via a vhash.
395 (match (find-best-packages-by-name name version)
398 (match (find-best-packages-by-name name #f)
402 (define (sexp->manifest sexp)
403 "Parse SEXP as a manifest."
404 (define (infer-search-paths name version)
405 ;; Infer the search path specifications for NAME-VERSION by looking up a
406 ;; same-named package in the distro. Useful for the old manifest formats
407 ;; that did not store search path info.
408 (let ((package (find-package name version)))
410 (package-native-search-paths package)
413 (define (infer-dependency item parent)
414 ;; Return a <manifest-entry> for ITEM.
415 (let-values (((name version)
416 (package-name->name+version
417 (store-path-package-name item))))
424 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
426 ((name version output path
427 ('propagated-inputs deps)
428 ('search-paths search-paths)
430 ;; For each of DEPS, keep a promise pointing to ENTRY.
431 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
433 (entry (manifest-entry
439 (search-paths (map sexp->search-path-specification
442 (properties (or (assoc-ref extra-stuff 'properties)
447 (('manifest ('version 0)
448 ('packages ((name version output path) ...)))
450 (map (lambda (name version output path)
456 (search-paths (infer-search-paths name version))))
457 name version output path)))
459 ;; Version 1 adds a list of propagated inputs to the
460 ;; name/version/output/path tuples.
461 (('manifest ('version 1)
462 ('packages ((name version output path deps) ...)))
464 (map (lambda (name version output path deps)
465 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
466 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
468 (let ((deps (match deps
469 (((labels directories) ...)
473 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
475 (entry (manifest-entry
482 (infer-search-paths name version)))))
484 name version output path deps)))
486 ;; Version 2 adds search paths and is slightly more verbose.
487 (('manifest ('version 2 minor-version ...)
488 ('packages ((name version output path
489 ('propagated-inputs deps)
490 ('search-paths search-paths)
494 (map (lambda (name version output path deps search-paths)
495 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
497 (entry (manifest-entry
504 (map sexp->search-path-specification
507 name version output path deps search-paths)))
509 ;; Version 3 represents DEPS as full-blown manifest entries.
510 (('manifest ('version 3 minor-version ...)
511 ('packages (entries ...)))
512 (manifest (map sexp->manifest-entry entries)))
515 (&message (message "unsupported manifest format")))))))
517 (define (read-manifest port)
518 "Return the packages listed in MANIFEST."
519 (sexp->manifest (read port)))
521 (define (concatenate-manifests lst)
522 "Concatenate the manifests listed in LST and return the resulting manifest."
523 (manifest (append-map manifest-entries lst)))
525 (define (map-manifest-entries proc manifest)
526 "Apply PROC to all the entries of MANIFEST and return a new manifest."
528 (map proc (manifest-entries manifest))))
530 (define (entry-predicate pattern)
531 "Return a procedure that returns #t when passed a manifest entry that
532 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
535 (($ <manifest-pattern> name version output)
537 (($ <manifest-entry> entry-name entry-version entry-output)
538 (and (string=? entry-name name)
539 (or (not entry-output) (not output)
540 (string=? entry-output output))
542 (string=? entry-version version))))))))
544 (define (manifest-remove manifest patterns)
545 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
546 must be a manifest-pattern."
547 (define (remove-entry pattern lst)
548 (remove (entry-predicate pattern) lst))
550 (make-manifest (fold remove-entry
551 (manifest-entries manifest)
554 (define (manifest-add manifest entries)
555 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
556 Remove MANIFEST entries that have the same name and output as ENTRIES."
557 (define (same-entry? entry name output)
559 (($ <manifest-entry> entry-name _ entry-output _)
560 (and (equal? name entry-name)
561 (equal? output entry-output)))))
564 (fold (lambda (entry result) ;XXX: quadratic
566 (($ <manifest-entry> name _ out _)
568 (remove (cut same-entry? <> name out)
570 (manifest-entries manifest)
573 (define (manifest-lookup manifest pattern)
574 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
576 (find (entry-predicate pattern)
577 (manifest-entries manifest)))
579 (define (manifest-installed? manifest pattern)
580 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
582 (->bool (manifest-lookup manifest pattern)))
584 (define (manifest-matching-entries manifest patterns)
585 "Return all the entries of MANIFEST that match one of the PATTERNS. Raise
586 an '&unmatched-pattern-error' if none of the entries of MANIFEST matches one
588 (fold-right (lambda (pattern matches)
589 (match (filter (entry-predicate pattern)
590 (manifest-entries manifest))
593 (&unmatched-pattern-error
595 (manifest manifest)))))
597 (append lst matches))))
601 (define (manifest-search-paths manifest)
602 "Return the list of search path specifications that apply to MANIFEST,
603 including the search path specification for $PATH."
606 (append-map manifest-entry-search-paths
607 (manifest-entries manifest)))))
611 ;;; Manifest transactions.
614 (define-record-type* <manifest-transaction> manifest-transaction
615 make-manifest-transaction
616 manifest-transaction?
617 (install manifest-transaction-install ; list of <manifest-entry>
619 (remove manifest-transaction-remove ; list of <manifest-pattern>
622 (define (manifest-transaction-install-entry entry transaction)
623 "Augment TRANSACTION's set of installed packages with ENTRY, a
625 (manifest-transaction
626 (inherit transaction)
628 (cons entry (manifest-transaction-install transaction)))))
630 (define (manifest-transaction-remove-pattern pattern transaction)
631 "Add PATTERN to TRANSACTION's list of packages to remove."
632 (manifest-transaction
633 (inherit transaction)
635 (cons pattern (manifest-transaction-remove transaction)))))
637 (define (manifest-transaction-null? transaction)
638 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
641 (($ <manifest-transaction> () ()) #t)
642 (($ <manifest-transaction> _ _) #f)))
644 (define (manifest-transaction-removal-candidate? entry transaction)
645 "Return true if ENTRY is a candidate for removal in TRANSACTION."
646 (any (lambda (pattern)
647 ((entry-predicate pattern) entry))
648 (manifest-transaction-remove transaction)))
650 (define (manifest-transaction-effects manifest transaction)
651 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
652 the list of packages that would be removed, installed, upgraded, or downgraded
653 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
654 where the head is the entry being upgraded and the tail is the entry that will
656 (define (manifest-entry->pattern entry)
658 (name (manifest-entry-name entry))
659 (output (manifest-entry-output entry))))
661 (let loop ((input (manifest-transaction-install transaction))
667 (let ((remove (manifest-transaction-remove transaction)))
668 (values (manifest-matching-entries manifest remove)
669 (reverse install) (reverse upgrade) (reverse downgrade))))
671 ;; Check whether installing ENTRY corresponds to the installation of a
672 ;; new package or to an upgrade.
674 ;; XXX: When the exact same output directory is installed, we're not
675 ;; really upgrading anything. Add a check for that case.
676 (let* ((pattern (manifest-entry->pattern entry))
677 (previous (manifest-lookup manifest pattern))
678 (newer? (and previous
679 (version>=? (manifest-entry-version entry)
680 (manifest-entry-version previous)))))
682 (if previous install (cons entry install))
683 (if (and previous newer?)
684 (alist-cons previous entry upgrade)
686 (if (and previous (not newer?))
687 (alist-cons previous entry downgrade)
690 (define (manifest-perform-transaction manifest transaction)
691 "Perform TRANSACTION on MANIFEST and return the new manifest."
692 (let ((install (manifest-transaction-install transaction))
693 (remove (manifest-transaction-remove transaction)))
694 (manifest-add (manifest-remove manifest remove)
702 (define (manifest-inputs manifest)
703 "Return a list of <gexp-input> objects for MANIFEST."
706 (($ <manifest-entry> name version output thing deps)
707 ;; THING may be a package or a file name. In the latter case, assume
708 ;; it's already valid.
709 (cons (gexp-input thing output)
710 (append-map entry->input deps)))))
712 (append-map entry->input (manifest-entries manifest)))
714 (define* (manifest-lookup-package manifest name #:optional version)
715 "Return as a monadic value the first package or store path referenced by
716 MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
718 ;; Return as a monadic value the package or store path referenced by the
719 ;; manifest ENTRY, or #f if not referenced.
720 (define (entry-lookup-package entry)
721 (define (find-among-inputs inputs)
722 (find (lambda (input)
723 (and (package? input)
724 (equal? name (package-name input))
726 (string-prefix? version (package-version input))
729 (define (find-among-store-items items)
731 (let-values (((name* version*)
732 (package-name->name+version
733 (store-path-package-name item))))
734 (and (string=? name name*)
736 (string-prefix? version version*)
740 (with-monad %store-monad
741 (match (manifest-entry-item entry)
742 ((? package? package)
743 (match (cons (list (package-name package) package)
744 (package-transitive-inputs package))
745 (((labels inputs . _) ...)
746 (return (find-among-inputs inputs)))))
748 (mlet %store-monad ((refs (references* item)))
749 (return (find-among-store-items refs))))
751 ;; XXX: ITEM might be a 'computed-file' or anything like that, in
752 ;; which case we don't know what to do. The fix may be to check
753 ;; references once ITEM is compiled, as proposed at
754 ;; <https://bugs.gnu.org/29927>.
758 entry-lookup-package (manifest-entries manifest)))
760 (define (info-dir-file manifest)
761 "Return a derivation that builds the 'dir' file for all the entries of
763 (define texinfo ;lazy reference
764 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
765 (define gzip ;lazy reference
766 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
767 (define glibc-utf8-locales ;lazy reference
768 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
771 (with-imported-modules '((guix build utils))
773 (use-modules (guix build utils)
774 (srfi srfi-1) (srfi srfi-26)
777 (define (info-file? file)
778 (or (string-suffix? ".info" file)
779 (string-suffix? ".info.gz" file)))
781 (define (info-files top)
782 (let ((infodir (string-append top "/share/info")))
783 (map (cut string-append infodir "/" <>)
784 (or (scandir infodir info-file?) '()))))
786 (define (info-file-language file)
787 (let* ((base (if (string-suffix? ".gz" file)
788 (basename file ".info.gz")
789 (basename file ".info")))
790 (dot (string-rindex base #\.)))
792 (string-drop base (+ 1 dot))
795 (define (install-info info)
796 (let ((language (info-file-language info)))
797 ;; We need to choose a valid locale for $LANGUAGE to be honored.
798 (setenv "LC_ALL" "en_US.utf8")
799 (setenv "LANGUAGE" language)
801 (system* #+(file-append texinfo "/bin/install-info")
803 (apply string-append #$output "/share/info/dir"
804 (if (string=? "en" language)
806 `("." ,language)))))))
808 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
809 (setenv "GUIX_LOCPATH"
810 #+(file-append glibc-utf8-locales "/lib/locale"))
812 (mkdir-p (string-append #$output "/share/info"))
813 (exit (every install-info
814 (append-map info-files
815 '#$(manifest-inputs manifest)))))))
817 (gexp->derivation "info-dir" build
821 `((type . profile-hook)
824 (define (ghc-package-cache-file manifest)
825 "Return a derivation that builds the GHC 'package.cache' file for all the
826 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
827 (define ghc ;lazy reference
828 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
831 (with-imported-modules '((guix build utils))
833 (use-modules (guix build utils)
834 (srfi srfi-1) (srfi srfi-26)
837 (define ghc-name-version
838 (let* ((base (basename #+ghc)))
840 (+ 1 (string-index base #\-)))))
843 (string-append "lib/" ghc-name-version "/package.conf.d"))
846 (string-append #$output "/" db-subdir))
848 (define (conf-files top)
849 (let ((db (string-append top "/" db-subdir)))
850 (if (file-exists? db)
851 (find-files db "\\.conf$")
854 (define (copy-conf-file conf)
855 (let ((base (basename conf)))
856 (copy-file conf (string-append db-dir "/" base))))
858 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
859 (for-each copy-conf-file
860 (append-map conf-files
862 '#$(manifest-inputs manifest))))
865 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
866 (string-append "--package-db=" db-dir)))))
867 (for-each delete-file (find-files db-dir "\\.conf$"))
870 (with-monad %store-monad
871 ;; Don't depend on GHC when there's nothing to do.
872 (if (any (cut string-prefix? "ghc" <>)
873 (map manifest-entry-name (manifest-entries manifest)))
874 (gexp->derivation "ghc-package-cache" build
878 `((type . profile-hook)
879 (hook . ghc-package-cache)))
882 (define (ca-certificate-bundle manifest)
883 "Return a derivation that builds a single-file bundle containing the CA
884 certificates in the /etc/ssl/certs sub-directories of the packages in
885 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
886 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
889 (define glibc-utf8-locales ;lazy reference
890 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
893 (with-imported-modules '((guix build utils))
895 (use-modules (guix build utils)
902 (define (pem-file? file)
903 (string-suffix? ".pem" file))
905 (define (ca-files top)
906 (let ((cert-dir (string-append top "/etc/ssl/certs")))
907 (map (cut string-append cert-dir "/" <>)
908 (or (scandir cert-dir pem-file?) '()))))
910 (define (concatenate-files files result)
911 "Make RESULT the concatenation of all of FILES."
912 (define (dump file port)
913 (display (call-with-input-file file get-string-all)
915 (newline port)) ;required, see <https://bugs.debian.org/635570>
917 (call-with-output-file result
919 (for-each (cut dump <> port) files))))
921 ;; Some file names in the NSS certificates are UTF-8 encoded so
922 ;; install a UTF-8 locale.
924 (string-append #+glibc-utf8-locales "/lib/locale/"
925 #+(version-major+minor
926 (package-version glibc-utf8-locales))))
927 (setlocale LC_ALL "en_US.utf8")
929 (match (append-map ca-files '#$(manifest-inputs manifest))
931 ;; Since there are no CA files, just create an empty directory. Do
932 ;; not create the etc/ssl/certs sub-directory, since that would
933 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
938 (let ((result (string-append #$output "/etc/ssl/certs")))
940 (concatenate-files ca-files
941 (string-append result
942 "/ca-certificates.crt"))
945 (gexp->derivation "ca-certificate-bundle" build
949 `((type . profile-hook)
950 (hook . ca-certificate-bundle))))
952 (define (glib-schemas manifest)
953 "Return a derivation that unions all schemas from manifest entries and
954 creates the Glib 'gschemas.compiled' file."
955 (define glib ; lazy reference
956 (module-ref (resolve-interface '(gnu packages glib)) 'glib))
958 (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
959 ;; XXX: Can't use glib-compile-schemas corresponding
960 ;; to the glib referenced by 'manifest'. Because
961 ;; '%glib' can be either a package or store path, and
962 ;; there's no way to get the "bin" output for the later.
963 (glib-compile-schemas
964 -> #~(string-append #+glib:bin
965 "/bin/glib-compile-schemas")))
968 (with-imported-modules '((guix build utils)
970 (guix build profiles)
974 (use-modules (guix build utils)
976 (guix build profiles)
979 (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
980 (schemadirs (filter file-exists?
981 (map (cut string-append <> "/share/glib-2.0/schemas")
982 '#$(manifest-inputs manifest)))))
984 ;; Union all the schemas.
985 (mkdir-p (string-append #$output "/share/glib-2.0"))
986 (union-build destdir schemadirs
987 #:log-port (%make-void-port "w"))
990 (when (file-is-directory? dir)
991 (ensure-writable-directory dir)
992 (invoke #+glib-compile-schemas
993 (string-append "--targetdir=" dir)
996 ;; Don't run the hook when there's nothing to do.
998 (gexp->derivation "glib-schemas" build
1002 `((type . profile-hook)
1003 (hook . glib-schemas)))
1006 (define (gtk-icon-themes manifest)
1007 "Return a derivation that unions all icon themes from manifest entries and
1008 creates the GTK+ 'icon-theme.cache' file for each theme."
1009 (define gtk+ ; lazy reference
1010 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
1012 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
1013 ;; XXX: Can't use gtk-update-icon-cache corresponding
1014 ;; to the gtk+ referenced by 'manifest'. Because
1015 ;; '%gtk+' can be either a package or store path, and
1016 ;; there's no way to get the "bin" output for the later.
1017 (gtk-update-icon-cache
1018 -> #~(string-append #+gtk+:bin
1019 "/bin/gtk-update-icon-cache")))
1022 (with-imported-modules '((guix build utils)
1024 (guix build profiles)
1028 (use-modules (guix build utils)
1030 (guix build profiles)
1034 (let* ((destdir (string-append #$output "/share/icons"))
1035 (icondirs (filter file-exists?
1036 (map (cut string-append <> "/share/icons")
1037 '#$(manifest-inputs manifest)))))
1039 ;; Union all the icons.
1040 (mkdir-p (string-append #$output "/share"))
1041 (union-build destdir icondirs
1042 #:log-port (%make-void-port "w"))
1044 ;; Update the 'icon-theme.cache' file for each icon theme.
1047 (let ((dir (string-append destdir "/" theme)))
1048 ;; Occasionally DESTDIR contains plain files, such as
1049 ;; "abiword_48.png". Ignore these.
1050 (when (file-is-directory? dir)
1051 (ensure-writable-directory dir)
1052 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
1053 (scandir destdir (negate (cut member <> '("." "..")))))))))
1055 ;; Don't run the hook when there's nothing to do.
1057 (gexp->derivation "gtk-icon-themes" build
1061 `((type . profile-hook)
1062 (hook . gtk-icon-themes)))
1065 (define (gtk-im-modules manifest)
1066 "Return a derivation that builds the cache files for input method modules
1067 for both major versions of GTK+."
1069 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
1070 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
1072 (define (build gtk gtk-version query)
1073 (let ((major (string-take gtk-version 1)))
1074 (with-imported-modules '((guix build utils)
1076 (guix build profiles)
1080 (use-modules (guix build utils)
1082 (guix build profiles)
1087 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
1089 (destdir (string-append #$output prefix))
1090 (moddirs (cons (string-append #$gtk prefix "/immodules")
1091 (filter file-exists?
1092 (map (cut string-append <> prefix "/immodules")
1093 '#$(manifest-inputs manifest)))))
1094 (modules (append-map (cut find-files <> "\\.so$")
1097 ;; Generate a new immodules cache file.
1098 (mkdir-p (string-append #$output prefix))
1099 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
1100 (outfile (string-append #$output prefix
1101 "/immodules-gtk" #$major ".cache")))
1105 (call-with-output-file outfile
1107 (while (not (eof-object? (peek-char pipe)))
1108 (write-char (read-char pipe) out))))
1111 (close-pipe pipe)))))))))
1113 ;; Don't run the hook when there's nothing to do.
1114 (let* ((pkg-gtk+ (module-ref ; lazy reference
1115 (resolve-interface '(gnu packages gtk)) 'gtk+))
1120 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
1121 ;; output of latest gtk+ package.
1123 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
1129 #$gtk+-2 "/bin/gtk-query-immodules-2.0"))
1131 (if (or gtk+ gtk+-2)
1132 (gexp->derivation "gtk-im-modules" gexp
1136 `((type . profile-hook)
1137 (hook . gtk-im-modules)))
1140 (define (xdg-desktop-database manifest)
1141 "Return a derivation that builds the @file{mimeinfo.cache} database from
1142 desktop files. It's used to query what applications can handle a given
1144 (define desktop-file-utils ; lazy reference
1145 (module-ref (resolve-interface '(gnu packages freedesktop))
1146 'desktop-file-utils))
1148 (mlet %store-monad ((glib
1149 (manifest-lookup-package
1152 (with-imported-modules '((guix build utils)
1155 (use-modules (srfi srfi-26)
1158 (let* ((destdir (string-append #$output "/share/applications"))
1159 (appdirs (filter file-exists?
1160 (map (cut string-append <>
1161 "/share/applications")
1162 '#$(manifest-inputs manifest))))
1163 (update-desktop-database (string-append
1164 #+desktop-file-utils
1165 "/bin/update-desktop-database")))
1166 (mkdir-p (string-append #$output "/share"))
1167 (union-build destdir appdirs
1168 #:log-port (%make-void-port "w"))
1169 (exit (zero? (system* update-desktop-database destdir)))))))
1171 ;; Don't run the hook when 'glib' is not referenced.
1173 (gexp->derivation "xdg-desktop-database" build
1177 `((type . profile-hook)
1178 (hook . xdg-desktop-database)))
1181 (define (xdg-mime-database manifest)
1182 "Return a derivation that builds the @file{mime.cache} database from manifest
1183 entries. It's used to query the MIME type of a given file."
1184 (define shared-mime-info ; lazy reference
1185 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1187 (mlet %store-monad ((glib
1188 (manifest-lookup-package
1191 (with-imported-modules '((guix build utils)
1194 (use-modules (srfi srfi-26)
1197 (let* ((datadir (string-append #$output "/share"))
1198 (destdir (string-append datadir "/mime"))
1199 (pkgdirs (filter file-exists?
1200 (map (cut string-append <>
1201 "/share/mime/packages")
1202 (cons #+shared-mime-info
1203 '#$(manifest-inputs manifest)))))
1204 (update-mime-database (string-append
1206 "/bin/update-mime-database")))
1208 (union-build (string-append destdir "/packages") pkgdirs
1209 #:log-port (%make-void-port "w"))
1210 (setenv "XDG_DATA_HOME" datadir)
1211 (exit (zero? (system* update-mime-database destdir)))))))
1213 ;; Don't run the hook when there are no GLib based applications.
1215 (gexp->derivation "xdg-mime-database" build
1219 `((type . profile-hook)
1220 (hook . xdg-mime-database)))
1223 ;; Several font packages may install font files into same directory, so
1224 ;; fonts.dir and fonts.scale file should be generated here, instead of in
1226 (define (fonts-dir-file manifest)
1227 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
1228 files for the fonts of the @var{manifest} entries."
1230 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1233 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1237 (use-modules (srfi srfi-26)
1240 (let ((fonts-dirs (filter file-exists?
1241 (map (cut string-append <>
1243 '#$(manifest-inputs manifest)))))
1245 (if (null? fonts-dirs)
1247 (let* ((share-dir (string-append #$output "/share"))
1248 (fonts-dir (string-append share-dir "/fonts"))
1249 (mkfontscale (string-append #+mkfontscale
1250 "/bin/mkfontscale"))
1251 (mkfontdir (string-append #+mkfontdir
1253 (empty-file? (lambda (filename)
1254 (call-with-ascii-input-file filename
1256 (eqv? #\0 (read-char p))))))
1257 (fonts-dir-file "fonts.dir")
1258 (fonts-scale-file "fonts.scale"))
1260 ;; Create all sub-directories, because we may create fonts.dir
1261 ;; and fonts.scale files in the sub-directories.
1262 (union-build fonts-dir fonts-dirs
1263 #:log-port (%make-void-port "w")
1264 #:create-all-directories? #t)
1265 (let ((directories (find-files fonts-dir
1267 (eq? 'directory (stat:type stat)))
1268 #:directories? #t)))
1269 (for-each (lambda (dir)
1270 (with-directory-excursion dir
1271 (when (file-exists? fonts-scale-file)
1272 (delete-file fonts-scale-file))
1273 (when (file-exists? fonts-dir-file)
1274 (delete-file fonts-dir-file))
1275 (unless (and (zero? (system* mkfontscale))
1276 (zero? (system* mkfontdir)))
1278 (when (and (file-exists? fonts-scale-file)
1279 (empty-file? fonts-scale-file))
1280 (delete-file fonts-scale-file))
1281 (when (and (file-exists? fonts-dir-file)
1282 (empty-file? fonts-dir-file))
1283 (delete-file fonts-dir-file))))
1286 (gexp->derivation "fonts-dir" build
1287 #:modules '((guix build utils)
1293 `((type . profile-hook)
1294 (hook . fonts-dir))))
1296 (define (manual-database manifest)
1297 "Return a derivation that builds the manual page database (\"mandb\") for
1298 the entries in MANIFEST."
1300 (module-ref (resolve-interface '(gnu packages guile))
1304 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
1307 (scheme-file "config.scm"
1309 (define-module #$'(guix config) ;placate Geiser
1313 #+(file-append zlib "/lib/libz")))))
1316 (cons `((guix config) => ,config.scm)
1317 (delete '(guix config)
1318 (source-module-closure `((guix build utils)
1322 (with-imported-modules modules
1323 (with-extensions (list gdbm-ffi) ;for (guix man-db)
1325 (use-modules (guix man-db)
1330 (define (compute-entries)
1331 ;; This is the most expensive part (I/O and CPU, due to
1332 ;; decompression), so report progress as we traverse INPUTS.
1333 (let* ((inputs '#$(manifest-inputs manifest))
1334 (total (length inputs)))
1335 (append-map (lambda (directory count)
1336 (format #t "\r[~3d/~3d] building list of \
1340 (let ((man (string-append directory
1342 (if (directory-exists? man)
1348 (define man-directory
1349 (string-append #$output "/share/man"))
1351 (mkdir-p man-directory)
1353 (format #t "Creating manual page database...~%")
1355 (let* ((start (current-time))
1356 (entries (compute-entries))
1357 (_ (write-mandb-database (string-append man-directory
1360 (duration (time-difference (current-time) start)))
1362 (format #t "~a entries processed in ~,1f s~%"
1364 (+ (time-second duration)
1365 (* (time-nanosecond duration) (expt 10 -9))))
1368 (gexp->derivation "manual-database" build
1370 ;; Work around GDBM 1.13 issue whereby uninitialized bytes
1371 ;; get written to disk:
1372 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
1373 #:env-vars `(("MALLOC_PERTURB_" . "1"))
1377 `((type . profile-hook)
1378 (hook . manual-database))))
1380 (define (texlive-configuration manifest)
1381 "Return a derivation that builds a TeXlive configuration for the entries in
1383 (define entry->texlive-input
1385 (($ <manifest-entry> name version output thing deps)
1386 (if (string-prefix? "texlive-" name)
1387 (cons (gexp-input thing output)
1388 (append-map entry->texlive-input deps))
1391 (with-imported-modules '((guix build utils)
1394 (use-modules (guix build utils)
1397 ;; Build a modifiable union of all texlive inputs. We do this so
1398 ;; that TeX live can resolve the parent and grandparent directories
1399 ;; correctly. There might be a more elegant way to accomplish this.
1400 (union-build #$output
1401 '#$(append-map entry->texlive-input
1402 (manifest-entries manifest))
1403 #:create-all-directories? #t
1404 #:log-port (%make-void-port "w"))
1405 (let ((texmf.cnf (string-append
1407 "/share/texmf-dist/web2c/texmf.cnf")))
1408 (when (file-exists? texmf.cnf)
1409 (substitute* texmf.cnf
1410 (("^TEXMFROOT = .*")
1411 (string-append "TEXMFROOT = " #$output "/share\n"))
1413 "TEXMF = $TEXMFROOT/share/texmf-dist\n"))))
1416 (with-monad %store-monad
1417 (if (any (cut string-prefix? "texlive-" <>)
1418 (map manifest-entry-name (manifest-entries manifest)))
1419 (gexp->derivation "texlive-configuration" build
1423 `((type . profile-hook)
1424 (hook . texlive-configuration)))
1427 (define %default-profile-hooks
1428 ;; This is the list of derivation-returning procedures that are called by
1429 ;; default when making a non-empty profile.
1433 ghc-package-cache-file
1434 ca-certificate-bundle
1438 texlive-configuration
1439 xdg-desktop-database
1442 (define* (profile-derivation manifest
1444 (hooks %default-profile-hooks)
1446 (allow-collisions? #f)
1447 (relative-symlinks? #f)
1449 "Return a derivation that builds a profile (aka. 'user environment') with
1450 the given MANIFEST. The profile includes additional derivations returned by
1451 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
1452 Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1453 entries in MANIFEST collide (for instance if there are two same-name packages
1454 with a different version number.)
1456 When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
1457 a dependency on the 'glibc-utf8-locales' package.
1459 When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
1460 This is one of the things to do for the result to be relocatable.
1462 When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1463 are cross-built for TARGET."
1464 (mlet* %store-monad ((system (if system
1469 (current-target-system)))
1470 (ok? (if allow-collisions?
1472 (check-for-collisions manifest system
1474 (extras (if (null? (manifest-entries manifest))
1481 (append (filter-map (lambda (drv)
1482 (and (derivation? drv)
1485 (manifest-inputs manifest)))
1487 (define glibc-utf8-locales ;lazy reference
1488 (module-ref (resolve-interface '(gnu packages base))
1489 'glibc-utf8-locales))
1491 (define set-utf8-locale
1492 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1493 ;; install a UTF-8 locale.
1496 #$(file-append glibc-utf8-locales "/lib/locale/"
1497 (version-major+minor
1498 (package-version glibc-utf8-locales))))
1499 (setlocale LC_ALL "en_US.utf8")))
1502 (with-imported-modules '((guix build profiles)
1508 (use-modules (guix build profiles)
1512 (setvbuf (current-output-port) _IOLBF)
1513 (setvbuf (current-error-port) _IOLBF)
1515 #+(if locales? set-utf8-locale #t)
1517 (define search-paths
1518 ;; Search paths of MANIFEST's packages, converted back to their
1520 (map sexp->search-path-specification
1522 '#$(map search-path-specification->sexp
1523 (manifest-search-paths manifest)))))
1525 (build-profile #$output '#$inputs
1526 #:symlink #$(if relative-symlinks?
1529 #:manifest '#$(manifest->gexp manifest)
1530 #:search-paths search-paths))))
1532 (gexp->derivation "profile" builder
1536 ;; Don't complain about _IO* on Guile 2.2.
1537 #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
1539 ;; Not worth offloading.
1542 ;; Disable substitution because it would trigger a
1543 ;; connection to the substitute server, which is likely
1544 ;; to have no substitute to offer.
1545 #:substitutable? #f)))
1547 (define* (profile-search-paths profile
1548 #:optional (manifest (profile-manifest profile))
1549 #:key (getenv (const #f)))
1550 "Read the manifest of PROFILE and evaluate the values of search path
1551 environment variables required by PROFILE; return a list of
1552 specification/value pairs. If MANIFEST is not #f, it is assumed to be the
1553 manifest of PROFILE, which avoids rereading it.
1555 Use GETENV to determine the current settings and report only settings not
1557 (evaluate-search-paths (manifest-search-paths manifest)
1558 (list profile) getenv))
1560 (define (profile-regexp profile)
1561 "Return a regular expression that matches PROFILE's name and number."
1562 (make-regexp (string-append "^" (regexp-quote (basename profile))
1565 (define (generation-number profile)
1566 "Return PROFILE's number or 0. An absolute file name must be used."
1567 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
1568 (basename (readlink profile))))
1569 (compose string->number (cut match:substring <> 1)))
1572 (define %profile-generation-rx
1573 ;; Regexp that matches profile generation.
1574 (make-regexp "(.*)-([0-9]+)-link$"))
1576 (define (generation-profile file)
1577 "If FILE is a profile generation GC root such as \"guix-profile-42-link\",
1578 return its corresponding profile---e.g., \"guix-profile\". Otherwise return
1580 (match (regexp-exec %profile-generation-rx file)
1582 (m (let ((profile (match:substring m 1)))
1583 (and (file-exists? (string-append profile "/manifest"))
1586 (define (generation-numbers profile)
1587 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
1588 former profiles were found."
1589 (match (scandir (dirname profile)
1590 (cute regexp-exec (profile-regexp profile) <>))
1591 (#f ; no profile directory
1595 ((profiles ...) ; former profiles around
1596 (sort (map (compose string->number
1597 (cut match:substring <> 1)
1598 (cute regexp-exec (profile-regexp profile) <>))
1602 (define (profile-generations profile)
1603 "Return a list of PROFILE's generations."
1604 (let ((generations (generation-numbers profile)))
1605 (if (equal? generations '(0))
1609 (define (relative-generation-spec->number profile spec)
1610 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
1611 may be a N, -N, or +N, where N is a number. If the spec is N, then the number
1612 returned is N. If it is -N, then the number returned is the profile's current
1613 generation number minus N. If it is +N, then the number returned is the
1614 profile's current generation number plus N. Return #f if there is no such
1616 (let ((number (string->number spec)))
1618 (case (string-ref spec 0)
1620 (relative-generation profile number))
1621 (else (if (memv number (profile-generations profile))
1626 (define* (relative-generation profile shift #:optional
1627 (current (generation-number profile)))
1628 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
1629 SHIFT is a positive or negative number.
1630 Return #f if there is no such generation."
1631 (let* ((abs-shift (abs shift))
1632 (numbers (profile-generations profile))
1633 (from-current (memq current
1634 (if (negative? shift)
1638 (< abs-shift (length from-current))
1639 (list-ref from-current abs-shift))))
1641 (define* (previous-generation-number profile #:optional
1642 (number (generation-number profile)))
1643 "Return the number of the generation before generation NUMBER of
1644 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
1645 case when generations have been deleted (there are \"holes\")."
1646 (or (relative-generation profile -1 number)
1649 (define (generation-file-name profile generation)
1650 "Return the file name for PROFILE's GENERATION."
1651 (format #f "~a-~a-link" profile generation))
1653 (define (generation-time profile number)
1654 "Return the creation time of a generation in the UTC format."
1655 (make-time time-utc 0
1656 (stat:ctime (stat (generation-file-name profile number)))))
1658 (define (link-to-empty-profile store generation)
1659 "Link GENERATION, a string, to the empty profile. An error is raised if
1661 (let* ((drv (run-with-store store
1662 (profile-derivation (manifest '())
1664 (prof (derivation->output-path drv "out")))
1665 (build-derivations store (list drv))
1666 (switch-symlinks generation prof)))
1668 (define (switch-to-generation profile number)
1669 "Atomically switch PROFILE to the generation NUMBER. Return the number of
1670 the generation that was current before switching."
1671 (let ((current (generation-number profile))
1672 (generation (generation-file-name profile number)))
1673 (cond ((not (file-exists? profile))
1674 (raise (condition (&profile-not-found-error
1675 (profile profile)))))
1676 ((not (file-exists? generation))
1677 (raise (condition (&missing-generation-error
1679 (generation number)))))
1681 (switch-symlinks profile (basename generation))
1684 (define (switch-to-previous-generation profile)
1685 "Atomically switch PROFILE to the previous generation. Return the former
1686 generation number and the current one."
1687 (let ((previous (previous-generation-number profile)))
1688 (values (switch-to-generation profile previous)
1691 (define (roll-back store profile)
1692 "Roll back to the previous generation of PROFILE. Return the number of the
1693 generation that was current before switching and the new generation number."
1694 (let* ((number (generation-number profile))
1695 (previous-number (previous-generation-number profile number))
1696 (previous-generation (generation-file-name profile previous-number)))
1697 (cond ((not (file-exists? profile)) ;invalid profile
1698 (raise (condition (&profile-not-found-error
1699 (profile profile)))))
1700 ((zero? number) ;empty profile
1701 (values number number))
1702 ((or (zero? previous-number) ;going to emptiness
1703 (not (file-exists? previous-generation)))
1704 (link-to-empty-profile store previous-generation)
1705 (switch-to-previous-generation profile))
1706 (else ;anything else
1707 (switch-to-previous-generation profile)))))
1709 (define (delete-generation store profile number)
1710 "Delete generation with NUMBER from PROFILE. Return the file name of the
1711 generation that has been deleted, or #f if nothing was done (for instance
1712 because the NUMBER is zero.)"
1713 (define (delete-and-return)
1714 (let ((generation (generation-file-name profile number)))
1715 (delete-file generation)
1718 (let* ((current-number (generation-number profile))
1719 (previous-number (previous-generation-number profile number))
1720 (previous-generation (generation-file-name profile previous-number)))
1721 (cond ((zero? number) #f) ;do not delete generation 0
1722 ((and (= number current-number)
1723 (not (file-exists? previous-generation)))
1724 (link-to-empty-profile store previous-generation)
1725 (switch-to-previous-generation profile)
1726 (delete-and-return))
1727 ((= number current-number)
1728 (roll-back store profile)
1729 (delete-and-return))
1731 (delete-and-return)))))
1733 (define %user-profile-directory
1734 (and=> (getenv "HOME")
1735 (cut string-append <> "/.guix-profile")))
1737 (define %profile-directory
1738 (string-append %state-directory "/profiles/"
1739 (or (and=> (or (getenv "USER")
1742 (passwd:name (getpwuid (getuid)))))
1743 (cut string-append "per-user/" <>))
1746 (define %current-profile
1747 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
1748 ;; coexist with Nix profiles.
1749 (string-append %profile-directory "/guix-profile"))
1751 (define (ensure-profile-directory)
1752 "Attempt to create /…/profiles/per-user/$USER if needed. Nowadays this is
1753 taken care of by the daemon."
1754 (let ((s (stat %profile-directory #f)))
1755 (unless (and s (eq? 'directory (stat:type s)))
1756 (catch 'system-error
1758 (mkdir-p %profile-directory))
1760 ;; Often, we cannot create %PROFILE-DIRECTORY because its
1761 ;; parent directory is root-owned and we're running
1767 (G_ "while creating directory `~a': ~a")
1769 (strerror (system-error-errno args)))))
1772 (format #f (G_ "Please create the @file{~a} directory, \
1773 with you as the owner.")
1774 %profile-directory))))))))
1776 ;; Bail out if it's not owned by the user.
1777 (unless (or (not s) (= (stat:uid s) (getuid)))
1781 (format #f (G_ "directory `~a' is not owned by you")
1782 %profile-directory)))
1785 (format #f (G_ "Please change the owner of @file{~a} \
1787 %profile-directory (or (getenv "USER")
1791 (define (canonicalize-profile profile)
1792 "If PROFILE points to a profile in %PROFILE-DIRECTORY, return that.
1793 Otherwise return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile'
1794 as if '-p' was omitted." ; see <http://bugs.gnu.org/17939>
1795 ;; Trim trailing slashes so 'readlink' can do its job.
1796 (let ((profile (string-trim-right profile #\/)))
1797 (catch 'system-error
1799 (let ((target (readlink profile)))
1800 (if (string=? (dirname target) %profile-directory)
1805 (define %known-shorthand-profiles
1806 ;; Known shorthand forms for profiles that the user manipulates.
1807 (list (string-append (config-directory #:ensure? #f) "/current")
1808 %user-profile-directory))
1810 (define (user-friendly-profile profile)
1811 "Return either ~/.guix-profile or ~/.config/guix/current if that's what
1812 PROFILE refers to, directly or indirectly, or PROFILE."
1813 (or (find (lambda (shorthand)
1815 (let ((target (false-if-exception
1816 (readlink shorthand))))
1817 (and target (string=? target profile)))))
1818 %known-shorthand-profiles)
1821 ;;; profiles.scm ends here