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
117 relative-generation-spec->number
119 previous-generation-number
126 %user-profile-directory
130 user-friendly-profile))
134 ;;; Tools to create and manipulate profiles---i.e., the representation of a
135 ;;; set of installed packages.
144 (define-condition-type &profile-error &error
146 (profile profile-error-profile))
148 (define-condition-type &profile-not-found-error &profile-error
149 profile-not-found-error?)
151 (define-condition-type &profile-collision-error &error
152 profile-collision-error?
153 (entry profile-collision-error-entry) ;<manifest-entry>
154 (conflict profile-collision-error-conflict)) ;<manifest-entry>
156 (define-condition-type &missing-generation-error &profile-error
157 missing-generation-error?
158 (generation missing-generation-error-generation))
165 (define-record-type <manifest>
168 (entries manifest-entries)) ; list of <manifest-entry>
170 ;; Convenient alias, to avoid name clashes.
171 (define make-manifest manifest)
173 (define-record-type* <manifest-entry> manifest-entry
176 (name manifest-entry-name) ; string
177 (version manifest-entry-version) ; string
178 (output manifest-entry-output ; string
180 (item manifest-entry-item) ; package | file-like | store path
181 (dependencies manifest-entry-dependencies ; <manifest-entry>*
183 (search-paths manifest-entry-search-paths ; search-path-specification*
185 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
186 (default (delay #f)))
187 (properties manifest-entry-properties ; list of symbol/value pairs
190 (define-record-type* <manifest-pattern> manifest-pattern
191 make-manifest-pattern
193 (name manifest-pattern-name) ; string
194 (version manifest-pattern-version ; string | #f
196 (output manifest-pattern-output ; string | #f
199 (define (manifest-transitive-entries manifest)
200 "Return the entries of MANIFEST along with their propagated inputs,
202 (let loop ((entries (manifest-entries manifest))
204 (visited (set))) ;compare with 'equal?'
209 (if (set-contains? visited head)
210 (loop tail result visited)
211 (loop (append (manifest-entry-dependencies head)
214 (set-insert head visited)))))))
216 (define (profile-manifest profile)
217 "Return the PROFILE's manifest."
218 (let ((file (string-append profile "/manifest")))
219 (if (file-exists? file)
220 (call-with-input-file file read-manifest)
223 (define (manifest-entry-lookup manifest)
224 "Return a lookup procedure for the entries of MANIFEST. The lookup
225 procedure takes two arguments: the entry name and output."
227 (let loop ((entries (manifest-entries manifest))
228 (mapping vlist-null))
229 (fold (lambda (entry result)
230 (vhash-cons (cons (manifest-entry-name entry)
231 (manifest-entry-output entry))
233 (loop (manifest-entry-dependencies entry)
238 (lambda (name output)
239 (match (vhash-assoc (cons name output) mapping)
243 (define* (lower-manifest-entry entry system #:key target)
244 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
246 (let ((item (manifest-entry-item entry)))
248 (with-monad %store-monad
250 (mlet %store-monad ((drv (lower-object item system
252 (output -> (manifest-entry-output entry)))
253 (return (manifest-entry
255 (item (derivation->output-path drv output))))))))
257 (define* (check-for-collisions manifest system #:key target)
258 "Check whether the entries of MANIFEST conflict with one another; raise a
259 '&profile-collision-error' when a conflict is encountered."
261 (manifest-entry-lookup manifest))
263 (with-monad %store-monad
265 (lambda (entry result)
266 (match (lookup (manifest-entry-name entry)
267 (manifest-entry-output entry))
268 ((? manifest-entry? second) ;potential conflict
269 (mlet %store-monad ((first (lower-manifest-entry entry system
272 (second (lower-manifest-entry second system
275 (if (string=? (manifest-entry-item first)
276 (manifest-entry-item second))
279 (&profile-collision-error
281 (conflict second)))))))
285 (manifest-transitive-entries manifest))))
287 (define* (package->manifest-entry package #:optional (output "out")
288 #:key (parent (delay #f)))
289 "Return a manifest entry for the OUTPUT of package PACKAGE."
290 ;; For each dependency, keep a promise pointing to its "parent" entry.
291 (letrec* ((deps (map (match-lambda
293 (package->manifest-entry package
294 #:parent (delay entry)))
295 ((label package output)
296 (package->manifest-entry package output
297 #:parent (delay entry))))
298 (package-propagated-inputs package)))
299 (entry (manifest-entry
300 (name (package-name package))
301 (version (package-version package))
304 (dependencies (delete-duplicates deps))
306 (package-transitive-native-search-paths package))
310 (define (packages->manifest packages)
311 "Return a list of manifest entries, one for each item listed in PACKAGES.
312 Elements of PACKAGES can be either package objects or package/string tuples
313 denoting a specific output of a package."
317 (package->manifest-entry package output))
318 ((? package? package)
319 (package->manifest-entry package)))
322 (define (manifest->gexp manifest)
323 "Return a representation of MANIFEST as a gexp."
324 (define (entry->gexp entry)
326 (($ <manifest-entry> name version output (? string? path)
327 (deps ...) (search-paths ...) _ (properties ...))
328 #~(#$name #$version #$output #$path
329 (propagated-inputs #$(map entry->gexp deps))
330 (search-paths #$(map search-path-specification->sexp
332 (properties . #$properties)))
333 (($ <manifest-entry> name version output package
334 (deps ...) (search-paths ...) _ (properties ...))
335 #~(#$name #$version #$output
336 (ungexp package (or output "out"))
337 (propagated-inputs #$(map entry->gexp deps))
338 (search-paths #$(map search-path-specification->sexp
340 (properties . #$properties)))))
343 (($ <manifest> (entries ...))
344 #~(manifest (version 3)
345 (packages #$(map entry->gexp entries))))))
347 (define (find-package name version)
348 "Return a package from the distro matching NAME and possibly VERSION. This
349 procedure is here for backward-compatibility and will eventually vanish."
350 (define find-best-packages-by-name ;break abstractions
351 (module-ref (resolve-interface '(gnu packages))
352 'find-best-packages-by-name))
354 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
355 ;; former traverses the module tree only once and then allows for efficient
356 ;; access via a vhash.
357 (match (find-best-packages-by-name name version)
360 (match (find-best-packages-by-name name #f)
364 (define (sexp->manifest sexp)
365 "Parse SEXP as a manifest."
366 (define (infer-search-paths name version)
367 ;; Infer the search path specifications for NAME-VERSION by looking up a
368 ;; same-named package in the distro. Useful for the old manifest formats
369 ;; that did not store search path info.
370 (let ((package (find-package name version)))
372 (package-native-search-paths package)
375 (define (infer-dependency item parent)
376 ;; Return a <manifest-entry> for ITEM.
377 (let-values (((name version)
378 (package-name->name+version
379 (store-path-package-name item))))
386 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
388 ((name version output path
389 ('propagated-inputs deps)
390 ('search-paths search-paths)
392 ;; For each of DEPS, keep a promise pointing to ENTRY.
393 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
395 (entry (manifest-entry
401 (search-paths (map sexp->search-path-specification
404 (properties (or (assoc-ref extra-stuff 'properties)
409 (('manifest ('version 0)
410 ('packages ((name version output path) ...)))
412 (map (lambda (name version output path)
418 (search-paths (infer-search-paths name version))))
419 name version output path)))
421 ;; Version 1 adds a list of propagated inputs to the
422 ;; name/version/output/path tuples.
423 (('manifest ('version 1)
424 ('packages ((name version output path deps) ...)))
426 (map (lambda (name version output path deps)
427 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
428 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
430 (let ((deps (match deps
431 (((labels directories) ...)
435 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
437 (entry (manifest-entry
444 (infer-search-paths name version)))))
446 name version output path deps)))
448 ;; Version 2 adds search paths and is slightly more verbose.
449 (('manifest ('version 2 minor-version ...)
450 ('packages ((name version output path
451 ('propagated-inputs deps)
452 ('search-paths search-paths)
456 (map (lambda (name version output path deps search-paths)
457 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
459 (entry (manifest-entry
466 (map sexp->search-path-specification
469 name version output path deps search-paths)))
471 ;; Version 3 represents DEPS as full-blown manifest entries.
472 (('manifest ('version 3 minor-version ...)
473 ('packages (entries ...)))
474 (manifest (map sexp->manifest-entry entries)))
477 (&message (message "unsupported manifest format")))))))
479 (define (read-manifest port)
480 "Return the packages listed in MANIFEST."
481 (sexp->manifest (read port)))
483 (define (entry-predicate pattern)
484 "Return a procedure that returns #t when passed a manifest entry that
485 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
488 (($ <manifest-pattern> name version output)
490 (($ <manifest-entry> entry-name entry-version entry-output)
491 (and (string=? entry-name name)
492 (or (not entry-output) (not output)
493 (string=? entry-output output))
495 (string=? entry-version version))))))))
497 (define (manifest-remove manifest patterns)
498 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
499 must be a manifest-pattern."
500 (define (remove-entry pattern lst)
501 (remove (entry-predicate pattern) lst))
503 (make-manifest (fold remove-entry
504 (manifest-entries manifest)
507 (define (manifest-add manifest entries)
508 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
509 Remove MANIFEST entries that have the same name and output as ENTRIES."
510 (define (same-entry? entry name output)
512 (($ <manifest-entry> entry-name _ entry-output _)
513 (and (equal? name entry-name)
514 (equal? output entry-output)))))
517 (fold (lambda (entry result) ;XXX: quadratic
519 (($ <manifest-entry> name _ out _)
521 (remove (cut same-entry? <> name out)
523 (manifest-entries manifest)
526 (define (manifest-lookup manifest pattern)
527 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
529 (find (entry-predicate pattern)
530 (manifest-entries manifest)))
532 (define (manifest-installed? manifest pattern)
533 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
535 (->bool (manifest-lookup manifest pattern)))
537 (define (manifest-matching-entries manifest patterns)
538 "Return all the entries of MANIFEST that match one of the PATTERNS."
540 (map entry-predicate patterns))
542 (define (matches? entry)
547 (filter matches? (manifest-entries manifest)))
549 (define (manifest-search-paths manifest)
550 "Return the list of search path specifications that apply to MANIFEST,
551 including the search path specification for $PATH."
554 (append-map manifest-entry-search-paths
555 (manifest-entries manifest)))))
559 ;;; Manifest transactions.
562 (define-record-type* <manifest-transaction> manifest-transaction
563 make-manifest-transaction
564 manifest-transaction?
565 (install manifest-transaction-install ; list of <manifest-entry>
567 (remove manifest-transaction-remove ; list of <manifest-pattern>
570 (define (manifest-transaction-install-entry entry transaction)
571 "Augment TRANSACTION's set of installed packages with ENTRY, a
573 (manifest-transaction
574 (inherit transaction)
576 (cons entry (manifest-transaction-install transaction)))))
578 (define (manifest-transaction-remove-pattern pattern transaction)
579 "Add PATTERN to TRANSACTION's list of packages to remove."
580 (manifest-transaction
581 (inherit transaction)
583 (cons pattern (manifest-transaction-remove transaction)))))
585 (define (manifest-transaction-null? transaction)
586 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
589 (($ <manifest-transaction> () ()) #t)
590 (($ <manifest-transaction> _ _) #f)))
592 (define (manifest-transaction-removal-candidate? entry transaction)
593 "Return true if ENTRY is a candidate for removal in TRANSACTION."
594 (any (lambda (pattern)
595 ((entry-predicate pattern) entry))
596 (manifest-transaction-remove transaction)))
598 (define (manifest-transaction-effects manifest transaction)
599 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
600 the list of packages that would be removed, installed, upgraded, or downgraded
601 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
602 where the head is the entry being upgraded and the tail is the entry that will
604 (define (manifest-entry->pattern entry)
606 (name (manifest-entry-name entry))
607 (output (manifest-entry-output entry))))
609 (let loop ((input (manifest-transaction-install transaction))
615 (let ((remove (manifest-transaction-remove transaction)))
616 (values (manifest-matching-entries manifest remove)
617 (reverse install) (reverse upgrade) (reverse downgrade))))
619 ;; Check whether installing ENTRY corresponds to the installation of a
620 ;; new package or to an upgrade.
622 ;; XXX: When the exact same output directory is installed, we're not
623 ;; really upgrading anything. Add a check for that case.
624 (let* ((pattern (manifest-entry->pattern entry))
625 (previous (manifest-lookup manifest pattern))
626 (newer? (and previous
627 (version>=? (manifest-entry-version entry)
628 (manifest-entry-version previous)))))
630 (if previous install (cons entry install))
631 (if (and previous newer?)
632 (alist-cons previous entry upgrade)
634 (if (and previous (not newer?))
635 (alist-cons previous entry downgrade)
638 (define (manifest-perform-transaction manifest transaction)
639 "Perform TRANSACTION on MANIFEST and return the new manifest."
640 (let ((install (manifest-transaction-install transaction))
641 (remove (manifest-transaction-remove transaction)))
642 (manifest-add (manifest-remove manifest remove)
650 (define (manifest-inputs manifest)
651 "Return a list of <gexp-input> objects for MANIFEST."
654 (($ <manifest-entry> name version output thing deps)
655 ;; THING may be a package or a file name. In the latter case, assume
656 ;; it's already valid.
657 (cons (gexp-input thing output)
658 (append-map entry->input deps)))))
660 (append-map entry->input (manifest-entries manifest)))
662 (define* (manifest-lookup-package manifest name #:optional version)
663 "Return as a monadic value the first package or store path referenced by
664 MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
666 ;; Return as a monadic value the package or store path referenced by the
667 ;; manifest ENTRY, or #f if not referenced.
668 (define (entry-lookup-package entry)
669 (define (find-among-inputs inputs)
670 (find (lambda (input)
671 (and (package? input)
672 (equal? name (package-name input))
674 (string-prefix? version (package-version input))
677 (define (find-among-store-items items)
679 (let-values (((name* version*)
680 (package-name->name+version
681 (store-path-package-name item))))
682 (and (string=? name name*)
684 (string-prefix? version version*)
688 (with-monad %store-monad
689 (match (manifest-entry-item entry)
690 ((? package? package)
691 (match (cons (list (package-name package) package)
692 (package-transitive-inputs package))
693 (((labels inputs . _) ...)
694 (return (find-among-inputs inputs)))))
696 (mlet %store-monad ((refs (references* item)))
697 (return (find-among-store-items refs))))
699 ;; XXX: ITEM might be a 'computed-file' or anything like that, in
700 ;; which case we don't know what to do. The fix may be to check
701 ;; references once ITEM is compiled, as proposed at
702 ;; <https://bugs.gnu.org/29927>.
706 entry-lookup-package (manifest-entries manifest)))
708 (define (info-dir-file manifest)
709 "Return a derivation that builds the 'dir' file for all the entries of
711 (define texinfo ;lazy reference
712 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
713 (define gzip ;lazy reference
714 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
715 (define glibc-utf8-locales ;lazy reference
716 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
719 (with-imported-modules '((guix build utils))
721 (use-modules (guix build utils)
722 (srfi srfi-1) (srfi srfi-26)
725 (define (info-file? file)
726 (or (string-suffix? ".info" file)
727 (string-suffix? ".info.gz" file)))
729 (define (info-files top)
730 (let ((infodir (string-append top "/share/info")))
731 (map (cut string-append infodir "/" <>)
732 (or (scandir infodir info-file?) '()))))
734 (define (info-file-language file)
735 (let* ((base (if (string-suffix? ".gz" file)
736 (basename file ".info.gz")
737 (basename file ".info")))
738 (dot (string-rindex base #\.)))
740 (string-drop base (+ 1 dot))
743 (define (install-info info)
744 (let ((language (info-file-language info)))
745 ;; We need to choose a valid locale for $LANGUAGE to be honored.
746 (setenv "LC_ALL" "en_US.utf8")
747 (setenv "LANGUAGE" language)
749 (system* #+(file-append texinfo "/bin/install-info")
751 (apply string-append #$output "/share/info/dir"
752 (if (string=? "en" language)
754 `("." ,language)))))))
756 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
757 (setenv "GUIX_LOCPATH"
758 #+(file-append glibc-utf8-locales "/lib/locale"))
760 (mkdir-p (string-append #$output "/share/info"))
761 (exit (every install-info
762 (append-map info-files
763 '#$(manifest-inputs manifest)))))))
765 (gexp->derivation "info-dir" build
767 #:substitutable? #f))
769 (define (ghc-package-cache-file manifest)
770 "Return a derivation that builds the GHC 'package.cache' file for all the
771 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
772 (define ghc ;lazy reference
773 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
776 (with-imported-modules '((guix build utils))
778 (use-modules (guix build utils)
779 (srfi srfi-1) (srfi srfi-26)
782 (define ghc-name-version
783 (let* ((base (basename #+ghc)))
785 (+ 1 (string-index base #\-)))))
788 (string-append "lib/" ghc-name-version "/package.conf.d"))
791 (string-append #$output "/" db-subdir))
793 (define (conf-files top)
794 (let ((db (string-append top "/" db-subdir)))
795 (if (file-exists? db)
796 (find-files db "\\.conf$")
799 (define (copy-conf-file conf)
800 (let ((base (basename conf)))
801 (copy-file conf (string-append db-dir "/" base))))
803 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
804 (for-each copy-conf-file
805 (append-map conf-files
807 '#$(manifest-inputs manifest))))
810 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
811 (string-append "--package-db=" db-dir)))))
812 (for-each delete-file (find-files db-dir "\\.conf$"))
815 (with-monad %store-monad
816 ;; Don't depend on GHC when there's nothing to do.
817 (if (any (cut string-prefix? "ghc" <>)
818 (map manifest-entry-name (manifest-entries manifest)))
819 (gexp->derivation "ghc-package-cache" build
824 (define (ca-certificate-bundle manifest)
825 "Return a derivation that builds a single-file bundle containing the CA
826 certificates in the /etc/ssl/certs sub-directories of the packages in
827 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
828 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
831 (define glibc-utf8-locales ;lazy reference
832 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
835 (with-imported-modules '((guix build utils))
837 (use-modules (guix build utils)
844 (define (pem-file? file)
845 (string-suffix? ".pem" file))
847 (define (ca-files top)
848 (let ((cert-dir (string-append top "/etc/ssl/certs")))
849 (map (cut string-append cert-dir "/" <>)
850 (or (scandir cert-dir pem-file?) '()))))
852 (define (concatenate-files files result)
853 "Make RESULT the concatenation of all of FILES."
854 (define (dump file port)
855 (display (call-with-input-file file get-string-all)
857 (newline port)) ;required, see <https://bugs.debian.org/635570>
859 (call-with-output-file result
861 (for-each (cut dump <> port) files))))
863 ;; Some file names in the NSS certificates are UTF-8 encoded so
864 ;; install a UTF-8 locale.
866 (string-append #+glibc-utf8-locales "/lib/locale/"
867 #+(version-major+minor
868 (package-version glibc-utf8-locales))))
869 (setlocale LC_ALL "en_US.utf8")
871 (match (append-map ca-files '#$(manifest-inputs manifest))
873 ;; Since there are no CA files, just create an empty directory. Do
874 ;; not create the etc/ssl/certs sub-directory, since that would
875 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
880 (let ((result (string-append #$output "/etc/ssl/certs")))
882 (concatenate-files ca-files
883 (string-append result
884 "/ca-certificates.crt"))
887 (gexp->derivation "ca-certificate-bundle" build
889 #:substitutable? #f))
891 (define (glib-schemas manifest)
892 "Return a derivation that unions all schemas from manifest entries and
893 creates the Glib 'gschemas.compiled' file."
894 (define glib ; lazy reference
895 (module-ref (resolve-interface '(gnu packages glib)) 'glib))
897 (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
898 ;; XXX: Can't use glib-compile-schemas corresponding
899 ;; to the glib referenced by 'manifest'. Because
900 ;; '%glib' can be either a package or store path, and
901 ;; there's no way to get the "bin" output for the later.
902 (glib-compile-schemas
903 -> #~(string-append #+glib:bin
904 "/bin/glib-compile-schemas")))
907 (with-imported-modules '((guix build utils)
909 (guix build profiles)
913 (use-modules (guix build utils)
915 (guix build profiles)
918 (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
919 (schemadirs (filter file-exists?
920 (map (cut string-append <> "/share/glib-2.0/schemas")
921 '#$(manifest-inputs manifest)))))
923 ;; Union all the schemas.
924 (mkdir-p (string-append #$output "/share/glib-2.0"))
925 (union-build destdir schemadirs
926 #:log-port (%make-void-port "w"))
929 (when (file-is-directory? dir)
930 (ensure-writable-directory dir)
931 (invoke #+glib-compile-schemas
932 (string-append "--targetdir=" dir)
935 ;; Don't run the hook when there's nothing to do.
937 (gexp->derivation "glib-schemas" build
942 (define (gtk-icon-themes manifest)
943 "Return a derivation that unions all icon themes from manifest entries and
944 creates the GTK+ 'icon-theme.cache' file for each theme."
945 (define gtk+ ; lazy reference
946 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
948 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
949 ;; XXX: Can't use gtk-update-icon-cache corresponding
950 ;; to the gtk+ referenced by 'manifest'. Because
951 ;; '%gtk+' can be either a package or store path, and
952 ;; there's no way to get the "bin" output for the later.
953 (gtk-update-icon-cache
954 -> #~(string-append #+gtk+:bin
955 "/bin/gtk-update-icon-cache")))
958 (with-imported-modules '((guix build utils)
960 (guix build profiles)
964 (use-modules (guix build utils)
966 (guix build profiles)
970 (let* ((destdir (string-append #$output "/share/icons"))
971 (icondirs (filter file-exists?
972 (map (cut string-append <> "/share/icons")
973 '#$(manifest-inputs manifest)))))
975 ;; Union all the icons.
976 (mkdir-p (string-append #$output "/share"))
977 (union-build destdir icondirs
978 #:log-port (%make-void-port "w"))
980 ;; Update the 'icon-theme.cache' file for each icon theme.
983 (let ((dir (string-append destdir "/" theme)))
984 ;; Occasionally DESTDIR contains plain files, such as
985 ;; "abiword_48.png". Ignore these.
986 (when (file-is-directory? dir)
987 (ensure-writable-directory dir)
988 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
989 (scandir destdir (negate (cut member <> '("." "..")))))))))
991 ;; Don't run the hook when there's nothing to do.
993 (gexp->derivation "gtk-icon-themes" build
998 (define (gtk-im-modules manifest)
999 "Return a derivation that builds the cache files for input method modules
1000 for both major versions of GTK+."
1002 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
1003 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
1005 (define (build gtk gtk-version query)
1006 (let ((major (string-take gtk-version 1)))
1007 (with-imported-modules '((guix build utils)
1009 (guix build profiles)
1013 (use-modules (guix build utils)
1015 (guix build profiles)
1020 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
1022 (destdir (string-append #$output prefix))
1023 (moddirs (cons (string-append #$gtk prefix "/immodules")
1024 (filter file-exists?
1025 (map (cut string-append <> prefix "/immodules")
1026 '#$(manifest-inputs manifest)))))
1027 (modules (append-map (cut find-files <> "\\.so$")
1030 ;; Generate a new immodules cache file.
1031 (mkdir-p (string-append #$output prefix))
1032 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
1033 (outfile (string-append #$output prefix
1034 "/immodules-gtk" #$major ".cache")))
1038 (call-with-output-file outfile
1040 (while (not (eof-object? (peek-char pipe)))
1041 (write-char (read-char pipe) out))))
1044 (close-pipe pipe)))))))))
1046 ;; Don't run the hook when there's nothing to do.
1047 (let* ((pkg-gtk+ (module-ref ; lazy reference
1048 (resolve-interface '(gnu packages gtk)) 'gtk+))
1053 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
1054 ;; output of latest gtk+ package.
1056 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
1062 #$gtk+-2 "/bin/gtk-query-immodules-2.0"))
1064 (if (or gtk+ gtk+-2)
1065 (gexp->derivation "gtk-im-modules" gexp
1067 #:substitutable? #f)
1070 (define (xdg-desktop-database manifest)
1071 "Return a derivation that builds the @file{mimeinfo.cache} database from
1072 desktop files. It's used to query what applications can handle a given
1074 (define desktop-file-utils ; lazy reference
1075 (module-ref (resolve-interface '(gnu packages freedesktop))
1076 'desktop-file-utils))
1078 (mlet %store-monad ((glib
1079 (manifest-lookup-package
1082 (with-imported-modules '((guix build utils)
1085 (use-modules (srfi srfi-26)
1088 (let* ((destdir (string-append #$output "/share/applications"))
1089 (appdirs (filter file-exists?
1090 (map (cut string-append <>
1091 "/share/applications")
1092 '#$(manifest-inputs manifest))))
1093 (update-desktop-database (string-append
1094 #+desktop-file-utils
1095 "/bin/update-desktop-database")))
1096 (mkdir-p (string-append #$output "/share"))
1097 (union-build destdir appdirs
1098 #:log-port (%make-void-port "w"))
1099 (exit (zero? (system* update-desktop-database destdir)))))))
1101 ;; Don't run the hook when 'glib' is not referenced.
1103 (gexp->derivation "xdg-desktop-database" build
1105 #:substitutable? #f)
1108 (define (xdg-mime-database manifest)
1109 "Return a derivation that builds the @file{mime.cache} database from manifest
1110 entries. It's used to query the MIME type of a given file."
1111 (define shared-mime-info ; lazy reference
1112 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1114 (mlet %store-monad ((glib
1115 (manifest-lookup-package
1118 (with-imported-modules '((guix build utils)
1121 (use-modules (srfi srfi-26)
1124 (let* ((datadir (string-append #$output "/share"))
1125 (destdir (string-append datadir "/mime"))
1126 (pkgdirs (filter file-exists?
1127 (map (cut string-append <>
1128 "/share/mime/packages")
1129 (cons #+shared-mime-info
1130 '#$(manifest-inputs manifest)))))
1131 (update-mime-database (string-append
1133 "/bin/update-mime-database")))
1135 (union-build (string-append destdir "/packages") pkgdirs
1136 #:log-port (%make-void-port "w"))
1137 (setenv "XDG_DATA_HOME" datadir)
1138 (exit (zero? (system* update-mime-database destdir)))))))
1140 ;; Don't run the hook when there are no GLib based applications.
1142 (gexp->derivation "xdg-mime-database" build
1144 #:substitutable? #f)
1147 ;; Several font packages may install font files into same directory, so
1148 ;; fonts.dir and fonts.scale file should be generated here, instead of in
1150 (define (fonts-dir-file manifest)
1151 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
1152 files for the fonts of the @var{manifest} entries."
1154 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1157 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1161 (use-modules (srfi srfi-26)
1164 (let ((fonts-dirs (filter file-exists?
1165 (map (cut string-append <>
1167 '#$(manifest-inputs manifest)))))
1169 (if (null? fonts-dirs)
1171 (let* ((share-dir (string-append #$output "/share"))
1172 (fonts-dir (string-append share-dir "/fonts"))
1173 (mkfontscale (string-append #+mkfontscale
1174 "/bin/mkfontscale"))
1175 (mkfontdir (string-append #+mkfontdir
1177 (empty-file? (lambda (filename)
1178 (call-with-ascii-input-file filename
1180 (eqv? #\0 (read-char p))))))
1181 (fonts-dir-file "fonts.dir")
1182 (fonts-scale-file "fonts.scale"))
1184 ;; Create all sub-directories, because we may create fonts.dir
1185 ;; and fonts.scale files in the sub-directories.
1186 (union-build fonts-dir fonts-dirs
1187 #:log-port (%make-void-port "w")
1188 #:create-all-directories? #t)
1189 (let ((directories (find-files fonts-dir
1191 (eq? 'directory (stat:type stat)))
1192 #:directories? #t)))
1193 (for-each (lambda (dir)
1194 (with-directory-excursion dir
1195 (when (file-exists? fonts-scale-file)
1196 (delete-file fonts-scale-file))
1197 (when (file-exists? fonts-dir-file)
1198 (delete-file fonts-dir-file))
1199 (unless (and (zero? (system* mkfontscale))
1200 (zero? (system* mkfontdir)))
1202 (when (and (file-exists? fonts-scale-file)
1203 (empty-file? fonts-scale-file))
1204 (delete-file fonts-scale-file))
1205 (when (and (file-exists? fonts-dir-file)
1206 (empty-file? fonts-dir-file))
1207 (delete-file fonts-dir-file))))
1210 (gexp->derivation "fonts-dir" build
1211 #:modules '((guix build utils)
1215 #:substitutable? #f))
1217 (define (manual-database manifest)
1218 "Return a derivation that builds the manual page database (\"mandb\") for
1219 the entries in MANIFEST."
1221 (module-ref (resolve-interface '(gnu packages guile))
1225 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
1228 (scheme-file "config.scm"
1230 (define-module (guix config)
1234 #+(file-append zlib "/lib/libz")))))
1237 (cons `((guix config) => ,config.scm)
1238 (delete '(guix config)
1239 (source-module-closure `((guix build utils)
1243 (with-imported-modules modules
1244 (with-extensions (list gdbm-ffi) ;for (guix man-db)
1246 (use-modules (guix man-db)
1251 (define (compute-entries)
1252 (append-map (lambda (directory)
1253 (let ((man (string-append directory "/share/man")))
1254 (if (directory-exists? man)
1257 '#$(manifest-inputs manifest)))
1259 (define man-directory
1260 (string-append #$output "/share/man"))
1262 (mkdir-p man-directory)
1264 (format #t "Creating manual page database...~%")
1266 (let* ((start (current-time))
1267 (entries (compute-entries))
1268 (_ (write-mandb-database (string-append man-directory
1271 (duration (time-difference (current-time) start)))
1272 (format #t "~a entries processed in ~,1f s~%"
1274 (+ (time-second duration)
1275 (* (time-nanosecond duration) (expt 10 -9))))
1278 (gexp->derivation "manual-database" build
1280 ;; Work around GDBM 1.13 issue whereby uninitialized bytes
1281 ;; get written to disk:
1282 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
1283 #:env-vars `(("MALLOC_PERTURB_" . "1"))
1287 (define %default-profile-hooks
1288 ;; This is the list of derivation-returning procedures that are called by
1289 ;; default when making a non-empty profile.
1293 ghc-package-cache-file
1294 ca-certificate-bundle
1298 xdg-desktop-database
1301 (define* (profile-derivation manifest
1303 (hooks %default-profile-hooks)
1305 (allow-collisions? #f)
1306 (relative-symlinks? #f)
1308 "Return a derivation that builds a profile (aka. 'user environment') with
1309 the given MANIFEST. The profile includes additional derivations returned by
1310 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
1311 Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1312 entries in MANIFEST collide (for instance if there are two same-name packages
1313 with a different version number.)
1315 When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
1316 a dependency on the 'glibc-utf8-locales' package.
1318 When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
1319 This is one of the things to do for the result to be relocatable.
1321 When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1322 are cross-built for TARGET."
1323 (mlet* %store-monad ((system (if system
1326 (ok? (if allow-collisions?
1328 (check-for-collisions manifest system
1330 (extras (if (null? (manifest-entries manifest))
1332 (sequence %store-monad
1337 (append (filter-map (lambda (drv)
1338 (and (derivation? drv)
1341 (manifest-inputs manifest)))
1343 (define glibc-utf8-locales ;lazy reference
1344 (module-ref (resolve-interface '(gnu packages base))
1345 'glibc-utf8-locales))
1347 (define set-utf8-locale
1348 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1349 ;; install a UTF-8 locale.
1352 #$(file-append glibc-utf8-locales "/lib/locale/"
1353 (version-major+minor
1354 (package-version glibc-utf8-locales))))
1355 (setlocale LC_ALL "en_US.utf8")))
1358 (with-imported-modules '((guix build profiles)
1364 (use-modules (guix build profiles)
1368 (setvbuf (current-output-port) _IOLBF)
1369 (setvbuf (current-error-port) _IOLBF)
1371 #+(if locales? set-utf8-locale #t)
1373 (define search-paths
1374 ;; Search paths of MANIFEST's packages, converted back to their
1376 (map sexp->search-path-specification
1378 '#$(map search-path-specification->sexp
1379 (manifest-search-paths manifest)))))
1381 (build-profile #$output '#$inputs
1382 #:symlink #$(if relative-symlinks?
1385 #:manifest '#$(manifest->gexp manifest)
1386 #:search-paths search-paths))))
1388 (gexp->derivation "profile" builder
1392 ;; Don't complain about _IO* on Guile 2.2.
1393 #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
1395 ;; Not worth offloading.
1398 ;; Disable substitution because it would trigger a
1399 ;; connection to the substitute server, which is likely
1400 ;; to have no substitute to offer.
1401 #:substitutable? #f)))
1403 (define (profile-regexp profile)
1404 "Return a regular expression that matches PROFILE's name and number."
1405 (make-regexp (string-append "^" (regexp-quote (basename profile))
1408 (define (generation-number profile)
1409 "Return PROFILE's number or 0. An absolute file name must be used."
1410 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
1411 (basename (readlink profile))))
1412 (compose string->number (cut match:substring <> 1)))
1415 (define (generation-numbers profile)
1416 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
1417 former profiles were found."
1418 (match (scandir (dirname profile)
1419 (cute regexp-exec (profile-regexp profile) <>))
1420 (#f ; no profile directory
1424 ((profiles ...) ; former profiles around
1425 (sort (map (compose string->number
1426 (cut match:substring <> 1)
1427 (cute regexp-exec (profile-regexp profile) <>))
1431 (define (profile-generations profile)
1432 "Return a list of PROFILE's generations."
1433 (let ((generations (generation-numbers profile)))
1434 (if (equal? generations '(0))
1438 (define (relative-generation-spec->number profile spec)
1439 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
1440 may be a N, -N, or +N, where N is a number. If the spec is N, then the number
1441 returned is N. If it is -N, then the number returned is the profile's current
1442 generation number minus N. If it is +N, then the number returned is the
1443 profile's current generation number plus N. Return #f if there is no such
1445 (let ((number (string->number spec)))
1447 (case (string-ref spec 0)
1449 (relative-generation profile number))
1450 (else (if (memv number (profile-generations profile))
1455 (define* (relative-generation profile shift #:optional
1456 (current (generation-number profile)))
1457 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
1458 SHIFT is a positive or negative number.
1459 Return #f if there is no such generation."
1460 (let* ((abs-shift (abs shift))
1461 (numbers (profile-generations profile))
1462 (from-current (memq current
1463 (if (negative? shift)
1467 (< abs-shift (length from-current))
1468 (list-ref from-current abs-shift))))
1470 (define* (previous-generation-number profile #:optional
1471 (number (generation-number profile)))
1472 "Return the number of the generation before generation NUMBER of
1473 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
1474 case when generations have been deleted (there are \"holes\")."
1475 (or (relative-generation profile -1 number)
1478 (define (generation-file-name profile generation)
1479 "Return the file name for PROFILE's GENERATION."
1480 (format #f "~a-~a-link" profile generation))
1482 (define (generation-time profile number)
1483 "Return the creation time of a generation in the UTC format."
1484 (make-time time-utc 0
1485 (stat:ctime (stat (generation-file-name profile number)))))
1487 (define (link-to-empty-profile store generation)
1488 "Link GENERATION, a string, to the empty profile. An error is raised if
1490 (let* ((drv (run-with-store store
1491 (profile-derivation (manifest '())
1493 (prof (derivation->output-path drv "out")))
1494 (build-derivations store (list drv))
1495 (switch-symlinks generation prof)))
1497 (define (switch-to-generation profile number)
1498 "Atomically switch PROFILE to the generation NUMBER. Return the number of
1499 the generation that was current before switching."
1500 (let ((current (generation-number profile))
1501 (generation (generation-file-name profile number)))
1502 (cond ((not (file-exists? profile))
1503 (raise (condition (&profile-not-found-error
1504 (profile profile)))))
1505 ((not (file-exists? generation))
1506 (raise (condition (&missing-generation-error
1508 (generation number)))))
1510 (switch-symlinks profile generation)
1513 (define (switch-to-previous-generation profile)
1514 "Atomically switch PROFILE to the previous generation. Return the former
1515 generation number and the current one."
1516 (let ((previous (previous-generation-number profile)))
1517 (values (switch-to-generation profile previous)
1520 (define (roll-back store profile)
1521 "Roll back to the previous generation of PROFILE. Return the number of the
1522 generation that was current before switching and the new generation number."
1523 (let* ((number (generation-number profile))
1524 (previous-number (previous-generation-number profile number))
1525 (previous-generation (generation-file-name profile previous-number)))
1526 (cond ((not (file-exists? profile)) ;invalid profile
1527 (raise (condition (&profile-not-found-error
1528 (profile profile)))))
1529 ((zero? number) ;empty profile
1530 (values number number))
1531 ((or (zero? previous-number) ;going to emptiness
1532 (not (file-exists? previous-generation)))
1533 (link-to-empty-profile store previous-generation)
1534 (switch-to-previous-generation profile))
1535 (else ;anything else
1536 (switch-to-previous-generation profile)))))
1538 (define (delete-generation store profile number)
1539 "Delete generation with NUMBER from PROFILE. Return the file name of the
1540 generation that has been deleted, or #f if nothing was done (for instance
1541 because the NUMBER is zero.)"
1542 (define (delete-and-return)
1543 (let ((generation (generation-file-name profile number)))
1544 (delete-file generation)
1547 (let* ((current-number (generation-number profile))
1548 (previous-number (previous-generation-number profile number))
1549 (previous-generation (generation-file-name profile previous-number)))
1550 (cond ((zero? number) #f) ;do not delete generation 0
1551 ((and (= number current-number)
1552 (not (file-exists? previous-generation)))
1553 (link-to-empty-profile store previous-generation)
1554 (switch-to-previous-generation profile)
1555 (delete-and-return))
1556 ((= number current-number)
1557 (roll-back store profile)
1558 (delete-and-return))
1560 (delete-and-return)))))
1562 (define %user-profile-directory
1563 (and=> (getenv "HOME")
1564 (cut string-append <> "/.guix-profile")))
1566 (define %profile-directory
1567 (string-append %state-directory "/profiles/"
1568 (or (and=> (or (getenv "USER")
1570 (cut string-append "per-user/" <>))
1573 (define %current-profile
1574 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
1575 ;; coexist with Nix profiles.
1576 (string-append %profile-directory "/guix-profile"))
1578 (define (canonicalize-profile profile)
1579 "If PROFILE is %USER-PROFILE-DIRECTORY, return %CURRENT-PROFILE. Otherwise
1580 return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile' as if
1581 '-p' was omitted." ; see <http://bugs.gnu.org/17939>
1583 ;; Trim trailing slashes so that the basename comparison below works as
1585 (let ((profile (string-trim-right profile #\/)))
1586 (if (and %user-profile-directory
1587 (string=? (canonicalize-path (dirname profile))
1588 (dirname %user-profile-directory))
1589 (string=? (basename profile) (basename %user-profile-directory)))
1593 (define (user-friendly-profile profile)
1594 "Return either ~/.guix-profile if that's what PROFILE refers to, directly or
1595 indirectly, or PROFILE."
1596 (if (and %user-profile-directory
1598 (string=? (readlink %user-profile-directory) profile)))
1599 %user-profile-directory
1602 ;;; profiles.scm ends here