1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
4 ;;; Copyright © 2014 Alex Kost <alezost@gmail.com>
5 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
6 ;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (guix profiles)
24 #:use-module ((guix utils) #:hide (package-name->name+version))
25 #:use-module ((guix build utils)
26 #:select (package-name->name+version))
27 #:use-module (guix records)
28 #:use-module (guix packages)
29 #:use-module (guix derivations)
30 #:use-module (guix search-paths)
31 #:use-module (guix gexp)
32 #:use-module (guix monads)
33 #:use-module (guix store)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 regex)
36 #:use-module (ice-9 ftw)
37 #:use-module (ice-9 format)
38 #:use-module (srfi srfi-1)
39 #:use-module (srfi srfi-9)
40 #:use-module (srfi srfi-11)
41 #:use-module (srfi srfi-19)
42 #:use-module (srfi srfi-26)
43 #:use-module (srfi srfi-34)
44 #:use-module (srfi srfi-35)
45 #:export (&profile-error
48 &profile-not-found-error
49 profile-not-found-error?
50 &missing-generation-error
51 missing-generation-error?
52 missing-generation-error-generation
54 manifest make-manifest
58 <manifest-entry> ; FIXME: eventually make it internal
62 manifest-entry-version
65 manifest-entry-dependencies
66 manifest-entry-search-paths
75 manifest-matching-entries
79 manifest-transaction-install
80 manifest-transaction-remove
81 manifest-perform-transaction
82 manifest-transaction-effects
85 package->manifest-entry
87 %default-profile-hooks
94 previous-generation-number
103 ;;; Tools to create and manipulate profiles---i.e., the representation of a
104 ;;; set of installed packages.
113 (define-condition-type &profile-error &error
115 (profile profile-error-profile))
117 (define-condition-type &profile-not-found-error &profile-error
118 profile-not-found-error?)
120 (define-condition-type &missing-generation-error &profile-error
121 missing-generation-error?
122 (generation missing-generation-error-generation))
129 (define-record-type <manifest>
132 (entries manifest-entries)) ; list of <manifest-entry>
134 ;; Convenient alias, to avoid name clashes.
135 (define make-manifest manifest)
137 (define-record-type* <manifest-entry> manifest-entry
140 (name manifest-entry-name) ; string
141 (version manifest-entry-version) ; string
142 (output manifest-entry-output ; string
144 (item manifest-entry-item) ; package | store path
145 (dependencies manifest-entry-dependencies ; (store path | package)*
147 (search-paths manifest-entry-search-paths ; search-path-specification*
150 (define-record-type* <manifest-pattern> manifest-pattern
151 make-manifest-pattern
153 (name manifest-pattern-name) ; string
154 (version manifest-pattern-version ; string | #f
156 (output manifest-pattern-output ; string | #f
159 (define (profile-manifest profile)
160 "Return the PROFILE's manifest."
161 (let ((file (string-append profile "/manifest")))
162 (if (file-exists? file)
163 (call-with-input-file file read-manifest)
166 (define* (package->manifest-entry package #:optional output)
167 "Return a manifest entry for the OUTPUT of package PACKAGE. When OUTPUT is
168 omitted or #f, use the first output of PACKAGE."
169 (let ((deps (map (match-lambda
171 (gexp-input package))
172 ((label package output)
173 (gexp-input package output)))
174 (package-transitive-propagated-inputs package))))
176 (name (package-name package))
177 (version (package-version package))
178 (output (or output (car (package-outputs package))))
180 (dependencies (delete-duplicates deps))
181 (search-paths (package-transitive-native-search-paths package)))))
183 (define (packages->manifest packages)
184 "Return a list of manifest entries, one for each item listed in PACKAGES.
185 Elements of PACKAGES can be either package objects or package/string tuples
186 denoting a specific output of a package."
190 (package->manifest-entry package output))
192 (package->manifest-entry package)))
195 (define (manifest->gexp manifest)
196 "Return a representation of MANIFEST as a gexp."
197 (define (entry->gexp entry)
199 (($ <manifest-entry> name version output (? string? path)
200 (deps ...) (search-paths ...))
201 #~(#$name #$version #$output #$path
202 (propagated-inputs #$deps)
203 (search-paths #$(map search-path-specification->sexp
205 (($ <manifest-entry> name version output (? package? package)
206 (deps ...) (search-paths ...))
207 #~(#$name #$version #$output
208 (ungexp package (or output "out"))
209 (propagated-inputs #$deps)
210 (search-paths #$(map search-path-specification->sexp
214 (($ <manifest> (entries ...))
215 #~(manifest (version 2)
216 (packages #$(map entry->gexp entries))))))
218 (define (find-package name version)
219 "Return a package from the distro matching NAME and possibly VERSION. This
220 procedure is here for backward-compatibility and will eventually vanish."
221 (define find-best-packages-by-name ;break abstractions
222 (module-ref (resolve-interface '(gnu packages))
223 'find-best-packages-by-name))
225 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
226 ;; former traverses the module tree only once and then allows for efficient
227 ;; access via a vhash.
228 (match (find-best-packages-by-name name version)
231 (match (find-best-packages-by-name name #f)
235 (define (sexp->manifest sexp)
236 "Parse SEXP as a manifest."
237 (define (infer-search-paths name version)
238 ;; Infer the search path specifications for NAME-VERSION by looking up a
239 ;; same-named package in the distro. Useful for the old manifest formats
240 ;; that did not store search path info.
241 (let ((package (find-package name version)))
243 (package-native-search-paths package)
247 (('manifest ('version 0)
248 ('packages ((name version output path) ...)))
250 (map (lambda (name version output path)
256 (search-paths (infer-search-paths name version))))
257 name version output path)))
259 ;; Version 1 adds a list of propagated inputs to the
260 ;; name/version/output/path tuples.
261 (('manifest ('version 1)
262 ('packages ((name version output path deps) ...)))
264 (map (lambda (name version output path deps)
265 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
266 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
268 (let ((deps (match deps
269 (((labels directories) ...)
279 (search-paths (infer-search-paths name version)))))
280 name version output path deps)))
282 ;; Version 2 adds search paths and is slightly more verbose.
283 (('manifest ('version 2 minor-version ...)
284 ('packages ((name version output path
285 ('propagated-inputs deps)
286 ('search-paths search-paths)
290 (map (lambda (name version output path deps search-paths)
297 (search-paths (map sexp->search-path-specification
299 name version output path deps search-paths)))
302 (&message (message "unsupported manifest format")))))))
304 (define (read-manifest port)
305 "Return the packages listed in MANIFEST."
306 (sexp->manifest (read port)))
308 (define (entry-predicate pattern)
309 "Return a procedure that returns #t when passed a manifest entry that
310 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
313 (($ <manifest-pattern> name version output)
315 (($ <manifest-entry> entry-name entry-version entry-output)
316 (and (string=? entry-name name)
317 (or (not entry-output) (not output)
318 (string=? entry-output output))
320 (string=? entry-version version))))))))
322 (define (manifest-remove manifest patterns)
323 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
324 must be a manifest-pattern."
325 (define (remove-entry pattern lst)
326 (remove (entry-predicate pattern) lst))
328 (make-manifest (fold remove-entry
329 (manifest-entries manifest)
332 (define (manifest-add manifest entries)
333 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
334 Remove MANIFEST entries that have the same name and output as ENTRIES."
335 (define (same-entry? entry name output)
337 (($ <manifest-entry> entry-name _ entry-output _ ...)
338 (and (equal? name entry-name)
339 (equal? output entry-output)))))
343 (fold (lambda (entry result)
345 (($ <manifest-entry> name _ out _ ...)
346 (filter (negate (cut same-entry? <> name out))
348 (manifest-entries manifest)
351 (define (manifest-lookup manifest pattern)
352 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
354 (find (entry-predicate pattern)
355 (manifest-entries manifest)))
357 (define (manifest-installed? manifest pattern)
358 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
360 (->bool (manifest-lookup manifest pattern)))
362 (define (manifest-matching-entries manifest patterns)
363 "Return all the entries of MANIFEST that match one of the PATTERNS."
365 (map entry-predicate patterns))
367 (define (matches? entry)
372 (filter matches? (manifest-entries manifest)))
376 ;;; Manifest transactions.
379 (define-record-type* <manifest-transaction> manifest-transaction
380 make-manifest-transaction
381 manifest-transaction?
382 (install manifest-transaction-install ; list of <manifest-entry>
384 (remove manifest-transaction-remove ; list of <manifest-pattern>
387 (define (manifest-transaction-effects manifest transaction)
388 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
389 the list of packages that would be removed, installed, upgraded, or downgraded
390 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
391 where the head is the entry being upgraded and the tail is the entry that will
393 (define (manifest-entry->pattern entry)
395 (name (manifest-entry-name entry))
396 (output (manifest-entry-output entry))))
398 (let loop ((input (manifest-transaction-install transaction))
404 (let ((remove (manifest-transaction-remove transaction)))
405 (values (manifest-matching-entries manifest remove)
406 (reverse install) (reverse upgrade) (reverse downgrade))))
408 ;; Check whether installing ENTRY corresponds to the installation of a
409 ;; new package or to an upgrade.
411 ;; XXX: When the exact same output directory is installed, we're not
412 ;; really upgrading anything. Add a check for that case.
413 (let* ((pattern (manifest-entry->pattern entry))
414 (previous (manifest-lookup manifest pattern))
415 (newer? (and previous
416 (version>=? (manifest-entry-version entry)
417 (manifest-entry-version previous)))))
419 (if previous install (cons entry install))
420 (if (and previous newer?)
421 (alist-cons previous entry upgrade)
423 (if (and previous (not newer?))
424 (alist-cons previous entry downgrade)
427 (define (manifest-perform-transaction manifest transaction)
428 "Perform TRANSACTION on MANIFEST and return new manifest."
429 (let ((install (manifest-transaction-install transaction))
430 (remove (manifest-transaction-remove transaction)))
431 (manifest-add (manifest-remove manifest remove)
439 (define (manifest-inputs manifest)
440 "Return a list of <gexp-input> objects for MANIFEST."
441 (append-map (match-lambda
442 (($ <manifest-entry> name version output thing deps)
443 ;; THING may be a package or a file name. In the latter case,
444 ;; assume it's already valid. Ditto for DEPS.
445 (cons (gexp-input thing output) deps)))
446 (manifest-entries manifest)))
448 (define (manifest-lookup-package manifest name)
449 "Return as a monadic value the first package or store path referenced by
450 MANIFEST that named NAME, or #f if not found."
451 ;; Return as a monadic value the package or store path referenced by the
452 ;; manifest ENTRY, or #f if not referenced.
453 (define (entry-lookup-package entry)
454 (define (find-among-inputs inputs)
455 (find (lambda (input)
456 (and (package? input)
457 (equal? name (package-name input))))
459 (define (find-among-store-items items)
461 (equal? name (package-name->name+version
462 (store-path-package-name item))))
467 (store-lift references))
469 (with-monad %store-monad
470 (match (manifest-entry-item entry)
471 ((? package? package)
472 (match (cons (list (package-name package) package)
473 (package-transitive-inputs package))
474 (((labels inputs . _) ...)
475 (return (find-among-inputs inputs)))))
477 (mlet %store-monad ((refs (references* item)))
478 (return (find-among-store-items refs)))))))
481 entry-lookup-package (manifest-entries manifest)))
483 (define (info-dir-file manifest)
484 "Return a derivation that builds the 'dir' file for all the entries of
486 (define texinfo ;lazy reference
487 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
488 (define gzip ;lazy reference
489 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
493 (use-modules (guix build utils)
494 (srfi srfi-1) (srfi srfi-26)
497 (define (info-file? file)
498 (or (string-suffix? ".info" file)
499 (string-suffix? ".info.gz" file)))
501 (define (info-files top)
502 (let ((infodir (string-append top "/share/info")))
503 (map (cut string-append infodir "/" <>)
504 (or (scandir infodir info-file?) '()))))
506 (define (install-info info)
507 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
509 (system* (string-append #+texinfo "/bin/install-info") "--silent"
510 info (string-append #$output "/share/info/dir"))))
512 (mkdir-p (string-append #$output "/share/info"))
514 (append-map info-files
515 '#$(manifest-inputs manifest)))))
517 (gexp->derivation "info-dir" build
518 #:modules '((guix build utils))
520 #:substitutable? #f))
522 (define (ghc-package-cache-file manifest)
523 "Return a derivation that builds the GHC 'package.cache' file for all the
524 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
525 (define ghc ;lazy reference
526 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
530 (use-modules (guix build utils)
531 (srfi srfi-1) (srfi srfi-26)
534 (define ghc-name-version
535 (let* ((base (basename #+ghc)))
537 (+ 1 (string-index base #\-)))))
540 (string-append "lib/" ghc-name-version "/package.conf.d"))
543 (string-append #$output "/" db-subdir))
545 (define (conf-files top)
546 (let ((db (string-append top "/" db-subdir)))
547 (if (file-exists? db)
548 (find-files db "\\.conf$")
551 (define (copy-conf-file conf)
552 (let ((base (basename conf)))
553 (copy-file conf (string-append db-dir "/" base))))
555 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
556 (for-each copy-conf-file
557 (append-map conf-files
559 '#$(manifest-inputs manifest))))
562 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
563 (string-append "--package-db=" db-dir)))))
564 (for-each delete-file (find-files db-dir "\\.conf$"))
567 (with-monad %store-monad
568 ;; Don't depend on GHC when there's nothing to do.
569 (if (any (cut string-prefix? "ghc" <>)
570 (map manifest-entry-name (manifest-entries manifest)))
571 (gexp->derivation "ghc-package-cache" build
572 #:modules '((guix build utils))
577 (define (ca-certificate-bundle manifest)
578 "Return a derivation that builds a single-file bundle containing the CA
579 certificates in the /etc/ssl/certs sub-directories of the packages in
580 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
581 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
584 (define glibc-utf8-locales ;lazy reference
585 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
589 (use-modules (guix build utils)
596 (define (pem-file? file)
597 (string-suffix? ".pem" file))
599 (define (ca-files top)
600 (let ((cert-dir (string-append top "/etc/ssl/certs")))
601 (map (cut string-append cert-dir "/" <>)
602 (or (scandir cert-dir pem-file?) '()))))
604 (define (concatenate-files files result)
605 "Make RESULT the concatenation of all of FILES."
606 (define (dump file port)
607 (display (call-with-input-file file get-string-all)
609 (newline port)) ;required, see <https://bugs.debian.org/635570>
611 (call-with-output-file result
613 (for-each (cut dump <> port) files))))
615 ;; Some file names in the NSS certificates are UTF-8 encoded so
616 ;; install a UTF-8 locale.
618 (string-append #+glibc-utf8-locales "/lib/locale/"
619 #+(package-version glibc-utf8-locales)))
620 (setlocale LC_ALL "en_US.utf8")
622 (match (append-map ca-files '#$(manifest-inputs manifest))
624 ;; Since there are no CA files, just create an empty directory. Do
625 ;; not create the etc/ssl/certs sub-directory, since that would
626 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
631 (let ((result (string-append #$output "/etc/ssl/certs")))
633 (concatenate-files ca-files
634 (string-append result
635 "/ca-certificates.crt"))
638 (gexp->derivation "ca-certificate-bundle" build
639 #:modules '((guix build utils))
641 #:substitutable? #f))
643 (define (gtk-icon-themes manifest)
644 "Return a derivation that unions all icon themes from manifest entries and
645 creates the GTK+ 'icon-theme.cache' file for each theme."
646 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+")))
649 (use-modules (guix build utils)
651 (guix build profiles)
655 (let* ((destdir (string-append #$output "/share/icons"))
656 (icondirs (filter file-exists?
657 (map (cut string-append <> "/share/icons")
658 '#$(manifest-inputs manifest))))
659 (update-icon-cache (string-append
660 #+gtk+ "/bin/gtk-update-icon-cache")))
662 ;; Union all the icons.
663 (mkdir-p (string-append #$output "/share"))
664 (union-build destdir icondirs
665 #:log-port (%make-void-port "w"))
667 ;; Update the 'icon-theme.cache' file for each icon theme.
670 (let ((dir (string-append destdir "/" theme)))
671 ;; Occasionally DESTDIR contains plain files, such as
672 ;; "abiword_48.png". Ignore these.
673 (when (file-is-directory? dir)
674 (ensure-writable-directory dir)
675 (system* update-icon-cache "-t" dir "--quiet"))))
676 (scandir destdir (negate (cut member <> '("." ".."))))))))
678 ;; Don't run the hook when there's nothing to do.
680 (gexp->derivation "gtk-icon-themes" build
681 #:modules '((guix build utils)
683 (guix build profiles)
690 (define (xdg-desktop-database manifest)
691 "Return a derivation that builds the @file{mimeinfo.cache} database from
692 desktop files. It's used to query what applications can handle a given
694 (mlet %store-monad ((desktop-file-utils
695 (manifest-lookup-package
696 manifest "desktop-file-utils")))
699 (use-modules (srfi srfi-26)
702 (let* ((destdir (string-append #$output "/share/applications"))
703 (appdirs (filter file-exists?
704 (map (cut string-append <>
705 "/share/applications")
706 '#$(manifest-inputs manifest))))
707 (update-desktop-database (string-append
709 "/bin/update-desktop-database")))
710 (mkdir-p (string-append #$output "/share"))
711 (union-build destdir appdirs
712 #:log-port (%make-void-port "w"))
713 (zero? (system* update-desktop-database destdir)))))
715 ;; Don't run the hook when 'desktop-file-utils' is not referenced.
716 (if desktop-file-utils
717 (gexp->derivation "xdg-desktop-database" build
718 #:modules '((guix build utils)
724 (define (xdg-mime-database manifest)
725 "Return a derivation that builds the @file{mime.cache} database from manifest
726 entries. It's used to query the MIME type of a given file."
727 (mlet %store-monad ((shared-mime-info
728 (manifest-lookup-package
729 manifest "shared-mime-info")))
732 (use-modules (srfi srfi-26)
735 (let* ((datadir (string-append #$output "/share"))
736 (destdir (string-append datadir "/mime"))
737 (pkgdirs (filter file-exists?
738 (map (cut string-append <>
739 "/share/mime/packages")
740 '#$(manifest-inputs manifest))))
741 (update-mime-database (string-append
743 "/bin/update-mime-database")))
745 (union-build (string-append destdir "/packages") pkgdirs
746 #:log-port (%make-void-port "w"))
747 (setenv "XDG_DATA_HOME" datadir)
748 (zero? (system* update-mime-database destdir)))))
750 ;; Don't run the hook when 'shared-mime-info' is referenced.
752 (gexp->derivation "xdg-mime-database" build
753 #:modules '((guix build utils)
759 (define %default-profile-hooks
760 ;; This is the list of derivation-returning procedures that are called by
761 ;; default when making a non-empty profile.
763 ghc-package-cache-file
764 ca-certificate-bundle
769 (define* (profile-derivation manifest
771 (hooks %default-profile-hooks)
773 "Return a derivation that builds a profile (aka. 'user environment') with
774 the given MANIFEST. The profile includes additional derivations returned by
775 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc."
776 (mlet %store-monad ((system (if system
779 (extras (if (null? (manifest-entries manifest))
781 (sequence %store-monad
786 (append (filter-map (lambda (drv)
787 (and (derivation? drv)
790 (manifest-inputs manifest)))
794 (use-modules (guix build profiles)
798 (setvbuf (current-output-port) _IOLBF)
799 (setvbuf (current-error-port) _IOLBF)
802 ;; Search paths of MANIFEST's packages, converted back to their
804 (map sexp->search-path-specification
806 '#$(map search-path-specification->sexp
807 (append-map manifest-entry-search-paths
808 (manifest-entries manifest))))))
810 (build-profile #$output '#$inputs
811 #:manifest '#$(manifest->gexp manifest)
812 #:search-paths search-paths)))
814 (gexp->derivation "profile" builder
816 #:modules '((guix build profiles)
822 ;; Not worth offloading.
825 ;; Disable substitution because it would trigger a
826 ;; connection to the substitute server, which is likely
827 ;; to have no substitute to offer.
828 #:substitutable? #f)))
830 (define (profile-regexp profile)
831 "Return a regular expression that matches PROFILE's name and number."
832 (make-regexp (string-append "^" (regexp-quote (basename profile))
835 (define (generation-number profile)
836 "Return PROFILE's number or 0. An absolute file name must be used."
837 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
838 (basename (readlink profile))))
839 (compose string->number (cut match:substring <> 1)))
842 (define (generation-numbers profile)
843 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
844 former profiles were found."
845 (define* (scandir name #:optional (select? (const #t))
846 (entry<? (@ (ice-9 i18n) string-locale<?)))
847 ;; XXX: Bug-fix version introduced in Guile v2.0.6-62-g139ce19.
848 (define (enter? dir stat result)
849 (and stat (string=? dir name)))
851 (define (visit basename result)
852 (if (select? basename)
853 (cons basename result)
856 (define (leaf name stat result)
858 (visit (basename name) result)))
860 (define (down name stat result)
863 (define (up name stat result)
866 (define (skip name stat result)
867 ;; All the sub-directories are skipped.
868 (visit (basename name) result))
870 (define (error name* stat errno result)
871 (if (string=? name name*) ; top-level NAME is unreadable
873 (visit (basename name*) result)))
875 (and=> (file-system-fold enter? leaf down up skip error #f name lstat)
877 (sort files entry<?))))
879 (match (scandir (dirname profile)
880 (cute regexp-exec (profile-regexp profile) <>))
881 (#f ; no profile directory
885 ((profiles ...) ; former profiles around
886 (sort (map (compose string->number
887 (cut match:substring <> 1)
888 (cute regexp-exec (profile-regexp profile) <>))
892 (define (profile-generations profile)
893 "Return a list of PROFILE's generations."
894 (let ((generations (generation-numbers profile)))
895 (if (equal? generations '(0))
899 (define* (relative-generation profile shift #:optional
900 (current (generation-number profile)))
901 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
902 SHIFT is a positive or negative number.
903 Return #f if there is no such generation."
904 (let* ((abs-shift (abs shift))
905 (numbers (profile-generations profile))
906 (from-current (memq current
907 (if (negative? shift)
911 (< abs-shift (length from-current))
912 (list-ref from-current abs-shift))))
914 (define* (previous-generation-number profile #:optional
915 (number (generation-number profile)))
916 "Return the number of the generation before generation NUMBER of
917 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
918 case when generations have been deleted (there are \"holes\")."
919 (or (relative-generation profile -1 number)
922 (define (generation-file-name profile generation)
923 "Return the file name for PROFILE's GENERATION."
924 (format #f "~a-~a-link" profile generation))
926 (define (generation-time profile number)
927 "Return the creation time of a generation in the UTC format."
928 (make-time time-utc 0
929 (stat:ctime (stat (generation-file-name profile number)))))
931 (define (link-to-empty-profile store generation)
932 "Link GENERATION, a string, to the empty profile. An error is raised if
934 (let* ((drv (run-with-store store
935 (profile-derivation (manifest '()))))
936 (prof (derivation->output-path drv "out")))
937 (build-derivations store (list drv))
938 (switch-symlinks generation prof)))
940 (define (switch-to-generation profile number)
941 "Atomically switch PROFILE to the generation NUMBER. Return the number of
942 the generation that was current before switching."
943 (let ((current (generation-number profile))
944 (generation (generation-file-name profile number)))
945 (cond ((not (file-exists? profile))
946 (raise (condition (&profile-not-found-error
947 (profile profile)))))
948 ((not (file-exists? generation))
949 (raise (condition (&missing-generation-error
951 (generation number)))))
953 (switch-symlinks profile generation)
956 (define (switch-to-previous-generation profile)
957 "Atomically switch PROFILE to the previous generation. Return the former
958 generation number and the current one."
959 (let ((previous (previous-generation-number profile)))
960 (values (switch-to-generation profile previous)
963 (define (roll-back store profile)
964 "Roll back to the previous generation of PROFILE. Return the number of the
965 generation that was current before switching and the new generation number."
966 (let* ((number (generation-number profile))
967 (previous-number (previous-generation-number profile number))
968 (previous-generation (generation-file-name profile previous-number)))
969 (cond ((not (file-exists? profile)) ;invalid profile
970 (raise (condition (&profile-not-found-error
971 (profile profile)))))
972 ((zero? number) ;empty profile
973 (values number number))
974 ((or (zero? previous-number) ;going to emptiness
975 (not (file-exists? previous-generation)))
976 (link-to-empty-profile store previous-generation)
977 (switch-to-previous-generation profile))
979 (switch-to-previous-generation profile)))))
981 (define (delete-generation store profile number)
982 "Delete generation with NUMBER from PROFILE. Return the file name of the
983 generation that has been deleted, or #f if nothing was done (for instance
984 because the NUMBER is zero.)"
985 (define (delete-and-return)
986 (let ((generation (generation-file-name profile number)))
987 (delete-file generation)
990 (let* ((current-number (generation-number profile))
991 (previous-number (previous-generation-number profile number))
992 (previous-generation (generation-file-name profile previous-number)))
993 (cond ((zero? number) #f) ;do not delete generation 0
994 ((and (= number current-number)
995 (not (file-exists? previous-generation)))
996 (link-to-empty-profile store previous-generation)
997 (switch-to-previous-generation profile)
999 ((= number current-number)
1000 (roll-back store profile)
1001 (delete-and-return))
1003 (delete-and-return)))))
1005 ;;; profiles.scm ends here