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
97 manifest-transaction-install
98 manifest-transaction-remove
99 manifest-transaction-install-entry
100 manifest-transaction-remove-pattern
101 manifest-transaction-null?
102 manifest-transaction-removal-candidate?
103 manifest-perform-transaction
104 manifest-transaction-effects
107 package->manifest-entry
109 ca-certificate-bundle
110 %default-profile-hooks
116 relative-generation-spec->number
118 previous-generation-number
125 %user-profile-directory
129 user-friendly-profile))
133 ;;; Tools to create and manipulate profiles---i.e., the representation of a
134 ;;; set of installed packages.
143 (define-condition-type &profile-error &error
145 (profile profile-error-profile))
147 (define-condition-type &profile-not-found-error &profile-error
148 profile-not-found-error?)
150 (define-condition-type &profile-collision-error &error
151 profile-collision-error?
152 (entry profile-collision-error-entry) ;<manifest-entry>
153 (conflict profile-collision-error-conflict)) ;<manifest-entry>
155 (define-condition-type &missing-generation-error &profile-error
156 missing-generation-error?
157 (generation missing-generation-error-generation))
164 (define-record-type <manifest>
167 (entries manifest-entries)) ; list of <manifest-entry>
169 ;; Convenient alias, to avoid name clashes.
170 (define make-manifest manifest)
172 (define-record-type* <manifest-entry> manifest-entry
175 (name manifest-entry-name) ; string
176 (version manifest-entry-version) ; string
177 (output manifest-entry-output ; string
179 (item manifest-entry-item) ; package | file-like | store path
180 (dependencies manifest-entry-dependencies ; <manifest-entry>*
182 (search-paths manifest-entry-search-paths ; search-path-specification*
184 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
185 (default (delay #f)))
186 (properties manifest-entry-properties ; list of symbol/value pairs
189 (define-record-type* <manifest-pattern> manifest-pattern
190 make-manifest-pattern
192 (name manifest-pattern-name) ; string
193 (version manifest-pattern-version ; string | #f
195 (output manifest-pattern-output ; string | #f
198 (define (manifest-transitive-entries manifest)
199 "Return the entries of MANIFEST along with their propagated inputs,
201 (let loop ((entries (manifest-entries manifest))
203 (visited (set))) ;compare with 'equal?'
208 (if (set-contains? visited head)
209 (loop tail result visited)
210 (loop (append (manifest-entry-dependencies head)
213 (set-insert head visited)))))))
215 (define (profile-manifest profile)
216 "Return the PROFILE's manifest."
217 (let ((file (string-append profile "/manifest")))
218 (if (file-exists? file)
219 (call-with-input-file file read-manifest)
222 (define (manifest-entry-lookup manifest)
223 "Return a lookup procedure for the entries of MANIFEST. The lookup
224 procedure takes two arguments: the entry name and output."
226 (let loop ((entries (manifest-entries manifest))
227 (mapping vlist-null))
228 (fold (lambda (entry result)
229 (vhash-cons (cons (manifest-entry-name entry)
230 (manifest-entry-output entry))
232 (loop (manifest-entry-dependencies entry)
237 (lambda (name output)
238 (match (vhash-assoc (cons name output) mapping)
242 (define* (lower-manifest-entry entry system #:key target)
243 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
245 (let ((item (manifest-entry-item entry)))
247 (with-monad %store-monad
249 (mlet %store-monad ((drv (lower-object item system
251 (output -> (manifest-entry-output entry)))
252 (return (manifest-entry
254 (item (derivation->output-path drv output))))))))
256 (define* (check-for-collisions manifest system #:key target)
257 "Check whether the entries of MANIFEST conflict with one another; raise a
258 '&profile-collision-error' when a conflict is encountered."
260 (manifest-entry-lookup manifest))
262 (with-monad %store-monad
264 (lambda (entry result)
265 (match (lookup (manifest-entry-name entry)
266 (manifest-entry-output entry))
267 ((? manifest-entry? second) ;potential conflict
268 (mlet %store-monad ((first (lower-manifest-entry entry system
271 (second (lower-manifest-entry second system
274 (if (string=? (manifest-entry-item first)
275 (manifest-entry-item second))
278 (&profile-collision-error
280 (conflict second)))))))
284 (manifest-transitive-entries manifest))))
286 (define* (package->manifest-entry package #:optional (output "out")
287 #:key (parent (delay #f)))
288 "Return a manifest entry for the OUTPUT of package PACKAGE."
289 ;; For each dependency, keep a promise pointing to its "parent" entry.
290 (letrec* ((deps (map (match-lambda
292 (package->manifest-entry package
293 #:parent (delay entry)))
294 ((label package output)
295 (package->manifest-entry package output
296 #:parent (delay entry))))
297 (package-propagated-inputs package)))
298 (entry (manifest-entry
299 (name (package-name package))
300 (version (package-version package))
303 (dependencies (delete-duplicates deps))
305 (package-transitive-native-search-paths package))
309 (define (packages->manifest packages)
310 "Return a list of manifest entries, one for each item listed in PACKAGES.
311 Elements of PACKAGES can be either package objects or package/string tuples
312 denoting a specific output of a package."
316 (package->manifest-entry package output))
317 ((? package? package)
318 (package->manifest-entry package)))
321 (define (manifest->gexp manifest)
322 "Return a representation of MANIFEST as a gexp."
323 (define (entry->gexp entry)
325 (($ <manifest-entry> name version output (? string? path)
326 (deps ...) (search-paths ...) _ (properties ...))
327 #~(#$name #$version #$output #$path
328 (propagated-inputs #$(map entry->gexp deps))
329 (search-paths #$(map search-path-specification->sexp
331 (properties . #$properties)))
332 (($ <manifest-entry> name version output package
333 (deps ...) (search-paths ...) _ (properties ...))
334 #~(#$name #$version #$output
335 (ungexp package (or output "out"))
336 (propagated-inputs #$(map entry->gexp deps))
337 (search-paths #$(map search-path-specification->sexp
339 (properties . #$properties)))))
342 (($ <manifest> (entries ...))
343 #~(manifest (version 3)
344 (packages #$(map entry->gexp entries))))))
346 (define (find-package name version)
347 "Return a package from the distro matching NAME and possibly VERSION. This
348 procedure is here for backward-compatibility and will eventually vanish."
349 (define find-best-packages-by-name ;break abstractions
350 (module-ref (resolve-interface '(gnu packages))
351 'find-best-packages-by-name))
353 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
354 ;; former traverses the module tree only once and then allows for efficient
355 ;; access via a vhash.
356 (match (find-best-packages-by-name name version)
359 (match (find-best-packages-by-name name #f)
363 (define (sexp->manifest sexp)
364 "Parse SEXP as a manifest."
365 (define (infer-search-paths name version)
366 ;; Infer the search path specifications for NAME-VERSION by looking up a
367 ;; same-named package in the distro. Useful for the old manifest formats
368 ;; that did not store search path info.
369 (let ((package (find-package name version)))
371 (package-native-search-paths package)
374 (define (infer-dependency item parent)
375 ;; Return a <manifest-entry> for ITEM.
376 (let-values (((name version)
377 (package-name->name+version
378 (store-path-package-name item))))
385 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
387 ((name version output path
388 ('propagated-inputs deps)
389 ('search-paths search-paths)
391 ;; For each of DEPS, keep a promise pointing to ENTRY.
392 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
394 (entry (manifest-entry
400 (search-paths (map sexp->search-path-specification
403 (properties (or (assoc-ref extra-stuff 'properties)
408 (('manifest ('version 0)
409 ('packages ((name version output path) ...)))
411 (map (lambda (name version output path)
417 (search-paths (infer-search-paths name version))))
418 name version output path)))
420 ;; Version 1 adds a list of propagated inputs to the
421 ;; name/version/output/path tuples.
422 (('manifest ('version 1)
423 ('packages ((name version output path deps) ...)))
425 (map (lambda (name version output path deps)
426 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
427 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
429 (let ((deps (match deps
430 (((labels directories) ...)
434 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
436 (entry (manifest-entry
443 (infer-search-paths name version)))))
445 name version output path deps)))
447 ;; Version 2 adds search paths and is slightly more verbose.
448 (('manifest ('version 2 minor-version ...)
449 ('packages ((name version output path
450 ('propagated-inputs deps)
451 ('search-paths search-paths)
455 (map (lambda (name version output path deps search-paths)
456 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
458 (entry (manifest-entry
465 (map sexp->search-path-specification
468 name version output path deps search-paths)))
470 ;; Version 3 represents DEPS as full-blown manifest entries.
471 (('manifest ('version 3 minor-version ...)
472 ('packages (entries ...)))
473 (manifest (map sexp->manifest-entry entries)))
476 (&message (message "unsupported manifest format")))))))
478 (define (read-manifest port)
479 "Return the packages listed in MANIFEST."
480 (sexp->manifest (read port)))
482 (define (entry-predicate pattern)
483 "Return a procedure that returns #t when passed a manifest entry that
484 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
487 (($ <manifest-pattern> name version output)
489 (($ <manifest-entry> entry-name entry-version entry-output)
490 (and (string=? entry-name name)
491 (or (not entry-output) (not output)
492 (string=? entry-output output))
494 (string=? entry-version version))))))))
496 (define (manifest-remove manifest patterns)
497 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
498 must be a manifest-pattern."
499 (define (remove-entry pattern lst)
500 (remove (entry-predicate pattern) lst))
502 (make-manifest (fold remove-entry
503 (manifest-entries manifest)
506 (define (manifest-add manifest entries)
507 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
508 Remove MANIFEST entries that have the same name and output as ENTRIES."
509 (define (same-entry? entry name output)
511 (($ <manifest-entry> entry-name _ entry-output _)
512 (and (equal? name entry-name)
513 (equal? output entry-output)))))
516 (fold (lambda (entry result) ;XXX: quadratic
518 (($ <manifest-entry> name _ out _)
520 (remove (cut same-entry? <> name out)
522 (manifest-entries manifest)
525 (define (manifest-lookup manifest pattern)
526 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
528 (find (entry-predicate pattern)
529 (manifest-entries manifest)))
531 (define (manifest-installed? manifest pattern)
532 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
534 (->bool (manifest-lookup manifest pattern)))
536 (define (manifest-matching-entries manifest patterns)
537 "Return all the entries of MANIFEST that match one of the PATTERNS."
539 (map entry-predicate patterns))
541 (define (matches? entry)
546 (filter matches? (manifest-entries manifest)))
550 ;;; Manifest transactions.
553 (define-record-type* <manifest-transaction> manifest-transaction
554 make-manifest-transaction
555 manifest-transaction?
556 (install manifest-transaction-install ; list of <manifest-entry>
558 (remove manifest-transaction-remove ; list of <manifest-pattern>
561 (define (manifest-transaction-install-entry entry transaction)
562 "Augment TRANSACTION's set of installed packages with ENTRY, a
564 (manifest-transaction
565 (inherit transaction)
567 (cons entry (manifest-transaction-install transaction)))))
569 (define (manifest-transaction-remove-pattern pattern transaction)
570 "Add PATTERN to TRANSACTION's list of packages to remove."
571 (manifest-transaction
572 (inherit transaction)
574 (cons pattern (manifest-transaction-remove transaction)))))
576 (define (manifest-transaction-null? transaction)
577 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
580 (($ <manifest-transaction> () ()) #t)
581 (($ <manifest-transaction> _ _) #f)))
583 (define (manifest-transaction-removal-candidate? entry transaction)
584 "Return true if ENTRY is a candidate for removal in TRANSACTION."
585 (any (lambda (pattern)
586 ((entry-predicate pattern) entry))
587 (manifest-transaction-remove transaction)))
589 (define (manifest-transaction-effects manifest transaction)
590 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
591 the list of packages that would be removed, installed, upgraded, or downgraded
592 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
593 where the head is the entry being upgraded and the tail is the entry that will
595 (define (manifest-entry->pattern entry)
597 (name (manifest-entry-name entry))
598 (output (manifest-entry-output entry))))
600 (let loop ((input (manifest-transaction-install transaction))
606 (let ((remove (manifest-transaction-remove transaction)))
607 (values (manifest-matching-entries manifest remove)
608 (reverse install) (reverse upgrade) (reverse downgrade))))
610 ;; Check whether installing ENTRY corresponds to the installation of a
611 ;; new package or to an upgrade.
613 ;; XXX: When the exact same output directory is installed, we're not
614 ;; really upgrading anything. Add a check for that case.
615 (let* ((pattern (manifest-entry->pattern entry))
616 (previous (manifest-lookup manifest pattern))
617 (newer? (and previous
618 (version>=? (manifest-entry-version entry)
619 (manifest-entry-version previous)))))
621 (if previous install (cons entry install))
622 (if (and previous newer?)
623 (alist-cons previous entry upgrade)
625 (if (and previous (not newer?))
626 (alist-cons previous entry downgrade)
629 (define (manifest-perform-transaction manifest transaction)
630 "Perform TRANSACTION on MANIFEST and return the new manifest."
631 (let ((install (manifest-transaction-install transaction))
632 (remove (manifest-transaction-remove transaction)))
633 (manifest-add (manifest-remove manifest remove)
641 (define (manifest-inputs manifest)
642 "Return a list of <gexp-input> objects for MANIFEST."
645 (($ <manifest-entry> name version output thing deps)
646 ;; THING may be a package or a file name. In the latter case, assume
647 ;; it's already valid.
648 (cons (gexp-input thing output)
649 (append-map entry->input deps)))))
651 (append-map entry->input (manifest-entries manifest)))
653 (define* (manifest-lookup-package manifest name #:optional version)
654 "Return as a monadic value the first package or store path referenced by
655 MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
657 ;; Return as a monadic value the package or store path referenced by the
658 ;; manifest ENTRY, or #f if not referenced.
659 (define (entry-lookup-package entry)
660 (define (find-among-inputs inputs)
661 (find (lambda (input)
662 (and (package? input)
663 (equal? name (package-name input))
665 (string-prefix? version (package-version input))
668 (define (find-among-store-items items)
670 (let-values (((name* version*)
671 (package-name->name+version
672 (store-path-package-name item))))
673 (and (string=? name name*)
675 (string-prefix? version version*)
679 (with-monad %store-monad
680 (match (manifest-entry-item entry)
681 ((? package? package)
682 (match (cons (list (package-name package) package)
683 (package-transitive-inputs package))
684 (((labels inputs . _) ...)
685 (return (find-among-inputs inputs)))))
687 (mlet %store-monad ((refs (references* item)))
688 (return (find-among-store-items refs))))
690 ;; XXX: ITEM might be a 'computed-file' or anything like that, in
691 ;; which case we don't know what to do. The fix may be to check
692 ;; references once ITEM is compiled, as proposed at
693 ;; <https://bugs.gnu.org/29927>.
697 entry-lookup-package (manifest-entries manifest)))
699 (define (info-dir-file manifest)
700 "Return a derivation that builds the 'dir' file for all the entries of
702 (define texinfo ;lazy reference
703 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
704 (define gzip ;lazy reference
705 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
708 (with-imported-modules '((guix build utils))
710 (use-modules (guix build utils)
711 (srfi srfi-1) (srfi srfi-26)
714 (define (info-file? file)
715 (or (string-suffix? ".info" file)
716 (string-suffix? ".info.gz" file)))
718 (define (info-files top)
719 (let ((infodir (string-append top "/share/info")))
720 (map (cut string-append infodir "/" <>)
721 (or (scandir infodir info-file?) '()))))
723 (define (install-info info)
724 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
726 (system* (string-append #+texinfo "/bin/install-info") "--silent"
727 info (string-append #$output "/share/info/dir"))))
729 (mkdir-p (string-append #$output "/share/info"))
730 (exit (every install-info
731 (append-map info-files
732 '#$(manifest-inputs manifest)))))))
734 (gexp->derivation "info-dir" build
736 #:substitutable? #f))
738 (define (ghc-package-cache-file manifest)
739 "Return a derivation that builds the GHC 'package.cache' file for all the
740 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
741 (define ghc ;lazy reference
742 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
745 (with-imported-modules '((guix build utils))
747 (use-modules (guix build utils)
748 (srfi srfi-1) (srfi srfi-26)
751 (define ghc-name-version
752 (let* ((base (basename #+ghc)))
754 (+ 1 (string-index base #\-)))))
757 (string-append "lib/" ghc-name-version "/package.conf.d"))
760 (string-append #$output "/" db-subdir))
762 (define (conf-files top)
763 (let ((db (string-append top "/" db-subdir)))
764 (if (file-exists? db)
765 (find-files db "\\.conf$")
768 (define (copy-conf-file conf)
769 (let ((base (basename conf)))
770 (copy-file conf (string-append db-dir "/" base))))
772 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
773 (for-each copy-conf-file
774 (append-map conf-files
776 '#$(manifest-inputs manifest))))
779 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
780 (string-append "--package-db=" db-dir)))))
781 (for-each delete-file (find-files db-dir "\\.conf$"))
784 (with-monad %store-monad
785 ;; Don't depend on GHC when there's nothing to do.
786 (if (any (cut string-prefix? "ghc" <>)
787 (map manifest-entry-name (manifest-entries manifest)))
788 (gexp->derivation "ghc-package-cache" build
793 (define (ca-certificate-bundle manifest)
794 "Return a derivation that builds a single-file bundle containing the CA
795 certificates in the /etc/ssl/certs sub-directories of the packages in
796 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
797 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
800 (define glibc-utf8-locales ;lazy reference
801 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
804 (with-imported-modules '((guix build utils))
806 (use-modules (guix build utils)
813 (define (pem-file? file)
814 (string-suffix? ".pem" file))
816 (define (ca-files top)
817 (let ((cert-dir (string-append top "/etc/ssl/certs")))
818 (map (cut string-append cert-dir "/" <>)
819 (or (scandir cert-dir pem-file?) '()))))
821 (define (concatenate-files files result)
822 "Make RESULT the concatenation of all of FILES."
823 (define (dump file port)
824 (display (call-with-input-file file get-string-all)
826 (newline port)) ;required, see <https://bugs.debian.org/635570>
828 (call-with-output-file result
830 (for-each (cut dump <> port) files))))
832 ;; Some file names in the NSS certificates are UTF-8 encoded so
833 ;; install a UTF-8 locale.
835 (string-append #+glibc-utf8-locales "/lib/locale/"
836 #+(version-major+minor
837 (package-version glibc-utf8-locales))))
838 (setlocale LC_ALL "en_US.utf8")
840 (match (append-map ca-files '#$(manifest-inputs manifest))
842 ;; Since there are no CA files, just create an empty directory. Do
843 ;; not create the etc/ssl/certs sub-directory, since that would
844 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
849 (let ((result (string-append #$output "/etc/ssl/certs")))
851 (concatenate-files ca-files
852 (string-append result
853 "/ca-certificates.crt"))
856 (gexp->derivation "ca-certificate-bundle" build
858 #:substitutable? #f))
860 (define (glib-schemas manifest)
861 "Return a derivation that unions all schemas from manifest entries and
862 creates the Glib 'gschemas.compiled' file."
863 (define glib ; lazy reference
864 (module-ref (resolve-interface '(gnu packages glib)) 'glib))
866 (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
867 ;; XXX: Can't use glib-compile-schemas corresponding
868 ;; to the glib referenced by 'manifest'. Because
869 ;; '%glib' can be either a package or store path, and
870 ;; there's no way to get the "bin" output for the later.
871 (glib-compile-schemas
872 -> #~(string-append #+glib:bin
873 "/bin/glib-compile-schemas")))
876 (with-imported-modules '((guix build utils)
878 (guix build profiles)
882 (use-modules (guix build utils)
884 (guix build profiles)
887 (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
888 (schemadirs (filter file-exists?
889 (map (cut string-append <> "/share/glib-2.0/schemas")
890 '#$(manifest-inputs manifest)))))
892 ;; Union all the schemas.
893 (mkdir-p (string-append #$output "/share/glib-2.0"))
894 (union-build destdir schemadirs
895 #:log-port (%make-void-port "w"))
898 (when (file-is-directory? dir)
899 (ensure-writable-directory dir)
900 (invoke #+glib-compile-schemas
901 (string-append "--targetdir=" dir)
904 ;; Don't run the hook when there's nothing to do.
906 (gexp->derivation "glib-schemas" build
911 (define (gtk-icon-themes manifest)
912 "Return a derivation that unions all icon themes from manifest entries and
913 creates the GTK+ 'icon-theme.cache' file for each theme."
914 (define gtk+ ; lazy reference
915 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
917 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
918 ;; XXX: Can't use gtk-update-icon-cache corresponding
919 ;; to the gtk+ referenced by 'manifest'. Because
920 ;; '%gtk+' can be either a package or store path, and
921 ;; there's no way to get the "bin" output for the later.
922 (gtk-update-icon-cache
923 -> #~(string-append #+gtk+:bin
924 "/bin/gtk-update-icon-cache")))
927 (with-imported-modules '((guix build utils)
929 (guix build profiles)
933 (use-modules (guix build utils)
935 (guix build profiles)
939 (let* ((destdir (string-append #$output "/share/icons"))
940 (icondirs (filter file-exists?
941 (map (cut string-append <> "/share/icons")
942 '#$(manifest-inputs manifest)))))
944 ;; Union all the icons.
945 (mkdir-p (string-append #$output "/share"))
946 (union-build destdir icondirs
947 #:log-port (%make-void-port "w"))
949 ;; Update the 'icon-theme.cache' file for each icon theme.
952 (let ((dir (string-append destdir "/" theme)))
953 ;; Occasionally DESTDIR contains plain files, such as
954 ;; "abiword_48.png". Ignore these.
955 (when (file-is-directory? dir)
956 (ensure-writable-directory dir)
957 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
958 (scandir destdir (negate (cut member <> '("." "..")))))))))
960 ;; Don't run the hook when there's nothing to do.
962 (gexp->derivation "gtk-icon-themes" build
967 (define (gtk-im-modules manifest)
968 "Return a derivation that builds the cache files for input method modules
969 for both major versions of GTK+."
971 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
972 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
974 (define (build gtk gtk-version query)
975 (let ((major (string-take gtk-version 1)))
976 (with-imported-modules '((guix build utils)
978 (guix build profiles)
982 (use-modules (guix build utils)
984 (guix build profiles)
989 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
991 (destdir (string-append #$output prefix))
992 (moddirs (cons (string-append #$gtk prefix "/immodules")
994 (map (cut string-append <> prefix "/immodules")
995 '#$(manifest-inputs manifest)))))
996 (modules (append-map (cut find-files <> "\\.so$")
999 ;; Generate a new immodules cache file.
1000 (mkdir-p (string-append #$output prefix))
1001 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
1002 (outfile (string-append #$output prefix
1003 "/immodules-gtk" #$major ".cache")))
1007 (call-with-output-file outfile
1009 (while (not (eof-object? (peek-char pipe)))
1010 (write-char (read-char pipe) out))))
1013 (close-pipe pipe)))))))))
1015 ;; Don't run the hook when there's nothing to do.
1016 (let* ((pkg-gtk+ (module-ref ; lazy reference
1017 (resolve-interface '(gnu packages gtk)) 'gtk+))
1022 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
1023 ;; output of latest gtk+ package.
1025 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
1031 #$gtk+-2 "/bin/gtk-query-immodules-2.0"))
1033 (if (or gtk+ gtk+-2)
1034 (gexp->derivation "gtk-im-modules" gexp
1036 #:substitutable? #f)
1039 (define (xdg-desktop-database manifest)
1040 "Return a derivation that builds the @file{mimeinfo.cache} database from
1041 desktop files. It's used to query what applications can handle a given
1043 (define desktop-file-utils ; lazy reference
1044 (module-ref (resolve-interface '(gnu packages freedesktop))
1045 'desktop-file-utils))
1047 (mlet %store-monad ((glib
1048 (manifest-lookup-package
1051 (with-imported-modules '((guix build utils)
1054 (use-modules (srfi srfi-26)
1057 (let* ((destdir (string-append #$output "/share/applications"))
1058 (appdirs (filter file-exists?
1059 (map (cut string-append <>
1060 "/share/applications")
1061 '#$(manifest-inputs manifest))))
1062 (update-desktop-database (string-append
1063 #+desktop-file-utils
1064 "/bin/update-desktop-database")))
1065 (mkdir-p (string-append #$output "/share"))
1066 (union-build destdir appdirs
1067 #:log-port (%make-void-port "w"))
1068 (exit (zero? (system* update-desktop-database destdir)))))))
1070 ;; Don't run the hook when 'glib' is not referenced.
1072 (gexp->derivation "xdg-desktop-database" build
1074 #:substitutable? #f)
1077 (define (xdg-mime-database manifest)
1078 "Return a derivation that builds the @file{mime.cache} database from manifest
1079 entries. It's used to query the MIME type of a given file."
1080 (define shared-mime-info ; lazy reference
1081 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1083 (mlet %store-monad ((glib
1084 (manifest-lookup-package
1087 (with-imported-modules '((guix build utils)
1090 (use-modules (srfi srfi-26)
1093 (let* ((datadir (string-append #$output "/share"))
1094 (destdir (string-append datadir "/mime"))
1095 (pkgdirs (filter file-exists?
1096 (map (cut string-append <>
1097 "/share/mime/packages")
1098 (cons #+shared-mime-info
1099 '#$(manifest-inputs manifest)))))
1100 (update-mime-database (string-append
1102 "/bin/update-mime-database")))
1104 (union-build (string-append destdir "/packages") pkgdirs
1105 #:log-port (%make-void-port "w"))
1106 (setenv "XDG_DATA_HOME" datadir)
1107 (exit (zero? (system* update-mime-database destdir)))))))
1109 ;; Don't run the hook when there are no GLib based applications.
1111 (gexp->derivation "xdg-mime-database" build
1113 #:substitutable? #f)
1116 ;; Several font packages may install font files into same directory, so
1117 ;; fonts.dir and fonts.scale file should be generated here, instead of in
1119 (define (fonts-dir-file manifest)
1120 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
1121 files for the fonts of the @var{manifest} entries."
1123 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1126 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1130 (use-modules (srfi srfi-26)
1133 (let ((fonts-dirs (filter file-exists?
1134 (map (cut string-append <>
1136 '#$(manifest-inputs manifest)))))
1138 (if (null? fonts-dirs)
1140 (let* ((share-dir (string-append #$output "/share"))
1141 (fonts-dir (string-append share-dir "/fonts"))
1142 (mkfontscale (string-append #+mkfontscale
1143 "/bin/mkfontscale"))
1144 (mkfontdir (string-append #+mkfontdir
1146 (empty-file? (lambda (filename)
1147 (call-with-ascii-input-file filename
1149 (eqv? #\0 (read-char p))))))
1150 (fonts-dir-file "fonts.dir")
1151 (fonts-scale-file "fonts.scale"))
1153 ;; Create all sub-directories, because we may create fonts.dir
1154 ;; and fonts.scale files in the sub-directories.
1155 (union-build fonts-dir fonts-dirs
1156 #:log-port (%make-void-port "w")
1157 #:create-all-directories? #t)
1158 (let ((directories (find-files fonts-dir
1160 (eq? 'directory (stat:type stat)))
1161 #:directories? #t)))
1162 (for-each (lambda (dir)
1163 (with-directory-excursion dir
1164 (when (file-exists? fonts-scale-file)
1165 (delete-file fonts-scale-file))
1166 (when (file-exists? fonts-dir-file)
1167 (delete-file fonts-dir-file))
1168 (unless (and (zero? (system* mkfontscale))
1169 (zero? (system* mkfontdir)))
1171 (when (and (file-exists? fonts-scale-file)
1172 (empty-file? fonts-scale-file))
1173 (delete-file fonts-scale-file))
1174 (when (and (file-exists? fonts-dir-file)
1175 (empty-file? fonts-dir-file))
1176 (delete-file fonts-dir-file))))
1179 (gexp->derivation "fonts-dir" build
1180 #:modules '((guix build utils)
1184 #:substitutable? #f))
1186 (define (manual-database manifest)
1187 "Return a derivation that builds the manual page database (\"mandb\") for
1188 the entries in MANIFEST."
1190 (module-ref (resolve-interface '(gnu packages guile))
1194 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
1197 (scheme-file "config.scm"
1199 (define-module (guix config)
1203 #+(file-append zlib "/lib/libz")))))
1206 (cons `((guix config) => ,config.scm)
1207 (delete '(guix config)
1208 (source-module-closure `((guix build utils)
1212 (with-imported-modules modules
1213 (with-extensions (list gdbm-ffi) ;for (guix man-db)
1215 (use-modules (guix man-db)
1220 (define (compute-entries)
1221 (append-map (lambda (directory)
1222 (let ((man (string-append directory "/share/man")))
1223 (if (directory-exists? man)
1226 '#$(manifest-inputs manifest)))
1228 (define man-directory
1229 (string-append #$output "/share/man"))
1231 (mkdir-p man-directory)
1233 (format #t "Creating manual page database...~%")
1235 (let* ((start (current-time))
1236 (entries (compute-entries))
1237 (_ (write-mandb-database (string-append man-directory
1240 (duration (time-difference (current-time) start)))
1241 (format #t "~a entries processed in ~,1f s~%"
1243 (+ (time-second duration)
1244 (* (time-nanosecond duration) (expt 10 -9))))
1247 (gexp->derivation "manual-database" build
1249 ;; Work around GDBM 1.13 issue whereby uninitialized bytes
1250 ;; get written to disk:
1251 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
1252 #:env-vars `(("MALLOC_PERTURB_" . "1"))
1256 (define %default-profile-hooks
1257 ;; This is the list of derivation-returning procedures that are called by
1258 ;; default when making a non-empty profile.
1262 ghc-package-cache-file
1263 ca-certificate-bundle
1267 xdg-desktop-database
1270 (define* (profile-derivation manifest
1272 (hooks %default-profile-hooks)
1274 (allow-collisions? #f)
1275 (relative-symlinks? #f)
1277 "Return a derivation that builds a profile (aka. 'user environment') with
1278 the given MANIFEST. The profile includes additional derivations returned by
1279 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
1280 Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1281 entries in MANIFEST collide (for instance if there are two same-name packages
1282 with a different version number.)
1284 When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
1285 a dependency on the 'glibc-utf8-locales' package.
1287 When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
1288 This is one of the things to do for the result to be relocatable.
1290 When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1291 are cross-built for TARGET."
1292 (mlet* %store-monad ((system (if system
1295 (ok? (if allow-collisions?
1297 (check-for-collisions manifest system
1299 (extras (if (null? (manifest-entries manifest))
1301 (sequence %store-monad
1306 (append (filter-map (lambda (drv)
1307 (and (derivation? drv)
1310 (manifest-inputs manifest)))
1312 (define glibc-utf8-locales ;lazy reference
1313 (module-ref (resolve-interface '(gnu packages base))
1314 'glibc-utf8-locales))
1316 (define set-utf8-locale
1317 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1318 ;; install a UTF-8 locale.
1321 #$(file-append glibc-utf8-locales "/lib/locale/"
1322 (version-major+minor
1323 (package-version glibc-utf8-locales))))
1324 (setlocale LC_ALL "en_US.utf8")))
1327 (with-imported-modules '((guix build profiles)
1333 (use-modules (guix build profiles)
1337 (setvbuf (current-output-port) _IOLBF)
1338 (setvbuf (current-error-port) _IOLBF)
1340 #+(if locales? set-utf8-locale #t)
1342 (define search-paths
1343 ;; Search paths of MANIFEST's packages, converted back to their
1345 (map sexp->search-path-specification
1347 '#$(map search-path-specification->sexp
1348 (append-map manifest-entry-search-paths
1349 (manifest-entries manifest))))))
1351 (build-profile #$output '#$inputs
1352 #:symlink #$(if relative-symlinks?
1355 #:manifest '#$(manifest->gexp manifest)
1356 #:search-paths search-paths))))
1358 (gexp->derivation "profile" builder
1362 ;; Don't complain about _IO* on Guile 2.2.
1363 #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
1365 ;; Not worth offloading.
1368 ;; Disable substitution because it would trigger a
1369 ;; connection to the substitute server, which is likely
1370 ;; to have no substitute to offer.
1371 #:substitutable? #f)))
1373 (define (profile-regexp profile)
1374 "Return a regular expression that matches PROFILE's name and number."
1375 (make-regexp (string-append "^" (regexp-quote (basename profile))
1378 (define (generation-number profile)
1379 "Return PROFILE's number or 0. An absolute file name must be used."
1380 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
1381 (basename (readlink profile))))
1382 (compose string->number (cut match:substring <> 1)))
1385 (define (generation-numbers profile)
1386 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
1387 former profiles were found."
1388 (match (scandir (dirname profile)
1389 (cute regexp-exec (profile-regexp profile) <>))
1390 (#f ; no profile directory
1394 ((profiles ...) ; former profiles around
1395 (sort (map (compose string->number
1396 (cut match:substring <> 1)
1397 (cute regexp-exec (profile-regexp profile) <>))
1401 (define (profile-generations profile)
1402 "Return a list of PROFILE's generations."
1403 (let ((generations (generation-numbers profile)))
1404 (if (equal? generations '(0))
1408 (define (relative-generation-spec->number profile spec)
1409 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
1410 may be a N, -N, or +N, where N is a number. If the spec is N, then the number
1411 returned is N. If it is -N, then the number returned is the profile's current
1412 generation number minus N. If it is +N, then the number returned is the
1413 profile's current generation number plus N. Return #f if there is no such
1415 (let ((number (string->number spec)))
1417 (case (string-ref spec 0)
1419 (relative-generation profile number))
1420 (else (if (memv number (profile-generations profile))
1425 (define* (relative-generation profile shift #:optional
1426 (current (generation-number profile)))
1427 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
1428 SHIFT is a positive or negative number.
1429 Return #f if there is no such generation."
1430 (let* ((abs-shift (abs shift))
1431 (numbers (profile-generations profile))
1432 (from-current (memq current
1433 (if (negative? shift)
1437 (< abs-shift (length from-current))
1438 (list-ref from-current abs-shift))))
1440 (define* (previous-generation-number profile #:optional
1441 (number (generation-number profile)))
1442 "Return the number of the generation before generation NUMBER of
1443 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
1444 case when generations have been deleted (there are \"holes\")."
1445 (or (relative-generation profile -1 number)
1448 (define (generation-file-name profile generation)
1449 "Return the file name for PROFILE's GENERATION."
1450 (format #f "~a-~a-link" profile generation))
1452 (define (generation-time profile number)
1453 "Return the creation time of a generation in the UTC format."
1454 (make-time time-utc 0
1455 (stat:ctime (stat (generation-file-name profile number)))))
1457 (define (link-to-empty-profile store generation)
1458 "Link GENERATION, a string, to the empty profile. An error is raised if
1460 (let* ((drv (run-with-store store
1461 (profile-derivation (manifest '())
1463 (prof (derivation->output-path drv "out")))
1464 (build-derivations store (list drv))
1465 (switch-symlinks generation prof)))
1467 (define (switch-to-generation profile number)
1468 "Atomically switch PROFILE to the generation NUMBER. Return the number of
1469 the generation that was current before switching."
1470 (let ((current (generation-number profile))
1471 (generation (generation-file-name profile number)))
1472 (cond ((not (file-exists? profile))
1473 (raise (condition (&profile-not-found-error
1474 (profile profile)))))
1475 ((not (file-exists? generation))
1476 (raise (condition (&missing-generation-error
1478 (generation number)))))
1480 (switch-symlinks profile generation)
1483 (define (switch-to-previous-generation profile)
1484 "Atomically switch PROFILE to the previous generation. Return the former
1485 generation number and the current one."
1486 (let ((previous (previous-generation-number profile)))
1487 (values (switch-to-generation profile previous)
1490 (define (roll-back store profile)
1491 "Roll back to the previous generation of PROFILE. Return the number of the
1492 generation that was current before switching and the new generation number."
1493 (let* ((number (generation-number profile))
1494 (previous-number (previous-generation-number profile number))
1495 (previous-generation (generation-file-name profile previous-number)))
1496 (cond ((not (file-exists? profile)) ;invalid profile
1497 (raise (condition (&profile-not-found-error
1498 (profile profile)))))
1499 ((zero? number) ;empty profile
1500 (values number number))
1501 ((or (zero? previous-number) ;going to emptiness
1502 (not (file-exists? previous-generation)))
1503 (link-to-empty-profile store previous-generation)
1504 (switch-to-previous-generation profile))
1505 (else ;anything else
1506 (switch-to-previous-generation profile)))))
1508 (define (delete-generation store profile number)
1509 "Delete generation with NUMBER from PROFILE. Return the file name of the
1510 generation that has been deleted, or #f if nothing was done (for instance
1511 because the NUMBER is zero.)"
1512 (define (delete-and-return)
1513 (let ((generation (generation-file-name profile number)))
1514 (delete-file generation)
1517 (let* ((current-number (generation-number profile))
1518 (previous-number (previous-generation-number profile number))
1519 (previous-generation (generation-file-name profile previous-number)))
1520 (cond ((zero? number) #f) ;do not delete generation 0
1521 ((and (= number current-number)
1522 (not (file-exists? previous-generation)))
1523 (link-to-empty-profile store previous-generation)
1524 (switch-to-previous-generation profile)
1525 (delete-and-return))
1526 ((= number current-number)
1527 (roll-back store profile)
1528 (delete-and-return))
1530 (delete-and-return)))))
1532 (define %user-profile-directory
1533 (and=> (getenv "HOME")
1534 (cut string-append <> "/.guix-profile")))
1536 (define %profile-directory
1537 (string-append %state-directory "/profiles/"
1538 (or (and=> (or (getenv "USER")
1540 (cut string-append "per-user/" <>))
1543 (define %current-profile
1544 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
1545 ;; coexist with Nix profiles.
1546 (string-append %profile-directory "/guix-profile"))
1548 (define (canonicalize-profile profile)
1549 "If PROFILE is %USER-PROFILE-DIRECTORY, return %CURRENT-PROFILE. Otherwise
1550 return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile' as if
1551 '-p' was omitted." ; see <http://bugs.gnu.org/17939>
1553 ;; Trim trailing slashes so that the basename comparison below works as
1555 (let ((profile (string-trim-right profile #\/)))
1556 (if (and %user-profile-directory
1557 (string=? (canonicalize-path (dirname profile))
1558 (dirname %user-profile-directory))
1559 (string=? (basename profile) (basename %user-profile-directory)))
1563 (define (user-friendly-profile profile)
1564 "Return either ~/.guix-profile if that's what PROFILE refers to, directly or
1565 indirectly, or PROFILE."
1566 (if (and %user-profile-directory
1568 (string=? (readlink %user-profile-directory) profile)))
1569 %user-profile-directory
1572 ;;; profiles.scm ends here