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 diagnostics) #:select (&fix-hint))
36 #:use-module (guix i18n)
37 #:use-module (guix records)
38 #:use-module (guix packages)
39 #:use-module (guix derivations)
40 #:use-module (guix search-paths)
41 #:use-module (guix gexp)
42 #:use-module (guix modules)
43 #:use-module (guix monads)
44 #:use-module (guix store)
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
97 manifest-pattern-version
98 manifest-pattern-output
100 concatenate-manifests
106 manifest-matching-entries
107 manifest-search-paths
111 manifest-transaction?
112 manifest-transaction-install
113 manifest-transaction-remove
114 manifest-transaction-install-entry
115 manifest-transaction-remove-pattern
116 manifest-transaction-null?
117 manifest-transaction-removal-candidate?
118 manifest-perform-transaction
119 manifest-transaction-effects
122 package->manifest-entry
124 ca-certificate-bundle
125 %default-profile-hooks
135 profile-allow-collisions?
136 profile-relative-symlinks?
142 relative-generation-spec->number
144 previous-generation-number
151 %user-profile-directory
154 ensure-profile-directory
156 user-friendly-profile
158 linux-module-database))
162 ;;; Tools to create and manipulate profiles---i.e., the representation of a
163 ;;; set of installed packages.
172 (define-condition-type &profile-error &error
174 (profile profile-error-profile))
176 (define-condition-type &profile-not-found-error &profile-error
177 profile-not-found-error?)
179 (define-condition-type &profile-collision-error &error
180 profile-collision-error?
181 (entry profile-collision-error-entry) ;<manifest-entry>
182 (conflict profile-collision-error-conflict)) ;<manifest-entry>
184 (define-condition-type &unmatched-pattern-error &error
185 unmatched-pattern-error?
186 (pattern unmatched-pattern-error-pattern) ;<manifest-pattern>
187 (manifest unmatched-pattern-error-manifest)) ;<manifest>
189 (define-condition-type &missing-generation-error &profile-error
190 missing-generation-error?
191 (generation missing-generation-error-generation))
198 (define-record-type <manifest>
201 (entries manifest-entries)) ; list of <manifest-entry>
203 ;; Convenient alias, to avoid name clashes.
204 (define make-manifest manifest)
206 (define-record-type* <manifest-entry> manifest-entry
209 (name manifest-entry-name) ; string
210 (version manifest-entry-version) ; string
211 (output manifest-entry-output ; string
213 (item manifest-entry-item) ; package | file-like | store path
214 (dependencies manifest-entry-dependencies ; <manifest-entry>*
216 (search-paths manifest-entry-search-paths ; search-path-specification*
218 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
219 (default (delay #f)))
220 (properties manifest-entry-properties ; list of symbol/value pairs
223 (define-record-type* <manifest-pattern> manifest-pattern
224 make-manifest-pattern
226 (name manifest-pattern-name) ; string
227 (version manifest-pattern-version ; string | #f
229 (output manifest-pattern-output ; string | #f
232 (define (list=? = lst1 lst2)
233 "Return true if LST1 and LST2 have the same length and their elements are
234 pairwise equal per =."
241 (and (= head1 head2) (list=? = tail1 tail2)))
245 (define (manifest-entry=? entry1 entry2)
246 "Return true if ENTRY1 is equivalent to ENTRY2, ignoring their 'properties'
249 (($ <manifest-entry> name1 version1 output1 item1 dependencies1 paths1)
251 (($ <manifest-entry> name2 version2 output2 item2 dependencies2 paths2)
252 (and (string=? name1 name2)
253 (string=? version1 version2)
254 (string=? output1 output2)
255 (equal? item1 item2) ;XXX: could be <package> vs. store item
256 (equal? paths1 paths2)
257 (list=? manifest-entry=? dependencies1 dependencies2)))))))
259 (define (manifest-transitive-entries manifest)
260 "Return the entries of MANIFEST along with their propagated inputs,
262 (let loop ((entries (manifest-entries manifest))
264 (visited vlist-null)) ;compare with 'manifest-entry=?'
269 (if (vhash-assoc head visited manifest-entry=?)
270 (loop tail result visited)
271 (loop (append (manifest-entry-dependencies head)
274 (vhash-cons head #t visited)))))))
276 (define (profile-manifest profile)
277 "Return the PROFILE's manifest."
278 (let ((file (string-append profile "/manifest")))
279 (if (file-exists? file)
280 (call-with-input-file file read-manifest)
283 (define (manifest-entry-lookup manifest)
284 "Return a lookup procedure for the entries of MANIFEST. The lookup
285 procedure takes two arguments: the entry name and output."
287 (let loop ((entries (manifest-entries manifest))
288 (mapping vlist-null))
289 (fold (lambda (entry result)
290 (vhash-cons (cons (manifest-entry-name entry)
291 (manifest-entry-output entry))
293 (loop (manifest-entry-dependencies entry)
298 (lambda (name output)
299 (match (vhash-assoc (cons name output) mapping)
303 (define* (lower-manifest-entry entry system #:key target)
304 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
306 (define (recurse entry)
307 (mapm/accumulate-builds (lambda (entry)
308 (lower-manifest-entry entry system
310 (manifest-entry-dependencies entry)))
312 (let ((item (manifest-entry-item entry)))
314 (with-monad %store-monad
316 (mlet %store-monad ((drv (lower-object item system
318 (dependencies (recurse entry))
319 (output -> (manifest-entry-output entry)))
320 (return (manifest-entry
322 (item (derivation->output-path drv output))
323 (dependencies dependencies)))))))
325 (define* (check-for-collisions manifest system #:key target)
326 "Check whether the entries of MANIFEST conflict with one another; raise a
327 '&profile-collision-error' when a conflict is encountered."
329 (manifest-entry-lookup manifest))
332 (filter-map (lambda (entry)
333 (let ((other (lookup (manifest-entry-name entry)
334 (manifest-entry-output entry))))
335 (and other (list entry other))))
336 (manifest-transitive-entries manifest)))
341 (mlet %store-monad ((first (lower-manifest-entry first system
343 (second (lower-manifest-entry second system
345 (return (list first second))))))
347 ;; Start by lowering CANDIDATES "in parallel".
348 (mlet %store-monad ((lst (mapm/accumulate-builds lower-pair candidates)))
350 (lambda (entries result)
353 (if (string=? (manifest-entry-item first)
354 (manifest-entry-item second))
357 (&profile-collision-error
359 (conflict second))))))))
363 (define* (package->manifest-entry package #:optional (output "out")
364 #:key (parent (delay #f))
366 "Return a manifest entry for the OUTPUT of package PACKAGE."
367 ;; For each dependency, keep a promise pointing to its "parent" entry.
368 (letrec* ((deps (map (match-lambda
370 (package->manifest-entry package
371 #:parent (delay entry)))
372 ((label package output)
373 (package->manifest-entry package output
374 #:parent (delay entry))))
375 (package-propagated-inputs package)))
376 (entry (manifest-entry
377 (name (package-name package))
378 (version (package-version package))
381 (dependencies (delete-duplicates deps))
383 (package-transitive-native-search-paths package))
385 (properties properties))))
388 (define (packages->manifest packages)
389 "Return a list of manifest entries, one for each item listed in PACKAGES.
390 Elements of PACKAGES can be either package objects or package/string tuples
391 denoting a specific output of a package."
392 (define inferiors-loaded?
393 ;; This hack allows us to provide seamless integration for inferior
394 ;; packages while not having a hard dependency on (guix inferior).
395 (resolve-module '(guix inferior) #f #f #:ensure #f))
397 (define (inferior->entry)
398 (module-ref (resolve-interface '(guix inferior))
399 'inferior-package->manifest-entry))
403 (((? package? package) output)
404 (package->manifest-entry package output))
405 ((? package? package)
406 (package->manifest-entry package))
408 (if inferiors-loaded?
409 ((inferior->entry) thing output)
410 (throw 'wrong-type-arg 'packages->manifest
411 "Wrong package object: ~S" (list thing) (list thing))))
413 (if inferiors-loaded?
414 ((inferior->entry) thing)
415 (throw 'wrong-type-arg 'packages->manifest
416 "Wrong package object: ~S" (list thing) (list thing)))))
419 (define (manifest->gexp manifest)
420 "Return a representation of MANIFEST as a gexp."
421 (define (entry->gexp entry)
423 (($ <manifest-entry> name version output (? string? path)
424 (deps ...) (search-paths ...) _ (properties ...))
425 #~(#$name #$version #$output #$path
426 (propagated-inputs #$(map entry->gexp deps))
427 (search-paths #$(map search-path-specification->sexp
429 (properties . #$properties)))
430 (($ <manifest-entry> name version output package
431 (deps ...) (search-paths ...) _ (properties ...))
432 #~(#$name #$version #$output
433 (ungexp package (or output "out"))
434 (propagated-inputs #$(map entry->gexp deps))
435 (search-paths #$(map search-path-specification->sexp
437 (properties . #$properties)))))
440 (($ <manifest> (entries ...))
441 #~(manifest (version 3)
442 (packages #$(map entry->gexp entries))))))
444 (define (find-package name version)
445 "Return a package from the distro matching NAME and possibly VERSION. This
446 procedure is here for backward-compatibility and will eventually vanish."
447 (define find-best-packages-by-name ;break abstractions
448 (module-ref (resolve-interface '(gnu packages))
449 'find-best-packages-by-name))
451 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
452 ;; former traverses the module tree only once and then allows for efficient
453 ;; access via a vhash.
454 (match (find-best-packages-by-name name version)
457 (match (find-best-packages-by-name name #f)
461 (define (sexp->manifest sexp)
462 "Parse SEXP as a manifest."
463 (define (infer-search-paths name version)
464 ;; Infer the search path specifications for NAME-VERSION by looking up a
465 ;; same-named package in the distro. Useful for the old manifest formats
466 ;; that did not store search path info.
467 (let ((package (find-package name version)))
469 (package-native-search-paths package)
472 (define (infer-dependency item parent)
473 ;; Return a <manifest-entry> for ITEM.
474 (let-values (((name version)
475 (package-name->name+version
476 (store-path-package-name item))))
483 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
485 ((name version output path
486 ('propagated-inputs deps)
487 ('search-paths search-paths)
489 ;; For each of DEPS, keep a promise pointing to ENTRY.
490 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
492 (entry (manifest-entry
498 (search-paths (map sexp->search-path-specification
501 (properties (or (assoc-ref extra-stuff 'properties)
506 (('manifest ('version 0)
507 ('packages ((name version output path) ...)))
509 (map (lambda (name version output path)
515 (search-paths (infer-search-paths name version))))
516 name version output path)))
518 ;; Version 1 adds a list of propagated inputs to the
519 ;; name/version/output/path tuples.
520 (('manifest ('version 1)
521 ('packages ((name version output path deps) ...)))
523 (map (lambda (name version output path deps)
524 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
525 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
527 (let ((deps (match deps
528 (((labels directories) ...)
532 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
534 (entry (manifest-entry
541 (infer-search-paths name version)))))
543 name version output path deps)))
545 ;; Version 2 adds search paths and is slightly more verbose.
546 (('manifest ('version 2 minor-version ...)
547 ('packages ((name version output path
548 ('propagated-inputs deps)
549 ('search-paths search-paths)
553 (map (lambda (name version output path deps search-paths)
554 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
556 (entry (manifest-entry
563 (map sexp->search-path-specification
566 name version output path deps search-paths)))
568 ;; Version 3 represents DEPS as full-blown manifest entries.
569 (('manifest ('version 3 minor-version ...)
570 ('packages (entries ...)))
571 (manifest (map sexp->manifest-entry entries)))
574 (&message (message "unsupported manifest format")))))))
576 (define (read-manifest port)
577 "Return the packages listed in MANIFEST."
578 (sexp->manifest (read port)))
580 (define (concatenate-manifests lst)
581 "Concatenate the manifests listed in LST and return the resulting manifest."
582 (manifest (append-map manifest-entries lst)))
584 (define (map-manifest-entries proc manifest)
585 "Apply PROC to all the entries of MANIFEST and return a new manifest."
587 (map proc (manifest-entries manifest))))
589 (define (entry-predicate pattern)
590 "Return a procedure that returns #t when passed a manifest entry that
591 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
594 (($ <manifest-pattern> name version output)
596 (($ <manifest-entry> entry-name entry-version entry-output)
597 (and (string=? entry-name name)
598 (or (not entry-output) (not output)
599 (string=? entry-output output))
601 (string=? entry-version version))))))))
603 (define (manifest-remove manifest patterns)
604 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
605 must be a manifest-pattern."
606 (define (remove-entry pattern lst)
607 (remove (entry-predicate pattern) lst))
609 (make-manifest (fold remove-entry
610 (manifest-entries manifest)
613 (define (manifest-add manifest entries)
614 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
615 Remove MANIFEST entries that have the same name and output as ENTRIES."
616 (define (same-entry? entry name output)
618 (($ <manifest-entry> entry-name _ entry-output _)
619 (and (equal? name entry-name)
620 (equal? output entry-output)))))
623 (fold (lambda (entry result) ;XXX: quadratic
625 (($ <manifest-entry> name _ out _)
627 (remove (cut same-entry? <> name out)
629 (manifest-entries manifest)
632 (define (manifest-lookup manifest pattern)
633 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
635 (find (entry-predicate pattern)
636 (manifest-entries manifest)))
638 (define (manifest-installed? manifest pattern)
639 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
641 (->bool (manifest-lookup manifest pattern)))
643 (define (manifest-matching-entries manifest patterns)
644 "Return all the entries of MANIFEST that match one of the PATTERNS. Raise
645 an '&unmatched-pattern-error' if none of the entries of MANIFEST matches one
647 (fold-right (lambda (pattern matches)
648 (match (filter (entry-predicate pattern)
649 (manifest-entries manifest))
652 (&unmatched-pattern-error
654 (manifest manifest)))))
656 (append lst matches))))
660 (define (manifest-search-paths manifest)
661 "Return the list of search path specifications that apply to MANIFEST,
662 including the search path specification for $PATH."
665 (append-map manifest-entry-search-paths
666 (manifest-entries manifest)))))
670 ;;; Manifest transactions.
673 (define-record-type* <manifest-transaction> manifest-transaction
674 make-manifest-transaction
675 manifest-transaction?
676 (install manifest-transaction-install ; list of <manifest-entry>
678 (remove manifest-transaction-remove ; list of <manifest-pattern>
681 (define (manifest-transaction-install-entry entry transaction)
682 "Augment TRANSACTION's set of installed packages with ENTRY, a
684 (manifest-transaction
685 (inherit transaction)
687 (cons entry (manifest-transaction-install transaction)))))
689 (define (manifest-transaction-remove-pattern pattern transaction)
690 "Add PATTERN to TRANSACTION's list of packages to remove."
691 (manifest-transaction
692 (inherit transaction)
694 (cons pattern (manifest-transaction-remove transaction)))))
696 (define (manifest-transaction-null? transaction)
697 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
700 (($ <manifest-transaction> () ()) #t)
701 (($ <manifest-transaction> _ _) #f)))
703 (define (manifest-transaction-removal-candidate? entry transaction)
704 "Return true if ENTRY is a candidate for removal in TRANSACTION."
705 (any (lambda (pattern)
706 ((entry-predicate pattern) entry))
707 (manifest-transaction-remove transaction)))
709 (define (manifest-transaction-effects manifest transaction)
710 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
711 the list of packages that would be removed, installed, upgraded, or downgraded
712 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
713 where the head is the entry being upgraded and the tail is the entry that will
715 (define (manifest-entry->pattern entry)
717 (name (manifest-entry-name entry))
718 (output (manifest-entry-output entry))))
720 (let loop ((input (manifest-transaction-install transaction))
726 (let ((remove (manifest-transaction-remove transaction)))
727 (values (manifest-matching-entries manifest remove)
728 (reverse install) (reverse upgrade) (reverse downgrade))))
730 ;; Check whether installing ENTRY corresponds to the installation of a
731 ;; new package or to an upgrade.
733 ;; XXX: When the exact same output directory is installed, we're not
734 ;; really upgrading anything. Add a check for that case.
735 (let* ((pattern (manifest-entry->pattern entry))
736 (previous (manifest-lookup manifest pattern))
737 (newer? (and previous
738 (version>=? (manifest-entry-version entry)
739 (manifest-entry-version previous)))))
741 (if previous install (cons entry install))
742 (if (and previous newer?)
743 (alist-cons previous entry upgrade)
745 (if (and previous (not newer?))
746 (alist-cons previous entry downgrade)
749 (define (manifest-perform-transaction manifest transaction)
750 "Perform TRANSACTION on MANIFEST and return the new manifest."
751 (let ((install (manifest-transaction-install transaction))
752 (remove (manifest-transaction-remove transaction)))
753 (manifest-add (manifest-remove manifest remove)
761 (define (manifest-inputs manifest)
762 "Return a list of <gexp-input> objects for MANIFEST."
765 (($ <manifest-entry> name version output thing deps)
766 ;; THING may be a package or a file name. In the latter case, assume
767 ;; it's already valid.
768 (cons (gexp-input thing output)
769 (append-map entry->input deps)))))
771 (append-map entry->input (manifest-entries manifest)))
773 (define* (manifest-lookup-package manifest name #:optional version)
774 "Return as a monadic value the first package or store path referenced by
775 MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
777 ;; Return as a monadic value the package or store path referenced by the
778 ;; manifest ENTRY, or #f if not referenced.
779 (define (entry-lookup-package entry)
780 (define (find-among-inputs inputs)
781 (find (lambda (input)
782 (and (package? input)
783 (equal? name (package-name input))
785 (string-prefix? version (package-version input))
788 (define (find-among-store-items items)
790 (let-values (((name* version*)
791 (package-name->name+version
792 (store-path-package-name item))))
793 (and (string=? name name*)
795 (string-prefix? version version*)
799 (with-monad %store-monad
800 (match (manifest-entry-item entry)
801 ((? package? package)
802 (match (cons (list (package-name package) package)
803 (package-transitive-inputs package))
804 (((labels inputs . _) ...)
805 (return (find-among-inputs inputs)))))
807 (mlet %store-monad ((refs (references* item)))
808 (return (find-among-store-items refs))))
810 ;; XXX: ITEM might be a 'computed-file' or anything like that, in
811 ;; which case we don't know what to do. The fix may be to check
812 ;; references once ITEM is compiled, as proposed at
813 ;; <https://bugs.gnu.org/29927>.
817 entry-lookup-package (manifest-entries manifest)))
819 (define (info-dir-file manifest)
820 "Return a derivation that builds the 'dir' file for all the entries of
822 (define texinfo ;lazy reference
823 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
824 (define gzip ;lazy reference
825 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
826 (define glibc-utf8-locales ;lazy reference
827 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
830 (with-imported-modules '((guix build utils))
832 (use-modules (guix build utils)
833 (srfi srfi-1) (srfi srfi-26)
836 (define (info-file? file)
837 (or (string-suffix? ".info" file)
838 (string-suffix? ".info.gz" file)))
840 (define (info-files top)
841 (let ((infodir (string-append top "/share/info")))
842 (map (cut string-append infodir "/" <>)
843 (or (scandir infodir info-file?) '()))))
845 (define (info-file-language file)
846 (let* ((base (if (string-suffix? ".gz" file)
847 (basename file ".info.gz")
848 (basename file ".info")))
849 (dot (string-rindex base #\.)))
851 (string-drop base (+ 1 dot))
854 (define (install-info info)
855 (let ((language (info-file-language info)))
856 ;; We need to choose a valid locale for $LANGUAGE to be honored.
857 (setenv "LC_ALL" "en_US.utf8")
858 (setenv "LANGUAGE" language)
860 (system* #+(file-append texinfo "/bin/install-info")
862 (apply string-append #$output "/share/info/dir"
863 (if (string=? "en" language)
865 `("." ,language)))))))
867 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
868 (setenv "GUIX_LOCPATH"
869 #+(file-append glibc-utf8-locales "/lib/locale"))
871 (mkdir-p (string-append #$output "/share/info"))
872 (exit (every install-info
873 (append-map info-files
874 '#$(manifest-inputs manifest)))))))
876 (gexp->derivation "info-dir" build
880 `((type . profile-hook)
883 (define (ghc-package-cache-file manifest)
884 "Return a derivation that builds the GHC 'package.cache' file for all the
885 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
886 (define ghc ;lazy reference
887 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
890 (with-imported-modules '((guix build utils))
892 (use-modules (guix build utils)
893 (srfi srfi-1) (srfi srfi-26)
896 (define ghc-name-version
897 (let* ((base (basename #+ghc)))
899 (+ 1 (string-index base #\-)))))
902 (string-append "lib/" ghc-name-version "/package.conf.d"))
905 (string-append #$output "/" db-subdir))
907 (define (conf-files top)
908 (let ((db (string-append top "/" db-subdir)))
909 (if (file-exists? db)
910 (find-files db "\\.conf$")
913 (define (copy-conf-file conf)
914 (let ((base (basename conf)))
915 (copy-file conf (string-append db-dir "/" base))))
917 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
918 (for-each copy-conf-file
919 (append-map conf-files
921 '#$(manifest-inputs manifest))))
924 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
925 (string-append "--package-db=" db-dir)))))
926 (for-each delete-file (find-files db-dir "\\.conf$"))
929 (with-monad %store-monad
930 ;; Don't depend on GHC when there's nothing to do.
931 (if (any (cut string-prefix? "ghc" <>)
932 (map manifest-entry-name (manifest-entries manifest)))
933 (gexp->derivation "ghc-package-cache" build
937 `((type . profile-hook)
938 (hook . ghc-package-cache)))
941 (define (ca-certificate-bundle manifest)
942 "Return a derivation that builds a single-file bundle containing the CA
943 certificates in the /etc/ssl/certs sub-directories of the packages in
944 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
945 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
948 (define glibc-utf8-locales ;lazy reference
949 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
952 (with-imported-modules '((guix build utils))
954 (use-modules (guix build utils)
961 (define (pem-file? file)
962 (string-suffix? ".pem" file))
964 (define (ca-files top)
965 (let ((cert-dir (string-append top "/etc/ssl/certs")))
966 (map (cut string-append cert-dir "/" <>)
967 (or (scandir cert-dir pem-file?) '()))))
969 (define (concatenate-files files result)
970 "Make RESULT the concatenation of all of FILES."
971 (define (dump file port)
972 (display (call-with-input-file file get-string-all)
974 (newline port)) ;required, see <https://bugs.debian.org/635570>
976 (call-with-output-file result
978 (for-each (cut dump <> port) files))))
980 ;; Some file names in the NSS certificates are UTF-8 encoded so
981 ;; install a UTF-8 locale.
983 (string-append #+glibc-utf8-locales "/lib/locale/"
984 #+(version-major+minor
985 (package-version glibc-utf8-locales))))
986 (setlocale LC_ALL "en_US.utf8")
988 (match (append-map ca-files '#$(manifest-inputs manifest))
990 ;; Since there are no CA files, just create an empty directory. Do
991 ;; not create the etc/ssl/certs sub-directory, since that would
992 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
997 (let ((result (string-append #$output "/etc/ssl/certs")))
999 (concatenate-files ca-files
1000 (string-append result
1001 "/ca-certificates.crt"))
1004 (gexp->derivation "ca-certificate-bundle" build
1008 `((type . profile-hook)
1009 (hook . ca-certificate-bundle))))
1011 (define (glib-schemas manifest)
1012 "Return a derivation that unions all schemas from manifest entries and
1013 creates the Glib 'gschemas.compiled' file."
1014 (define glib ; lazy reference
1015 (module-ref (resolve-interface '(gnu packages glib)) 'glib))
1017 (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
1018 ;; XXX: Can't use glib-compile-schemas corresponding
1019 ;; to the glib referenced by 'manifest'. Because
1020 ;; '%glib' can be either a package or store path, and
1021 ;; there's no way to get the "bin" output for the later.
1022 (glib-compile-schemas
1023 -> #~(string-append #+glib:bin
1024 "/bin/glib-compile-schemas")))
1027 (with-imported-modules '((guix build utils)
1029 (guix build profiles)
1033 (use-modules (guix build utils)
1035 (guix build profiles)
1038 (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
1039 (schemadirs (filter file-exists?
1040 (map (cut string-append <> "/share/glib-2.0/schemas")
1041 '#$(manifest-inputs manifest)))))
1043 ;; Union all the schemas.
1044 (mkdir-p (string-append #$output "/share/glib-2.0"))
1045 (union-build destdir schemadirs
1046 #:log-port (%make-void-port "w"))
1048 (let ((dir destdir))
1049 (when (file-is-directory? dir)
1050 (ensure-writable-directory dir)
1051 (invoke #+glib-compile-schemas
1052 (string-append "--targetdir=" dir)
1055 ;; Don't run the hook when there's nothing to do.
1057 (gexp->derivation "glib-schemas" build
1061 `((type . profile-hook)
1062 (hook . glib-schemas)))
1065 (define (gtk-icon-themes manifest)
1066 "Return a derivation that unions all icon themes from manifest entries and
1067 creates the GTK+ 'icon-theme.cache' file for each theme."
1068 (define gtk+ ; lazy reference
1069 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
1071 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
1072 ;; XXX: Can't use gtk-update-icon-cache corresponding
1073 ;; to the gtk+ referenced by 'manifest'. Because
1074 ;; '%gtk+' can be either a package or store path, and
1075 ;; there's no way to get the "bin" output for the later.
1076 (gtk-update-icon-cache
1077 -> #~(string-append #+gtk+:bin
1078 "/bin/gtk-update-icon-cache")))
1081 (with-imported-modules '((guix build utils)
1083 (guix build profiles)
1087 (use-modules (guix build utils)
1089 (guix build profiles)
1093 (let* ((destdir (string-append #$output "/share/icons"))
1094 (icondirs (filter file-exists?
1095 (map (cut string-append <> "/share/icons")
1096 '#$(manifest-inputs manifest)))))
1098 ;; Union all the icons.
1099 (mkdir-p (string-append #$output "/share"))
1100 (union-build destdir icondirs
1101 #:log-port (%make-void-port "w"))
1103 ;; Update the 'icon-theme.cache' file for each icon theme.
1106 (let ((dir (string-append destdir "/" theme)))
1107 ;; Occasionally DESTDIR contains plain files, such as
1108 ;; "abiword_48.png". Ignore these.
1109 (when (file-is-directory? dir)
1110 (ensure-writable-directory dir)
1111 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
1112 (scandir destdir (negate (cut member <> '("." "..")))))))))
1114 ;; Don't run the hook when there's nothing to do.
1116 (gexp->derivation "gtk-icon-themes" build
1120 `((type . profile-hook)
1121 (hook . gtk-icon-themes)))
1124 (define (gtk-im-modules manifest)
1125 "Return a derivation that builds the cache files for input method modules
1126 for both major versions of GTK+."
1128 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
1129 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
1131 (define (build gtk gtk-version query)
1132 (let ((major (string-take gtk-version 1)))
1133 (with-imported-modules '((guix build utils)
1135 (guix build profiles)
1139 (use-modules (guix build utils)
1141 (guix build profiles)
1146 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
1148 (destdir (string-append #$output prefix))
1149 (moddirs (cons (string-append #$gtk prefix "/immodules")
1150 (filter file-exists?
1151 (map (cut string-append <> prefix "/immodules")
1152 '#$(manifest-inputs manifest)))))
1153 (modules (append-map (cut find-files <> "\\.so$")
1156 ;; Generate a new immodules cache file.
1157 (mkdir-p (string-append #$output prefix))
1158 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
1159 (outfile (string-append #$output prefix
1160 "/immodules-gtk" #$major ".cache")))
1164 (call-with-output-file outfile
1166 (while (not (eof-object? (peek-char pipe)))
1167 (write-char (read-char pipe) out))))
1170 (close-pipe pipe)))))))))
1172 ;; Don't run the hook when there's nothing to do.
1173 (let* ((pkg-gtk+ (module-ref ; lazy reference
1174 (resolve-interface '(gnu packages gtk)) 'gtk+))
1175 (pkg-gtk+2 (module-ref ; lazy reference
1176 (resolve-interface '(gnu packages gtk)) 'gtk+-2))
1181 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
1182 ;; output of latest gtk+ package.
1184 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
1190 #$pkg-gtk+2:bin "/bin/gtk-query-immodules-2.0"))
1192 (if (or gtk+ gtk+-2)
1193 (gexp->derivation "gtk-im-modules" gexp
1197 `((type . profile-hook)
1198 (hook . gtk-im-modules)))
1201 (define (linux-module-database manifest)
1202 "Return a derivation that unites all the kernel modules of the manifest
1203 and creates the dependency graph of all these kernel modules.
1205 This is meant to be used as a profile hook."
1206 (define kmod ; lazy reference
1207 (module-ref (resolve-interface '(gnu packages linux)) 'kmod))
1210 (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
1213 (with-imported-modules (source-module-closure
1214 '((guix build utils)
1215 (gnu build linux-modules)))
1216 (with-extensions (list guile-zlib)
1218 (use-modules (ice-9 ftw)
1220 (srfi srfi-1) ; append-map
1221 (gnu build linux-modules))
1223 (let* ((inputs '#$(manifest-inputs manifest))
1225 (map (lambda (directory)
1226 (string-append directory "/lib/modules"))
1230 (or (scandir directory
1232 (not (string-prefix? "." basename))))
1234 ;; Note: Should usually result in one entry.
1235 (versions (delete-duplicates
1236 (append-map directory-entries
1237 module-directories))))
1240 (let ((old-path (getenv "PATH")))
1241 (setenv "PATH" #+(file-append kmod "/bin"))
1242 (make-linux-module-directory inputs version #$output)
1243 (setenv "PATH" old-path)))
1245 ;; Nothing here, maybe because this is a kernel with
1246 ;; CONFIG_MODULES=n.
1248 (_ (error "Specified Linux kernel and Linux kernel modules
1249 are not all of the same version"))))))))
1250 (gexp->derivation "linux-module-database" build
1254 `((type . profile-hook)
1255 (hook . linux-module-database))))
1257 (define (xdg-desktop-database manifest)
1258 "Return a derivation that builds the @file{mimeinfo.cache} database from
1259 desktop files. It's used to query what applications can handle a given
1261 (define desktop-file-utils ; lazy reference
1262 (module-ref (resolve-interface '(gnu packages freedesktop))
1263 'desktop-file-utils))
1265 (mlet %store-monad ((glib
1266 (manifest-lookup-package
1269 (with-imported-modules '((guix build utils)
1272 (use-modules (srfi srfi-26)
1275 (let* ((destdir (string-append #$output "/share/applications"))
1276 (appdirs (filter file-exists?
1277 (map (cut string-append <>
1278 "/share/applications")
1279 '#$(manifest-inputs manifest))))
1280 (update-desktop-database (string-append
1281 #+desktop-file-utils
1282 "/bin/update-desktop-database")))
1283 (mkdir-p (string-append #$output "/share"))
1284 (union-build destdir appdirs
1285 #:log-port (%make-void-port "w"))
1286 (exit (zero? (system* update-desktop-database destdir)))))))
1288 ;; Don't run the hook when 'glib' is not referenced.
1290 (gexp->derivation "xdg-desktop-database" build
1294 `((type . profile-hook)
1295 (hook . xdg-desktop-database)))
1298 (define (xdg-mime-database manifest)
1299 "Return a derivation that builds the @file{mime.cache} database from manifest
1300 entries. It's used to query the MIME type of a given file."
1301 (define shared-mime-info ; lazy reference
1302 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1304 (mlet %store-monad ((glib
1305 (manifest-lookup-package
1308 (with-imported-modules '((guix build utils)
1311 (use-modules (srfi srfi-26)
1314 (let* ((datadir (string-append #$output "/share"))
1315 (destdir (string-append datadir "/mime"))
1316 (pkgdirs (filter file-exists?
1317 (map (cut string-append <>
1318 "/share/mime/packages")
1319 (cons #+shared-mime-info
1320 '#$(manifest-inputs manifest)))))
1321 (update-mime-database (string-append
1323 "/bin/update-mime-database")))
1325 (union-build (string-append destdir "/packages") pkgdirs
1326 #:log-port (%make-void-port "w"))
1327 (setenv "XDG_DATA_HOME" datadir)
1328 (exit (zero? (system* update-mime-database destdir)))))))
1330 ;; Don't run the hook when there are no GLib based applications.
1332 (gexp->derivation "xdg-mime-database" build
1336 `((type . profile-hook)
1337 (hook . xdg-mime-database)))
1340 ;; Several font packages may install font files into same directory, so
1341 ;; fonts.dir and fonts.scale file should be generated here, instead of in
1343 (define (fonts-dir-file manifest)
1344 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
1345 files for the fonts of the @var{manifest} entries."
1347 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1350 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1354 (use-modules (srfi srfi-26)
1357 (let ((fonts-dirs (filter file-exists?
1358 (map (cut string-append <>
1360 '#$(manifest-inputs manifest)))))
1362 (if (null? fonts-dirs)
1364 (let* ((share-dir (string-append #$output "/share"))
1365 (fonts-dir (string-append share-dir "/fonts"))
1366 (mkfontscale (string-append #+mkfontscale
1367 "/bin/mkfontscale"))
1368 (mkfontdir (string-append #+mkfontdir
1370 (empty-file? (lambda (filename)
1371 (call-with-ascii-input-file filename
1373 (eqv? #\0 (read-char p))))))
1374 (fonts-dir-file "fonts.dir")
1375 (fonts-scale-file "fonts.scale"))
1377 ;; Create all sub-directories, because we may create fonts.dir
1378 ;; and fonts.scale files in the sub-directories.
1379 (union-build fonts-dir fonts-dirs
1380 #:log-port (%make-void-port "w")
1381 #:create-all-directories? #t)
1382 (let ((directories (find-files fonts-dir
1384 (eq? 'directory (stat:type stat)))
1385 #:directories? #t)))
1386 (for-each (lambda (dir)
1387 (with-directory-excursion dir
1388 (when (file-exists? fonts-scale-file)
1389 (delete-file fonts-scale-file))
1390 (when (file-exists? fonts-dir-file)
1391 (delete-file fonts-dir-file))
1392 (unless (and (zero? (system* mkfontscale))
1393 (zero? (system* mkfontdir)))
1395 (when (and (file-exists? fonts-scale-file)
1396 (empty-file? fonts-scale-file))
1397 (delete-file fonts-scale-file))
1398 (when (and (file-exists? fonts-dir-file)
1399 (empty-file? fonts-dir-file))
1400 (delete-file fonts-dir-file))))
1403 (gexp->derivation "fonts-dir" build
1404 #:modules '((guix build utils)
1410 `((type . profile-hook)
1411 (hook . fonts-dir))))
1413 (define (manual-database manifest)
1414 "Return a derivation that builds the manual page database (\"mandb\") for
1415 the entries in MANIFEST."
1417 (module-ref (resolve-interface '(gnu packages guile))
1421 (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
1424 (delete '(guix config)
1425 (source-module-closure `((guix build utils)
1429 (with-imported-modules modules
1430 (with-extensions (list gdbm-ffi ;for (guix man-db)
1433 (use-modules (guix man-db)
1439 (define (print-string msg)
1443 (define-syntax-rule (print fmt args ...)
1444 ;; Build up the string and display it at once.
1445 (print-string (format #f fmt args ...)))
1447 (define (compute-entry directory count total)
1448 (print "\r[~3d/~3d] building list of man-db entries..."
1450 (let ((man (string-append directory "/share/man")))
1451 (if (directory-exists? man)
1455 (define (compute-entries)
1456 ;; This is the most expensive part (I/O and CPU, due to
1457 ;; decompression), so report progress as we traverse INPUTS.
1458 ;; Cap at 4 threads because we don't see any speedup beyond that
1459 ;; on an SSD laptop.
1460 (let* ((inputs '#$(manifest-inputs manifest))
1461 (total (length inputs))
1462 (threads (min (parallel-job-count) 4)))
1464 (n-par-map threads compute-entry inputs
1466 (make-list total total)))))
1468 (define man-directory
1469 (string-append #$output "/share/man"))
1471 (mkdir-p man-directory)
1473 (format #t "Creating manual page database...~%")
1475 (let* ((start (current-time))
1476 (entries (compute-entries))
1477 (_ (write-mandb-database (string-append man-directory
1480 (duration (time-difference (current-time) start)))
1482 (format #t "~a entries processed in ~,1f s~%"
1484 (+ (time-second duration)
1485 (* (time-nanosecond duration) (expt 10 -9))))
1488 (gexp->derivation "manual-database" build
1490 ;; Work around GDBM 1.13 issue whereby uninitialized bytes
1491 ;; get written to disk:
1492 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
1493 #:env-vars `(("MALLOC_PERTURB_" . "1"))
1498 `((type . profile-hook)
1499 (hook . manual-database))))
1501 (define (texlive-configuration manifest)
1502 "Return a derivation that builds a TeXlive configuration for the entries in
1504 (define entry->texlive-input
1506 (($ <manifest-entry> name version output thing deps)
1507 (if (string-prefix? "texlive-" name)
1508 (cons (gexp-input thing output)
1509 (append-map entry->texlive-input deps))
1512 (with-imported-modules '((guix build utils)
1515 (use-modules (guix build utils)
1518 ;; Build a modifiable union of all texlive inputs. We do this so
1519 ;; that TeX live can resolve the parent and grandparent directories
1520 ;; correctly. There might be a more elegant way to accomplish this.
1521 (union-build #$output
1522 '#$(append-map entry->texlive-input
1523 (manifest-entries manifest))
1524 #:create-all-directories? #t
1525 #:log-port (%make-void-port "w"))
1526 (let ((texmf.cnf (string-append
1528 "/share/texmf-dist/web2c/texmf.cnf")))
1529 (when (file-exists? texmf.cnf)
1530 (substitute* texmf.cnf
1531 (("^TEXMFROOT = .*")
1532 (string-append "TEXMFROOT = " #$output "/share\n"))
1534 "TEXMF = $TEXMFROOT/share/texmf-dist\n"))))
1537 (with-monad %store-monad
1538 (if (any (cut string-prefix? "texlive-" <>)
1539 (map manifest-entry-name (manifest-entries manifest)))
1540 (gexp->derivation "texlive-configuration" build
1544 `((type . profile-hook)
1545 (hook . texlive-configuration)))
1548 (define %default-profile-hooks
1549 ;; This is the list of derivation-returning procedures that are called by
1550 ;; default when making a non-empty profile.
1554 ghc-package-cache-file
1555 ca-certificate-bundle
1559 texlive-configuration
1560 xdg-desktop-database
1563 (define* (profile-derivation manifest
1566 (hooks %default-profile-hooks)
1568 (allow-collisions? #f)
1569 (relative-symlinks? #f)
1571 "Return a derivation that builds a profile (aka. 'user environment') with
1572 the given MANIFEST. The profile includes additional derivations returned by
1573 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
1574 Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1575 entries in MANIFEST collide (for instance if there are two same-name packages
1576 with a different version number.)
1578 When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
1579 a dependency on the 'glibc-utf8-locales' package.
1581 When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
1582 This is one of the things to do for the result to be relocatable.
1584 When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1585 are cross-built for TARGET."
1586 (mlet* %store-monad ((system (if system
1591 (current-target-system)))
1592 (ok? (if allow-collisions?
1594 (check-for-collisions manifest system
1596 (extras (if (null? (manifest-entries manifest))
1598 (mapm/accumulate-builds (lambda (hook)
1602 (append (filter-map (lambda (drv)
1603 (and (derivation? drv)
1606 (manifest-inputs manifest)))
1608 (define glibc-utf8-locales ;lazy reference
1609 (module-ref (resolve-interface '(gnu packages base))
1610 'glibc-utf8-locales))
1612 (define set-utf8-locale
1613 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1614 ;; install a UTF-8 locale.
1617 #$(file-append glibc-utf8-locales "/lib/locale/"
1618 (version-major+minor
1619 (package-version glibc-utf8-locales))))
1620 (setlocale LC_ALL "en_US.utf8")))
1623 (with-imported-modules '((guix build profiles)
1629 (use-modules (guix build profiles)
1633 (let ((line (cond-expand (guile-2.2 'line)
1634 (else _IOLBF)))) ;Guile 2.0
1635 (setvbuf (current-output-port) line)
1636 (setvbuf (current-error-port) line))
1638 #+(if locales? set-utf8-locale #t)
1640 (define search-paths
1641 ;; Search paths of MANIFEST's packages, converted back to their
1643 (map sexp->search-path-specification
1645 '#$(map search-path-specification->sexp
1646 (manifest-search-paths manifest)))))
1648 (build-profile #$output '#$inputs
1649 #:symlink #$(if relative-symlinks?
1652 #:manifest '#$(manifest->gexp manifest)
1653 #:search-paths search-paths))))
1655 (gexp->derivation name builder
1659 ;; Don't complain about _IO* on Guile 2.2.
1660 #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
1662 ;; Not worth offloading.
1665 ;; Disable substitution because it would trigger a
1666 ;; connection to the substitute server, which is likely
1667 ;; to have no substitute to offer.
1670 #:properties `((type . profile)
1674 (manifest-entries manifest))))))))
1676 ;; Declarative profile.
1677 (define-record-type* <profile> profile make-profile
1679 (name profile-name (default "profile")) ;string
1680 (content profile-content) ;<manifest>
1681 (hooks profile-hooks ;list of procedures
1682 (default %default-profile-hooks))
1683 (locales? profile-locales? ;Boolean
1685 (allow-collisions? profile-allow-collisions? ;Boolean
1687 (relative-symlinks? profile-relative-symlinks? ;Boolean
1690 (define-gexp-compiler (profile-compiler (profile <profile>) system target)
1691 "Compile PROFILE to a derivation."
1693 (($ <profile> name manifest hooks
1694 locales? allow-collisions? relative-symlinks?)
1695 (profile-derivation manifest
1699 #:allow-collisions? allow-collisions?
1700 #:relative-symlinks? relative-symlinks?
1701 #:system system #:target target))))
1703 (define* (profile-search-paths profile
1704 #:optional (manifest (profile-manifest profile))
1705 #:key (getenv (const #f)))
1706 "Read the manifest of PROFILE and evaluate the values of search path
1707 environment variables required by PROFILE; return a list of
1708 specification/value pairs. If MANIFEST is not #f, it is assumed to be the
1709 manifest of PROFILE, which avoids rereading it.
1711 Use GETENV to determine the current settings and report only settings not
1713 (evaluate-search-paths (manifest-search-paths manifest)
1714 (list profile) getenv))
1716 (define (profile-regexp profile)
1717 "Return a regular expression that matches PROFILE's name and number."
1718 (make-regexp (string-append "^" (regexp-quote (basename profile))
1721 (define (generation-number profile)
1722 "Return PROFILE's number or 0. An absolute file name must be used."
1723 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
1724 (basename (readlink profile))))
1725 (compose string->number (cut match:substring <> 1)))
1728 (define %profile-generation-rx
1729 ;; Regexp that matches profile generation.
1730 (make-regexp "(.*)-([0-9]+)-link$"))
1732 (define (generation-profile file)
1733 "If FILE is a profile generation GC root such as \"guix-profile-42-link\",
1734 return its corresponding profile---e.g., \"guix-profile\". Otherwise return
1736 (match (regexp-exec %profile-generation-rx file)
1738 (m (let ((profile (match:substring m 1)))
1739 (and (file-exists? (string-append profile "/manifest"))
1742 (define (generation-numbers profile)
1743 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
1744 former profiles were found."
1745 (match (scandir (dirname profile)
1746 (cute regexp-exec (profile-regexp profile) <>))
1747 (#f ; no profile directory
1751 ((profiles ...) ; former profiles around
1752 (sort (map (compose string->number
1753 (cut match:substring <> 1)
1754 (cute regexp-exec (profile-regexp profile) <>))
1758 (define (profile-generations profile)
1759 "Return a list of PROFILE's generations."
1760 (let ((generations (generation-numbers profile)))
1761 (if (equal? generations '(0))
1765 (define (relative-generation-spec->number profile spec)
1766 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
1767 may be a N, -N, or +N, where N is a number. If the spec is N, then the number
1768 returned is N. If it is -N, then the number returned is the profile's current
1769 generation number minus N. If it is +N, then the number returned is the
1770 profile's current generation number plus N. Return #f if there is no such
1772 (let ((number (string->number spec)))
1774 (case (string-ref spec 0)
1776 (relative-generation profile number))
1777 (else (if (memv number (profile-generations profile))
1782 (define* (relative-generation profile shift #:optional
1783 (current (generation-number profile)))
1784 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
1785 SHIFT is a positive or negative number.
1786 Return #f if there is no such generation."
1787 (let* ((abs-shift (abs shift))
1788 (numbers (profile-generations profile))
1789 (from-current (memq current
1790 (if (negative? shift)
1794 (< abs-shift (length from-current))
1795 (list-ref from-current abs-shift))))
1797 (define* (previous-generation-number profile #:optional
1798 (number (generation-number profile)))
1799 "Return the number of the generation before generation NUMBER of
1800 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
1801 case when generations have been deleted (there are \"holes\")."
1802 (or (relative-generation profile -1 number)
1805 (define (generation-file-name profile generation)
1806 "Return the file name for PROFILE's GENERATION."
1807 (format #f "~a-~a-link" profile generation))
1809 (define (generation-time profile number)
1810 "Return the creation time of a generation in the UTC format."
1811 (make-time time-utc 0
1812 (stat:ctime (stat (generation-file-name profile number)))))
1814 (define (link-to-empty-profile store generation)
1815 "Link GENERATION, a string, to the empty profile. An error is raised if
1817 (let* ((drv (run-with-store store
1818 (profile-derivation (manifest '())
1820 (prof (derivation->output-path drv "out")))
1821 (build-derivations store (list drv))
1822 (switch-symlinks generation prof)))
1824 (define (switch-to-generation profile number)
1825 "Atomically switch PROFILE to the generation NUMBER. Return the number of
1826 the generation that was current before switching."
1827 (let ((current (generation-number profile))
1828 (generation (generation-file-name profile number)))
1829 (cond ((not (file-exists? profile))
1830 (raise (condition (&profile-not-found-error
1831 (profile profile)))))
1832 ((not (file-exists? generation))
1833 (raise (condition (&missing-generation-error
1835 (generation number)))))
1837 (switch-symlinks profile (basename generation))
1840 (define (switch-to-previous-generation profile)
1841 "Atomically switch PROFILE to the previous generation. Return the former
1842 generation number and the current one."
1843 (let ((previous (previous-generation-number profile)))
1844 (values (switch-to-generation profile previous)
1847 (define (roll-back store profile)
1848 "Roll back to the previous generation of PROFILE. Return the number of the
1849 generation that was current before switching and the new generation number."
1850 (let* ((number (generation-number profile))
1851 (previous-number (previous-generation-number profile number))
1852 (previous-generation (generation-file-name profile previous-number)))
1853 (cond ((not (file-exists? profile)) ;invalid profile
1854 (raise (condition (&profile-not-found-error
1855 (profile profile)))))
1856 ((zero? number) ;empty profile
1857 (values number number))
1858 ((or (zero? previous-number) ;going to emptiness
1859 (not (file-exists? previous-generation)))
1860 (link-to-empty-profile store previous-generation)
1861 (switch-to-previous-generation profile))
1862 (else ;anything else
1863 (switch-to-previous-generation profile)))))
1865 (define (delete-generation store profile number)
1866 "Delete generation with NUMBER from PROFILE. Return the file name of the
1867 generation that has been deleted, or #f if nothing was done (for instance
1868 because the NUMBER is zero.)"
1869 (define (delete-and-return)
1870 (let ((generation (generation-file-name profile number)))
1871 (delete-file generation)
1874 (let* ((current-number (generation-number profile))
1875 (previous-number (previous-generation-number profile number))
1876 (previous-generation (generation-file-name profile previous-number)))
1877 (cond ((zero? number) #f) ;do not delete generation 0
1878 ((and (= number current-number)
1879 (not (file-exists? previous-generation)))
1880 (link-to-empty-profile store previous-generation)
1881 (switch-to-previous-generation profile)
1882 (delete-and-return))
1883 ((= number current-number)
1884 (roll-back store profile)
1885 (delete-and-return))
1887 (delete-and-return)))))
1889 (define %user-profile-directory
1890 (and=> (getenv "HOME")
1891 (cut string-append <> "/.guix-profile")))
1893 (define %profile-directory
1894 (string-append %state-directory "/profiles/"
1895 (or (and=> (or (getenv "USER")
1898 (passwd:name (getpwuid (getuid)))))
1899 (cut string-append "per-user/" <>))
1902 (define %current-profile
1903 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
1904 ;; coexist with Nix profiles.
1905 (string-append %profile-directory "/guix-profile"))
1907 (define (ensure-profile-directory)
1908 "Attempt to create /…/profiles/per-user/$USER if needed. Nowadays this is
1909 taken care of by the daemon."
1910 (let ((s (stat %profile-directory #f)))
1911 (unless (and s (eq? 'directory (stat:type s)))
1912 (catch 'system-error
1914 (mkdir-p %profile-directory))
1916 ;; Often, we cannot create %PROFILE-DIRECTORY because its
1917 ;; parent directory is root-owned and we're running
1923 (G_ "while creating directory `~a': ~a")
1925 (strerror (system-error-errno args)))))
1928 (format #f (G_ "Please create the @file{~a} directory, \
1929 with you as the owner.")
1930 %profile-directory))))))))
1932 ;; Bail out if it's not owned by the user.
1933 (unless (or (not s) (= (stat:uid s) (getuid)))
1937 (format #f (G_ "directory `~a' is not owned by you")
1938 %profile-directory)))
1941 (format #f (G_ "Please change the owner of @file{~a} \
1943 %profile-directory (or (getenv "USER")
1947 (define (canonicalize-profile profile)
1948 "If PROFILE points to a profile in %PROFILE-DIRECTORY, return that.
1949 Otherwise return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile'
1950 as if '-p' was omitted." ; see <http://bugs.gnu.org/17939>
1951 ;; Trim trailing slashes so 'readlink' can do its job.
1952 (let ((profile (string-trim-right profile #\/)))
1953 (catch 'system-error
1955 (let ((target (readlink profile)))
1956 (if (string=? (dirname target) %profile-directory)
1961 (define %known-shorthand-profiles
1962 ;; Known shorthand forms for profiles that the user manipulates.
1963 (list (string-append (config-directory #:ensure? #f) "/current")
1964 %user-profile-directory))
1966 (define (user-friendly-profile profile)
1967 "Return either ~/.guix-profile or ~/.config/guix/current if that's what
1968 PROFILE refers to, directly or indirectly, or PROFILE."
1969 (or (find (lambda (shorthand)
1971 (let ((target (false-if-exception
1972 (readlink shorthand))))
1973 (and target (string=? target profile)))))
1974 %known-shorthand-profiles)
1977 ;;; profiles.scm ends here