1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 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, 2018, 2019 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 mkdir-p))
32 #:use-module (guix i18n)
33 #:use-module (guix records)
34 #:use-module (guix packages)
35 #:use-module (guix derivations)
36 #:use-module (guix search-paths)
37 #:use-module (guix gexp)
38 #:use-module (guix modules)
39 #:use-module (guix monads)
40 #:use-module (guix store)
41 #:use-module (guix sets)
42 #:use-module (ice-9 vlist)
43 #:use-module (ice-9 match)
44 #:use-module (ice-9 regex)
45 #:use-module (ice-9 ftw)
46 #:use-module (ice-9 format)
47 #:use-module (srfi srfi-1)
48 #:use-module (srfi srfi-9)
49 #:use-module (srfi srfi-11)
50 #:use-module (srfi srfi-19)
51 #:use-module (srfi srfi-26)
52 #:use-module (srfi srfi-34)
53 #:use-module (srfi srfi-35)
54 #:export (&profile-error
57 &profile-not-found-error
58 profile-not-found-error?
59 &profile-collision-error
60 profile-collision-error?
61 profile-collision-error-entry
62 profile-collision-error-conflict
63 &missing-generation-error
64 missing-generation-error?
65 missing-generation-error-generation
66 &unmatched-pattern-error
67 unmatched-pattern-error?
68 unmatched-pattern-error-pattern
69 unmatched-pattern-error-manifest
71 manifest make-manifest
74 manifest-transitive-entries
76 <manifest-entry> ; FIXME: eventually make it internal
80 manifest-entry-version
83 manifest-entry-dependencies
84 manifest-entry-search-paths
86 manifest-entry-properties
91 manifest-pattern-version
92 manifest-pattern-output
98 manifest-matching-entries
102 manifest-transaction?
103 manifest-transaction-install
104 manifest-transaction-remove
105 manifest-transaction-install-entry
106 manifest-transaction-remove-pattern
107 manifest-transaction-null?
108 manifest-transaction-removal-candidate?
109 manifest-perform-transaction
110 manifest-transaction-effects
113 package->manifest-entry
115 ca-certificate-bundle
116 %default-profile-hooks
124 relative-generation-spec->number
126 previous-generation-number
133 %user-profile-directory
136 ensure-profile-directory
138 user-friendly-profile))
142 ;;; Tools to create and manipulate profiles---i.e., the representation of a
143 ;;; set of installed packages.
152 (define-condition-type &profile-error &error
154 (profile profile-error-profile))
156 (define-condition-type &profile-not-found-error &profile-error
157 profile-not-found-error?)
159 (define-condition-type &profile-collision-error &error
160 profile-collision-error?
161 (entry profile-collision-error-entry) ;<manifest-entry>
162 (conflict profile-collision-error-conflict)) ;<manifest-entry>
164 (define-condition-type &unmatched-pattern-error &error
165 unmatched-pattern-error?
166 (pattern unmatched-pattern-error-pattern) ;<manifest-pattern>
167 (manifest unmatched-pattern-error-manifest)) ;<manifest>
169 (define-condition-type &missing-generation-error &profile-error
170 missing-generation-error?
171 (generation missing-generation-error-generation))
178 (define-record-type <manifest>
181 (entries manifest-entries)) ; list of <manifest-entry>
183 ;; Convenient alias, to avoid name clashes.
184 (define make-manifest manifest)
186 (define-record-type* <manifest-entry> manifest-entry
189 (name manifest-entry-name) ; string
190 (version manifest-entry-version) ; string
191 (output manifest-entry-output ; string
193 (item manifest-entry-item) ; package | file-like | store path
194 (dependencies manifest-entry-dependencies ; <manifest-entry>*
196 (search-paths manifest-entry-search-paths ; search-path-specification*
198 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
199 (default (delay #f)))
200 (properties manifest-entry-properties ; list of symbol/value pairs
203 (define-record-type* <manifest-pattern> manifest-pattern
204 make-manifest-pattern
206 (name manifest-pattern-name) ; string
207 (version manifest-pattern-version ; string | #f
209 (output manifest-pattern-output ; string | #f
212 (define (manifest-transitive-entries manifest)
213 "Return the entries of MANIFEST along with their propagated inputs,
215 (let loop ((entries (manifest-entries manifest))
217 (visited (set))) ;compare with 'equal?'
222 (if (set-contains? visited head)
223 (loop tail result visited)
224 (loop (append (manifest-entry-dependencies head)
227 (set-insert head visited)))))))
229 (define (profile-manifest profile)
230 "Return the PROFILE's manifest."
231 (let ((file (string-append profile "/manifest")))
232 (if (file-exists? file)
233 (call-with-input-file file read-manifest)
236 (define (manifest-entry-lookup manifest)
237 "Return a lookup procedure for the entries of MANIFEST. The lookup
238 procedure takes two arguments: the entry name and output."
240 (let loop ((entries (manifest-entries manifest))
241 (mapping vlist-null))
242 (fold (lambda (entry result)
243 (vhash-cons (cons (manifest-entry-name entry)
244 (manifest-entry-output entry))
246 (loop (manifest-entry-dependencies entry)
251 (lambda (name output)
252 (match (vhash-assoc (cons name output) mapping)
256 (define* (lower-manifest-entry entry system #:key target)
257 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
259 (let ((item (manifest-entry-item entry)))
261 (with-monad %store-monad
263 (mlet %store-monad ((drv (lower-object item system
265 (output -> (manifest-entry-output entry)))
266 (return (manifest-entry
268 (item (derivation->output-path drv output))))))))
270 (define* (check-for-collisions manifest system #:key target)
271 "Check whether the entries of MANIFEST conflict with one another; raise a
272 '&profile-collision-error' when a conflict is encountered."
274 (manifest-entry-lookup manifest))
276 (with-monad %store-monad
278 (lambda (entry result)
279 (match (lookup (manifest-entry-name entry)
280 (manifest-entry-output entry))
281 ((? manifest-entry? second) ;potential conflict
282 (mlet %store-monad ((first (lower-manifest-entry entry system
285 (second (lower-manifest-entry second system
288 (if (string=? (manifest-entry-item first)
289 (manifest-entry-item second))
292 (&profile-collision-error
294 (conflict second)))))))
298 (manifest-transitive-entries manifest))))
300 (define* (package->manifest-entry package #:optional (output "out")
301 #:key (parent (delay #f))
303 "Return a manifest entry for the OUTPUT of package PACKAGE."
304 ;; For each dependency, keep a promise pointing to its "parent" entry.
305 (letrec* ((deps (map (match-lambda
307 (package->manifest-entry package
308 #:parent (delay entry)))
309 ((label package output)
310 (package->manifest-entry package output
311 #:parent (delay entry))))
312 (package-propagated-inputs package)))
313 (entry (manifest-entry
314 (name (package-name package))
315 (version (package-version package))
318 (dependencies (delete-duplicates deps))
320 (package-transitive-native-search-paths package))
322 (properties properties))))
325 (define (packages->manifest packages)
326 "Return a list of manifest entries, one for each item listed in PACKAGES.
327 Elements of PACKAGES can be either package objects or package/string tuples
328 denoting a specific output of a package."
329 (define inferiors-loaded?
330 ;; This hack allows us to provide seamless integration for inferior
331 ;; packages while not having a hard dependency on (guix inferior).
332 (resolve-module '(guix inferior) #f #f #:ensure #f))
334 (define (inferior->entry)
335 (module-ref (resolve-interface '(guix inferior))
336 'inferior-package->manifest-entry))
341 (package->manifest-entry package output))
342 ((? package? package)
343 (package->manifest-entry package))
345 (if inferiors-loaded?
346 ((inferior->entry) thing output)
347 (throw 'wrong-type-arg 'packages->manifest
348 "Wrong package object: ~S" (list thing) (list thing))))
350 (if inferiors-loaded?
351 ((inferior->entry) thing)
352 (throw 'wrong-type-arg 'packages->manifest
353 "Wrong package object: ~S" (list thing) (list thing)))))
356 (define (manifest->gexp manifest)
357 "Return a representation of MANIFEST as a gexp."
358 (define (entry->gexp entry)
360 (($ <manifest-entry> name version output (? string? path)
361 (deps ...) (search-paths ...) _ (properties ...))
362 #~(#$name #$version #$output #$path
363 (propagated-inputs #$(map entry->gexp deps))
364 (search-paths #$(map search-path-specification->sexp
366 (properties . #$properties)))
367 (($ <manifest-entry> name version output package
368 (deps ...) (search-paths ...) _ (properties ...))
369 #~(#$name #$version #$output
370 (ungexp package (or output "out"))
371 (propagated-inputs #$(map entry->gexp deps))
372 (search-paths #$(map search-path-specification->sexp
374 (properties . #$properties)))))
377 (($ <manifest> (entries ...))
378 #~(manifest (version 3)
379 (packages #$(map entry->gexp entries))))))
381 (define (find-package name version)
382 "Return a package from the distro matching NAME and possibly VERSION. This
383 procedure is here for backward-compatibility and will eventually vanish."
384 (define find-best-packages-by-name ;break abstractions
385 (module-ref (resolve-interface '(gnu packages))
386 'find-best-packages-by-name))
388 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
389 ;; former traverses the module tree only once and then allows for efficient
390 ;; access via a vhash.
391 (match (find-best-packages-by-name name version)
394 (match (find-best-packages-by-name name #f)
398 (define (sexp->manifest sexp)
399 "Parse SEXP as a manifest."
400 (define (infer-search-paths name version)
401 ;; Infer the search path specifications for NAME-VERSION by looking up a
402 ;; same-named package in the distro. Useful for the old manifest formats
403 ;; that did not store search path info.
404 (let ((package (find-package name version)))
406 (package-native-search-paths package)
409 (define (infer-dependency item parent)
410 ;; Return a <manifest-entry> for ITEM.
411 (let-values (((name version)
412 (package-name->name+version
413 (store-path-package-name item))))
420 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
422 ((name version output path
423 ('propagated-inputs deps)
424 ('search-paths search-paths)
426 ;; For each of DEPS, keep a promise pointing to ENTRY.
427 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
429 (entry (manifest-entry
435 (search-paths (map sexp->search-path-specification
438 (properties (or (assoc-ref extra-stuff 'properties)
443 (('manifest ('version 0)
444 ('packages ((name version output path) ...)))
446 (map (lambda (name version output path)
452 (search-paths (infer-search-paths name version))))
453 name version output path)))
455 ;; Version 1 adds a list of propagated inputs to the
456 ;; name/version/output/path tuples.
457 (('manifest ('version 1)
458 ('packages ((name version output path deps) ...)))
460 (map (lambda (name version output path deps)
461 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
462 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
464 (let ((deps (match deps
465 (((labels directories) ...)
469 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
471 (entry (manifest-entry
478 (infer-search-paths name version)))))
480 name version output path deps)))
482 ;; Version 2 adds search paths and is slightly more verbose.
483 (('manifest ('version 2 minor-version ...)
484 ('packages ((name version output path
485 ('propagated-inputs deps)
486 ('search-paths search-paths)
490 (map (lambda (name version output path deps search-paths)
491 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
493 (entry (manifest-entry
500 (map sexp->search-path-specification
503 name version output path deps search-paths)))
505 ;; Version 3 represents DEPS as full-blown manifest entries.
506 (('manifest ('version 3 minor-version ...)
507 ('packages (entries ...)))
508 (manifest (map sexp->manifest-entry entries)))
511 (&message (message "unsupported manifest format")))))))
513 (define (read-manifest port)
514 "Return the packages listed in MANIFEST."
515 (sexp->manifest (read port)))
517 (define (entry-predicate pattern)
518 "Return a procedure that returns #t when passed a manifest entry that
519 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
522 (($ <manifest-pattern> name version output)
524 (($ <manifest-entry> entry-name entry-version entry-output)
525 (and (string=? entry-name name)
526 (or (not entry-output) (not output)
527 (string=? entry-output output))
529 (string=? entry-version version))))))))
531 (define (manifest-remove manifest patterns)
532 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
533 must be a manifest-pattern."
534 (define (remove-entry pattern lst)
535 (remove (entry-predicate pattern) lst))
537 (make-manifest (fold remove-entry
538 (manifest-entries manifest)
541 (define (manifest-add manifest entries)
542 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
543 Remove MANIFEST entries that have the same name and output as ENTRIES."
544 (define (same-entry? entry name output)
546 (($ <manifest-entry> entry-name _ entry-output _)
547 (and (equal? name entry-name)
548 (equal? output entry-output)))))
551 (fold (lambda (entry result) ;XXX: quadratic
553 (($ <manifest-entry> name _ out _)
555 (remove (cut same-entry? <> name out)
557 (manifest-entries manifest)
560 (define (manifest-lookup manifest pattern)
561 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
563 (find (entry-predicate pattern)
564 (manifest-entries manifest)))
566 (define (manifest-installed? manifest pattern)
567 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
569 (->bool (manifest-lookup manifest pattern)))
571 (define (manifest-matching-entries manifest patterns)
572 "Return all the entries of MANIFEST that match one of the PATTERNS. Raise
573 an '&unmatched-pattern-error' if none of the entries of MANIFEST matches one
575 (fold-right (lambda (pattern matches)
576 (match (filter (entry-predicate pattern)
577 (manifest-entries manifest))
580 (&unmatched-pattern-error
582 (manifest manifest)))))
584 (append lst matches))))
588 (define (manifest-search-paths manifest)
589 "Return the list of search path specifications that apply to MANIFEST,
590 including the search path specification for $PATH."
593 (append-map manifest-entry-search-paths
594 (manifest-entries manifest)))))
598 ;;; Manifest transactions.
601 (define-record-type* <manifest-transaction> manifest-transaction
602 make-manifest-transaction
603 manifest-transaction?
604 (install manifest-transaction-install ; list of <manifest-entry>
606 (remove manifest-transaction-remove ; list of <manifest-pattern>
609 (define (manifest-transaction-install-entry entry transaction)
610 "Augment TRANSACTION's set of installed packages with ENTRY, a
612 (manifest-transaction
613 (inherit transaction)
615 (cons entry (manifest-transaction-install transaction)))))
617 (define (manifest-transaction-remove-pattern pattern transaction)
618 "Add PATTERN to TRANSACTION's list of packages to remove."
619 (manifest-transaction
620 (inherit transaction)
622 (cons pattern (manifest-transaction-remove transaction)))))
624 (define (manifest-transaction-null? transaction)
625 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
628 (($ <manifest-transaction> () ()) #t)
629 (($ <manifest-transaction> _ _) #f)))
631 (define (manifest-transaction-removal-candidate? entry transaction)
632 "Return true if ENTRY is a candidate for removal in TRANSACTION."
633 (any (lambda (pattern)
634 ((entry-predicate pattern) entry))
635 (manifest-transaction-remove transaction)))
637 (define (manifest-transaction-effects manifest transaction)
638 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
639 the list of packages that would be removed, installed, upgraded, or downgraded
640 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
641 where the head is the entry being upgraded and the tail is the entry that will
643 (define (manifest-entry->pattern entry)
645 (name (manifest-entry-name entry))
646 (output (manifest-entry-output entry))))
648 (let loop ((input (manifest-transaction-install transaction))
654 (let ((remove (manifest-transaction-remove transaction)))
655 (values (manifest-matching-entries manifest remove)
656 (reverse install) (reverse upgrade) (reverse downgrade))))
658 ;; Check whether installing ENTRY corresponds to the installation of a
659 ;; new package or to an upgrade.
661 ;; XXX: When the exact same output directory is installed, we're not
662 ;; really upgrading anything. Add a check for that case.
663 (let* ((pattern (manifest-entry->pattern entry))
664 (previous (manifest-lookup manifest pattern))
665 (newer? (and previous
666 (version>=? (manifest-entry-version entry)
667 (manifest-entry-version previous)))))
669 (if previous install (cons entry install))
670 (if (and previous newer?)
671 (alist-cons previous entry upgrade)
673 (if (and previous (not newer?))
674 (alist-cons previous entry downgrade)
677 (define (manifest-perform-transaction manifest transaction)
678 "Perform TRANSACTION on MANIFEST and return the new manifest."
679 (let ((install (manifest-transaction-install transaction))
680 (remove (manifest-transaction-remove transaction)))
681 (manifest-add (manifest-remove manifest remove)
689 (define (manifest-inputs manifest)
690 "Return a list of <gexp-input> objects for MANIFEST."
693 (($ <manifest-entry> name version output thing deps)
694 ;; THING may be a package or a file name. In the latter case, assume
695 ;; it's already valid.
696 (cons (gexp-input thing output)
697 (append-map entry->input deps)))))
699 (append-map entry->input (manifest-entries manifest)))
701 (define* (manifest-lookup-package manifest name #:optional version)
702 "Return as a monadic value the first package or store path referenced by
703 MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
705 ;; Return as a monadic value the package or store path referenced by the
706 ;; manifest ENTRY, or #f if not referenced.
707 (define (entry-lookup-package entry)
708 (define (find-among-inputs inputs)
709 (find (lambda (input)
710 (and (package? input)
711 (equal? name (package-name input))
713 (string-prefix? version (package-version input))
716 (define (find-among-store-items items)
718 (let-values (((name* version*)
719 (package-name->name+version
720 (store-path-package-name item))))
721 (and (string=? name name*)
723 (string-prefix? version version*)
727 (with-monad %store-monad
728 (match (manifest-entry-item entry)
729 ((? package? package)
730 (match (cons (list (package-name package) package)
731 (package-transitive-inputs package))
732 (((labels inputs . _) ...)
733 (return (find-among-inputs inputs)))))
735 (mlet %store-monad ((refs (references* item)))
736 (return (find-among-store-items refs))))
738 ;; XXX: ITEM might be a 'computed-file' or anything like that, in
739 ;; which case we don't know what to do. The fix may be to check
740 ;; references once ITEM is compiled, as proposed at
741 ;; <https://bugs.gnu.org/29927>.
745 entry-lookup-package (manifest-entries manifest)))
747 (define (info-dir-file manifest)
748 "Return a derivation that builds the 'dir' file for all the entries of
750 (define texinfo ;lazy reference
751 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
752 (define gzip ;lazy reference
753 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
754 (define glibc-utf8-locales ;lazy reference
755 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
758 (with-imported-modules '((guix build utils))
760 (use-modules (guix build utils)
761 (srfi srfi-1) (srfi srfi-26)
764 (define (info-file? file)
765 (or (string-suffix? ".info" file)
766 (string-suffix? ".info.gz" file)))
768 (define (info-files top)
769 (let ((infodir (string-append top "/share/info")))
770 (map (cut string-append infodir "/" <>)
771 (or (scandir infodir info-file?) '()))))
773 (define (info-file-language file)
774 (let* ((base (if (string-suffix? ".gz" file)
775 (basename file ".info.gz")
776 (basename file ".info")))
777 (dot (string-rindex base #\.)))
779 (string-drop base (+ 1 dot))
782 (define (install-info info)
783 (let ((language (info-file-language info)))
784 ;; We need to choose a valid locale for $LANGUAGE to be honored.
785 (setenv "LC_ALL" "en_US.utf8")
786 (setenv "LANGUAGE" language)
788 (system* #+(file-append texinfo "/bin/install-info")
790 (apply string-append #$output "/share/info/dir"
791 (if (string=? "en" language)
793 `("." ,language)))))))
795 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
796 (setenv "GUIX_LOCPATH"
797 #+(file-append glibc-utf8-locales "/lib/locale"))
799 (mkdir-p (string-append #$output "/share/info"))
800 (exit (every install-info
801 (append-map info-files
802 '#$(manifest-inputs manifest)))))))
804 (gexp->derivation "info-dir" build
808 `((type . profile-hook)
811 (define (ghc-package-cache-file manifest)
812 "Return a derivation that builds the GHC 'package.cache' file for all the
813 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
814 (define ghc ;lazy reference
815 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
818 (with-imported-modules '((guix build utils))
820 (use-modules (guix build utils)
821 (srfi srfi-1) (srfi srfi-26)
824 (define ghc-name-version
825 (let* ((base (basename #+ghc)))
827 (+ 1 (string-index base #\-)))))
830 (string-append "lib/" ghc-name-version "/package.conf.d"))
833 (string-append #$output "/" db-subdir))
835 (define (conf-files top)
836 (let ((db (string-append top "/" db-subdir)))
837 (if (file-exists? db)
838 (find-files db "\\.conf$")
841 (define (copy-conf-file conf)
842 (let ((base (basename conf)))
843 (copy-file conf (string-append db-dir "/" base))))
845 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
846 (for-each copy-conf-file
847 (append-map conf-files
849 '#$(manifest-inputs manifest))))
852 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
853 (string-append "--package-db=" db-dir)))))
854 (for-each delete-file (find-files db-dir "\\.conf$"))
857 (with-monad %store-monad
858 ;; Don't depend on GHC when there's nothing to do.
859 (if (any (cut string-prefix? "ghc" <>)
860 (map manifest-entry-name (manifest-entries manifest)))
861 (gexp->derivation "ghc-package-cache" build
865 `((type . profile-hook)
866 (hook . ghc-package-cache)))
869 (define (ca-certificate-bundle manifest)
870 "Return a derivation that builds a single-file bundle containing the CA
871 certificates in the /etc/ssl/certs sub-directories of the packages in
872 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
873 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
876 (define glibc-utf8-locales ;lazy reference
877 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
880 (with-imported-modules '((guix build utils))
882 (use-modules (guix build utils)
889 (define (pem-file? file)
890 (string-suffix? ".pem" file))
892 (define (ca-files top)
893 (let ((cert-dir (string-append top "/etc/ssl/certs")))
894 (map (cut string-append cert-dir "/" <>)
895 (or (scandir cert-dir pem-file?) '()))))
897 (define (concatenate-files files result)
898 "Make RESULT the concatenation of all of FILES."
899 (define (dump file port)
900 (display (call-with-input-file file get-string-all)
902 (newline port)) ;required, see <https://bugs.debian.org/635570>
904 (call-with-output-file result
906 (for-each (cut dump <> port) files))))
908 ;; Some file names in the NSS certificates are UTF-8 encoded so
909 ;; install a UTF-8 locale.
911 (string-append #+glibc-utf8-locales "/lib/locale/"
912 #+(version-major+minor
913 (package-version glibc-utf8-locales))))
914 (setlocale LC_ALL "en_US.utf8")
916 (match (append-map ca-files '#$(manifest-inputs manifest))
918 ;; Since there are no CA files, just create an empty directory. Do
919 ;; not create the etc/ssl/certs sub-directory, since that would
920 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
925 (let ((result (string-append #$output "/etc/ssl/certs")))
927 (concatenate-files ca-files
928 (string-append result
929 "/ca-certificates.crt"))
932 (gexp->derivation "ca-certificate-bundle" build
936 `((type . profile-hook)
937 (hook . ca-certificate-bundle))))
939 (define (glib-schemas manifest)
940 "Return a derivation that unions all schemas from manifest entries and
941 creates the Glib 'gschemas.compiled' file."
942 (define glib ; lazy reference
943 (module-ref (resolve-interface '(gnu packages glib)) 'glib))
945 (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
946 ;; XXX: Can't use glib-compile-schemas corresponding
947 ;; to the glib referenced by 'manifest'. Because
948 ;; '%glib' can be either a package or store path, and
949 ;; there's no way to get the "bin" output for the later.
950 (glib-compile-schemas
951 -> #~(string-append #+glib:bin
952 "/bin/glib-compile-schemas")))
955 (with-imported-modules '((guix build utils)
957 (guix build profiles)
961 (use-modules (guix build utils)
963 (guix build profiles)
966 (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
967 (schemadirs (filter file-exists?
968 (map (cut string-append <> "/share/glib-2.0/schemas")
969 '#$(manifest-inputs manifest)))))
971 ;; Union all the schemas.
972 (mkdir-p (string-append #$output "/share/glib-2.0"))
973 (union-build destdir schemadirs
974 #:log-port (%make-void-port "w"))
977 (when (file-is-directory? dir)
978 (ensure-writable-directory dir)
979 (invoke #+glib-compile-schemas
980 (string-append "--targetdir=" dir)
983 ;; Don't run the hook when there's nothing to do.
985 (gexp->derivation "glib-schemas" build
989 `((type . profile-hook)
990 (hook . glib-schemas)))
993 (define (gtk-icon-themes manifest)
994 "Return a derivation that unions all icon themes from manifest entries and
995 creates the GTK+ 'icon-theme.cache' file for each theme."
996 (define gtk+ ; lazy reference
997 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
999 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
1000 ;; XXX: Can't use gtk-update-icon-cache corresponding
1001 ;; to the gtk+ referenced by 'manifest'. Because
1002 ;; '%gtk+' can be either a package or store path, and
1003 ;; there's no way to get the "bin" output for the later.
1004 (gtk-update-icon-cache
1005 -> #~(string-append #+gtk+:bin
1006 "/bin/gtk-update-icon-cache")))
1009 (with-imported-modules '((guix build utils)
1011 (guix build profiles)
1015 (use-modules (guix build utils)
1017 (guix build profiles)
1021 (let* ((destdir (string-append #$output "/share/icons"))
1022 (icondirs (filter file-exists?
1023 (map (cut string-append <> "/share/icons")
1024 '#$(manifest-inputs manifest)))))
1026 ;; Union all the icons.
1027 (mkdir-p (string-append #$output "/share"))
1028 (union-build destdir icondirs
1029 #:log-port (%make-void-port "w"))
1031 ;; Update the 'icon-theme.cache' file for each icon theme.
1034 (let ((dir (string-append destdir "/" theme)))
1035 ;; Occasionally DESTDIR contains plain files, such as
1036 ;; "abiword_48.png". Ignore these.
1037 (when (file-is-directory? dir)
1038 (ensure-writable-directory dir)
1039 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
1040 (scandir destdir (negate (cut member <> '("." "..")))))))))
1042 ;; Don't run the hook when there's nothing to do.
1044 (gexp->derivation "gtk-icon-themes" build
1048 `((type . profile-hook)
1049 (hook . gtk-icon-themes)))
1052 (define (gtk-im-modules manifest)
1053 "Return a derivation that builds the cache files for input method modules
1054 for both major versions of GTK+."
1056 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
1057 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
1059 (define (build gtk gtk-version query)
1060 (let ((major (string-take gtk-version 1)))
1061 (with-imported-modules '((guix build utils)
1063 (guix build profiles)
1067 (use-modules (guix build utils)
1069 (guix build profiles)
1074 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
1076 (destdir (string-append #$output prefix))
1077 (moddirs (cons (string-append #$gtk prefix "/immodules")
1078 (filter file-exists?
1079 (map (cut string-append <> prefix "/immodules")
1080 '#$(manifest-inputs manifest)))))
1081 (modules (append-map (cut find-files <> "\\.so$")
1084 ;; Generate a new immodules cache file.
1085 (mkdir-p (string-append #$output prefix))
1086 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
1087 (outfile (string-append #$output prefix
1088 "/immodules-gtk" #$major ".cache")))
1092 (call-with-output-file outfile
1094 (while (not (eof-object? (peek-char pipe)))
1095 (write-char (read-char pipe) out))))
1098 (close-pipe pipe)))))))))
1100 ;; Don't run the hook when there's nothing to do.
1101 (let* ((pkg-gtk+ (module-ref ; lazy reference
1102 (resolve-interface '(gnu packages gtk)) 'gtk+))
1107 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
1108 ;; output of latest gtk+ package.
1110 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
1116 #$gtk+-2 "/bin/gtk-query-immodules-2.0"))
1118 (if (or gtk+ gtk+-2)
1119 (gexp->derivation "gtk-im-modules" gexp
1123 `((type . profile-hook)
1124 (hook . gtk-im-modules)))
1127 (define (xdg-desktop-database manifest)
1128 "Return a derivation that builds the @file{mimeinfo.cache} database from
1129 desktop files. It's used to query what applications can handle a given
1131 (define desktop-file-utils ; lazy reference
1132 (module-ref (resolve-interface '(gnu packages freedesktop))
1133 'desktop-file-utils))
1135 (mlet %store-monad ((glib
1136 (manifest-lookup-package
1139 (with-imported-modules '((guix build utils)
1142 (use-modules (srfi srfi-26)
1145 (let* ((destdir (string-append #$output "/share/applications"))
1146 (appdirs (filter file-exists?
1147 (map (cut string-append <>
1148 "/share/applications")
1149 '#$(manifest-inputs manifest))))
1150 (update-desktop-database (string-append
1151 #+desktop-file-utils
1152 "/bin/update-desktop-database")))
1153 (mkdir-p (string-append #$output "/share"))
1154 (union-build destdir appdirs
1155 #:log-port (%make-void-port "w"))
1156 (exit (zero? (system* update-desktop-database destdir)))))))
1158 ;; Don't run the hook when 'glib' is not referenced.
1160 (gexp->derivation "xdg-desktop-database" build
1164 `((type . profile-hook)
1165 (hook . xdg-desktop-database)))
1168 (define (xdg-mime-database manifest)
1169 "Return a derivation that builds the @file{mime.cache} database from manifest
1170 entries. It's used to query the MIME type of a given file."
1171 (define shared-mime-info ; lazy reference
1172 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1174 (mlet %store-monad ((glib
1175 (manifest-lookup-package
1178 (with-imported-modules '((guix build utils)
1181 (use-modules (srfi srfi-26)
1184 (let* ((datadir (string-append #$output "/share"))
1185 (destdir (string-append datadir "/mime"))
1186 (pkgdirs (filter file-exists?
1187 (map (cut string-append <>
1188 "/share/mime/packages")
1189 (cons #+shared-mime-info
1190 '#$(manifest-inputs manifest)))))
1191 (update-mime-database (string-append
1193 "/bin/update-mime-database")))
1195 (union-build (string-append destdir "/packages") pkgdirs
1196 #:log-port (%make-void-port "w"))
1197 (setenv "XDG_DATA_HOME" datadir)
1198 (exit (zero? (system* update-mime-database destdir)))))))
1200 ;; Don't run the hook when there are no GLib based applications.
1202 (gexp->derivation "xdg-mime-database" build
1206 `((type . profile-hook)
1207 (hook . xdg-mime-database)))
1210 ;; Several font packages may install font files into same directory, so
1211 ;; fonts.dir and fonts.scale file should be generated here, instead of in
1213 (define (fonts-dir-file manifest)
1214 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
1215 files for the fonts of the @var{manifest} entries."
1217 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1220 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1224 (use-modules (srfi srfi-26)
1227 (let ((fonts-dirs (filter file-exists?
1228 (map (cut string-append <>
1230 '#$(manifest-inputs manifest)))))
1232 (if (null? fonts-dirs)
1234 (let* ((share-dir (string-append #$output "/share"))
1235 (fonts-dir (string-append share-dir "/fonts"))
1236 (mkfontscale (string-append #+mkfontscale
1237 "/bin/mkfontscale"))
1238 (mkfontdir (string-append #+mkfontdir
1240 (empty-file? (lambda (filename)
1241 (call-with-ascii-input-file filename
1243 (eqv? #\0 (read-char p))))))
1244 (fonts-dir-file "fonts.dir")
1245 (fonts-scale-file "fonts.scale"))
1247 ;; Create all sub-directories, because we may create fonts.dir
1248 ;; and fonts.scale files in the sub-directories.
1249 (union-build fonts-dir fonts-dirs
1250 #:log-port (%make-void-port "w")
1251 #:create-all-directories? #t)
1252 (let ((directories (find-files fonts-dir
1254 (eq? 'directory (stat:type stat)))
1255 #:directories? #t)))
1256 (for-each (lambda (dir)
1257 (with-directory-excursion dir
1258 (when (file-exists? fonts-scale-file)
1259 (delete-file fonts-scale-file))
1260 (when (file-exists? fonts-dir-file)
1261 (delete-file fonts-dir-file))
1262 (unless (and (zero? (system* mkfontscale))
1263 (zero? (system* mkfontdir)))
1265 (when (and (file-exists? fonts-scale-file)
1266 (empty-file? fonts-scale-file))
1267 (delete-file fonts-scale-file))
1268 (when (and (file-exists? fonts-dir-file)
1269 (empty-file? fonts-dir-file))
1270 (delete-file fonts-dir-file))))
1273 (gexp->derivation "fonts-dir" build
1274 #:modules '((guix build utils)
1280 `((type . profile-hook)
1281 (hook . fonts-dir))))
1283 (define (manual-database manifest)
1284 "Return a derivation that builds the manual page database (\"mandb\") for
1285 the entries in MANIFEST."
1287 (module-ref (resolve-interface '(gnu packages guile))
1291 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
1294 (scheme-file "config.scm"
1296 (define-module #$'(guix config) ;placate Geiser
1300 #+(file-append zlib "/lib/libz")))))
1303 (cons `((guix config) => ,config.scm)
1304 (delete '(guix config)
1305 (source-module-closure `((guix build utils)
1309 (with-imported-modules modules
1310 (with-extensions (list gdbm-ffi) ;for (guix man-db)
1312 (use-modules (guix man-db)
1317 (define (compute-entries)
1318 ;; This is the most expensive part (I/O and CPU, due to
1319 ;; decompression), so report progress as we traverse INPUTS.
1320 (let* ((inputs '#$(manifest-inputs manifest))
1321 (total (length inputs)))
1322 (append-map (lambda (directory count)
1323 (format #t "\r[~3d/~3d] building list of \
1327 (let ((man (string-append directory
1329 (if (directory-exists? man)
1335 (define man-directory
1336 (string-append #$output "/share/man"))
1338 (mkdir-p man-directory)
1340 (format #t "Creating manual page database...~%")
1342 (let* ((start (current-time))
1343 (entries (compute-entries))
1344 (_ (write-mandb-database (string-append man-directory
1347 (duration (time-difference (current-time) start)))
1349 (format #t "~a entries processed in ~,1f s~%"
1351 (+ (time-second duration)
1352 (* (time-nanosecond duration) (expt 10 -9))))
1355 (gexp->derivation "manual-database" build
1357 ;; Work around GDBM 1.13 issue whereby uninitialized bytes
1358 ;; get written to disk:
1359 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
1360 #:env-vars `(("MALLOC_PERTURB_" . "1"))
1364 `((type . profile-hook)
1365 (hook . manual-database))))
1367 (define (texlive-configuration manifest)
1368 "Return a derivation that builds a TeXlive configuration for the entries in
1370 (define entry->texlive-input
1372 (($ <manifest-entry> name version output thing deps)
1373 (if (string-prefix? "texlive-" name)
1374 (cons (gexp-input thing output)
1375 (append-map entry->texlive-input deps))
1378 (with-imported-modules '((guix build utils)
1381 (use-modules (guix build utils)
1384 ;; Build a modifiable union of all texlive inputs. We do this so
1385 ;; that TeX live can resolve the parent and grandparent directories
1386 ;; correctly. There might be a more elegant way to accomplish this.
1387 (union-build #$output
1388 '#$(append-map entry->texlive-input
1389 (manifest-entries manifest))
1390 #:create-all-directories? #t
1391 #:log-port (%make-void-port "w"))
1392 (let ((texmf.cnf (string-append
1394 "/share/texmf-dist/web2c/texmf.cnf")))
1395 (when (file-exists? texmf.cnf)
1396 (substitute* texmf.cnf
1397 (("^TEXMFROOT = .*")
1398 (string-append "TEXMFROOT = " #$output "/share\n"))
1400 "TEXMF = $TEXMFROOT/share/texmf-dist\n"))))
1403 (with-monad %store-monad
1404 (if (any (cut string-prefix? "texlive-" <>)
1405 (map manifest-entry-name (manifest-entries manifest)))
1406 (gexp->derivation "texlive-configuration" build
1410 `((type . profile-hook)
1411 (hook . texlive-configuration)))
1414 (define %default-profile-hooks
1415 ;; This is the list of derivation-returning procedures that are called by
1416 ;; default when making a non-empty profile.
1420 ghc-package-cache-file
1421 ca-certificate-bundle
1425 texlive-configuration
1426 xdg-desktop-database
1429 (define* (profile-derivation manifest
1431 (hooks %default-profile-hooks)
1433 (allow-collisions? #f)
1434 (relative-symlinks? #f)
1436 "Return a derivation that builds a profile (aka. 'user environment') with
1437 the given MANIFEST. The profile includes additional derivations returned by
1438 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
1439 Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1440 entries in MANIFEST collide (for instance if there are two same-name packages
1441 with a different version number.)
1443 When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
1444 a dependency on the 'glibc-utf8-locales' package.
1446 When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
1447 This is one of the things to do for the result to be relocatable.
1449 When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1450 are cross-built for TARGET."
1451 (mlet* %store-monad ((system (if system
1454 (ok? (if allow-collisions?
1456 (check-for-collisions manifest system
1458 (extras (if (null? (manifest-entries manifest))
1465 (append (filter-map (lambda (drv)
1466 (and (derivation? drv)
1469 (manifest-inputs manifest)))
1471 (define glibc-utf8-locales ;lazy reference
1472 (module-ref (resolve-interface '(gnu packages base))
1473 'glibc-utf8-locales))
1475 (define set-utf8-locale
1476 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1477 ;; install a UTF-8 locale.
1480 #$(file-append glibc-utf8-locales "/lib/locale/"
1481 (version-major+minor
1482 (package-version glibc-utf8-locales))))
1483 (setlocale LC_ALL "en_US.utf8")))
1486 (with-imported-modules '((guix build profiles)
1492 (use-modules (guix build profiles)
1496 (setvbuf (current-output-port) _IOLBF)
1497 (setvbuf (current-error-port) _IOLBF)
1499 #+(if locales? set-utf8-locale #t)
1501 (define search-paths
1502 ;; Search paths of MANIFEST's packages, converted back to their
1504 (map sexp->search-path-specification
1506 '#$(map search-path-specification->sexp
1507 (manifest-search-paths manifest)))))
1509 (build-profile #$output '#$inputs
1510 #:symlink #$(if relative-symlinks?
1513 #:manifest '#$(manifest->gexp manifest)
1514 #:search-paths search-paths))))
1516 (gexp->derivation "profile" builder
1520 ;; Don't complain about _IO* on Guile 2.2.
1521 #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
1523 ;; Not worth offloading.
1526 ;; Disable substitution because it would trigger a
1527 ;; connection to the substitute server, which is likely
1528 ;; to have no substitute to offer.
1529 #:substitutable? #f)))
1531 (define* (profile-search-paths profile
1532 #:optional (manifest (profile-manifest profile))
1533 #:key (getenv (const #f)))
1534 "Read the manifest of PROFILE and evaluate the values of search path
1535 environment variables required by PROFILE; return a list of
1536 specification/value pairs. If MANIFEST is not #f, it is assumed to be the
1537 manifest of PROFILE, which avoids rereading it.
1539 Use GETENV to determine the current settings and report only settings not
1541 (evaluate-search-paths (manifest-search-paths manifest)
1542 (list profile) getenv))
1544 (define (profile-regexp profile)
1545 "Return a regular expression that matches PROFILE's name and number."
1546 (make-regexp (string-append "^" (regexp-quote (basename profile))
1549 (define (generation-number profile)
1550 "Return PROFILE's number or 0. An absolute file name must be used."
1551 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
1552 (basename (readlink profile))))
1553 (compose string->number (cut match:substring <> 1)))
1556 (define %profile-generation-rx
1557 ;; Regexp that matches profile generation.
1558 (make-regexp "(.*)-([0-9]+)-link$"))
1560 (define (generation-profile file)
1561 "If FILE is a profile generation GC root such as \"guix-profile-42-link\",
1562 return its corresponding profile---e.g., \"guix-profile\". Otherwise return
1564 (match (regexp-exec %profile-generation-rx file)
1566 (m (let ((profile (match:substring m 1)))
1567 (and (file-exists? (string-append profile "/manifest"))
1570 (define (generation-numbers profile)
1571 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
1572 former profiles were found."
1573 (match (scandir (dirname profile)
1574 (cute regexp-exec (profile-regexp profile) <>))
1575 (#f ; no profile directory
1579 ((profiles ...) ; former profiles around
1580 (sort (map (compose string->number
1581 (cut match:substring <> 1)
1582 (cute regexp-exec (profile-regexp profile) <>))
1586 (define (profile-generations profile)
1587 "Return a list of PROFILE's generations."
1588 (let ((generations (generation-numbers profile)))
1589 (if (equal? generations '(0))
1593 (define (relative-generation-spec->number profile spec)
1594 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
1595 may be a N, -N, or +N, where N is a number. If the spec is N, then the number
1596 returned is N. If it is -N, then the number returned is the profile's current
1597 generation number minus N. If it is +N, then the number returned is the
1598 profile's current generation number plus N. Return #f if there is no such
1600 (let ((number (string->number spec)))
1602 (case (string-ref spec 0)
1604 (relative-generation profile number))
1605 (else (if (memv number (profile-generations profile))
1610 (define* (relative-generation profile shift #:optional
1611 (current (generation-number profile)))
1612 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
1613 SHIFT is a positive or negative number.
1614 Return #f if there is no such generation."
1615 (let* ((abs-shift (abs shift))
1616 (numbers (profile-generations profile))
1617 (from-current (memq current
1618 (if (negative? shift)
1622 (< abs-shift (length from-current))
1623 (list-ref from-current abs-shift))))
1625 (define* (previous-generation-number profile #:optional
1626 (number (generation-number profile)))
1627 "Return the number of the generation before generation NUMBER of
1628 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
1629 case when generations have been deleted (there are \"holes\")."
1630 (or (relative-generation profile -1 number)
1633 (define (generation-file-name profile generation)
1634 "Return the file name for PROFILE's GENERATION."
1635 (format #f "~a-~a-link" profile generation))
1637 (define (generation-time profile number)
1638 "Return the creation time of a generation in the UTC format."
1639 (make-time time-utc 0
1640 (stat:ctime (stat (generation-file-name profile number)))))
1642 (define (link-to-empty-profile store generation)
1643 "Link GENERATION, a string, to the empty profile. An error is raised if
1645 (let* ((drv (run-with-store store
1646 (profile-derivation (manifest '())
1648 (prof (derivation->output-path drv "out")))
1649 (build-derivations store (list drv))
1650 (switch-symlinks generation prof)))
1652 (define (switch-to-generation profile number)
1653 "Atomically switch PROFILE to the generation NUMBER. Return the number of
1654 the generation that was current before switching."
1655 (let ((current (generation-number profile))
1656 (generation (generation-file-name profile number)))
1657 (cond ((not (file-exists? profile))
1658 (raise (condition (&profile-not-found-error
1659 (profile profile)))))
1660 ((not (file-exists? generation))
1661 (raise (condition (&missing-generation-error
1663 (generation number)))))
1665 (switch-symlinks profile (basename generation))
1668 (define (switch-to-previous-generation profile)
1669 "Atomically switch PROFILE to the previous generation. Return the former
1670 generation number and the current one."
1671 (let ((previous (previous-generation-number profile)))
1672 (values (switch-to-generation profile previous)
1675 (define (roll-back store profile)
1676 "Roll back to the previous generation of PROFILE. Return the number of the
1677 generation that was current before switching and the new generation number."
1678 (let* ((number (generation-number profile))
1679 (previous-number (previous-generation-number profile number))
1680 (previous-generation (generation-file-name profile previous-number)))
1681 (cond ((not (file-exists? profile)) ;invalid profile
1682 (raise (condition (&profile-not-found-error
1683 (profile profile)))))
1684 ((zero? number) ;empty profile
1685 (values number number))
1686 ((or (zero? previous-number) ;going to emptiness
1687 (not (file-exists? previous-generation)))
1688 (link-to-empty-profile store previous-generation)
1689 (switch-to-previous-generation profile))
1690 (else ;anything else
1691 (switch-to-previous-generation profile)))))
1693 (define (delete-generation store profile number)
1694 "Delete generation with NUMBER from PROFILE. Return the file name of the
1695 generation that has been deleted, or #f if nothing was done (for instance
1696 because the NUMBER is zero.)"
1697 (define (delete-and-return)
1698 (let ((generation (generation-file-name profile number)))
1699 (delete-file generation)
1702 (let* ((current-number (generation-number profile))
1703 (previous-number (previous-generation-number profile number))
1704 (previous-generation (generation-file-name profile previous-number)))
1705 (cond ((zero? number) #f) ;do not delete generation 0
1706 ((and (= number current-number)
1707 (not (file-exists? previous-generation)))
1708 (link-to-empty-profile store previous-generation)
1709 (switch-to-previous-generation profile)
1710 (delete-and-return))
1711 ((= number current-number)
1712 (roll-back store profile)
1713 (delete-and-return))
1715 (delete-and-return)))))
1717 (define %user-profile-directory
1718 (and=> (getenv "HOME")
1719 (cut string-append <> "/.guix-profile")))
1721 (define %profile-directory
1722 (string-append %state-directory "/profiles/"
1723 (or (and=> (or (getenv "USER")
1725 (cut string-append "per-user/" <>))
1728 (define %current-profile
1729 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
1730 ;; coexist with Nix profiles.
1731 (string-append %profile-directory "/guix-profile"))
1733 (define (ensure-profile-directory)
1734 "Attempt to create /…/profiles/per-user/$USER if needed."
1735 (let ((s (stat %profile-directory #f)))
1736 (unless (and s (eq? 'directory (stat:type s)))
1737 (catch 'system-error
1739 (mkdir-p %profile-directory))
1741 ;; Often, we cannot create %PROFILE-DIRECTORY because its
1742 ;; parent directory is root-owned and we're running
1748 (G_ "while creating directory `~a': ~a")
1750 (strerror (system-error-errno args)))))
1753 (format #f (G_ "Please create the @file{~a} directory, \
1754 with you as the owner.")
1755 %profile-directory))))))))
1757 ;; Bail out if it's not owned by the user.
1758 (unless (or (not s) (= (stat:uid s) (getuid)))
1762 (format #f (G_ "directory `~a' is not owned by you")
1763 %profile-directory)))
1766 (format #f (G_ "Please change the owner of @file{~a} \
1768 %profile-directory (or (getenv "USER")
1772 (define (canonicalize-profile profile)
1773 "If PROFILE points to a profile in %PROFILE-DIRECTORY, return that.
1774 Otherwise return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile'
1775 as if '-p' was omitted." ; see <http://bugs.gnu.org/17939>
1776 ;; Trim trailing slashes so 'readlink' can do its job.
1777 (let ((profile (string-trim-right profile #\/)))
1778 (catch 'system-error
1780 (let ((target (readlink profile)))
1781 (if (string=? (dirname target) %profile-directory)
1786 (define %known-shorthand-profiles
1787 ;; Known shorthand forms for profiles that the user manipulates.
1788 (list (string-append (config-directory #:ensure? #f) "/current")
1789 %user-profile-directory))
1791 (define (user-friendly-profile profile)
1792 "Return either ~/.guix-profile or ~/.config/guix/current if that's what
1793 PROFILE refers to, directly or indirectly, or PROFILE."
1794 (or (find (lambda (shorthand)
1796 (let ((target (false-if-exception
1797 (readlink shorthand))))
1798 (and target (string=? target profile)))))
1799 %known-shorthand-profiles)
1802 ;;; profiles.scm ends here