1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013-2022 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, 2017, 2018, 2019, 2021, 2022 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, 2021 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>
14 ;;; Copyright © 2014 David Thompson <davet@gnu.org>
16 ;;; This file is part of GNU Guix.
18 ;;; GNU Guix is free software; you can redistribute it and/or modify it
19 ;;; under the terms of the GNU General Public License as published by
20 ;;; the Free Software Foundation; either version 3 of the License, or (at
21 ;;; your option) any later version.
23 ;;; GNU Guix is distributed in the hope that it will be useful, but
24 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;;; GNU General Public License for more details.
28 ;;; You should have received a copy of the GNU General Public License
29 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
31 (define-module (guix profiles)
32 #:use-module ((guix config) #:select (%state-directory))
33 #:use-module ((guix utils) #:hide (package-name->name+version))
34 #:use-module ((guix build utils)
35 #:select (package-name->name+version mkdir-p))
36 #:use-module ((guix diagnostics) #:select (&fix-hint formatted-message))
37 #:use-module (guix i18n)
38 #:use-module (guix records)
39 #:use-module (guix packages)
40 #:use-module (guix derivations)
41 #:use-module (guix search-paths)
42 #:use-module (guix gexp)
43 #:use-module (guix modules)
44 #:use-module (guix monads)
45 #:use-module (guix store)
46 #:use-module (ice-9 vlist)
47 #:use-module (ice-9 match)
48 #:use-module (ice-9 regex)
49 #:use-module (ice-9 ftw)
50 #:use-module (ice-9 format)
51 #:use-module (srfi srfi-1)
52 #:use-module (srfi srfi-9)
53 #:use-module (srfi srfi-11)
54 #:use-module (srfi srfi-19)
55 #:use-module (srfi srfi-26)
56 #:use-module (srfi srfi-34)
57 #:use-module (srfi srfi-35)
58 #:autoload (srfi srfi-98) (get-environment-variables)
59 #:export (&profile-error
62 &profile-not-found-error
63 profile-not-found-error?
64 &profile-collision-error
65 profile-collision-error?
66 profile-collision-error-entry
67 profile-collision-error-conflict
68 &missing-generation-error
69 missing-generation-error?
70 missing-generation-error-generation
71 &unmatched-pattern-error
72 unmatched-pattern-error?
73 unmatched-pattern-error-pattern
74 unmatched-pattern-error-manifest
76 manifest make-manifest
79 manifest-transitive-entries
81 <manifest-entry> ; FIXME: eventually make it internal
85 manifest-entry-version
88 manifest-entry-dependencies
89 manifest-entry-search-paths
91 manifest-entry-properties
99 manifest-pattern-version
100 manifest-pattern-output
102 concatenate-manifests
108 manifest-matching-entries
109 manifest-search-paths
115 manifest-transaction?
116 manifest-transaction-install
117 manifest-transaction-remove
118 manifest-transaction-install-entry
119 manifest-transaction-remove-pattern
120 manifest-transaction-null?
121 manifest-transaction-removal-candidate?
122 manifest-perform-transaction
123 manifest-transaction-effects
126 package->manifest-entry
127 package->development-manifest
129 ca-certificate-bundle
130 %default-profile-hooks
141 profile-allow-collisions?
142 profile-relative-symlinks?
148 relative-generation-spec->number
150 previous-generation-number
157 %user-profile-directory
160 ensure-profile-directory
162 user-friendly-profile
164 linux-module-database))
168 ;;; Tools to create and manipulate profiles---i.e., the representation of a
169 ;;; set of installed packages.
178 (define-condition-type &profile-error &error
180 (profile profile-error-profile))
182 (define-condition-type &profile-not-found-error &profile-error
183 profile-not-found-error?)
185 (define-condition-type &profile-collision-error &error
186 profile-collision-error?
187 (entry profile-collision-error-entry) ;<manifest-entry>
188 (conflict profile-collision-error-conflict)) ;<manifest-entry>
190 (define-condition-type &unmatched-pattern-error &error
191 unmatched-pattern-error?
192 (pattern unmatched-pattern-error-pattern) ;<manifest-pattern>
193 (manifest unmatched-pattern-error-manifest)) ;<manifest>
195 (define-condition-type &missing-generation-error &profile-error
196 missing-generation-error?
197 (generation missing-generation-error-generation))
204 (define-record-type <manifest>
207 (entries manifest-entries)) ; list of <manifest-entry>
209 ;; Convenient alias, to avoid name clashes.
210 (define make-manifest manifest)
212 (define-record-type* <manifest-entry> manifest-entry
215 (name manifest-entry-name) ; string
216 (version manifest-entry-version) ; string
217 (output manifest-entry-output ; string
219 (item manifest-entry-item) ; package | file-like | store path
220 (dependencies manifest-entry-dependencies ; <manifest-entry>*
222 (search-paths manifest-entry-search-paths ; search-path-specification*
224 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
225 (default (delay #f)))
226 (properties manifest-entry-properties ; list of symbol/value pairs
229 (define-record-type* <manifest-pattern> manifest-pattern
230 make-manifest-pattern
232 (name manifest-pattern-name) ; string
233 (version manifest-pattern-version ; string | #f
235 (output manifest-pattern-output ; string | #f
238 (define (list=? = lst1 lst2)
239 "Return true if LST1 and LST2 have the same length and their elements are
240 pairwise equal per =."
247 (and (= head1 head2) (list=? = tail1 tail2)))
251 (define (manifest-entry=? entry1 entry2)
252 "Return true if ENTRY1 is equivalent to ENTRY2, ignoring their 'properties'
255 (($ <manifest-entry> name1 version1 output1 item1 dependencies1 paths1)
257 (($ <manifest-entry> name2 version2 output2 item2 dependencies2 paths2)
258 (and (string=? name1 name2)
259 (string=? version1 version2)
260 (string=? output1 output2)
261 (equal? item1 item2) ;XXX: could be <package> vs. store item
262 (equal? paths1 paths2)
263 (list=? manifest-entry=? dependencies1 dependencies2)))))))
265 (define (manifest-transitive-entries manifest)
266 "Return the entries of MANIFEST along with their propagated inputs,
268 (let loop ((entries (manifest-entries manifest))
270 (visited vlist-null)) ;compare with 'manifest-entry=?'
275 (if (vhash-assoc head visited manifest-entry=?)
276 (loop tail result visited)
277 (loop (append (manifest-entry-dependencies head)
280 (vhash-cons head #t visited)))))))
282 (define (profile-manifest profile)
283 "Return the PROFILE's manifest."
284 (let ((file (string-append profile "/manifest")))
285 (if (file-exists? file)
286 (call-with-input-file file read-manifest)
289 (define (manifest-entry-lookup manifest)
290 "Return a lookup procedure for the entries of MANIFEST. The lookup
291 procedure takes two arguments: the entry name and output."
293 (let loop ((entries (manifest-entries manifest))
294 (mapping vlist-null))
295 (fold (lambda (entry result)
296 (vhash-cons (cons (manifest-entry-name entry)
297 (manifest-entry-output entry))
299 (loop (manifest-entry-dependencies entry)
304 (lambda (name output)
305 (match (vhash-assoc (cons name output) mapping)
309 (define* (lower-manifest-entry entry system #:key target)
310 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
312 (define (recurse entry)
313 (mapm/accumulate-builds (lambda (entry)
314 (lower-manifest-entry entry system
316 (manifest-entry-dependencies entry)))
318 (let ((item (manifest-entry-item entry)))
320 (with-monad %store-monad
322 (mlet %store-monad ((drv (lower-object item system
324 (dependencies (recurse entry))
325 (output -> (manifest-entry-output entry)))
326 (return (manifest-entry
328 (item (derivation->output-path drv output))
329 (dependencies dependencies)))))))
331 (define* (check-for-collisions manifest system #:key target)
332 "Check whether the entries of MANIFEST conflict with one another; raise a
333 '&profile-collision-error' when a conflict is encountered."
335 (manifest-entry-lookup manifest))
338 (filter-map (lambda (entry)
339 (let ((other (lookup (manifest-entry-name entry)
340 (manifest-entry-output entry))))
342 (not (eq? (manifest-entry-item entry)
343 (manifest-entry-item other)))
344 (list entry other))))
345 (manifest-transitive-entries manifest)))
350 (mlet %store-monad ((first (lower-manifest-entry first system
352 (second (lower-manifest-entry second system
354 (return (list first second))))))
356 ;; Start by lowering CANDIDATES "in parallel".
357 (mlet %store-monad ((lst (mapm/accumulate-builds lower-pair candidates)))
359 (lambda (entries result)
362 (if (string=? (manifest-entry-item first)
363 (manifest-entry-item second))
366 (&profile-collision-error
368 (conflict second))))))))
372 (define (default-properties package)
373 "Return the default properties of a manifest entry for PACKAGE."
374 ;; Preserve transformation options by default.
375 (match (assq-ref (package-properties package) 'transformations)
377 (transformations `((transformations . ,transformations)))))
379 (define* (package->manifest-entry package #:optional (output "out")
380 #:key (parent (delay #f))
381 (properties (default-properties package)))
382 "Return a manifest entry for the OUTPUT of package PACKAGE."
383 ;; For each dependency, keep a promise pointing to its "parent" entry.
384 (letrec* ((deps (map (match-lambda
386 (package->manifest-entry package
387 #:parent (delay entry)))
388 ((label package output)
389 (package->manifest-entry package output
390 #:parent (delay entry))))
391 (package-propagated-inputs package)))
392 (entry (manifest-entry
393 (name (package-name package))
394 (version (package-version package))
397 (dependencies (delete-duplicates deps))
399 (package-transitive-native-search-paths package))
401 (properties properties))))
404 (define* (package->development-manifest package
406 (system (%current-system))
408 "Return a manifest for the \"development inputs\" of PACKAGE for SYSTEM,
409 optionally when cross-compiling to TARGET. Development inputs include both
410 explicit and implicit inputs of PACKAGE."
412 (filter-map (match-lambda
413 ((label (? package? package))
414 (package->manifest-entry package))
415 ((label (? package? package) output)
416 (package->manifest-entry package output))
417 ;; TODO: Support <inferior-package>.
420 (package-development-inputs package system #:target target))))
422 (define (packages->manifest packages)
423 "Return a list of manifest entries, one for each item listed in PACKAGES.
424 Elements of PACKAGES can be either package objects or package/string tuples
425 denoting a specific output of a package."
426 (define inferiors-loaded?
427 ;; This hack allows us to provide seamless integration for inferior
428 ;; packages while not having a hard dependency on (guix inferior).
429 (resolve-module '(guix inferior) #f #f #:ensure #f))
431 (define (inferior->entry)
432 (module-ref (resolve-interface '(guix inferior))
433 'inferior-package->manifest-entry))
438 (((? package? package) output)
439 (package->manifest-entry package output))
440 ((? package? package)
441 (package->manifest-entry package))
443 (if inferiors-loaded?
444 ((inferior->entry) thing output)
445 (throw 'wrong-type-arg 'packages->manifest
446 "Wrong package object: ~S" (list thing) (list thing))))
448 (if inferiors-loaded?
449 ((inferior->entry) thing)
450 (throw 'wrong-type-arg 'packages->manifest
451 "Wrong package object: ~S" (list thing) (list thing)))))
455 (define (manifest->gexp manifest)
456 "Return a representation of MANIFEST as a gexp."
457 (define (entry->gexp entry)
459 (($ <manifest-entry> name version output (? string? path)
460 (deps ...) (search-paths ...) _ (properties ...))
461 #~(#$name #$version #$output #$path
462 (propagated-inputs #$(map entry->gexp deps))
463 (search-paths #$(map search-path-specification->sexp
465 (properties . #$properties)))
466 (($ <manifest-entry> name version output package
467 (deps ...) (search-paths ...) _ (properties ...))
468 #~(#$name #$version #$output
469 (ungexp package (or output "out"))
470 (propagated-inputs #$(map entry->gexp deps))
471 (search-paths #$(map search-path-specification->sexp
473 (properties . #$properties)))))
476 (($ <manifest> (entries ...))
477 #~(manifest (version 3)
478 (packages #$(map entry->gexp entries))))))
480 (define (find-package name version)
481 "Return a package from the distro matching NAME and possibly VERSION. This
482 procedure is here for backward-compatibility and will eventually vanish."
483 (define find-best-packages-by-name ;break abstractions
484 (module-ref (resolve-interface '(gnu packages))
485 'find-best-packages-by-name))
487 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
488 ;; former traverses the module tree only once and then allows for efficient
489 ;; access via a vhash.
490 (match (find-best-packages-by-name name version)
493 (match (find-best-packages-by-name name #f)
497 (define (sexp->manifest sexp)
498 "Parse SEXP as a manifest."
499 (define (infer-search-paths name version)
500 ;; Infer the search path specifications for NAME-VERSION by looking up a
501 ;; same-named package in the distro. Useful for the old manifest formats
502 ;; that did not store search path info.
503 (let ((package (find-package name version)))
505 (package-native-search-paths package)
508 (define (infer-dependency item parent)
509 ;; Return a <manifest-entry> for ITEM.
510 (let-values (((name version)
511 (package-name->name+version
512 (store-path-package-name item))))
519 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
521 ((name version output path
522 ('propagated-inputs deps)
523 ('search-paths search-paths)
525 ;; For each of DEPS, keep a promise pointing to ENTRY.
526 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
528 (entry (manifest-entry
534 (search-paths (map sexp->search-path-specification
537 (properties (or (assoc-ref extra-stuff 'properties)
542 (('manifest ('version 0)
543 ('packages ((name version output path) ...)))
545 (map (lambda (name version output path)
551 (search-paths (infer-search-paths name version))))
552 name version output path)))
554 ;; Version 1 adds a list of propagated inputs to the
555 ;; name/version/output/path tuples.
556 (('manifest ('version 1)
557 ('packages ((name version output path deps) ...)))
559 (map (lambda (name version output path deps)
560 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
561 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
563 (let ((deps (match deps
564 (((labels directories) ...)
568 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
570 (entry (manifest-entry
577 (infer-search-paths name version)))))
579 name version output path deps)))
581 ;; Version 2 adds search paths and is slightly more verbose.
582 (('manifest ('version 2 minor-version ...)
583 ('packages ((name version output path
584 ('propagated-inputs deps)
585 ('search-paths search-paths)
589 (map (lambda (name version output path deps search-paths)
590 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
592 (entry (manifest-entry
599 (map sexp->search-path-specification
602 name version output path deps search-paths)))
604 ;; Version 3 represents DEPS as full-blown manifest entries.
605 (('manifest ('version 3 minor-version ...)
606 ('packages (entries ...)))
607 (manifest (map sexp->manifest-entry entries)))
610 (&message (message "unsupported manifest format")))))))
612 (define (read-manifest port)
613 "Return the packages listed in MANIFEST."
614 (sexp->manifest (read port)))
616 (define (concatenate-manifests lst)
617 "Concatenate the manifests listed in LST and return the resulting manifest."
618 (manifest (append-map manifest-entries lst)))
620 (define (map-manifest-entries proc manifest)
621 "Apply PROC to all the entries of MANIFEST and return a new manifest."
623 (map proc (manifest-entries manifest))))
625 (define (entry-predicate pattern)
626 "Return a procedure that returns #t when passed a manifest entry that
627 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
630 (($ <manifest-pattern> name version output)
632 (($ <manifest-entry> entry-name entry-version entry-output)
633 (and (string=? entry-name name)
634 (or (not entry-output) (not output)
635 (string=? entry-output output))
637 (string=? entry-version version))))))))
639 (define (manifest-remove manifest patterns)
640 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
641 must be a manifest-pattern."
642 (define (remove-entry pattern lst)
643 (remove (entry-predicate pattern) lst))
645 (make-manifest (fold remove-entry
646 (manifest-entries manifest)
649 (define (manifest-add manifest entries)
650 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
651 Remove MANIFEST entries that have the same name and output as ENTRIES."
652 (define (same-entry? entry name output)
654 (($ <manifest-entry> entry-name _ entry-output _)
655 (and (equal? name entry-name)
656 (equal? output entry-output)))))
659 (fold (lambda (entry result) ;XXX: quadratic
661 (($ <manifest-entry> name _ out _)
663 (remove (cut same-entry? <> name out)
665 (manifest-entries manifest)
668 (define (manifest-lookup manifest pattern)
669 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
671 (find (entry-predicate pattern)
672 (manifest-entries manifest)))
674 (define (manifest-installed? manifest pattern)
675 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
677 (->bool (manifest-lookup manifest pattern)))
679 (define (manifest-matching-entries manifest patterns)
680 "Return all the entries of MANIFEST that match one of the PATTERNS. Raise
681 an '&unmatched-pattern-error' if none of the entries of MANIFEST matches one
683 (fold-right (lambda (pattern matches)
684 (match (filter (entry-predicate pattern)
685 (manifest-entries manifest))
688 (&unmatched-pattern-error
690 (manifest manifest)))))
692 (append lst matches))))
696 (define (manifest-search-paths manifest)
697 "Return the list of search path specifications that apply to MANIFEST,
698 including the search path specification for $PATH."
701 (append-map manifest-entry-search-paths
702 (manifest-entries manifest)))))
704 (define* (manifest->code manifest
705 #:key (entry-package-version (const "")))
706 "Return an sexp representing code to build an approximate version of
707 MANIFEST; the code is wrapped in a top-level 'begin' form. Call
708 ENTRY-PACKAGE-VERSION to determine the version number to use in the spec for a
709 given entry; it can be set to 'manifest-entry-version' for fully-specified
710 version numbers, or to some other procedure to disambiguate versions for
711 packages for which several versions are available."
712 (define (entry-transformations entry)
713 ;; Return the transformations that apply to ENTRY.
714 (assoc-ref (manifest-entry-properties entry) 'transformations))
716 (define transformation-procedures
717 ;; List of transformation options/procedure name pairs.
718 (let loop ((entries (manifest-entries manifest))
724 (match (entry-transformations entry)
726 (loop tail counter result))
728 (if (assoc-ref result options)
729 (loop tail counter result)
730 (loop tail (+ 1 counter)
733 (format #f "transform~a" counter))
736 (define (qualified-name entry)
737 ;; Return the name of ENTRY possibly with "@" followed by a version.
738 (match (entry-package-version entry)
739 ("" (manifest-entry-name entry))
740 (version (string-append (manifest-entry-name entry)
743 (if (null? transformation-procedures)
744 `(begin ;simplest case
745 (specifications->manifest
746 (list ,@(map (lambda (entry)
747 (match (manifest-entry-output entry)
748 ("out" (qualified-name entry))
749 (output (string-append (qualified-name entry)
751 (manifest-entries manifest)))))
752 (let* ((transform (lambda (options exp)
755 (let ((proc (assoc-ref transformation-procedures
758 `(begin ;transformations apply
759 (use-modules (guix transformations))
764 (options->transformation ',options))))
765 transformation-procedures)
768 (list ,@(map (lambda (entry)
770 (entry-transformations entry))
773 (qualified-name entry))
775 (match (manifest-entry-output entry)
778 `(specification->package ,name)))
782 `(specification->package ,name))
784 (manifest-entries manifest))))))))
788 ;;; Manifest transactions.
791 (define-record-type* <manifest-transaction> manifest-transaction
792 make-manifest-transaction
793 manifest-transaction?
794 (install manifest-transaction-install ; list of <manifest-entry>
796 (remove manifest-transaction-remove ; list of <manifest-pattern>
799 (define (manifest-transaction-install-entry entry transaction)
800 "Augment TRANSACTION's set of installed packages with ENTRY, a
802 (manifest-transaction
803 (inherit transaction)
805 (cons entry (manifest-transaction-install transaction)))))
807 (define (manifest-transaction-remove-pattern pattern transaction)
808 "Add PATTERN to TRANSACTION's list of packages to remove."
809 (manifest-transaction
810 (inherit transaction)
812 (cons pattern (manifest-transaction-remove transaction)))))
814 (define (manifest-transaction-null? transaction)
815 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
818 (($ <manifest-transaction> () ()) #t)
819 (($ <manifest-transaction> _ _) #f)))
821 (define (manifest-transaction-removal-candidate? entry transaction)
822 "Return true if ENTRY is a candidate for removal in TRANSACTION."
823 (any (lambda (pattern)
824 ((entry-predicate pattern) entry))
825 (manifest-transaction-remove transaction)))
827 (define (manifest-transaction-effects manifest transaction)
828 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
829 the list of packages that would be removed, installed, upgraded, or downgraded
830 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
831 where the head is the entry being upgraded and the tail is the entry that will
833 (define (manifest-entry->pattern entry)
835 (name (manifest-entry-name entry))
836 (output (manifest-entry-output entry))))
837 (define manifest-entry-pair=?
839 (((m1a . m2a) (m1b . m2b))
840 (and (manifest-entry=? m1a m1b)
841 (manifest-entry=? m2a m2b)))
844 (let loop ((input (manifest-transaction-install transaction))
850 (let ((remove (manifest-transaction-remove transaction)))
851 (values (delete-duplicates
852 (manifest-matching-entries manifest remove)
854 (delete-duplicates (reverse install) manifest-entry=?)
857 manifest-entry-pair=?)
860 manifest-entry-pair=?))))
862 ;; Check whether installing ENTRY corresponds to the installation of a
863 ;; new package or to an upgrade.
865 ;; XXX: When the exact same output directory is installed, we're not
866 ;; really upgrading anything. Add a check for that case.
867 (let* ((pattern (manifest-entry->pattern entry))
868 (previous (manifest-lookup manifest pattern))
869 (newer? (and previous
870 (version>=? (manifest-entry-version entry)
871 (manifest-entry-version previous)))))
873 (if previous install (cons entry install))
874 (if (and previous newer?)
875 (alist-cons previous entry upgrade)
877 (if (and previous (not newer?))
878 (alist-cons previous entry downgrade)
881 (define (manifest-perform-transaction manifest transaction)
882 "Perform TRANSACTION on MANIFEST and return the new manifest."
883 (let ((install (manifest-transaction-install transaction))
884 (remove (manifest-transaction-remove transaction)))
885 (manifest-add (manifest-remove manifest remove)
893 (define (manifest-inputs manifest)
894 "Return a list of <gexp-input> objects for MANIFEST."
897 (($ <manifest-entry> name version output thing deps)
898 ;; THING may be a package or a file name. In the latter case, assume
899 ;; it's already valid.
900 (cons (gexp-input thing output)
901 (append-map entry->input deps)))))
903 (append-map entry->input (manifest-entries manifest)))
905 (define* (manifest-lookup-package manifest name #:optional version)
906 "Return as a monadic value the first package or store path referenced by
907 MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
909 ;; Return as a monadic value the package or store path referenced by the
910 ;; manifest ENTRY, or #f if not referenced.
911 (define (entry-lookup-package entry)
912 (define (find-among-inputs inputs)
913 (find (lambda (input)
914 (and (package? input)
915 (equal? name (package-name input))
917 (string-prefix? version (package-version input))
920 (define (find-among-store-items items)
922 (let-values (((name* version*)
923 (package-name->name+version
924 (store-path-package-name item))))
925 (and (string=? name name*)
927 (string-prefix? version version*)
931 (with-monad %store-monad
932 (match (manifest-entry-item entry)
933 ((? package? package)
934 (match (cons (list (package-name package) package)
935 (package-transitive-inputs package))
936 (((labels inputs . _) ...)
937 (return (find-among-inputs inputs)))))
939 (mlet %store-monad ((refs (references* item)))
940 (return (find-among-store-items refs))))
942 ;; XXX: ITEM might be a 'computed-file' or anything like that, in
943 ;; which case we don't know what to do. The fix may be to check
944 ;; references once ITEM is compiled, as proposed at
945 ;; <https://bugs.gnu.org/29927>.
949 entry-lookup-package (manifest-entries manifest)))
951 (define (info-dir-file manifest)
952 "Return a derivation that builds the 'dir' file for all the entries of
954 (define texinfo ;lazy reference
955 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
956 (define gzip ;lazy reference
957 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
958 (define glibc-utf8-locales ;lazy reference
959 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
962 (with-imported-modules '((guix build utils))
964 (use-modules (guix build utils)
965 (srfi srfi-1) (srfi srfi-26)
968 (define (info-file? file)
969 (or (string-suffix? ".info" file)
970 (string-suffix? ".info.gz" file)))
972 (define (info-files top)
973 (let ((infodir (string-append top "/share/info")))
974 (map (cut string-append infodir "/" <>)
975 (or (scandir infodir info-file?) '()))))
977 (define (info-file-language file)
978 (let* ((base (if (string-suffix? ".gz" file)
979 (basename file ".info.gz")
980 (basename file ".info")))
981 (dot (string-rindex base #\.)))
983 (string-drop base (+ 1 dot))
986 (define (install-info info)
987 (let ((language (info-file-language info)))
988 ;; We need to choose a valid locale for $LANGUAGE to be honored.
989 (setenv "LC_ALL" "en_US.utf8")
990 (setenv "LANGUAGE" language)
992 (system* #+(file-append texinfo "/bin/install-info")
994 (apply string-append #$output "/share/info/dir"
995 (if (string=? "en" language)
997 `("." ,language)))))))
999 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
1000 (setenv "GUIX_LOCPATH"
1001 #+(file-append glibc-utf8-locales "/lib/locale"))
1003 (mkdir-p (string-append #$output "/share/info"))
1004 (exit (every install-info
1005 (append-map info-files
1006 '#$(manifest-inputs manifest)))))))
1008 (gexp->derivation "info-dir" build
1012 `((type . profile-hook)
1013 (hook . info-dir))))
1015 (define (ghc-package-cache-file manifest)
1016 "Return a derivation that builds the GHC 'package.cache' file for all the
1017 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
1018 (define ghc ;lazy reference
1019 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
1022 (with-imported-modules '((guix build utils))
1024 (use-modules (guix build utils)
1025 (srfi srfi-1) (srfi srfi-26)
1028 (define ghc-name-version
1029 (let* ((base (basename #+ghc)))
1031 (+ 1 (string-index base #\-)))))
1034 (string-append "lib/" ghc-name-version "/package.conf.d"))
1037 (string-append #$output "/" db-subdir))
1039 (define (conf-files top)
1040 (let ((db (string-append top "/" db-subdir)))
1041 (if (file-exists? db)
1042 (find-files db "\\.conf$")
1045 (define (copy-conf-file conf)
1046 (let ((base (basename conf)))
1047 (copy-file conf (string-append db-dir "/" base))))
1049 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
1050 (for-each copy-conf-file
1051 (append-map conf-files
1053 '#$(manifest-inputs manifest))))
1056 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
1057 (string-append "--package-db=" db-dir)))))
1058 (for-each delete-file (find-files db-dir "\\.conf$"))
1061 (with-monad %store-monad
1062 ;; Don't depend on GHC when there's nothing to do.
1063 (if (any (cut string-prefix? "ghc" <>)
1064 (map manifest-entry-name (manifest-entries manifest)))
1065 (gexp->derivation "ghc-package-cache" build
1069 `((type . profile-hook)
1070 (hook . ghc-package-cache)))
1073 (define (ca-certificate-bundle manifest)
1074 "Return a derivation that builds a single-file bundle containing the CA
1075 certificates in the /etc/ssl/certs sub-directories of the packages in
1076 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
1077 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
1078 ;; for a discussion.
1080 (define glibc-utf8-locales ;lazy reference
1081 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
1084 (with-imported-modules '((guix build utils))
1086 (use-modules (guix build utils)
1093 (define (pem-file? file)
1094 (string-suffix? ".pem" file))
1096 (define (ca-files top)
1097 (let ((cert-dir (string-append top "/etc/ssl/certs")))
1098 (map (cut string-append cert-dir "/" <>)
1099 (or (scandir cert-dir pem-file?) '()))))
1101 (define (concatenate-files files result)
1102 "Make RESULT the concatenation of all of FILES."
1103 (define (dump file port)
1104 (display (call-with-input-file file get-string-all)
1106 (newline port)) ;required, see <https://bugs.debian.org/635570>
1108 (call-with-output-file result
1110 (for-each (cut dump <> port) files))))
1112 ;; Some file names in the NSS certificates are UTF-8 encoded so
1113 ;; install a UTF-8 locale.
1115 (string-append #+glibc-utf8-locales "/lib/locale/"
1116 #+(version-major+minor
1117 (package-version glibc-utf8-locales))))
1118 (setlocale LC_ALL "en_US.utf8")
1120 (match (append-map ca-files '#$(manifest-inputs manifest))
1122 ;; Since there are no CA files, just create an empty directory. Do
1123 ;; not create the etc/ssl/certs sub-directory, since that would
1124 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
1129 (let ((result (string-append #$output "/etc/ssl/certs")))
1131 (concatenate-files ca-files
1132 (string-append result
1133 "/ca-certificates.crt"))
1136 (gexp->derivation "ca-certificate-bundle" build
1140 `((type . profile-hook)
1141 (hook . ca-certificate-bundle))))
1143 (define (emacs-subdirs manifest)
1145 (with-imported-modules (source-module-closure
1146 '((guix build profiles)
1147 (guix build utils)))
1149 (use-modules (guix build utils)
1150 (guix build profiles)
1151 (ice-9 ftw) ; scandir
1152 (srfi srfi-1) ; append-map
1155 (let ((destdir (string-append #$output "/share/emacs/site-lisp"))
1161 (map (cute string-append dir "/" <>)
1162 (scandir dir (negate (cute member <> '("." "..")))))))
1163 (filter file-exists?
1164 (map (cute string-append <> "/share/emacs/site-lisp")
1165 '#$(manifest-inputs manifest))))))
1167 (with-directory-excursion destdir
1168 (call-with-output-file "subdirs.el"
1171 `(normal-top-level-add-to-load-path
1172 (list ,@(delete-duplicates subdirs)))
1176 (gexp->derivation "emacs-subdirs" build
1180 `((type . profile-hook)
1181 (hook . emacs-subdirs))))
1183 (define (gdk-pixbuf-loaders-cache-file manifest)
1184 "Return a derivation that produces a loaders cache file for every gdk-pixbuf
1185 loaders discovered in MANIFEST."
1186 (define gdk-pixbuf ;lazy reference
1187 (module-ref (resolve-interface '(gnu packages gtk)) 'gdk-pixbuf))
1190 ((gdk-pixbuf (manifest-lookup-package manifest "gdk-pixbuf"))
1191 (librsvg (manifest-lookup-package manifest "librsvg"))
1192 (gdk-pixbuf-bin -> (if (string? gdk-pixbuf)
1193 (string-append gdk-pixbuf "/bin")
1194 (file-append gdk-pixbuf "/bin"))))
1197 (with-imported-modules (source-module-closure
1198 '((guix build glib-or-gtk-build-system)))
1200 (use-modules (guix build glib-or-gtk-build-system))
1201 (setenv "PATH" (string-append #$gdk-pixbuf-bin ":" (getenv "PATH")))
1203 (generate-gdk-pixbuf-loaders-cache
1204 ;; XXX: MANIFEST-LOOKUP-PACKAGE transitively searches through
1205 ;; every input referenced by the manifest, while MANIFEST-INPUTS
1206 ;; only retrieves the immediate inputs as well as their
1207 ;; propagated inputs; to avoid causing an empty output derivation
1208 ;; we must ensure that the inputs contain at least one
1209 ;; loaders.cache file. This is why we include gdk-pixbuf or
1210 ;; librsvg when they are transitively found.
1211 (list #$@(if gdk-pixbuf
1217 #$@(manifest-inputs manifest))
1221 (gexp->derivation "gdk-pixbuf-loaders-cache-file" build
1225 '((type . profile-hook)
1226 (hook . gdk-pixbuf-loaders-cache-file)))
1229 (define (glib-schemas manifest)
1230 "Return a derivation that unions all schemas from manifest entries and
1231 creates the Glib 'gschemas.compiled' file."
1232 (define glib ; lazy reference
1233 (module-ref (resolve-interface '(gnu packages glib)) 'glib))
1235 (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
1236 ;; XXX: Can't use glib-compile-schemas corresponding
1237 ;; to the glib referenced by 'manifest'. Because
1238 ;; '%glib' can be either a package or store path, and
1239 ;; there's no way to get the "bin" output for the later.
1240 (glib-compile-schemas
1241 -> #~(string-append #+glib:bin
1242 "/bin/glib-compile-schemas")))
1245 (with-imported-modules '((guix build utils)
1247 (guix build profiles)
1251 (use-modules (guix build utils)
1253 (guix build profiles)
1256 (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
1257 (schemadirs (filter file-exists?
1258 (map (cut string-append <> "/share/glib-2.0/schemas")
1259 '#$(manifest-inputs manifest)))))
1261 ;; Union all the schemas.
1262 (mkdir-p (string-append #$output "/share/glib-2.0"))
1263 (union-build destdir schemadirs
1264 #:log-port (%make-void-port "w"))
1266 (let ((dir destdir))
1267 (when (file-is-directory? dir)
1268 (ensure-writable-directory dir)
1269 (invoke #+glib-compile-schemas
1270 (string-append "--targetdir=" dir)
1273 ;; Don't run the hook when there's nothing to do.
1275 (gexp->derivation "glib-schemas" build
1279 `((type . profile-hook)
1280 (hook . glib-schemas)))
1283 (define (gtk-icon-themes manifest)
1284 "Return a derivation that unions all icon themes from manifest entries and
1285 creates the GTK+ 'icon-theme.cache' file for each theme."
1286 (define gtk+ ; lazy reference
1287 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
1289 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
1290 ;; XXX: Can't use gtk-update-icon-cache corresponding
1291 ;; to the gtk+ referenced by 'manifest'. Because
1292 ;; '%gtk+' can be either a package or store path, and
1293 ;; there's no way to get the "bin" output for the later.
1294 (gtk-update-icon-cache
1295 -> #~(string-append #+gtk+:bin
1296 "/bin/gtk-update-icon-cache")))
1299 (with-imported-modules '((guix build utils)
1301 (guix build profiles)
1305 (use-modules (guix build utils)
1307 (guix build profiles)
1311 (let* ((destdir (string-append #$output "/share/icons"))
1312 (icondirs (filter file-exists?
1313 (map (cut string-append <> "/share/icons")
1314 '#$(manifest-inputs manifest)))))
1316 ;; Union all the icons.
1317 (mkdir-p (string-append #$output "/share"))
1318 (union-build destdir icondirs
1319 #:log-port (%make-void-port "w"))
1321 ;; Update the 'icon-theme.cache' file for each icon theme.
1324 (let ((dir (string-append destdir "/" theme)))
1325 ;; Occasionally DESTDIR contains plain files, such as
1326 ;; "abiword_48.png". Ignore these.
1327 (when (file-is-directory? dir)
1328 (ensure-writable-directory dir)
1329 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
1330 (scandir destdir (negate (cut member <> '("." "..")))))))))
1332 ;; Don't run the hook when there's nothing to do.
1334 (gexp->derivation "gtk-icon-themes" build
1338 `((type . profile-hook)
1339 (hook . gtk-icon-themes)))
1342 (define (gtk-im-modules manifest)
1343 "Return a derivation that builds the cache files for input method modules
1344 for both major versions of GTK+."
1346 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
1347 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
1349 (define (build gtk gtk-version query)
1350 (let ((major (string-take gtk-version 1)))
1351 (with-imported-modules '((guix build utils)
1353 (guix build profiles)
1357 (use-modules (guix build utils)
1359 (guix build profiles)
1364 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
1366 (destdir (string-append #$output prefix))
1367 (moddirs (cons (string-append #$gtk prefix "/immodules")
1368 (filter file-exists?
1369 (map (cut string-append <> prefix "/immodules")
1370 '#$(manifest-inputs manifest)))))
1371 (modules (append-map (cut find-files <> "\\.so$")
1374 ;; Generate a new immodules cache file.
1375 (mkdir-p (string-append #$output prefix))
1376 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
1377 (outfile (string-append #$output prefix
1378 "/immodules-gtk" #$major ".cache")))
1382 (call-with-output-file outfile
1384 (while (not (eof-object? (peek-char pipe)))
1385 (write-char (read-char pipe) out))))
1388 (close-pipe pipe)))))))))
1390 ;; Don't run the hook when there's nothing to do.
1391 (let* ((pkg-gtk+ (module-ref ; lazy reference
1392 (resolve-interface '(gnu packages gtk)) 'gtk+))
1393 (pkg-gtk+2 (module-ref ; lazy reference
1394 (resolve-interface '(gnu packages gtk)) 'gtk+-2))
1399 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
1400 ;; output of latest gtk+ package.
1402 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
1408 #$pkg-gtk+2:bin "/bin/gtk-query-immodules-2.0"))
1410 (if (or gtk+ gtk+-2)
1411 (gexp->derivation "gtk-im-modules" gexp
1415 `((type . profile-hook)
1416 (hook . gtk-im-modules)))
1419 (define (linux-module-database manifest)
1420 "Return a derivation that unites all the kernel modules of the manifest
1421 and creates the dependency graph of all these kernel modules.
1423 This is meant to be used as a profile hook."
1424 (define kmod ; lazy reference
1425 (module-ref (resolve-interface '(gnu packages linux)) 'kmod))
1428 (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
1431 (with-imported-modules (source-module-closure
1432 '((guix build utils)
1433 (gnu build linux-modules)))
1434 (with-extensions (list guile-zlib)
1436 (use-modules (ice-9 ftw)
1438 (srfi srfi-1) ; append-map
1439 (gnu build linux-modules))
1441 (let* ((inputs '#$(manifest-inputs manifest))
1443 (map (lambda (directory)
1444 (string-append directory "/lib/modules"))
1448 (or (scandir directory
1450 (not (string-prefix? "." basename))))
1452 ;; Note: Should usually result in one entry.
1453 (versions (delete-duplicates
1454 (append-map directory-entries
1455 module-directories))))
1458 (let ((old-path (getenv "PATH")))
1459 (setenv "PATH" #+(file-append kmod "/bin"))
1460 (make-linux-module-directory inputs version #$output)
1461 (setenv "PATH" old-path)))
1463 ;; Nothing here, maybe because this is a kernel with
1464 ;; CONFIG_MODULES=n.
1466 (_ (error "Specified Linux kernel and Linux kernel modules
1467 are not all of the same version"))))))))
1468 (gexp->derivation "linux-module-database" build
1472 `((type . profile-hook)
1473 (hook . linux-module-database))))
1475 (define (xdg-desktop-database manifest)
1476 "Return a derivation that builds the @file{mimeinfo.cache} database from
1477 desktop files. It's used to query what applications can handle a given
1479 (define desktop-file-utils ; lazy reference
1480 (module-ref (resolve-interface '(gnu packages freedesktop))
1481 'desktop-file-utils))
1483 (mlet %store-monad ((glib
1484 (manifest-lookup-package
1487 (with-imported-modules '((guix build utils)
1490 (use-modules (srfi srfi-26)
1493 (let* ((destdir (string-append #$output "/share/applications"))
1494 (appdirs (filter file-exists?
1495 (map (cut string-append <>
1496 "/share/applications")
1497 '#$(manifest-inputs manifest))))
1498 (update-desktop-database (string-append
1499 #+desktop-file-utils
1500 "/bin/update-desktop-database")))
1501 (mkdir-p (string-append #$output "/share"))
1502 (union-build destdir appdirs
1503 #:log-port (%make-void-port "w"))
1504 (exit (zero? (system* update-desktop-database destdir)))))))
1506 ;; Don't run the hook when 'glib' is not referenced.
1508 (gexp->derivation "xdg-desktop-database" build
1512 `((type . profile-hook)
1513 (hook . xdg-desktop-database)))
1516 (define (xdg-mime-database manifest)
1517 "Return a derivation that builds the @file{mime.cache} database from manifest
1518 entries. It's used to query the MIME type of a given file."
1519 (define shared-mime-info ; lazy reference
1520 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1522 (mlet %store-monad ((glib (manifest-lookup-package manifest "glib")))
1524 (with-imported-modules '((guix build utils)
1527 (use-modules (guix build utils)
1532 (let* ((datadir (string-append #$output "/share"))
1533 (destdir (string-append datadir "/mime"))
1534 (pkgdirs (filter file-exists?
1535 (map (cut string-append <>
1536 "/share/mime/packages")
1537 (cons #+shared-mime-info
1538 '#$(manifest-inputs manifest))))))
1542 ;; PKGDIRS contains nothing but 'shared-mime-info', which
1543 ;; already contains its database, so nothing to do.
1545 (symlink #$(file-append shared-mime-info "/share/mime")
1548 ;; PKGDIRS contains additional packages providing
1549 ;; 'share/mime/packages' (very few packages do so) so rebuild
1550 ;; the database. TODO: Find a way to avoid reprocessing
1551 ;; 'shared-mime-info', which is the most expensive one.
1553 (union-build (string-append destdir "/packages") pkgdirs
1554 #:log-port (%make-void-port "w"))
1555 (setenv "XDG_DATA_HOME" datadir)
1556 (invoke #+(file-append shared-mime-info
1557 "/bin/update-mime-database")
1560 ;; Don't run the hook when there are no GLib based applications.
1562 (gexp->derivation "xdg-mime-database" build
1566 `((type . profile-hook)
1567 (hook . xdg-mime-database)))
1570 ;; Several font packages may install font files into same directory, so
1571 ;; fonts.dir and fonts.scale file should be generated here, instead of in
1573 (define (fonts-dir-file manifest)
1574 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
1575 files for the fonts of the @var{manifest} entries."
1577 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1580 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1584 (use-modules (srfi srfi-26)
1587 (let ((fonts-dirs (filter file-exists?
1588 (map (cut string-append <>
1590 '#$(manifest-inputs manifest)))))
1592 (if (null? fonts-dirs)
1594 (let* ((share-dir (string-append #$output "/share"))
1595 (fonts-dir (string-append share-dir "/fonts"))
1596 (mkfontscale (string-append #+mkfontscale
1597 "/bin/mkfontscale"))
1598 (mkfontdir (string-append #+mkfontdir
1600 (empty-file? (lambda (filename)
1601 (call-with-ascii-input-file filename
1603 (eqv? #\0 (read-char p))))))
1604 (fonts-dir-file "fonts.dir")
1605 (fonts-scale-file "fonts.scale"))
1607 ;; Create all sub-directories, because we may create fonts.dir
1608 ;; and fonts.scale files in the sub-directories.
1609 (union-build fonts-dir fonts-dirs
1610 #:log-port (%make-void-port "w")
1611 #:create-all-directories? #t)
1612 (let ((directories (find-files fonts-dir
1614 (eq? 'directory (stat:type stat)))
1615 #:directories? #t)))
1616 (for-each (lambda (dir)
1617 (with-directory-excursion dir
1618 (when (file-exists? fonts-scale-file)
1619 (delete-file fonts-scale-file))
1620 (when (file-exists? fonts-dir-file)
1621 (delete-file fonts-dir-file))
1622 (unless (and (zero? (system* mkfontscale))
1623 (zero? (system* mkfontdir)))
1625 (when (and (file-exists? fonts-scale-file)
1626 (empty-file? fonts-scale-file))
1627 (delete-file fonts-scale-file))
1628 (when (and (file-exists? fonts-dir-file)
1629 (empty-file? fonts-dir-file))
1630 (delete-file fonts-dir-file))))
1633 (gexp->derivation "fonts-dir" build
1634 #:modules '((guix build utils)
1640 `((type . profile-hook)
1641 (hook . fonts-dir))))
1643 (define (manual-database manifest)
1644 "Return a derivation that builds the manual page database (\"mandb\") for
1645 the entries in MANIFEST."
1647 (module-ref (resolve-interface '(gnu packages guile))
1651 (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
1654 (delete '(guix config)
1655 (source-module-closure `((guix build utils)
1659 (with-imported-modules modules
1660 (with-extensions (list gdbm-ffi ;for (guix man-db)
1663 (use-modules (guix man-db)
1669 (define (print-string msg)
1673 (define-syntax-rule (print fmt args ...)
1674 ;; Build up the string and display it at once.
1675 (print-string (format #f fmt args ...)))
1677 (define (compute-entry directory count total)
1678 (print "\r[~3d/~3d] building list of man-db entries..."
1680 (let ((man (string-append directory "/share/man")))
1681 (if (directory-exists? man)
1685 (define (compute-entries)
1686 ;; This is the most expensive part (I/O and CPU, due to
1687 ;; decompression), so report progress as we traverse INPUTS.
1688 ;; Cap at 4 threads because we don't see any speedup beyond that
1689 ;; on an SSD laptop.
1690 (let* ((inputs '#$(manifest-inputs manifest))
1691 (total (length inputs))
1692 (threads (min (parallel-job-count) 4)))
1694 (n-par-map threads compute-entry inputs
1696 (make-list total total)))))
1698 (define man-directory
1699 (string-append #$output "/share/man"))
1701 (mkdir-p man-directory)
1703 (format #t "Creating manual page database...~%")
1705 (let* ((start (current-time))
1706 (entries (compute-entries))
1707 (_ (write-mandb-database (string-append man-directory
1710 (duration (time-difference (current-time) start)))
1712 (format #t "~a entries processed in ~,1f s~%"
1714 (+ (time-second duration)
1715 (* (time-nanosecond duration) (expt 10 -9))))
1718 (gexp->derivation "manual-database" build
1720 ;; Work around GDBM 1.13 issue whereby uninitialized bytes
1721 ;; get written to disk:
1722 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
1723 #:env-vars `(("MALLOC_PERTURB_" . "1"))
1728 `((type . profile-hook)
1729 (hook . manual-database))))
1731 (define (manual-database/optional manifest)
1732 "Return a derivation to build the manual database of MANIFEST, but only if
1733 MANIFEST contains the \"man-db\" package. Otherwise, return #f."
1734 ;; Building the man database (for "man -k") is expensive and rarely used.
1735 ;; Build it only if the profile also contains "man-db".
1736 (mlet %store-monad ((man-db (manifest-lookup-package manifest "man-db")))
1738 (manual-database manifest)
1741 (define (texlive-font-maps manifest)
1742 "Return a derivation that builds the TeX Live font maps for the entries in
1744 (define entry->texlive-input
1746 (($ <manifest-entry> name version output thing deps)
1747 (if (string-prefix? "texlive-" name)
1748 (cons (gexp-input thing output)
1749 (append-map entry->texlive-input deps))
1752 (module-ref (resolve-interface '(gnu packages tex)) 'texlive-bin))
1754 (module-ref (resolve-interface '(gnu packages base)) 'coreutils))
1756 (module-ref (resolve-interface '(gnu packages base)) 'grep))
1758 (module-ref (resolve-interface '(gnu packages base)) 'sed))
1760 (module-ref (resolve-interface '(gnu packages tex))
1761 'texlive-default-updmap.cfg))
1763 (with-imported-modules '((guix build utils)
1766 (use-modules (guix build utils)
1770 ;; Build a modifiable union of all texlive inputs. We do this so
1771 ;; that TeX live can resolve the parent and grandparent directories
1772 ;; correctly. There might be a more elegant way to accomplish this.
1773 (union-build "/tmp/texlive"
1774 '#$(append-map entry->texlive-input
1775 (manifest-entries manifest))
1776 #:create-all-directories? #t
1777 #:log-port (%make-void-port "w"))
1779 ;; XXX: This is annoying, but it's necessary because texlive-bin
1780 ;; does not provide wrapped executables.
1782 (string-append #$(file-append coreutils "/bin")
1784 #$(file-append grep "/bin")
1786 #$(file-append sed "/bin")))
1787 (setenv "PERL5LIB" #$(file-append texlive-bin "/share/tlpkg"))
1788 (setenv "GUIX_TEXMF" "/tmp/texlive/share/texmf-dist")
1790 ;; Remove invalid maps from config file.
1791 (let* ((web2c (string-append #$output "/share/texmf-dist/web2c/"))
1792 (maproot (string-append #$output "/share/texmf-dist/fonts/map/"))
1793 (updmap.cfg (string-append web2c "updmap.cfg")))
1795 (copy-file #$updmap.cfg updmap.cfg)
1796 (make-file-writable updmap.cfg)
1797 (let* ((port (open-pipe* OPEN_WRITE
1798 #$(file-append texlive-bin "/bin/updmap-sys")
1802 (string-append "--cnffile=" updmap.cfg))))
1803 (display "Y\n" port)
1804 (when (not (zero? (status:exit-val (close-pipe port))))
1805 (error "failed to filter updmap.cfg")))
1807 ;; Generate font maps.
1808 (invoke #$(file-append texlive-bin "/bin/updmap-sys")
1809 (string-append "--cnffile=" updmap.cfg)
1810 (string-append "--dvipdfmxoutputdir="
1811 maproot "dvipdfmx/updmap")
1812 (string-append "--dvipsoutputdir="
1813 maproot "dvips/updmap")
1814 (string-append "--pdftexoutputdir="
1815 maproot "pdftex/updmap"))
1817 ;; Create ls-R file. I know, that's not *just* for font maps, but
1818 ;; we've generated new files, so there's no point in running it
1819 ;; any earlier. The ls-R file must act on a full TeX Live tree,
1820 ;; but we have two: the one in /tmp containing all packages and
1821 ;; the one in #$output containing the generated font maps. To
1822 ;; avoid having to merge ls-R files, we copy the generated stuff
1823 ;; to /tmp and run mktexlsr only once.
1824 (let ((a (string-append #$output "/share/texmf-dist"))
1825 (b "/tmp/texlive/share/texmf-dist")
1826 (mktexlsr #$(file-append texlive-bin "/bin/mktexlsr")))
1827 (copy-recursively a b)
1829 (install-file (string-append b "/ls-R") a))))))
1831 (mlet %store-monad ((texlive-base (manifest-lookup-package manifest "texlive-base")))
1833 (gexp->derivation "texlive-font-maps" build
1837 `((type . profile-hook)
1838 (hook . texlive-font-maps)))
1841 (define %default-profile-hooks
1842 ;; This is the list of derivation-returning procedures that are called by
1843 ;; default when making a non-empty profile.
1845 manual-database/optional
1847 ghc-package-cache-file
1848 ca-certificate-bundle
1850 gdk-pixbuf-loaders-cache-file
1855 xdg-desktop-database
1858 (define* (profile-derivation manifest
1861 (hooks %default-profile-hooks)
1863 (allow-unsupported-packages? #f)
1864 (allow-collisions? #f)
1865 (relative-symlinks? #f)
1867 "Return a derivation that builds a profile (aka. 'user environment') with
1868 the given MANIFEST. The profile includes additional derivations returned by
1869 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
1870 Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1871 entries in MANIFEST collide (for instance if there are two same-name packages
1872 with a different version number.) Unless ALLOW-UNSUPPORTED-PACKAGES? is true
1873 or TARGET is set, raise an error if MANIFEST contains a package that does not
1876 When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
1877 a dependency on the 'glibc-utf8-locales' package.
1879 When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
1880 This is one of the things to do for the result to be relocatable.
1882 When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1883 are cross-built for TARGET."
1884 (define (check-supported-packages system)
1885 ;; Raise an error if a package in MANIFEST does not support SYSTEM.
1886 (map-manifest-entries
1889 (match (manifest-entry-item entry)
1890 ((? package? package)
1891 (unless (supported-package? package system)
1892 (raise (formatted-message (G_ "package ~a does not support ~a")
1893 (package-full-name package) system))))
1897 (mlet* %store-monad ((system (if system
1902 (current-target-system)))
1903 (ok? -> (or allow-unsupported-packages? target
1904 (check-supported-packages system)))
1905 (ok? (if allow-collisions?
1907 (check-for-collisions manifest system
1909 (extras (if (null? (manifest-entries manifest))
1911 (mapm/accumulate-builds (lambda (hook)
1914 (define extra-inputs
1915 (filter-map (lambda (drv)
1916 (and (derivation? drv) (gexp-input drv)))
1919 (define glibc-utf8-locales ;lazy reference
1920 (module-ref (resolve-interface '(gnu packages base))
1921 'glibc-utf8-locales))
1923 (define set-utf8-locale
1924 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1925 ;; install a UTF-8 locale.
1928 #$(file-append glibc-utf8-locales "/lib/locale/"
1929 (version-major+minor
1930 (package-version glibc-utf8-locales))))
1931 (setlocale LC_ALL "en_US.utf8")))
1934 (with-imported-modules '((guix build profiles)
1940 (use-modules (guix build profiles)
1944 (let ((line (cond-expand (guile-2.2 'line)
1945 (else _IOLBF)))) ;Guile 2.0
1946 (setvbuf (current-output-port) line)
1947 (setvbuf (current-error-port) line))
1949 #+(if locales? set-utf8-locale #t)
1951 (build-profile #$output '#$(manifest->gexp manifest)
1952 #:extra-inputs '#$extra-inputs
1953 #:symlink #$(if relative-symlinks?
1957 (gexp->derivation name builder
1961 ;; Don't complain about _IO* on Guile 2.2.
1962 #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
1964 ;; Not worth offloading.
1967 ;; Disable substitution because it would trigger a
1968 ;; connection to the substitute server, which is likely
1969 ;; to have no substitute to offer.
1972 #:properties `((type . profile)
1976 (manifest-entries manifest))))))))
1978 ;; Declarative profile.
1979 (define-record-type* <profile> profile make-profile
1981 (name profile-name (default "profile")) ;string
1982 (content profile-content) ;<manifest>
1983 (hooks profile-hooks ;list of procedures
1984 (default %default-profile-hooks))
1985 (locales? profile-locales? ;Boolean
1987 (allow-collisions? profile-allow-collisions? ;Boolean
1989 (relative-symlinks? profile-relative-symlinks? ;Boolean
1992 (define-gexp-compiler (profile-compiler (profile <profile>) system target)
1993 "Compile PROFILE to a derivation."
1995 (($ <profile> name manifest hooks
1996 locales? allow-collisions? relative-symlinks?)
1997 (profile-derivation manifest
2001 #:allow-collisions? allow-collisions?
2002 #:relative-symlinks? relative-symlinks?
2003 #:system system #:target target))))
2005 (define* (profile-search-paths profile
2006 #:optional (manifest (profile-manifest profile))
2007 #:key (getenv (const #f)))
2008 "Read the manifest of PROFILE and evaluate the values of search path
2009 environment variables required by PROFILE; return a list of
2010 specification/value pairs. If MANIFEST is not #f, it is assumed to be the
2011 manifest of PROFILE, which avoids rereading it.
2013 Use GETENV to determine the current settings and report only settings not
2015 (evaluate-search-paths (manifest-search-paths manifest)
2016 (list profile) getenv))
2018 (define %precious-variables
2019 ;; Environment variables in the default 'load-profile' white list.
2020 '("HOME" "USER" "LOGNAME" "DISPLAY" "XAUTHORITY" "TERM" "TZ" "PAGER"))
2022 (define (purify-environment white-list white-list-regexps)
2023 "Unset all environment variables except those that match the regexps in
2024 WHITE-LIST-REGEXPS and those listed in WHITE-LIST."
2026 (remove (lambda (variable)
2027 (or (member variable white-list)
2028 (find (cut regexp-exec <> variable)
2029 white-list-regexps)))
2030 (match (get-environment-variables)
2034 (define* (load-profile profile
2035 #:optional (manifest (profile-manifest profile))
2036 #:key pure? (white-list-regexps '())
2037 (white-list %precious-variables))
2038 "Set the environment variables specified by MANIFEST for PROFILE. When
2039 PURE? is #t, unset the variables in the current environment except those that
2040 match the regexps in WHITE-LIST-REGEXPS and those listed in WHITE-LIST.
2041 Otherwise, augment existing environment variables with additional search
2044 (purify-environment white-list white-list-regexps))
2045 (for-each (match-lambda
2046 ((($ <search-path-specification> variable _ separator) . value)
2047 (let ((current (getenv variable)))
2049 (if (and current (not pure?))
2051 (string-append value separator current)
2054 (profile-search-paths profile manifest)))
2056 (define (profile-regexp profile)
2057 "Return a regular expression that matches PROFILE's name and number."
2058 (make-regexp (string-append "^" (regexp-quote (basename profile))
2061 (define* (generation-number profile
2062 #:optional (base-profile profile))
2063 "Return PROFILE's number or 0. An absolute file name must be used.
2065 Optionally, if BASE-PROFILE is provided, use it instead of PROFILE to
2066 construct the regexp matching generations. This is useful in special cases
2067 like: (generation-number \"/run/current-system\" %system-profile)."
2068 (or (and=> (false-if-exception (regexp-exec (profile-regexp base-profile)
2069 (basename (readlink profile))))
2070 (compose string->number (cut match:substring <> 1)))
2073 (define %profile-generation-rx
2074 ;; Regexp that matches profile generation.
2075 (make-regexp "(.*)-([0-9]+)-link$"))
2077 (define (generation-profile file)
2078 "If FILE is a profile generation GC root such as \"guix-profile-42-link\",
2079 return its corresponding profile---e.g., \"guix-profile\". Otherwise return
2081 (match (regexp-exec %profile-generation-rx file)
2083 (m (let ((profile (match:substring m 1)))
2084 (and (file-exists? (string-append profile "/manifest"))
2087 (define (generation-numbers profile)
2088 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
2089 former profiles were found."
2090 (match (scandir (dirname profile)
2091 (cute regexp-exec (profile-regexp profile) <>))
2092 (#f ; no profile directory
2096 ((profiles ...) ; former profiles around
2097 (sort (map (compose string->number
2098 (cut match:substring <> 1)
2099 (cute regexp-exec (profile-regexp profile) <>))
2103 (define (profile-generations profile)
2104 "Return a list of PROFILE's generations."
2105 (let ((generations (generation-numbers profile)))
2106 (if (equal? generations '(0))
2110 (define (relative-generation-spec->number profile spec)
2111 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
2112 may be a N, -N, or +N, where N is a number. If the spec is N, then the number
2113 returned is N. If it is -N, then the number returned is the profile's current
2114 generation number minus N. If it is +N, then the number returned is the
2115 profile's current generation number plus N. Return #f if there is no such
2117 (let ((number (string->number spec)))
2119 (case (string-ref spec 0)
2121 (relative-generation profile number))
2122 (else (if (memv number (profile-generations profile))
2127 (define* (relative-generation profile shift #:optional
2128 (current (generation-number profile)))
2129 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
2130 SHIFT is a positive or negative number.
2131 Return #f if there is no such generation."
2132 (let* ((abs-shift (abs shift))
2133 (numbers (profile-generations profile))
2134 (from-current (memq current
2135 (if (negative? shift)
2139 (< abs-shift (length from-current))
2140 (list-ref from-current abs-shift))))
2142 (define* (previous-generation-number profile #:optional
2143 (number (generation-number profile)))
2144 "Return the number of the generation before generation NUMBER of
2145 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
2146 case when generations have been deleted (there are \"holes\")."
2147 (or (relative-generation profile -1 number)
2150 (define (generation-file-name profile generation)
2151 "Return the file name for PROFILE's GENERATION."
2152 (format #f "~a-~a-link" profile generation))
2154 (define (generation-time profile number)
2155 "Return the creation time of a generation in the UTC format."
2156 (make-time time-utc 0
2157 (stat:ctime (stat (generation-file-name profile number)))))
2159 (define (link-to-empty-profile store generation)
2160 "Link GENERATION, a string, to the empty profile. An error is raised if
2162 (let* ((drv (run-with-store store
2163 (profile-derivation (manifest '())
2165 (prof (derivation->output-path drv "out")))
2166 (build-derivations store (list drv))
2167 (switch-symlinks generation prof)))
2169 (define (switch-to-generation profile number)
2170 "Atomically switch PROFILE to the generation NUMBER. Return the number of
2171 the generation that was current before switching."
2172 (let ((current (generation-number profile))
2173 (generation (generation-file-name profile number)))
2174 (cond ((not (file-exists? profile))
2175 (raise (condition (&profile-not-found-error
2176 (profile profile)))))
2177 ((not (file-exists? generation))
2178 (raise (condition (&missing-generation-error
2180 (generation number)))))
2182 (switch-symlinks profile (basename generation))
2185 (define (switch-to-previous-generation profile)
2186 "Atomically switch PROFILE to the previous generation. Return the former
2187 generation number and the current one."
2188 (let ((previous (previous-generation-number profile)))
2189 (values (switch-to-generation profile previous)
2192 (define (roll-back store profile)
2193 "Roll back to the previous generation of PROFILE. Return the number of the
2194 generation that was current before switching and the new generation number."
2195 (let* ((number (generation-number profile))
2196 (previous-number (previous-generation-number profile number))
2197 (previous-generation (generation-file-name profile previous-number)))
2198 (cond ((not (file-exists? profile)) ;invalid profile
2199 (raise (condition (&profile-not-found-error
2200 (profile profile)))))
2201 ((zero? number) ;empty profile
2202 (values number number))
2203 ((or (zero? previous-number) ;going to emptiness
2204 (not (file-exists? previous-generation)))
2205 (link-to-empty-profile store previous-generation)
2206 (switch-to-previous-generation profile))
2207 (else ;anything else
2208 (switch-to-previous-generation profile)))))
2210 (define (delete-generation store profile number)
2211 "Delete generation with NUMBER from PROFILE. Return the file name of the
2212 generation that has been deleted, or #f if nothing was done (for instance
2213 because the NUMBER is zero.)"
2214 (define (delete-and-return)
2215 (let ((generation (generation-file-name profile number)))
2216 (delete-file generation)
2219 (let* ((current-number (generation-number profile))
2220 (previous-number (previous-generation-number profile number))
2221 (previous-generation (generation-file-name profile previous-number)))
2222 (cond ((zero? number) #f) ;do not delete generation 0
2223 ((and (= number current-number)
2224 (not (file-exists? previous-generation)))
2225 (link-to-empty-profile store previous-generation)
2226 (switch-to-previous-generation profile)
2227 (delete-and-return))
2228 ((= number current-number)
2229 (roll-back store profile)
2230 (delete-and-return))
2232 (delete-and-return)))))
2234 (define %user-profile-directory
2235 (and=> (getenv "HOME")
2236 (cut string-append <> "/.guix-profile")))
2238 (define %profile-directory
2239 (string-append %state-directory "/profiles/"
2240 (or (and=> (or (getenv "USER")
2243 (passwd:name (getpwuid (getuid)))))
2244 (cut string-append "per-user/" <>))
2247 (define %current-profile
2248 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
2249 ;; coexist with Nix profiles.
2250 (string-append %profile-directory "/guix-profile"))
2252 (define (ensure-profile-directory)
2253 "Attempt to create /…/profiles/per-user/$USER if needed. Nowadays this is
2254 taken care of by the daemon."
2255 (let ((s (stat %profile-directory #f)))
2256 (unless (and s (eq? 'directory (stat:type s)))
2257 (catch 'system-error
2259 (mkdir-p %profile-directory))
2261 ;; Often, we cannot create %PROFILE-DIRECTORY because its
2262 ;; parent directory is root-owned and we're running
2268 (G_ "while creating directory `~a': ~a")
2270 (strerror (system-error-errno args)))))
2273 (format #f (G_ "Please create the @file{~a} directory, \
2274 with you as the owner.")
2275 %profile-directory))))))))
2277 ;; Bail out if it's not owned by the user.
2278 (unless (or (not s) (= (stat:uid s) (getuid)))
2282 (format #f (G_ "directory `~a' is not owned by you")
2283 %profile-directory)))
2286 (format #f (G_ "Please change the owner of @file{~a} \
2288 %profile-directory (or (getenv "USER")
2292 (define (canonicalize-profile profile)
2293 "If PROFILE points to a profile in %PROFILE-DIRECTORY, return that.
2294 Otherwise return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile'
2295 as if '-p' was omitted." ; see <http://bugs.gnu.org/17939>
2296 ;; Trim trailing slashes so 'readlink' can do its job.
2297 (let ((profile (string-trim-right profile #\/)))
2298 (catch 'system-error
2300 (let ((target (readlink profile)))
2301 (if (string=? (dirname target) %profile-directory)
2306 (define %known-shorthand-profiles
2307 ;; Known shorthand forms for profiles that the user manipulates.
2308 (list (string-append (config-directory #:ensure? #f) "/current")
2309 %user-profile-directory))
2311 (define (user-friendly-profile profile)
2312 "Return either ~/.guix-profile or ~/.config/guix/current if that's what
2313 PROFILE refers to, directly or indirectly, or PROFILE."
2314 (or (find (lambda (shorthand)
2316 (let ((target (false-if-exception
2317 (readlink shorthand))))
2318 (and target (string=? target profile)))))
2319 %known-shorthand-profiles)
2322 ;;; profiles.scm ends here