1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017 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 utils) #:hide (package-name->name+version))
29 #:use-module ((guix build utils)
30 #:select (package-name->name+version))
31 #:use-module (guix records)
32 #:use-module (guix packages)
33 #:use-module (guix derivations)
34 #:use-module (guix search-paths)
35 #:use-module (guix gexp)
36 #:use-module (guix monads)
37 #:use-module (guix store)
38 #:use-module (guix sets)
39 #:use-module (ice-9 vlist)
40 #:use-module (ice-9 match)
41 #:use-module (ice-9 regex)
42 #:use-module (ice-9 ftw)
43 #:use-module (ice-9 format)
44 #:use-module (srfi srfi-1)
45 #:use-module (srfi srfi-9)
46 #:use-module (srfi srfi-11)
47 #:use-module (srfi srfi-19)
48 #:use-module (srfi srfi-26)
49 #:use-module (srfi srfi-34)
50 #:use-module (srfi srfi-35)
51 #:export (&profile-error
54 &profile-not-found-error
55 profile-not-found-error?
56 &profile-collistion-error
57 profile-collision-error?
58 profile-collision-error-entry
59 profile-collision-error-conflict
60 &missing-generation-error
61 missing-generation-error?
62 missing-generation-error-generation
64 manifest make-manifest
67 manifest-transitive-entries
69 <manifest-entry> ; FIXME: eventually make it internal
73 manifest-entry-version
76 manifest-entry-dependencies
77 manifest-entry-search-paths
83 manifest-pattern-version
84 manifest-pattern-output
90 manifest-matching-entries
94 manifest-transaction-install
95 manifest-transaction-remove
96 manifest-transaction-install-entry
97 manifest-transaction-remove-pattern
98 manifest-transaction-null?
99 manifest-transaction-removal-candidate?
100 manifest-perform-transaction
101 manifest-transaction-effects
104 package->manifest-entry
106 ca-certificate-bundle
107 %default-profile-hooks
113 relative-generation-spec->number
115 previous-generation-number
124 ;;; Tools to create and manipulate profiles---i.e., the representation of a
125 ;;; set of installed packages.
134 (define-condition-type &profile-error &error
136 (profile profile-error-profile))
138 (define-condition-type &profile-not-found-error &profile-error
139 profile-not-found-error?)
141 (define-condition-type &profile-collision-error &error
142 profile-collision-error?
143 (entry profile-collision-error-entry) ;<manifest-entry>
144 (conflict profile-collision-error-conflict)) ;<manifest-entry>
146 (define-condition-type &missing-generation-error &profile-error
147 missing-generation-error?
148 (generation missing-generation-error-generation))
155 (define-record-type <manifest>
158 (entries manifest-entries)) ; list of <manifest-entry>
160 ;; Convenient alias, to avoid name clashes.
161 (define make-manifest manifest)
163 (define-record-type* <manifest-entry> manifest-entry
166 (name manifest-entry-name) ; string
167 (version manifest-entry-version) ; string
168 (output manifest-entry-output ; string
170 (item manifest-entry-item) ; package | store path
171 (dependencies manifest-entry-dependencies ; <manifest-entry>*
173 (search-paths manifest-entry-search-paths ; search-path-specification*
175 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
176 (default (delay #f))))
178 (define-record-type* <manifest-pattern> manifest-pattern
179 make-manifest-pattern
181 (name manifest-pattern-name) ; string
182 (version manifest-pattern-version ; string | #f
184 (output manifest-pattern-output ; string | #f
187 (define (manifest-transitive-entries manifest)
188 "Return the entries of MANIFEST along with their propagated inputs,
190 (let loop ((entries (manifest-entries manifest))
192 (visited (set))) ;compare with 'equal?'
197 (if (set-contains? visited head)
198 (loop tail result visited)
199 (loop (append (manifest-entry-dependencies head)
202 (set-insert head visited)))))))
204 (define (profile-manifest profile)
205 "Return the PROFILE's manifest."
206 (let ((file (string-append profile "/manifest")))
207 (if (file-exists? file)
208 (call-with-input-file file read-manifest)
211 (define (manifest-entry-lookup manifest)
212 "Return a lookup procedure for the entries of MANIFEST. The lookup
213 procedure takes two arguments: the entry name and output."
215 (let loop ((entries (manifest-entries manifest))
216 (mapping vlist-null))
217 (fold (lambda (entry result)
218 (vhash-cons (cons (manifest-entry-name entry)
219 (manifest-entry-output entry))
221 (loop (manifest-entry-dependencies entry)
226 (lambda (name output)
227 (match (vhash-assoc (cons name output) mapping)
231 (define* (lower-manifest-entry entry system #:key target)
232 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
234 (let ((item (manifest-entry-item entry)))
236 (with-monad %store-monad
238 (mlet %store-monad ((drv (lower-object item system
240 (output -> (manifest-entry-output entry)))
241 (return (manifest-entry
243 (item (derivation->output-path drv output))))))))
245 (define* (check-for-collisions manifest system #:key target)
246 "Check whether the entries of MANIFEST conflict with one another; raise a
247 '&profile-collision-error' when a conflict is encountered."
249 (manifest-entry-lookup manifest))
251 (with-monad %store-monad
253 (lambda (entry result)
254 (match (lookup (manifest-entry-name entry)
255 (manifest-entry-output entry))
256 ((? manifest-entry? second) ;potential conflict
257 (mlet %store-monad ((first (lower-manifest-entry entry system
260 (second (lower-manifest-entry second system
263 (if (string=? (manifest-entry-item first)
264 (manifest-entry-item second))
267 (&profile-collision-error
269 (conflict second)))))))
273 (manifest-transitive-entries manifest))))
275 (define* (package->manifest-entry package #:optional (output "out")
276 #:key (parent (delay #f)))
277 "Return a manifest entry for the OUTPUT of package PACKAGE."
278 ;; For each dependency, keep a promise pointing to its "parent" entry.
279 (letrec* ((deps (map (match-lambda
281 (package->manifest-entry package
282 #:parent (delay entry)))
283 ((label package output)
284 (package->manifest-entry package output
285 #:parent (delay entry))))
286 (package-propagated-inputs package)))
287 (entry (manifest-entry
288 (name (package-name package))
289 (version (package-version package))
292 (dependencies (delete-duplicates deps))
294 (package-transitive-native-search-paths package))
298 (define (packages->manifest packages)
299 "Return a list of manifest entries, one for each item listed in PACKAGES.
300 Elements of PACKAGES can be either package objects or package/string tuples
301 denoting a specific output of a package."
305 (package->manifest-entry package output))
306 ((? package? package)
307 (package->manifest-entry package)))
310 (define (manifest->gexp manifest)
311 "Return a representation of MANIFEST as a gexp."
312 (define (entry->gexp entry)
314 (($ <manifest-entry> name version output (? string? path)
315 (deps ...) (search-paths ...))
316 #~(#$name #$version #$output #$path
317 (propagated-inputs #$(map entry->gexp deps))
318 (search-paths #$(map search-path-specification->sexp
320 (($ <manifest-entry> name version output (? package? package)
321 (deps ...) (search-paths ...))
322 #~(#$name #$version #$output
323 (ungexp package (or output "out"))
324 (propagated-inputs #$(map entry->gexp deps))
325 (search-paths #$(map search-path-specification->sexp
329 (($ <manifest> (entries ...))
330 #~(manifest (version 3)
331 (packages #$(map entry->gexp entries))))))
333 (define (find-package name version)
334 "Return a package from the distro matching NAME and possibly VERSION. This
335 procedure is here for backward-compatibility and will eventually vanish."
336 (define find-best-packages-by-name ;break abstractions
337 (module-ref (resolve-interface '(gnu packages))
338 'find-best-packages-by-name))
340 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
341 ;; former traverses the module tree only once and then allows for efficient
342 ;; access via a vhash.
343 (match (find-best-packages-by-name name version)
346 (match (find-best-packages-by-name name #f)
350 (define (sexp->manifest sexp)
351 "Parse SEXP as a manifest."
352 (define (infer-search-paths name version)
353 ;; Infer the search path specifications for NAME-VERSION by looking up a
354 ;; same-named package in the distro. Useful for the old manifest formats
355 ;; that did not store search path info.
356 (let ((package (find-package name version)))
358 (package-native-search-paths package)
361 (define (infer-dependency item parent)
362 ;; Return a <manifest-entry> for ITEM.
363 (let-values (((name version)
364 (package-name->name+version
365 (store-path-package-name item))))
372 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
374 ((name version output path
375 ('propagated-inputs deps)
376 ('search-paths search-paths)
378 ;; For each of DEPS, keep a promise pointing to ENTRY.
379 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
381 (entry (manifest-entry
387 (search-paths (map sexp->search-path-specification
393 (('manifest ('version 0)
394 ('packages ((name version output path) ...)))
396 (map (lambda (name version output path)
402 (search-paths (infer-search-paths name version))))
403 name version output path)))
405 ;; Version 1 adds a list of propagated inputs to the
406 ;; name/version/output/path tuples.
407 (('manifest ('version 1)
408 ('packages ((name version output path deps) ...)))
410 (map (lambda (name version output path deps)
411 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
412 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
414 (let ((deps (match deps
415 (((labels directories) ...)
419 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
421 (entry (manifest-entry
428 (infer-search-paths name version)))))
430 name version output path deps)))
432 ;; Version 2 adds search paths and is slightly more verbose.
433 (('manifest ('version 2 minor-version ...)
434 ('packages ((name version output path
435 ('propagated-inputs deps)
436 ('search-paths search-paths)
440 (map (lambda (name version output path deps search-paths)
441 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
443 (entry (manifest-entry
450 (map sexp->search-path-specification
453 name version output path deps search-paths)))
455 ;; Version 3 represents DEPS as full-blown manifest entries.
456 (('manifest ('version 3 minor-version ...)
457 ('packages (entries ...)))
458 (manifest (map sexp->manifest-entry entries)))
461 (&message (message "unsupported manifest format")))))))
463 (define (read-manifest port)
464 "Return the packages listed in MANIFEST."
465 (sexp->manifest (read port)))
467 (define (entry-predicate pattern)
468 "Return a procedure that returns #t when passed a manifest entry that
469 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
472 (($ <manifest-pattern> name version output)
474 (($ <manifest-entry> entry-name entry-version entry-output)
475 (and (string=? entry-name name)
476 (or (not entry-output) (not output)
477 (string=? entry-output output))
479 (string=? entry-version version))))))))
481 (define (manifest-remove manifest patterns)
482 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
483 must be a manifest-pattern."
484 (define (remove-entry pattern lst)
485 (remove (entry-predicate pattern) lst))
487 (make-manifest (fold remove-entry
488 (manifest-entries manifest)
491 (define (manifest-add manifest entries)
492 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
493 Remove MANIFEST entries that have the same name and output as ENTRIES."
494 (define (same-entry? entry name output)
496 (($ <manifest-entry> entry-name _ entry-output _ ...)
497 (and (equal? name entry-name)
498 (equal? output entry-output)))))
502 (fold (lambda (entry result)
504 (($ <manifest-entry> name _ out _ ...)
505 (filter (negate (cut same-entry? <> name out))
507 (manifest-entries manifest)
510 (define (manifest-lookup manifest pattern)
511 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
513 (find (entry-predicate pattern)
514 (manifest-entries manifest)))
516 (define (manifest-installed? manifest pattern)
517 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
519 (->bool (manifest-lookup manifest pattern)))
521 (define (manifest-matching-entries manifest patterns)
522 "Return all the entries of MANIFEST that match one of the PATTERNS."
524 (map entry-predicate patterns))
526 (define (matches? entry)
531 (filter matches? (manifest-entries manifest)))
535 ;;; Manifest transactions.
538 (define-record-type* <manifest-transaction> manifest-transaction
539 make-manifest-transaction
540 manifest-transaction?
541 (install manifest-transaction-install ; list of <manifest-entry>
543 (remove manifest-transaction-remove ; list of <manifest-pattern>
546 (define (manifest-transaction-install-entry entry transaction)
547 "Augment TRANSACTION's set of installed packages with ENTRY, a
549 (manifest-transaction
550 (inherit transaction)
552 (cons entry (manifest-transaction-install transaction)))))
554 (define (manifest-transaction-remove-pattern pattern transaction)
555 "Add PATTERN to TRANSACTION's list of packages to remove."
556 (manifest-transaction
557 (inherit transaction)
559 (cons pattern (manifest-transaction-remove transaction)))))
561 (define (manifest-transaction-null? transaction)
562 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
565 (($ <manifest-transaction> () ()) #t)
566 (($ <manifest-transaction> _ _) #f)))
568 (define (manifest-transaction-removal-candidate? entry transaction)
569 "Return true if ENTRY is a candidate for removal in TRANSACTION."
570 (any (lambda (pattern)
571 ((entry-predicate pattern) entry))
572 (manifest-transaction-remove transaction)))
574 (define (manifest-transaction-effects manifest transaction)
575 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
576 the list of packages that would be removed, installed, upgraded, or downgraded
577 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
578 where the head is the entry being upgraded and the tail is the entry that will
580 (define (manifest-entry->pattern entry)
582 (name (manifest-entry-name entry))
583 (output (manifest-entry-output entry))))
585 (let loop ((input (manifest-transaction-install transaction))
591 (let ((remove (manifest-transaction-remove transaction)))
592 (values (manifest-matching-entries manifest remove)
593 (reverse install) (reverse upgrade) (reverse downgrade))))
595 ;; Check whether installing ENTRY corresponds to the installation of a
596 ;; new package or to an upgrade.
598 ;; XXX: When the exact same output directory is installed, we're not
599 ;; really upgrading anything. Add a check for that case.
600 (let* ((pattern (manifest-entry->pattern entry))
601 (previous (manifest-lookup manifest pattern))
602 (newer? (and previous
603 (version>=? (manifest-entry-version entry)
604 (manifest-entry-version previous)))))
606 (if previous install (cons entry install))
607 (if (and previous newer?)
608 (alist-cons previous entry upgrade)
610 (if (and previous (not newer?))
611 (alist-cons previous entry downgrade)
614 (define (manifest-perform-transaction manifest transaction)
615 "Perform TRANSACTION on MANIFEST and return the new manifest."
616 (let ((install (manifest-transaction-install transaction))
617 (remove (manifest-transaction-remove transaction)))
618 (manifest-add (manifest-remove manifest remove)
626 (define (manifest-inputs manifest)
627 "Return a list of <gexp-input> objects for MANIFEST."
630 (($ <manifest-entry> name version output thing deps)
631 ;; THING may be a package or a file name. In the latter case, assume
632 ;; it's already valid.
633 (cons (gexp-input thing output)
634 (append-map entry->input deps)))))
636 (append-map entry->input (manifest-entries manifest)))
638 (define* (manifest-lookup-package manifest name #:optional version)
639 "Return as a monadic value the first package or store path referenced by
640 MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
642 ;; Return as a monadic value the package or store path referenced by the
643 ;; manifest ENTRY, or #f if not referenced.
644 (define (entry-lookup-package entry)
645 (define (find-among-inputs inputs)
646 (find (lambda (input)
647 (and (package? input)
648 (equal? name (package-name input))
650 (string-prefix? version (package-version input))
653 (define (find-among-store-items items)
655 (let-values (((name* version*)
656 (package-name->name+version
657 (store-path-package-name item))))
658 (and (string=? name name*)
660 (string-prefix? version version*)
664 (with-monad %store-monad
665 (match (manifest-entry-item entry)
666 ((? package? package)
667 (match (cons (list (package-name package) package)
668 (package-transitive-inputs package))
669 (((labels inputs . _) ...)
670 (return (find-among-inputs inputs)))))
672 (mlet %store-monad ((refs (references* item)))
673 (return (find-among-store-items refs)))))))
676 entry-lookup-package (manifest-entries manifest)))
678 (define (info-dir-file manifest)
679 "Return a derivation that builds the 'dir' file for all the entries of
681 (define texinfo ;lazy reference
682 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
683 (define gzip ;lazy reference
684 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
687 (with-imported-modules '((guix build utils))
689 (use-modules (guix build utils)
690 (srfi srfi-1) (srfi srfi-26)
693 (define (info-file? file)
694 (or (string-suffix? ".info" file)
695 (string-suffix? ".info.gz" file)))
697 (define (info-files top)
698 (let ((infodir (string-append top "/share/info")))
699 (map (cut string-append infodir "/" <>)
700 (or (scandir infodir info-file?) '()))))
702 (define (install-info info)
703 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
705 (system* (string-append #+texinfo "/bin/install-info") "--silent"
706 info (string-append #$output "/share/info/dir"))))
708 (mkdir-p (string-append #$output "/share/info"))
709 (exit (every install-info
710 (append-map info-files
711 '#$(manifest-inputs manifest)))))))
713 (gexp->derivation "info-dir" build
715 #:substitutable? #f))
717 (define (ghc-package-cache-file manifest)
718 "Return a derivation that builds the GHC 'package.cache' file for all the
719 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
720 (define ghc ;lazy reference
721 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
724 (with-imported-modules '((guix build utils))
726 (use-modules (guix build utils)
727 (srfi srfi-1) (srfi srfi-26)
730 (define ghc-name-version
731 (let* ((base (basename #+ghc)))
733 (+ 1 (string-index base #\-)))))
736 (string-append "lib/" ghc-name-version "/package.conf.d"))
739 (string-append #$output "/" db-subdir))
741 (define (conf-files top)
742 (let ((db (string-append top "/" db-subdir)))
743 (if (file-exists? db)
744 (find-files db "\\.conf$")
747 (define (copy-conf-file conf)
748 (let ((base (basename conf)))
749 (copy-file conf (string-append db-dir "/" base))))
751 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
752 (for-each copy-conf-file
753 (append-map conf-files
755 '#$(manifest-inputs manifest))))
758 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
759 (string-append "--package-db=" db-dir)))))
760 (for-each delete-file (find-files db-dir "\\.conf$"))
763 (with-monad %store-monad
764 ;; Don't depend on GHC when there's nothing to do.
765 (if (any (cut string-prefix? "ghc" <>)
766 (map manifest-entry-name (manifest-entries manifest)))
767 (gexp->derivation "ghc-package-cache" build
772 (define (ca-certificate-bundle manifest)
773 "Return a derivation that builds a single-file bundle containing the CA
774 certificates in the /etc/ssl/certs sub-directories of the packages in
775 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
776 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
779 (define glibc-utf8-locales ;lazy reference
780 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
783 (with-imported-modules '((guix build utils))
785 (use-modules (guix build utils)
792 (define (pem-file? file)
793 (string-suffix? ".pem" file))
795 (define (ca-files top)
796 (let ((cert-dir (string-append top "/etc/ssl/certs")))
797 (map (cut string-append cert-dir "/" <>)
798 (or (scandir cert-dir pem-file?) '()))))
800 (define (concatenate-files files result)
801 "Make RESULT the concatenation of all of FILES."
802 (define (dump file port)
803 (display (call-with-input-file file get-string-all)
805 (newline port)) ;required, see <https://bugs.debian.org/635570>
807 (call-with-output-file result
809 (for-each (cut dump <> port) files))))
811 ;; Some file names in the NSS certificates are UTF-8 encoded so
812 ;; install a UTF-8 locale.
814 (string-append #+glibc-utf8-locales "/lib/locale/"
815 #+(package-version glibc-utf8-locales)))
816 (setlocale LC_ALL "en_US.utf8")
818 (match (append-map ca-files '#$(manifest-inputs manifest))
820 ;; Since there are no CA files, just create an empty directory. Do
821 ;; not create the etc/ssl/certs sub-directory, since that would
822 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
827 (let ((result (string-append #$output "/etc/ssl/certs")))
829 (concatenate-files ca-files
830 (string-append result
831 "/ca-certificates.crt"))
834 (gexp->derivation "ca-certificate-bundle" build
836 #:substitutable? #f))
838 (define (gtk-icon-themes manifest)
839 "Return a derivation that unions all icon themes from manifest entries and
840 creates the GTK+ 'icon-theme.cache' file for each theme."
841 (define gtk+ ; lazy reference
842 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
844 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
845 ;; XXX: Can't use gtk-update-icon-cache corresponding
846 ;; to the gtk+ referenced by 'manifest'. Because
847 ;; '%gtk+' can be either a package or store path, and
848 ;; there's no way to get the "bin" output for the later.
849 (gtk-update-icon-cache
850 -> #~(string-append #+gtk+:bin
851 "/bin/gtk-update-icon-cache")))
854 (with-imported-modules '((guix build utils)
856 (guix build profiles)
860 (use-modules (guix build utils)
862 (guix build profiles)
866 (let* ((destdir (string-append #$output "/share/icons"))
867 (icondirs (filter file-exists?
868 (map (cut string-append <> "/share/icons")
869 '#$(manifest-inputs manifest)))))
871 ;; Union all the icons.
872 (mkdir-p (string-append #$output "/share"))
873 (union-build destdir icondirs
874 #:log-port (%make-void-port "w"))
876 ;; Update the 'icon-theme.cache' file for each icon theme.
879 (let ((dir (string-append destdir "/" theme)))
880 ;; Occasionally DESTDIR contains plain files, such as
881 ;; "abiword_48.png". Ignore these.
882 (when (file-is-directory? dir)
883 (ensure-writable-directory dir)
884 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
885 (scandir destdir (negate (cut member <> '("." "..")))))))))
887 ;; Don't run the hook when there's nothing to do.
889 (gexp->derivation "gtk-icon-themes" build
894 (define (gtk-im-modules manifest)
895 "Return a derivation that builds the cache files for input method modules
896 for both major versions of GTK+."
898 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
899 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
901 (define (build gtk gtk-version query)
902 (let ((major (string-take gtk-version 1)))
903 (with-imported-modules '((guix build utils)
905 (guix build profiles)
909 (use-modules (guix build utils)
911 (guix build profiles)
916 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
918 (destdir (string-append #$output prefix))
919 (moddirs (cons (string-append #$gtk prefix "/immodules")
921 (map (cut string-append <> prefix "/immodules")
922 '#$(manifest-inputs manifest)))))
923 (modules (append-map (cut find-files <> "\\.so$")
926 ;; Generate a new immodules cache file.
927 (mkdir-p (string-append #$output prefix))
928 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
929 (outfile (string-append #$output prefix
930 "/immodules-gtk" #$major ".cache")))
934 (call-with-output-file outfile
936 (while (not (eof-object? (peek-char pipe)))
937 (write-char (read-char pipe) out))))
940 (close-pipe pipe)))))))))
942 ;; Don't run the hook when there's nothing to do.
943 (let* ((pkg-gtk+ (module-ref ; lazy reference
944 (resolve-interface '(gnu packages gtk)) 'gtk+))
949 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
950 ;; output of latest gtk+ package.
952 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
958 #$gtk+-2 "/bin/gtk-query-immodules-2.0"))
961 (gexp->derivation "gtk-im-modules" gexp
966 (define (xdg-desktop-database manifest)
967 "Return a derivation that builds the @file{mimeinfo.cache} database from
968 desktop files. It's used to query what applications can handle a given
970 (define desktop-file-utils ; lazy reference
971 (module-ref (resolve-interface '(gnu packages freedesktop))
972 'desktop-file-utils))
974 (mlet %store-monad ((glib
975 (manifest-lookup-package
978 (with-imported-modules '((guix build utils)
981 (use-modules (srfi srfi-26)
984 (let* ((destdir (string-append #$output "/share/applications"))
985 (appdirs (filter file-exists?
986 (map (cut string-append <>
987 "/share/applications")
988 '#$(manifest-inputs manifest))))
989 (update-desktop-database (string-append
991 "/bin/update-desktop-database")))
992 (mkdir-p (string-append #$output "/share"))
993 (union-build destdir appdirs
994 #:log-port (%make-void-port "w"))
995 (exit (zero? (system* update-desktop-database destdir)))))))
997 ;; Don't run the hook when 'glib' is not referenced.
999 (gexp->derivation "xdg-desktop-database" build
1001 #:substitutable? #f)
1004 (define (xdg-mime-database manifest)
1005 "Return a derivation that builds the @file{mime.cache} database from manifest
1006 entries. It's used to query the MIME type of a given file."
1007 (define shared-mime-info ; lazy reference
1008 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1010 (mlet %store-monad ((glib
1011 (manifest-lookup-package
1014 (with-imported-modules '((guix build utils)
1017 (use-modules (srfi srfi-26)
1020 (let* ((datadir (string-append #$output "/share"))
1021 (destdir (string-append datadir "/mime"))
1022 (pkgdirs (filter file-exists?
1023 (map (cut string-append <>
1024 "/share/mime/packages")
1025 (cons #+shared-mime-info
1026 '#$(manifest-inputs manifest)))))
1027 (update-mime-database (string-append
1029 "/bin/update-mime-database")))
1031 (union-build (string-append destdir "/packages") pkgdirs
1032 #:log-port (%make-void-port "w"))
1033 (setenv "XDG_DATA_HOME" datadir)
1034 (exit (zero? (system* update-mime-database destdir)))))))
1036 ;; Don't run the hook when there are no GLib based applications.
1038 (gexp->derivation "xdg-mime-database" build
1040 #:substitutable? #f)
1043 ;; Several font packages may install font files into same directory, so
1044 ;; fonts.dir and fonts.scale file should be generated here, instead of in
1046 (define (fonts-dir-file manifest)
1047 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
1048 files for the fonts of the @var{manifest} entries."
1050 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1053 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1057 (use-modules (srfi srfi-26)
1060 (let ((fonts-dirs (filter file-exists?
1061 (map (cut string-append <>
1063 '#$(manifest-inputs manifest)))))
1065 (if (null? fonts-dirs)
1067 (let* ((share-dir (string-append #$output "/share"))
1068 (fonts-dir (string-append share-dir "/fonts"))
1069 (mkfontscale (string-append #+mkfontscale
1070 "/bin/mkfontscale"))
1071 (mkfontdir (string-append #+mkfontdir
1073 (empty-file? (lambda (filename)
1074 (call-with-ascii-input-file filename
1076 (eqv? #\0 (read-char p))))))
1077 (fonts-dir-file "fonts.dir")
1078 (fonts-scale-file "fonts.scale"))
1080 ;; Create all sub-directories, because we may create fonts.dir
1081 ;; and fonts.scale files in the sub-directories.
1082 (union-build fonts-dir fonts-dirs
1083 #:log-port (%make-void-port "w")
1084 #:create-all-directories? #t)
1085 (let ((directories (find-files fonts-dir
1087 (eq? 'directory (stat:type stat)))
1088 #:directories? #t)))
1089 (for-each (lambda (dir)
1090 (with-directory-excursion dir
1091 (when (file-exists? fonts-scale-file)
1092 (delete-file fonts-scale-file))
1093 (when (file-exists? fonts-dir-file)
1094 (delete-file fonts-dir-file))
1095 (unless (and (zero? (system* mkfontscale))
1096 (zero? (system* mkfontdir)))
1098 (when (and (file-exists? fonts-scale-file)
1099 (empty-file? fonts-scale-file))
1100 (delete-file fonts-scale-file))
1101 (when (and (file-exists? fonts-dir-file)
1102 (empty-file? fonts-dir-file))
1103 (delete-file fonts-dir-file))))
1106 (gexp->derivation "fonts-dir" build
1107 #:modules '((guix build utils)
1111 #:substitutable? #f))
1113 (define (manual-database manifest)
1114 "Return a derivation that builds the manual page database (\"mandb\") for
1115 the entries in MANIFEST."
1116 (define man-db ;lazy reference
1117 (module-ref (resolve-interface '(gnu packages man)) 'man-db))
1121 (use-modules (guix build utils)
1127 (filter-map (lambda (directory)
1128 (let ((man (string-append directory "/share/man")))
1129 (and (directory-exists? man)
1131 '#$(manifest-inputs manifest)))
1133 (define manpages-collection-dir
1134 (string-append (getenv "PWD") "/manpages-collection"))
1136 (define man-directory
1137 (string-append #$output "/share/man"))
1139 (define (get-manpage-tail-path manpage-path)
1140 (let ((index (string-contains manpage-path "/share/man/")))
1142 (error "Manual path doesn't contain \"/share/man/\":"
1144 (string-drop manpage-path (+ index (string-length "/share/man/")))))
1146 (define (populate-manpages-collection-dir entries)
1147 (let ((manpages (append-map (cut find-files <> #:stat stat) entries)))
1148 (for-each (lambda (manpage)
1149 (let* ((dest-file (string-append
1150 manpages-collection-dir "/"
1151 (get-manpage-tail-path manpage))))
1152 (mkdir-p (dirname dest-file))
1153 (catch 'system-error
1155 (symlink manpage dest-file))
1157 ;; Different packages may contain the same
1158 ;; manpage. Simply ignore the symlink error.
1162 (mkdir-p manpages-collection-dir)
1163 (populate-manpages-collection-dir entries)
1165 ;; Create a mandb config file which contains a custom made
1166 ;; manpath. The associated catpath is the location where the database
1168 (copy-file #+(file-append man-db "/etc/man_db.conf")
1170 (substitute* "man_db.conf"
1171 (("MANDB_MAP /usr/man /var/cache/man/fsstnd")
1172 (string-append "MANDB_MAP " manpages-collection-dir " "
1175 (mkdir-p man-directory)
1176 (setenv "MANPATH" (string-join entries ":"))
1178 (format #t "Creating manual page database for ~a packages... "
1181 (let* ((start-time (current-time))
1182 (exit-status (system* #+(file-append man-db "/bin/mandb")
1183 "--quiet" "--create"
1184 "-C" "man_db.conf"))
1185 (duration (time-difference (current-time) start-time)))
1186 (format #t "done in ~,3f s~%"
1187 (+ (time-second duration)
1188 (* (time-nanosecond duration) (expt 10 -9))))
1190 (zero? exit-status))))
1192 (gexp->derivation "manual-database" build
1193 #:modules '((guix build utils)
1198 (define %default-profile-hooks
1199 ;; This is the list of derivation-returning procedures that are called by
1200 ;; default when making a non-empty profile.
1204 ghc-package-cache-file
1205 ca-certificate-bundle
1208 xdg-desktop-database
1211 (define* (profile-derivation manifest
1213 (hooks %default-profile-hooks)
1215 (allow-collisions? #f)
1217 "Return a derivation that builds a profile (aka. 'user environment') with
1218 the given MANIFEST. The profile includes additional derivations returned by
1219 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
1220 Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1221 entries in MANIFEST collide (for instance if there are two same-name packages
1222 with a different version number.)
1224 When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
1225 a dependency on the 'glibc-utf8-locales' package.
1227 When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1228 are cross-built for TARGET."
1229 (mlet* %store-monad ((system (if system
1232 (ok? (if allow-collisions?
1234 (check-for-collisions manifest system
1236 (extras (if (null? (manifest-entries manifest))
1238 (sequence %store-monad
1243 (append (filter-map (lambda (drv)
1244 (and (derivation? drv)
1247 (manifest-inputs manifest)))
1249 (define glibc-utf8-locales ;lazy reference
1250 (module-ref (resolve-interface '(gnu packages base))
1251 'glibc-utf8-locales))
1253 (define set-utf8-locale
1254 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1255 ;; install a UTF-8 locale.
1258 #$(file-append glibc-utf8-locales "/lib/locale/"
1259 (package-version glibc-utf8-locales)))
1260 (setlocale LC_ALL "en_US.utf8")))
1263 (with-imported-modules '((guix build profiles)
1269 (use-modules (guix build profiles)
1273 (setvbuf (current-output-port) _IOLBF)
1274 (setvbuf (current-error-port) _IOLBF)
1276 #+(if locales? set-utf8-locale #t)
1278 (define search-paths
1279 ;; Search paths of MANIFEST's packages, converted back to their
1281 (map sexp->search-path-specification
1283 '#$(map search-path-specification->sexp
1284 (append-map manifest-entry-search-paths
1285 (manifest-entries manifest))))))
1287 (build-profile #$output '#$inputs
1288 #:manifest '#$(manifest->gexp manifest)
1289 #:search-paths search-paths))))
1291 (gexp->derivation "profile" builder
1295 ;; Not worth offloading.
1298 ;; Disable substitution because it would trigger a
1299 ;; connection to the substitute server, which is likely
1300 ;; to have no substitute to offer.
1301 #:substitutable? #f)))
1303 (define (profile-regexp profile)
1304 "Return a regular expression that matches PROFILE's name and number."
1305 (make-regexp (string-append "^" (regexp-quote (basename profile))
1308 (define (generation-number profile)
1309 "Return PROFILE's number or 0. An absolute file name must be used."
1310 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
1311 (basename (readlink profile))))
1312 (compose string->number (cut match:substring <> 1)))
1315 (define (generation-numbers profile)
1316 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
1317 former profiles were found."
1318 (match (scandir (dirname profile)
1319 (cute regexp-exec (profile-regexp profile) <>))
1320 (#f ; no profile directory
1324 ((profiles ...) ; former profiles around
1325 (sort (map (compose string->number
1326 (cut match:substring <> 1)
1327 (cute regexp-exec (profile-regexp profile) <>))
1331 (define (profile-generations profile)
1332 "Return a list of PROFILE's generations."
1333 (let ((generations (generation-numbers profile)))
1334 (if (equal? generations '(0))
1338 (define (relative-generation-spec->number profile spec)
1339 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
1340 may be a N, -N, or +N, where N is a number. If the spec is N, then the number
1341 returned is N. If it is -N, then the number returned is the profile's current
1342 generation number minus N. If it is +N, then the number returned is the
1343 profile's current generation number plus N. Return #f if there is no such
1345 (let ((number (string->number spec)))
1347 (case (string-ref spec 0)
1349 (relative-generation profile number))
1350 (else (if (memv number (profile-generations profile))
1355 (define* (relative-generation profile shift #:optional
1356 (current (generation-number profile)))
1357 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
1358 SHIFT is a positive or negative number.
1359 Return #f if there is no such generation."
1360 (let* ((abs-shift (abs shift))
1361 (numbers (profile-generations profile))
1362 (from-current (memq current
1363 (if (negative? shift)
1367 (< abs-shift (length from-current))
1368 (list-ref from-current abs-shift))))
1370 (define* (previous-generation-number profile #:optional
1371 (number (generation-number profile)))
1372 "Return the number of the generation before generation NUMBER of
1373 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
1374 case when generations have been deleted (there are \"holes\")."
1375 (or (relative-generation profile -1 number)
1378 (define (generation-file-name profile generation)
1379 "Return the file name for PROFILE's GENERATION."
1380 (format #f "~a-~a-link" profile generation))
1382 (define (generation-time profile number)
1383 "Return the creation time of a generation in the UTC format."
1384 (make-time time-utc 0
1385 (stat:ctime (stat (generation-file-name profile number)))))
1387 (define (link-to-empty-profile store generation)
1388 "Link GENERATION, a string, to the empty profile. An error is raised if
1390 (let* ((drv (run-with-store store
1391 (profile-derivation (manifest '())
1393 (prof (derivation->output-path drv "out")))
1394 (build-derivations store (list drv))
1395 (switch-symlinks generation prof)))
1397 (define (switch-to-generation profile number)
1398 "Atomically switch PROFILE to the generation NUMBER. Return the number of
1399 the generation that was current before switching."
1400 (let ((current (generation-number profile))
1401 (generation (generation-file-name profile number)))
1402 (cond ((not (file-exists? profile))
1403 (raise (condition (&profile-not-found-error
1404 (profile profile)))))
1405 ((not (file-exists? generation))
1406 (raise (condition (&missing-generation-error
1408 (generation number)))))
1410 (switch-symlinks profile generation)
1413 (define (switch-to-previous-generation profile)
1414 "Atomically switch PROFILE to the previous generation. Return the former
1415 generation number and the current one."
1416 (let ((previous (previous-generation-number profile)))
1417 (values (switch-to-generation profile previous)
1420 (define (roll-back store profile)
1421 "Roll back to the previous generation of PROFILE. Return the number of the
1422 generation that was current before switching and the new generation number."
1423 (let* ((number (generation-number profile))
1424 (previous-number (previous-generation-number profile number))
1425 (previous-generation (generation-file-name profile previous-number)))
1426 (cond ((not (file-exists? profile)) ;invalid profile
1427 (raise (condition (&profile-not-found-error
1428 (profile profile)))))
1429 ((zero? number) ;empty profile
1430 (values number number))
1431 ((or (zero? previous-number) ;going to emptiness
1432 (not (file-exists? previous-generation)))
1433 (link-to-empty-profile store previous-generation)
1434 (switch-to-previous-generation profile))
1435 (else ;anything else
1436 (switch-to-previous-generation profile)))))
1438 (define (delete-generation store profile number)
1439 "Delete generation with NUMBER from PROFILE. Return the file name of the
1440 generation that has been deleted, or #f if nothing was done (for instance
1441 because the NUMBER is zero.)"
1442 (define (delete-and-return)
1443 (let ((generation (generation-file-name profile number)))
1444 (delete-file generation)
1447 (let* ((current-number (generation-number profile))
1448 (previous-number (previous-generation-number profile number))
1449 (previous-generation (generation-file-name profile previous-number)))
1450 (cond ((zero? number) #f) ;do not delete generation 0
1451 ((and (= number current-number)
1452 (not (file-exists? previous-generation)))
1453 (link-to-empty-profile store previous-generation)
1454 (switch-to-previous-generation profile)
1455 (delete-and-return))
1456 ((= number current-number)
1457 (roll-back store profile)
1458 (delete-and-return))
1460 (delete-and-return)))))
1462 ;;; profiles.scm ends here