1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 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 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>
12 ;;; This file is part of GNU Guix.
14 ;;; GNU Guix is free software; you can redistribute it and/or modify it
15 ;;; under the terms of the GNU General Public License as published by
16 ;;; the Free Software Foundation; either version 3 of the License, or (at
17 ;;; your option) any later version.
19 ;;; GNU Guix is distributed in the hope that it will be useful, but
20 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;;; GNU General Public License for more details.
24 ;;; You should have received a copy of the GNU General Public License
25 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
27 (define-module (guix profiles)
28 #:use-module ((guix config) #:select (%state-directory))
29 #:use-module ((guix utils) #:hide (package-name->name+version))
30 #:use-module ((guix build utils)
31 #:select (package-name->name+version))
32 #:use-module (guix records)
33 #:use-module (guix packages)
34 #:use-module (guix derivations)
35 #:use-module (guix search-paths)
36 #:use-module (guix gexp)
37 #:use-module (guix modules)
38 #:use-module (guix monads)
39 #:use-module (guix store)
40 #:use-module (guix sets)
41 #:use-module (ice-9 vlist)
42 #:use-module (ice-9 match)
43 #:use-module (ice-9 regex)
44 #:use-module (ice-9 ftw)
45 #:use-module (ice-9 format)
46 #:use-module (srfi srfi-1)
47 #:use-module (srfi srfi-9)
48 #:use-module (srfi srfi-11)
49 #:use-module (srfi srfi-19)
50 #:use-module (srfi srfi-26)
51 #:use-module (srfi srfi-34)
52 #:use-module (srfi srfi-35)
53 #:export (&profile-error
56 &profile-not-found-error
57 profile-not-found-error?
58 &profile-collistion-error
59 profile-collision-error?
60 profile-collision-error-entry
61 profile-collision-error-conflict
62 &missing-generation-error
63 missing-generation-error?
64 missing-generation-error-generation
66 manifest make-manifest
69 manifest-transitive-entries
71 <manifest-entry> ; FIXME: eventually make it internal
75 manifest-entry-version
78 manifest-entry-dependencies
79 manifest-entry-search-paths
81 manifest-entry-properties
86 manifest-pattern-version
87 manifest-pattern-output
93 manifest-matching-entries
98 manifest-transaction-install
99 manifest-transaction-remove
100 manifest-transaction-install-entry
101 manifest-transaction-remove-pattern
102 manifest-transaction-null?
103 manifest-transaction-removal-candidate?
104 manifest-perform-transaction
105 manifest-transaction-effects
108 package->manifest-entry
110 ca-certificate-bundle
111 %default-profile-hooks
118 relative-generation-spec->number
120 previous-generation-number
127 %user-profile-directory
131 user-friendly-profile))
135 ;;; Tools to create and manipulate profiles---i.e., the representation of a
136 ;;; set of installed packages.
145 (define-condition-type &profile-error &error
147 (profile profile-error-profile))
149 (define-condition-type &profile-not-found-error &profile-error
150 profile-not-found-error?)
152 (define-condition-type &profile-collision-error &error
153 profile-collision-error?
154 (entry profile-collision-error-entry) ;<manifest-entry>
155 (conflict profile-collision-error-conflict)) ;<manifest-entry>
157 (define-condition-type &missing-generation-error &profile-error
158 missing-generation-error?
159 (generation missing-generation-error-generation))
166 (define-record-type <manifest>
169 (entries manifest-entries)) ; list of <manifest-entry>
171 ;; Convenient alias, to avoid name clashes.
172 (define make-manifest manifest)
174 (define-record-type* <manifest-entry> manifest-entry
177 (name manifest-entry-name) ; string
178 (version manifest-entry-version) ; string
179 (output manifest-entry-output ; string
181 (item manifest-entry-item) ; package | file-like | store path
182 (dependencies manifest-entry-dependencies ; <manifest-entry>*
184 (search-paths manifest-entry-search-paths ; search-path-specification*
186 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
187 (default (delay #f)))
188 (properties manifest-entry-properties ; list of symbol/value pairs
191 (define-record-type* <manifest-pattern> manifest-pattern
192 make-manifest-pattern
194 (name manifest-pattern-name) ; string
195 (version manifest-pattern-version ; string | #f
197 (output manifest-pattern-output ; string | #f
200 (define (manifest-transitive-entries manifest)
201 "Return the entries of MANIFEST along with their propagated inputs,
203 (let loop ((entries (manifest-entries manifest))
205 (visited (set))) ;compare with 'equal?'
210 (if (set-contains? visited head)
211 (loop tail result visited)
212 (loop (append (manifest-entry-dependencies head)
215 (set-insert head visited)))))))
217 (define (profile-manifest profile)
218 "Return the PROFILE's manifest."
219 (let ((file (string-append profile "/manifest")))
220 (if (file-exists? file)
221 (call-with-input-file file read-manifest)
224 (define (manifest-entry-lookup manifest)
225 "Return a lookup procedure for the entries of MANIFEST. The lookup
226 procedure takes two arguments: the entry name and output."
228 (let loop ((entries (manifest-entries manifest))
229 (mapping vlist-null))
230 (fold (lambda (entry result)
231 (vhash-cons (cons (manifest-entry-name entry)
232 (manifest-entry-output entry))
234 (loop (manifest-entry-dependencies entry)
239 (lambda (name output)
240 (match (vhash-assoc (cons name output) mapping)
244 (define* (lower-manifest-entry entry system #:key target)
245 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
247 (let ((item (manifest-entry-item entry)))
249 (with-monad %store-monad
251 (mlet %store-monad ((drv (lower-object item system
253 (output -> (manifest-entry-output entry)))
254 (return (manifest-entry
256 (item (derivation->output-path drv output))))))))
258 (define* (check-for-collisions manifest system #:key target)
259 "Check whether the entries of MANIFEST conflict with one another; raise a
260 '&profile-collision-error' when a conflict is encountered."
262 (manifest-entry-lookup manifest))
264 (with-monad %store-monad
266 (lambda (entry result)
267 (match (lookup (manifest-entry-name entry)
268 (manifest-entry-output entry))
269 ((? manifest-entry? second) ;potential conflict
270 (mlet %store-monad ((first (lower-manifest-entry entry system
273 (second (lower-manifest-entry second system
276 (if (string=? (manifest-entry-item first)
277 (manifest-entry-item second))
280 (&profile-collision-error
282 (conflict second)))))))
286 (manifest-transitive-entries manifest))))
288 (define* (package->manifest-entry package #:optional (output "out")
289 #:key (parent (delay #f))
291 "Return a manifest entry for the OUTPUT of package PACKAGE."
292 ;; For each dependency, keep a promise pointing to its "parent" entry.
293 (letrec* ((deps (map (match-lambda
295 (package->manifest-entry package
296 #:parent (delay entry)))
297 ((label package output)
298 (package->manifest-entry package output
299 #:parent (delay entry))))
300 (package-propagated-inputs package)))
301 (entry (manifest-entry
302 (name (package-name package))
303 (version (package-version package))
306 (dependencies (delete-duplicates deps))
308 (package-transitive-native-search-paths package))
310 (properties properties))))
313 (define (packages->manifest packages)
314 "Return a list of manifest entries, one for each item listed in PACKAGES.
315 Elements of PACKAGES can be either package objects or package/string tuples
316 denoting a specific output of a package."
317 (define inferiors-loaded?
318 ;; This hack allows us to provide seamless integration for inferior
319 ;; packages while not having a hard dependency on (guix inferior).
320 (resolve-module '(guix inferior) #f #f #:ensure #f))
322 (define (inferior->entry)
323 (module-ref (resolve-interface '(guix inferior))
324 'inferior-package->manifest-entry))
329 (package->manifest-entry package output))
330 ((? package? package)
331 (package->manifest-entry package))
333 (if inferiors-loaded?
334 ((inferior->entry) thing output)
335 (throw 'wrong-type-arg 'packages->manifest
336 "Wrong package object: ~S" (list thing) (list thing))))
338 (if inferiors-loaded?
339 ((inferior->entry) thing)
340 (throw 'wrong-type-arg 'packages->manifest
341 "Wrong package object: ~S" (list thing) (list thing)))))
344 (define (manifest->gexp manifest)
345 "Return a representation of MANIFEST as a gexp."
346 (define (entry->gexp entry)
348 (($ <manifest-entry> name version output (? string? path)
349 (deps ...) (search-paths ...) _ (properties ...))
350 #~(#$name #$version #$output #$path
351 (propagated-inputs #$(map entry->gexp deps))
352 (search-paths #$(map search-path-specification->sexp
354 (properties . #$properties)))
355 (($ <manifest-entry> name version output package
356 (deps ...) (search-paths ...) _ (properties ...))
357 #~(#$name #$version #$output
358 (ungexp package (or output "out"))
359 (propagated-inputs #$(map entry->gexp deps))
360 (search-paths #$(map search-path-specification->sexp
362 (properties . #$properties)))))
365 (($ <manifest> (entries ...))
366 #~(manifest (version 3)
367 (packages #$(map entry->gexp entries))))))
369 (define (find-package name version)
370 "Return a package from the distro matching NAME and possibly VERSION. This
371 procedure is here for backward-compatibility and will eventually vanish."
372 (define find-best-packages-by-name ;break abstractions
373 (module-ref (resolve-interface '(gnu packages))
374 'find-best-packages-by-name))
376 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
377 ;; former traverses the module tree only once and then allows for efficient
378 ;; access via a vhash.
379 (match (find-best-packages-by-name name version)
382 (match (find-best-packages-by-name name #f)
386 (define (sexp->manifest sexp)
387 "Parse SEXP as a manifest."
388 (define (infer-search-paths name version)
389 ;; Infer the search path specifications for NAME-VERSION by looking up a
390 ;; same-named package in the distro. Useful for the old manifest formats
391 ;; that did not store search path info.
392 (let ((package (find-package name version)))
394 (package-native-search-paths package)
397 (define (infer-dependency item parent)
398 ;; Return a <manifest-entry> for ITEM.
399 (let-values (((name version)
400 (package-name->name+version
401 (store-path-package-name item))))
408 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
410 ((name version output path
411 ('propagated-inputs deps)
412 ('search-paths search-paths)
414 ;; For each of DEPS, keep a promise pointing to ENTRY.
415 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
417 (entry (manifest-entry
423 (search-paths (map sexp->search-path-specification
426 (properties (or (assoc-ref extra-stuff 'properties)
431 (('manifest ('version 0)
432 ('packages ((name version output path) ...)))
434 (map (lambda (name version output path)
440 (search-paths (infer-search-paths name version))))
441 name version output path)))
443 ;; Version 1 adds a list of propagated inputs to the
444 ;; name/version/output/path tuples.
445 (('manifest ('version 1)
446 ('packages ((name version output path deps) ...)))
448 (map (lambda (name version output path deps)
449 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
450 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
452 (let ((deps (match deps
453 (((labels directories) ...)
457 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
459 (entry (manifest-entry
466 (infer-search-paths name version)))))
468 name version output path deps)))
470 ;; Version 2 adds search paths and is slightly more verbose.
471 (('manifest ('version 2 minor-version ...)
472 ('packages ((name version output path
473 ('propagated-inputs deps)
474 ('search-paths search-paths)
478 (map (lambda (name version output path deps search-paths)
479 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
481 (entry (manifest-entry
488 (map sexp->search-path-specification
491 name version output path deps search-paths)))
493 ;; Version 3 represents DEPS as full-blown manifest entries.
494 (('manifest ('version 3 minor-version ...)
495 ('packages (entries ...)))
496 (manifest (map sexp->manifest-entry entries)))
499 (&message (message "unsupported manifest format")))))))
501 (define (read-manifest port)
502 "Return the packages listed in MANIFEST."
503 (sexp->manifest (read port)))
505 (define (entry-predicate pattern)
506 "Return a procedure that returns #t when passed a manifest entry that
507 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
510 (($ <manifest-pattern> name version output)
512 (($ <manifest-entry> entry-name entry-version entry-output)
513 (and (string=? entry-name name)
514 (or (not entry-output) (not output)
515 (string=? entry-output output))
517 (string=? entry-version version))))))))
519 (define (manifest-remove manifest patterns)
520 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
521 must be a manifest-pattern."
522 (define (remove-entry pattern lst)
523 (remove (entry-predicate pattern) lst))
525 (make-manifest (fold remove-entry
526 (manifest-entries manifest)
529 (define (manifest-add manifest entries)
530 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
531 Remove MANIFEST entries that have the same name and output as ENTRIES."
532 (define (same-entry? entry name output)
534 (($ <manifest-entry> entry-name _ entry-output _)
535 (and (equal? name entry-name)
536 (equal? output entry-output)))))
539 (fold (lambda (entry result) ;XXX: quadratic
541 (($ <manifest-entry> name _ out _)
543 (remove (cut same-entry? <> name out)
545 (manifest-entries manifest)
548 (define (manifest-lookup manifest pattern)
549 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
551 (find (entry-predicate pattern)
552 (manifest-entries manifest)))
554 (define (manifest-installed? manifest pattern)
555 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
557 (->bool (manifest-lookup manifest pattern)))
559 (define (manifest-matching-entries manifest patterns)
560 "Return all the entries of MANIFEST that match one of the PATTERNS."
562 (map entry-predicate patterns))
564 (define (matches? entry)
569 (filter matches? (manifest-entries manifest)))
571 (define (manifest-search-paths manifest)
572 "Return the list of search path specifications that apply to MANIFEST,
573 including the search path specification for $PATH."
576 (append-map manifest-entry-search-paths
577 (manifest-entries manifest)))))
581 ;;; Manifest transactions.
584 (define-record-type* <manifest-transaction> manifest-transaction
585 make-manifest-transaction
586 manifest-transaction?
587 (install manifest-transaction-install ; list of <manifest-entry>
589 (remove manifest-transaction-remove ; list of <manifest-pattern>
592 (define (manifest-transaction-install-entry entry transaction)
593 "Augment TRANSACTION's set of installed packages with ENTRY, a
595 (manifest-transaction
596 (inherit transaction)
598 (cons entry (manifest-transaction-install transaction)))))
600 (define (manifest-transaction-remove-pattern pattern transaction)
601 "Add PATTERN to TRANSACTION's list of packages to remove."
602 (manifest-transaction
603 (inherit transaction)
605 (cons pattern (manifest-transaction-remove transaction)))))
607 (define (manifest-transaction-null? transaction)
608 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
611 (($ <manifest-transaction> () ()) #t)
612 (($ <manifest-transaction> _ _) #f)))
614 (define (manifest-transaction-removal-candidate? entry transaction)
615 "Return true if ENTRY is a candidate for removal in TRANSACTION."
616 (any (lambda (pattern)
617 ((entry-predicate pattern) entry))
618 (manifest-transaction-remove transaction)))
620 (define (manifest-transaction-effects manifest transaction)
621 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
622 the list of packages that would be removed, installed, upgraded, or downgraded
623 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
624 where the head is the entry being upgraded and the tail is the entry that will
626 (define (manifest-entry->pattern entry)
628 (name (manifest-entry-name entry))
629 (output (manifest-entry-output entry))))
631 (let loop ((input (manifest-transaction-install transaction))
637 (let ((remove (manifest-transaction-remove transaction)))
638 (values (manifest-matching-entries manifest remove)
639 (reverse install) (reverse upgrade) (reverse downgrade))))
641 ;; Check whether installing ENTRY corresponds to the installation of a
642 ;; new package or to an upgrade.
644 ;; XXX: When the exact same output directory is installed, we're not
645 ;; really upgrading anything. Add a check for that case.
646 (let* ((pattern (manifest-entry->pattern entry))
647 (previous (manifest-lookup manifest pattern))
648 (newer? (and previous
649 (version>=? (manifest-entry-version entry)
650 (manifest-entry-version previous)))))
652 (if previous install (cons entry install))
653 (if (and previous newer?)
654 (alist-cons previous entry upgrade)
656 (if (and previous (not newer?))
657 (alist-cons previous entry downgrade)
660 (define (manifest-perform-transaction manifest transaction)
661 "Perform TRANSACTION on MANIFEST and return the new manifest."
662 (let ((install (manifest-transaction-install transaction))
663 (remove (manifest-transaction-remove transaction)))
664 (manifest-add (manifest-remove manifest remove)
672 (define (manifest-inputs manifest)
673 "Return a list of <gexp-input> objects for MANIFEST."
676 (($ <manifest-entry> name version output thing deps)
677 ;; THING may be a package or a file name. In the latter case, assume
678 ;; it's already valid.
679 (cons (gexp-input thing output)
680 (append-map entry->input deps)))))
682 (append-map entry->input (manifest-entries manifest)))
684 (define* (manifest-lookup-package manifest name #:optional version)
685 "Return as a monadic value the first package or store path referenced by
686 MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
688 ;; Return as a monadic value the package or store path referenced by the
689 ;; manifest ENTRY, or #f if not referenced.
690 (define (entry-lookup-package entry)
691 (define (find-among-inputs inputs)
692 (find (lambda (input)
693 (and (package? input)
694 (equal? name (package-name input))
696 (string-prefix? version (package-version input))
699 (define (find-among-store-items items)
701 (let-values (((name* version*)
702 (package-name->name+version
703 (store-path-package-name item))))
704 (and (string=? name name*)
706 (string-prefix? version version*)
710 (with-monad %store-monad
711 (match (manifest-entry-item entry)
712 ((? package? package)
713 (match (cons (list (package-name package) package)
714 (package-transitive-inputs package))
715 (((labels inputs . _) ...)
716 (return (find-among-inputs inputs)))))
718 (mlet %store-monad ((refs (references* item)))
719 (return (find-among-store-items refs))))
721 ;; XXX: ITEM might be a 'computed-file' or anything like that, in
722 ;; which case we don't know what to do. The fix may be to check
723 ;; references once ITEM is compiled, as proposed at
724 ;; <https://bugs.gnu.org/29927>.
728 entry-lookup-package (manifest-entries manifest)))
730 (define (info-dir-file manifest)
731 "Return a derivation that builds the 'dir' file for all the entries of
733 (define texinfo ;lazy reference
734 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
735 (define gzip ;lazy reference
736 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
737 (define glibc-utf8-locales ;lazy reference
738 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
741 (with-imported-modules '((guix build utils))
743 (use-modules (guix build utils)
744 (srfi srfi-1) (srfi srfi-26)
747 (define (info-file? file)
748 (or (string-suffix? ".info" file)
749 (string-suffix? ".info.gz" file)))
751 (define (info-files top)
752 (let ((infodir (string-append top "/share/info")))
753 (map (cut string-append infodir "/" <>)
754 (or (scandir infodir info-file?) '()))))
756 (define (info-file-language file)
757 (let* ((base (if (string-suffix? ".gz" file)
758 (basename file ".info.gz")
759 (basename file ".info")))
760 (dot (string-rindex base #\.)))
762 (string-drop base (+ 1 dot))
765 (define (install-info info)
766 (let ((language (info-file-language info)))
767 ;; We need to choose a valid locale for $LANGUAGE to be honored.
768 (setenv "LC_ALL" "en_US.utf8")
769 (setenv "LANGUAGE" language)
771 (system* #+(file-append texinfo "/bin/install-info")
773 (apply string-append #$output "/share/info/dir"
774 (if (string=? "en" language)
776 `("." ,language)))))))
778 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
779 (setenv "GUIX_LOCPATH"
780 #+(file-append glibc-utf8-locales "/lib/locale"))
782 (mkdir-p (string-append #$output "/share/info"))
783 (exit (every install-info
784 (append-map info-files
785 '#$(manifest-inputs manifest)))))))
787 (gexp->derivation "info-dir" build
789 #:substitutable? #f))
791 (define (ghc-package-cache-file manifest)
792 "Return a derivation that builds the GHC 'package.cache' file for all the
793 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
794 (define ghc ;lazy reference
795 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
798 (with-imported-modules '((guix build utils))
800 (use-modules (guix build utils)
801 (srfi srfi-1) (srfi srfi-26)
804 (define ghc-name-version
805 (let* ((base (basename #+ghc)))
807 (+ 1 (string-index base #\-)))))
810 (string-append "lib/" ghc-name-version "/package.conf.d"))
813 (string-append #$output "/" db-subdir))
815 (define (conf-files top)
816 (let ((db (string-append top "/" db-subdir)))
817 (if (file-exists? db)
818 (find-files db "\\.conf$")
821 (define (copy-conf-file conf)
822 (let ((base (basename conf)))
823 (copy-file conf (string-append db-dir "/" base))))
825 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
826 (for-each copy-conf-file
827 (append-map conf-files
829 '#$(manifest-inputs manifest))))
832 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
833 (string-append "--package-db=" db-dir)))))
834 (for-each delete-file (find-files db-dir "\\.conf$"))
837 (with-monad %store-monad
838 ;; Don't depend on GHC when there's nothing to do.
839 (if (any (cut string-prefix? "ghc" <>)
840 (map manifest-entry-name (manifest-entries manifest)))
841 (gexp->derivation "ghc-package-cache" build
846 (define (ca-certificate-bundle manifest)
847 "Return a derivation that builds a single-file bundle containing the CA
848 certificates in the /etc/ssl/certs sub-directories of the packages in
849 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
850 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
853 (define glibc-utf8-locales ;lazy reference
854 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
857 (with-imported-modules '((guix build utils))
859 (use-modules (guix build utils)
866 (define (pem-file? file)
867 (string-suffix? ".pem" file))
869 (define (ca-files top)
870 (let ((cert-dir (string-append top "/etc/ssl/certs")))
871 (map (cut string-append cert-dir "/" <>)
872 (or (scandir cert-dir pem-file?) '()))))
874 (define (concatenate-files files result)
875 "Make RESULT the concatenation of all of FILES."
876 (define (dump file port)
877 (display (call-with-input-file file get-string-all)
879 (newline port)) ;required, see <https://bugs.debian.org/635570>
881 (call-with-output-file result
883 (for-each (cut dump <> port) files))))
885 ;; Some file names in the NSS certificates are UTF-8 encoded so
886 ;; install a UTF-8 locale.
888 (string-append #+glibc-utf8-locales "/lib/locale/"
889 #+(version-major+minor
890 (package-version glibc-utf8-locales))))
891 (setlocale LC_ALL "en_US.utf8")
893 (match (append-map ca-files '#$(manifest-inputs manifest))
895 ;; Since there are no CA files, just create an empty directory. Do
896 ;; not create the etc/ssl/certs sub-directory, since that would
897 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
902 (let ((result (string-append #$output "/etc/ssl/certs")))
904 (concatenate-files ca-files
905 (string-append result
906 "/ca-certificates.crt"))
909 (gexp->derivation "ca-certificate-bundle" build
911 #:substitutable? #f))
913 (define (glib-schemas manifest)
914 "Return a derivation that unions all schemas from manifest entries and
915 creates the Glib 'gschemas.compiled' file."
916 (define glib ; lazy reference
917 (module-ref (resolve-interface '(gnu packages glib)) 'glib))
919 (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
920 ;; XXX: Can't use glib-compile-schemas corresponding
921 ;; to the glib referenced by 'manifest'. Because
922 ;; '%glib' can be either a package or store path, and
923 ;; there's no way to get the "bin" output for the later.
924 (glib-compile-schemas
925 -> #~(string-append #+glib:bin
926 "/bin/glib-compile-schemas")))
929 (with-imported-modules '((guix build utils)
931 (guix build profiles)
935 (use-modules (guix build utils)
937 (guix build profiles)
940 (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
941 (schemadirs (filter file-exists?
942 (map (cut string-append <> "/share/glib-2.0/schemas")
943 '#$(manifest-inputs manifest)))))
945 ;; Union all the schemas.
946 (mkdir-p (string-append #$output "/share/glib-2.0"))
947 (union-build destdir schemadirs
948 #:log-port (%make-void-port "w"))
951 (when (file-is-directory? dir)
952 (ensure-writable-directory dir)
953 (invoke #+glib-compile-schemas
954 (string-append "--targetdir=" dir)
957 ;; Don't run the hook when there's nothing to do.
959 (gexp->derivation "glib-schemas" build
964 (define (gtk-icon-themes manifest)
965 "Return a derivation that unions all icon themes from manifest entries and
966 creates the GTK+ 'icon-theme.cache' file for each theme."
967 (define gtk+ ; lazy reference
968 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
970 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
971 ;; XXX: Can't use gtk-update-icon-cache corresponding
972 ;; to the gtk+ referenced by 'manifest'. Because
973 ;; '%gtk+' can be either a package or store path, and
974 ;; there's no way to get the "bin" output for the later.
975 (gtk-update-icon-cache
976 -> #~(string-append #+gtk+:bin
977 "/bin/gtk-update-icon-cache")))
980 (with-imported-modules '((guix build utils)
982 (guix build profiles)
986 (use-modules (guix build utils)
988 (guix build profiles)
992 (let* ((destdir (string-append #$output "/share/icons"))
993 (icondirs (filter file-exists?
994 (map (cut string-append <> "/share/icons")
995 '#$(manifest-inputs manifest)))))
997 ;; Union all the icons.
998 (mkdir-p (string-append #$output "/share"))
999 (union-build destdir icondirs
1000 #:log-port (%make-void-port "w"))
1002 ;; Update the 'icon-theme.cache' file for each icon theme.
1005 (let ((dir (string-append destdir "/" theme)))
1006 ;; Occasionally DESTDIR contains plain files, such as
1007 ;; "abiword_48.png". Ignore these.
1008 (when (file-is-directory? dir)
1009 (ensure-writable-directory dir)
1010 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
1011 (scandir destdir (negate (cut member <> '("." "..")))))))))
1013 ;; Don't run the hook when there's nothing to do.
1015 (gexp->derivation "gtk-icon-themes" build
1017 #:substitutable? #f)
1020 (define (gtk-im-modules manifest)
1021 "Return a derivation that builds the cache files for input method modules
1022 for both major versions of GTK+."
1024 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
1025 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
1027 (define (build gtk gtk-version query)
1028 (let ((major (string-take gtk-version 1)))
1029 (with-imported-modules '((guix build utils)
1031 (guix build profiles)
1035 (use-modules (guix build utils)
1037 (guix build profiles)
1042 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
1044 (destdir (string-append #$output prefix))
1045 (moddirs (cons (string-append #$gtk prefix "/immodules")
1046 (filter file-exists?
1047 (map (cut string-append <> prefix "/immodules")
1048 '#$(manifest-inputs manifest)))))
1049 (modules (append-map (cut find-files <> "\\.so$")
1052 ;; Generate a new immodules cache file.
1053 (mkdir-p (string-append #$output prefix))
1054 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
1055 (outfile (string-append #$output prefix
1056 "/immodules-gtk" #$major ".cache")))
1060 (call-with-output-file outfile
1062 (while (not (eof-object? (peek-char pipe)))
1063 (write-char (read-char pipe) out))))
1066 (close-pipe pipe)))))))))
1068 ;; Don't run the hook when there's nothing to do.
1069 (let* ((pkg-gtk+ (module-ref ; lazy reference
1070 (resolve-interface '(gnu packages gtk)) 'gtk+))
1075 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
1076 ;; output of latest gtk+ package.
1078 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
1084 #$gtk+-2 "/bin/gtk-query-immodules-2.0"))
1086 (if (or gtk+ gtk+-2)
1087 (gexp->derivation "gtk-im-modules" gexp
1089 #:substitutable? #f)
1092 (define (xdg-desktop-database manifest)
1093 "Return a derivation that builds the @file{mimeinfo.cache} database from
1094 desktop files. It's used to query what applications can handle a given
1096 (define desktop-file-utils ; lazy reference
1097 (module-ref (resolve-interface '(gnu packages freedesktop))
1098 'desktop-file-utils))
1100 (mlet %store-monad ((glib
1101 (manifest-lookup-package
1104 (with-imported-modules '((guix build utils)
1107 (use-modules (srfi srfi-26)
1110 (let* ((destdir (string-append #$output "/share/applications"))
1111 (appdirs (filter file-exists?
1112 (map (cut string-append <>
1113 "/share/applications")
1114 '#$(manifest-inputs manifest))))
1115 (update-desktop-database (string-append
1116 #+desktop-file-utils
1117 "/bin/update-desktop-database")))
1118 (mkdir-p (string-append #$output "/share"))
1119 (union-build destdir appdirs
1120 #:log-port (%make-void-port "w"))
1121 (exit (zero? (system* update-desktop-database destdir)))))))
1123 ;; Don't run the hook when 'glib' is not referenced.
1125 (gexp->derivation "xdg-desktop-database" build
1127 #:substitutable? #f)
1130 (define (xdg-mime-database manifest)
1131 "Return a derivation that builds the @file{mime.cache} database from manifest
1132 entries. It's used to query the MIME type of a given file."
1133 (define shared-mime-info ; lazy reference
1134 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1136 (mlet %store-monad ((glib
1137 (manifest-lookup-package
1140 (with-imported-modules '((guix build utils)
1143 (use-modules (srfi srfi-26)
1146 (let* ((datadir (string-append #$output "/share"))
1147 (destdir (string-append datadir "/mime"))
1148 (pkgdirs (filter file-exists?
1149 (map (cut string-append <>
1150 "/share/mime/packages")
1151 (cons #+shared-mime-info
1152 '#$(manifest-inputs manifest)))))
1153 (update-mime-database (string-append
1155 "/bin/update-mime-database")))
1157 (union-build (string-append destdir "/packages") pkgdirs
1158 #:log-port (%make-void-port "w"))
1159 (setenv "XDG_DATA_HOME" datadir)
1160 (exit (zero? (system* update-mime-database destdir)))))))
1162 ;; Don't run the hook when there are no GLib based applications.
1164 (gexp->derivation "xdg-mime-database" build
1166 #:substitutable? #f)
1169 ;; Several font packages may install font files into same directory, so
1170 ;; fonts.dir and fonts.scale file should be generated here, instead of in
1172 (define (fonts-dir-file manifest)
1173 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
1174 files for the fonts of the @var{manifest} entries."
1176 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1179 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1183 (use-modules (srfi srfi-26)
1186 (let ((fonts-dirs (filter file-exists?
1187 (map (cut string-append <>
1189 '#$(manifest-inputs manifest)))))
1191 (if (null? fonts-dirs)
1193 (let* ((share-dir (string-append #$output "/share"))
1194 (fonts-dir (string-append share-dir "/fonts"))
1195 (mkfontscale (string-append #+mkfontscale
1196 "/bin/mkfontscale"))
1197 (mkfontdir (string-append #+mkfontdir
1199 (empty-file? (lambda (filename)
1200 (call-with-ascii-input-file filename
1202 (eqv? #\0 (read-char p))))))
1203 (fonts-dir-file "fonts.dir")
1204 (fonts-scale-file "fonts.scale"))
1206 ;; Create all sub-directories, because we may create fonts.dir
1207 ;; and fonts.scale files in the sub-directories.
1208 (union-build fonts-dir fonts-dirs
1209 #:log-port (%make-void-port "w")
1210 #:create-all-directories? #t)
1211 (let ((directories (find-files fonts-dir
1213 (eq? 'directory (stat:type stat)))
1214 #:directories? #t)))
1215 (for-each (lambda (dir)
1216 (with-directory-excursion dir
1217 (when (file-exists? fonts-scale-file)
1218 (delete-file fonts-scale-file))
1219 (when (file-exists? fonts-dir-file)
1220 (delete-file fonts-dir-file))
1221 (unless (and (zero? (system* mkfontscale))
1222 (zero? (system* mkfontdir)))
1224 (when (and (file-exists? fonts-scale-file)
1225 (empty-file? fonts-scale-file))
1226 (delete-file fonts-scale-file))
1227 (when (and (file-exists? fonts-dir-file)
1228 (empty-file? fonts-dir-file))
1229 (delete-file fonts-dir-file))))
1232 (gexp->derivation "fonts-dir" build
1233 #:modules '((guix build utils)
1237 #:substitutable? #f))
1239 (define (manual-database manifest)
1240 "Return a derivation that builds the manual page database (\"mandb\") for
1241 the entries in MANIFEST."
1243 (module-ref (resolve-interface '(gnu packages guile))
1247 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
1250 (scheme-file "config.scm"
1252 (define-module (guix config)
1256 #+(file-append zlib "/lib/libz")))))
1259 (cons `((guix config) => ,config.scm)
1260 (delete '(guix config)
1261 (source-module-closure `((guix build utils)
1265 (with-imported-modules modules
1266 (with-extensions (list gdbm-ffi) ;for (guix man-db)
1268 (use-modules (guix man-db)
1273 (define (compute-entries)
1274 (append-map (lambda (directory)
1275 (let ((man (string-append directory "/share/man")))
1276 (if (directory-exists? man)
1279 '#$(manifest-inputs manifest)))
1281 (define man-directory
1282 (string-append #$output "/share/man"))
1284 (mkdir-p man-directory)
1286 (format #t "Creating manual page database...~%")
1288 (let* ((start (current-time))
1289 (entries (compute-entries))
1290 (_ (write-mandb-database (string-append man-directory
1293 (duration (time-difference (current-time) start)))
1294 (format #t "~a entries processed in ~,1f s~%"
1296 (+ (time-second duration)
1297 (* (time-nanosecond duration) (expt 10 -9))))
1300 (gexp->derivation "manual-database" build
1302 ;; Work around GDBM 1.13 issue whereby uninitialized bytes
1303 ;; get written to disk:
1304 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
1305 #:env-vars `(("MALLOC_PERTURB_" . "1"))
1309 (define %default-profile-hooks
1310 ;; This is the list of derivation-returning procedures that are called by
1311 ;; default when making a non-empty profile.
1315 ghc-package-cache-file
1316 ca-certificate-bundle
1320 xdg-desktop-database
1323 (define* (profile-derivation manifest
1325 (hooks %default-profile-hooks)
1327 (allow-collisions? #f)
1328 (relative-symlinks? #f)
1330 "Return a derivation that builds a profile (aka. 'user environment') with
1331 the given MANIFEST. The profile includes additional derivations returned by
1332 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
1333 Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1334 entries in MANIFEST collide (for instance if there are two same-name packages
1335 with a different version number.)
1337 When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
1338 a dependency on the 'glibc-utf8-locales' package.
1340 When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
1341 This is one of the things to do for the result to be relocatable.
1343 When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1344 are cross-built for TARGET."
1345 (mlet* %store-monad ((system (if system
1348 (ok? (if allow-collisions?
1350 (check-for-collisions manifest system
1352 (extras (if (null? (manifest-entries manifest))
1354 (sequence %store-monad
1359 (append (filter-map (lambda (drv)
1360 (and (derivation? drv)
1363 (manifest-inputs manifest)))
1365 (define glibc-utf8-locales ;lazy reference
1366 (module-ref (resolve-interface '(gnu packages base))
1367 'glibc-utf8-locales))
1369 (define set-utf8-locale
1370 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1371 ;; install a UTF-8 locale.
1374 #$(file-append glibc-utf8-locales "/lib/locale/"
1375 (version-major+minor
1376 (package-version glibc-utf8-locales))))
1377 (setlocale LC_ALL "en_US.utf8")))
1380 (with-imported-modules '((guix build profiles)
1386 (use-modules (guix build profiles)
1390 (setvbuf (current-output-port) _IOLBF)
1391 (setvbuf (current-error-port) _IOLBF)
1393 #+(if locales? set-utf8-locale #t)
1395 (define search-paths
1396 ;; Search paths of MANIFEST's packages, converted back to their
1398 (map sexp->search-path-specification
1400 '#$(map search-path-specification->sexp
1401 (manifest-search-paths manifest)))))
1403 (build-profile #$output '#$inputs
1404 #:symlink #$(if relative-symlinks?
1407 #:manifest '#$(manifest->gexp manifest)
1408 #:search-paths search-paths))))
1410 (gexp->derivation "profile" builder
1414 ;; Don't complain about _IO* on Guile 2.2.
1415 #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
1417 ;; Not worth offloading.
1420 ;; Disable substitution because it would trigger a
1421 ;; connection to the substitute server, which is likely
1422 ;; to have no substitute to offer.
1423 #:substitutable? #f)))
1425 (define* (profile-search-paths profile
1426 #:optional (manifest (profile-manifest profile))
1427 #:key (getenv (const #f)))
1428 "Read the manifest of PROFILE and evaluate the values of search path
1429 environment variables required by PROFILE; return a list of
1430 specification/value pairs. If MANIFEST is not #f, it is assumed to be the
1431 manifest of PROFILE, which avoids rereading it.
1433 Use GETENV to determine the current settings and report only settings not
1435 (evaluate-search-paths (manifest-search-paths manifest)
1436 (list profile) getenv))
1438 (define (profile-regexp profile)
1439 "Return a regular expression that matches PROFILE's name and number."
1440 (make-regexp (string-append "^" (regexp-quote (basename profile))
1443 (define (generation-number profile)
1444 "Return PROFILE's number or 0. An absolute file name must be used."
1445 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
1446 (basename (readlink profile))))
1447 (compose string->number (cut match:substring <> 1)))
1450 (define (generation-numbers profile)
1451 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
1452 former profiles were found."
1453 (match (scandir (dirname profile)
1454 (cute regexp-exec (profile-regexp profile) <>))
1455 (#f ; no profile directory
1459 ((profiles ...) ; former profiles around
1460 (sort (map (compose string->number
1461 (cut match:substring <> 1)
1462 (cute regexp-exec (profile-regexp profile) <>))
1466 (define (profile-generations profile)
1467 "Return a list of PROFILE's generations."
1468 (let ((generations (generation-numbers profile)))
1469 (if (equal? generations '(0))
1473 (define (relative-generation-spec->number profile spec)
1474 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
1475 may be a N, -N, or +N, where N is a number. If the spec is N, then the number
1476 returned is N. If it is -N, then the number returned is the profile's current
1477 generation number minus N. If it is +N, then the number returned is the
1478 profile's current generation number plus N. Return #f if there is no such
1480 (let ((number (string->number spec)))
1482 (case (string-ref spec 0)
1484 (relative-generation profile number))
1485 (else (if (memv number (profile-generations profile))
1490 (define* (relative-generation profile shift #:optional
1491 (current (generation-number profile)))
1492 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
1493 SHIFT is a positive or negative number.
1494 Return #f if there is no such generation."
1495 (let* ((abs-shift (abs shift))
1496 (numbers (profile-generations profile))
1497 (from-current (memq current
1498 (if (negative? shift)
1502 (< abs-shift (length from-current))
1503 (list-ref from-current abs-shift))))
1505 (define* (previous-generation-number profile #:optional
1506 (number (generation-number profile)))
1507 "Return the number of the generation before generation NUMBER of
1508 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
1509 case when generations have been deleted (there are \"holes\")."
1510 (or (relative-generation profile -1 number)
1513 (define (generation-file-name profile generation)
1514 "Return the file name for PROFILE's GENERATION."
1515 (format #f "~a-~a-link" profile generation))
1517 (define (generation-time profile number)
1518 "Return the creation time of a generation in the UTC format."
1519 (make-time time-utc 0
1520 (stat:ctime (stat (generation-file-name profile number)))))
1522 (define (link-to-empty-profile store generation)
1523 "Link GENERATION, a string, to the empty profile. An error is raised if
1525 (let* ((drv (run-with-store store
1526 (profile-derivation (manifest '())
1528 (prof (derivation->output-path drv "out")))
1529 (build-derivations store (list drv))
1530 (switch-symlinks generation prof)))
1532 (define (switch-to-generation profile number)
1533 "Atomically switch PROFILE to the generation NUMBER. Return the number of
1534 the generation that was current before switching."
1535 (let ((current (generation-number profile))
1536 (generation (generation-file-name profile number)))
1537 (cond ((not (file-exists? profile))
1538 (raise (condition (&profile-not-found-error
1539 (profile profile)))))
1540 ((not (file-exists? generation))
1541 (raise (condition (&missing-generation-error
1543 (generation number)))))
1545 (switch-symlinks profile (basename generation))
1548 (define (switch-to-previous-generation profile)
1549 "Atomically switch PROFILE to the previous generation. Return the former
1550 generation number and the current one."
1551 (let ((previous (previous-generation-number profile)))
1552 (values (switch-to-generation profile previous)
1555 (define (roll-back store profile)
1556 "Roll back to the previous generation of PROFILE. Return the number of the
1557 generation that was current before switching and the new generation number."
1558 (let* ((number (generation-number profile))
1559 (previous-number (previous-generation-number profile number))
1560 (previous-generation (generation-file-name profile previous-number)))
1561 (cond ((not (file-exists? profile)) ;invalid profile
1562 (raise (condition (&profile-not-found-error
1563 (profile profile)))))
1564 ((zero? number) ;empty profile
1565 (values number number))
1566 ((or (zero? previous-number) ;going to emptiness
1567 (not (file-exists? previous-generation)))
1568 (link-to-empty-profile store previous-generation)
1569 (switch-to-previous-generation profile))
1570 (else ;anything else
1571 (switch-to-previous-generation profile)))))
1573 (define (delete-generation store profile number)
1574 "Delete generation with NUMBER from PROFILE. Return the file name of the
1575 generation that has been deleted, or #f if nothing was done (for instance
1576 because the NUMBER is zero.)"
1577 (define (delete-and-return)
1578 (let ((generation (generation-file-name profile number)))
1579 (delete-file generation)
1582 (let* ((current-number (generation-number profile))
1583 (previous-number (previous-generation-number profile number))
1584 (previous-generation (generation-file-name profile previous-number)))
1585 (cond ((zero? number) #f) ;do not delete generation 0
1586 ((and (= number current-number)
1587 (not (file-exists? previous-generation)))
1588 (link-to-empty-profile store previous-generation)
1589 (switch-to-previous-generation profile)
1590 (delete-and-return))
1591 ((= number current-number)
1592 (roll-back store profile)
1593 (delete-and-return))
1595 (delete-and-return)))))
1597 (define %user-profile-directory
1598 (and=> (getenv "HOME")
1599 (cut string-append <> "/.guix-profile")))
1601 (define %profile-directory
1602 (string-append %state-directory "/profiles/"
1603 (or (and=> (or (getenv "USER")
1605 (cut string-append "per-user/" <>))
1608 (define %current-profile
1609 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
1610 ;; coexist with Nix profiles.
1611 (string-append %profile-directory "/guix-profile"))
1613 (define (canonicalize-profile profile)
1614 "If PROFILE is %USER-PROFILE-DIRECTORY, return %CURRENT-PROFILE. Otherwise
1615 return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile' as if
1616 '-p' was omitted." ; see <http://bugs.gnu.org/17939>
1618 ;; Trim trailing slashes so that the basename comparison below works as
1620 (let ((profile (string-trim-right profile #\/)))
1621 (if (and %user-profile-directory
1622 (string=? (canonicalize-path (dirname profile))
1623 (dirname %user-profile-directory))
1624 (string=? (basename profile) (basename %user-profile-directory)))
1628 (define (user-friendly-profile profile)
1629 "Return either ~/.guix-profile if that's what PROFILE refers to, directly or
1630 indirectly, or PROFILE."
1631 (if (and %user-profile-directory
1633 (string=? (readlink %user-profile-directory) profile)))
1634 %user-profile-directory
1637 ;;; profiles.scm ends here