profiles: Add 'properties' field to manifest entries.
[jackhill/guix/guix.git] / guix / profiles.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
4 ;;; Copyright © 2014, 2016 Alex Kost <alezost@gmail.com>
5 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
6 ;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
7 ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
8 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
9 ;;; Copyright © 2017 Huang Ying <huang.ying.caritas@gmail.com>
10 ;;; Copyright © 2017 Maxim Cournoyer <maxim.cournoyer@gmail.com>
11 ;;;
12 ;;; This file is part of GNU Guix.
13 ;;;
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.
18 ;;;
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.
23 ;;;
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/>.
26
27 (define-module (guix profiles)
28 #:use-module ((guix config) #:select (%state-directory))
29 #:use-module ((guix utils) #:hide (package-name->name+version))
30 #:use-module ((guix build utils)
31 #:select (package-name->name+version))
32 #:use-module (guix records)
33 #:use-module (guix packages)
34 #:use-module (guix derivations)
35 #:use-module (guix search-paths)
36 #:use-module (guix gexp)
37 #:use-module (guix modules)
38 #:use-module (guix monads)
39 #:use-module (guix store)
40 #:use-module (guix sets)
41 #:use-module (ice-9 vlist)
42 #:use-module (ice-9 match)
43 #:use-module (ice-9 regex)
44 #:use-module (ice-9 ftw)
45 #:use-module (ice-9 format)
46 #:use-module (srfi srfi-1)
47 #:use-module (srfi srfi-9)
48 #:use-module (srfi srfi-11)
49 #:use-module (srfi srfi-19)
50 #:use-module (srfi srfi-26)
51 #:use-module (srfi srfi-34)
52 #:use-module (srfi srfi-35)
53 #:export (&profile-error
54 profile-error?
55 profile-error-profile
56 &profile-not-found-error
57 profile-not-found-error?
58 &profile-collistion-error
59 profile-collision-error?
60 profile-collision-error-entry
61 profile-collision-error-conflict
62 &missing-generation-error
63 missing-generation-error?
64 missing-generation-error-generation
65
66 manifest make-manifest
67 manifest?
68 manifest-entries
69 manifest-transitive-entries
70
71 <manifest-entry> ; FIXME: eventually make it internal
72 manifest-entry
73 manifest-entry?
74 manifest-entry-name
75 manifest-entry-version
76 manifest-entry-output
77 manifest-entry-item
78 manifest-entry-dependencies
79 manifest-entry-search-paths
80 manifest-entry-parent
81 manifest-entry-properties
82
83 manifest-pattern
84 manifest-pattern?
85 manifest-pattern-name
86 manifest-pattern-version
87 manifest-pattern-output
88
89 manifest-remove
90 manifest-add
91 manifest-lookup
92 manifest-installed?
93 manifest-matching-entries
94
95 manifest-transaction
96 manifest-transaction?
97 manifest-transaction-install
98 manifest-transaction-remove
99 manifest-transaction-install-entry
100 manifest-transaction-remove-pattern
101 manifest-transaction-null?
102 manifest-transaction-removal-candidate?
103 manifest-perform-transaction
104 manifest-transaction-effects
105
106 profile-manifest
107 package->manifest-entry
108 packages->manifest
109 ca-certificate-bundle
110 %default-profile-hooks
111 profile-derivation
112
113 generation-number
114 generation-numbers
115 profile-generations
116 relative-generation-spec->number
117 relative-generation
118 previous-generation-number
119 generation-time
120 generation-file-name
121 switch-to-generation
122 roll-back
123 delete-generation
124
125 %user-profile-directory
126 %profile-directory
127 %current-profile
128 canonicalize-profile
129 user-friendly-profile))
130
131 ;;; Commentary:
132 ;;;
133 ;;; Tools to create and manipulate profiles---i.e., the representation of a
134 ;;; set of installed packages.
135 ;;;
136 ;;; Code:
137
138 \f
139 ;;;
140 ;;; Condition types.
141 ;;;
142
143 (define-condition-type &profile-error &error
144 profile-error?
145 (profile profile-error-profile))
146
147 (define-condition-type &profile-not-found-error &profile-error
148 profile-not-found-error?)
149
150 (define-condition-type &profile-collision-error &error
151 profile-collision-error?
152 (entry profile-collision-error-entry) ;<manifest-entry>
153 (conflict profile-collision-error-conflict)) ;<manifest-entry>
154
155 (define-condition-type &missing-generation-error &profile-error
156 missing-generation-error?
157 (generation missing-generation-error-generation))
158
159 \f
160 ;;;
161 ;;; Manifests.
162 ;;;
163
164 (define-record-type <manifest>
165 (manifest entries)
166 manifest?
167 (entries manifest-entries)) ; list of <manifest-entry>
168
169 ;; Convenient alias, to avoid name clashes.
170 (define make-manifest manifest)
171
172 (define-record-type* <manifest-entry> manifest-entry
173 make-manifest-entry
174 manifest-entry?
175 (name manifest-entry-name) ; string
176 (version manifest-entry-version) ; string
177 (output manifest-entry-output ; string
178 (default "out"))
179 (item manifest-entry-item) ; package | file-like | store path
180 (dependencies manifest-entry-dependencies ; <manifest-entry>*
181 (default '()))
182 (search-paths manifest-entry-search-paths ; search-path-specification*
183 (default '()))
184 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
185 (default (delay #f)))
186 (properties manifest-entry-properties ; list of symbol/value pairs
187 (default '())))
188
189 (define-record-type* <manifest-pattern> manifest-pattern
190 make-manifest-pattern
191 manifest-pattern?
192 (name manifest-pattern-name) ; string
193 (version manifest-pattern-version ; string | #f
194 (default #f))
195 (output manifest-pattern-output ; string | #f
196 (default "out")))
197
198 (define (manifest-transitive-entries manifest)
199 "Return the entries of MANIFEST along with their propagated inputs,
200 recursively."
201 (let loop ((entries (manifest-entries manifest))
202 (result '())
203 (visited (set))) ;compare with 'equal?'
204 (match entries
205 (()
206 (reverse result))
207 ((head . tail)
208 (if (set-contains? visited head)
209 (loop tail result visited)
210 (loop (append (manifest-entry-dependencies head)
211 tail)
212 (cons head result)
213 (set-insert head visited)))))))
214
215 (define (profile-manifest profile)
216 "Return the PROFILE's manifest."
217 (let ((file (string-append profile "/manifest")))
218 (if (file-exists? file)
219 (call-with-input-file file read-manifest)
220 (manifest '()))))
221
222 (define (manifest-entry-lookup manifest)
223 "Return a lookup procedure for the entries of MANIFEST. The lookup
224 procedure takes two arguments: the entry name and output."
225 (define mapping
226 (let loop ((entries (manifest-entries manifest))
227 (mapping vlist-null))
228 (fold (lambda (entry result)
229 (vhash-cons (cons (manifest-entry-name entry)
230 (manifest-entry-output entry))
231 entry
232 (loop (manifest-entry-dependencies entry)
233 result)))
234 mapping
235 entries)))
236
237 (lambda (name output)
238 (match (vhash-assoc (cons name output) mapping)
239 ((_ . entry) entry)
240 (#f #f))))
241
242 (define* (lower-manifest-entry entry system #:key target)
243 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
244 file name."
245 (let ((item (manifest-entry-item entry)))
246 (if (string? item)
247 (with-monad %store-monad
248 (return entry))
249 (mlet %store-monad ((drv (lower-object item system
250 #:target target))
251 (output -> (manifest-entry-output entry)))
252 (return (manifest-entry
253 (inherit entry)
254 (item (derivation->output-path drv output))))))))
255
256 (define* (check-for-collisions manifest system #:key target)
257 "Check whether the entries of MANIFEST conflict with one another; raise a
258 '&profile-collision-error' when a conflict is encountered."
259 (define lookup
260 (manifest-entry-lookup manifest))
261
262 (with-monad %store-monad
263 (foldm %store-monad
264 (lambda (entry result)
265 (match (lookup (manifest-entry-name entry)
266 (manifest-entry-output entry))
267 ((? manifest-entry? second) ;potential conflict
268 (mlet %store-monad ((first (lower-manifest-entry entry system
269 #:target
270 target))
271 (second (lower-manifest-entry second system
272 #:target
273 target)))
274 (if (string=? (manifest-entry-item first)
275 (manifest-entry-item second))
276 (return result)
277 (raise (condition
278 (&profile-collision-error
279 (entry first)
280 (conflict second)))))))
281 (#f ;no conflict
282 (return result))))
283 #t
284 (manifest-transitive-entries manifest))))
285
286 (define* (package->manifest-entry package #:optional (output "out")
287 #:key (parent (delay #f)))
288 "Return a manifest entry for the OUTPUT of package PACKAGE."
289 ;; For each dependency, keep a promise pointing to its "parent" entry.
290 (letrec* ((deps (map (match-lambda
291 ((label package)
292 (package->manifest-entry package
293 #:parent (delay entry)))
294 ((label package output)
295 (package->manifest-entry package output
296 #:parent (delay entry))))
297 (package-propagated-inputs package)))
298 (entry (manifest-entry
299 (name (package-name package))
300 (version (package-version package))
301 (output output)
302 (item package)
303 (dependencies (delete-duplicates deps))
304 (search-paths
305 (package-transitive-native-search-paths package))
306 (parent parent))))
307 entry))
308
309 (define (packages->manifest packages)
310 "Return a list of manifest entries, one for each item listed in PACKAGES.
311 Elements of PACKAGES can be either package objects or package/string tuples
312 denoting a specific output of a package."
313 (manifest
314 (map (match-lambda
315 ((package output)
316 (package->manifest-entry package output))
317 ((? package? package)
318 (package->manifest-entry package)))
319 packages)))
320
321 (define (manifest->gexp manifest)
322 "Return a representation of MANIFEST as a gexp."
323 (define (entry->gexp entry)
324 (match entry
325 (($ <manifest-entry> name version output (? string? path)
326 (deps ...) (search-paths ...) _ (properties ...))
327 #~(#$name #$version #$output #$path
328 (propagated-inputs #$(map entry->gexp deps))
329 (search-paths #$(map search-path-specification->sexp
330 search-paths))
331 (properties . #$properties)))
332 (($ <manifest-entry> name version output package
333 (deps ...) (search-paths ...) _ (properties ...))
334 #~(#$name #$version #$output
335 (ungexp package (or output "out"))
336 (propagated-inputs #$(map entry->gexp deps))
337 (search-paths #$(map search-path-specification->sexp
338 search-paths))
339 (properties . #$properties)))))
340
341 (match manifest
342 (($ <manifest> (entries ...))
343 #~(manifest (version 3)
344 (packages #$(map entry->gexp entries))))))
345
346 (define (find-package name version)
347 "Return a package from the distro matching NAME and possibly VERSION. This
348 procedure is here for backward-compatibility and will eventually vanish."
349 (define find-best-packages-by-name ;break abstractions
350 (module-ref (resolve-interface '(gnu packages))
351 'find-best-packages-by-name))
352
353 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
354 ;; former traverses the module tree only once and then allows for efficient
355 ;; access via a vhash.
356 (match (find-best-packages-by-name name version)
357 ((p _ ...) p)
358 (_
359 (match (find-best-packages-by-name name #f)
360 ((p _ ...) p)
361 (_ #f)))))
362
363 (define (sexp->manifest sexp)
364 "Parse SEXP as a manifest."
365 (define (infer-search-paths name version)
366 ;; Infer the search path specifications for NAME-VERSION by looking up a
367 ;; same-named package in the distro. Useful for the old manifest formats
368 ;; that did not store search path info.
369 (let ((package (find-package name version)))
370 (if package
371 (package-native-search-paths package)
372 '())))
373
374 (define (infer-dependency item parent)
375 ;; Return a <manifest-entry> for ITEM.
376 (let-values (((name version)
377 (package-name->name+version
378 (store-path-package-name item))))
379 (manifest-entry
380 (name name)
381 (version version)
382 (item item)
383 (parent parent))))
384
385 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
386 (match sexp
387 ((name version output path
388 ('propagated-inputs deps)
389 ('search-paths search-paths)
390 extra-stuff ...)
391 ;; For each of DEPS, keep a promise pointing to ENTRY.
392 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
393 deps))
394 (entry (manifest-entry
395 (name name)
396 (version version)
397 (output output)
398 (item path)
399 (dependencies deps*)
400 (search-paths (map sexp->search-path-specification
401 search-paths))
402 (parent parent)
403 (properties (or (assoc-ref extra-stuff 'properties)
404 '())))))
405 entry))))
406
407 (match sexp
408 (('manifest ('version 0)
409 ('packages ((name version output path) ...)))
410 (manifest
411 (map (lambda (name version output path)
412 (manifest-entry
413 (name name)
414 (version version)
415 (output output)
416 (item path)
417 (search-paths (infer-search-paths name version))))
418 name version output path)))
419
420 ;; Version 1 adds a list of propagated inputs to the
421 ;; name/version/output/path tuples.
422 (('manifest ('version 1)
423 ('packages ((name version output path deps) ...)))
424 (manifest
425 (map (lambda (name version output path deps)
426 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
427 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
428 ;; such lists.
429 (let ((deps (match deps
430 (((labels directories) ...)
431 directories)
432 ((directories ...)
433 directories))))
434 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
435 deps))
436 (entry (manifest-entry
437 (name name)
438 (version version)
439 (output output)
440 (item path)
441 (dependencies deps*)
442 (search-paths
443 (infer-search-paths name version)))))
444 entry)))
445 name version output path deps)))
446
447 ;; Version 2 adds search paths and is slightly more verbose.
448 (('manifest ('version 2 minor-version ...)
449 ('packages ((name version output path
450 ('propagated-inputs deps)
451 ('search-paths search-paths)
452 extra-stuff ...)
453 ...)))
454 (manifest
455 (map (lambda (name version output path deps search-paths)
456 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
457 deps))
458 (entry (manifest-entry
459 (name name)
460 (version version)
461 (output output)
462 (item path)
463 (dependencies deps*)
464 (search-paths
465 (map sexp->search-path-specification
466 search-paths)))))
467 entry))
468 name version output path deps search-paths)))
469
470 ;; Version 3 represents DEPS as full-blown manifest entries.
471 (('manifest ('version 3 minor-version ...)
472 ('packages (entries ...)))
473 (manifest (map sexp->manifest-entry entries)))
474 (_
475 (raise (condition
476 (&message (message "unsupported manifest format")))))))
477
478 (define (read-manifest port)
479 "Return the packages listed in MANIFEST."
480 (sexp->manifest (read port)))
481
482 (define (entry-predicate pattern)
483 "Return a procedure that returns #t when passed a manifest entry that
484 matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
485 are ignored."
486 (match pattern
487 (($ <manifest-pattern> name version output)
488 (match-lambda
489 (($ <manifest-entry> entry-name entry-version entry-output)
490 (and (string=? entry-name name)
491 (or (not entry-output) (not output)
492 (string=? entry-output output))
493 (or (not version)
494 (string=? entry-version version))))))))
495
496 (define (manifest-remove manifest patterns)
497 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
498 must be a manifest-pattern."
499 (define (remove-entry pattern lst)
500 (remove (entry-predicate pattern) lst))
501
502 (make-manifest (fold remove-entry
503 (manifest-entries manifest)
504 patterns)))
505
506 (define (manifest-add manifest entries)
507 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
508 Remove MANIFEST entries that have the same name and output as ENTRIES."
509 (define (same-entry? entry name output)
510 (match entry
511 (($ <manifest-entry> entry-name _ entry-output _)
512 (and (equal? name entry-name)
513 (equal? output entry-output)))))
514
515 (make-manifest
516 (fold (lambda (entry result) ;XXX: quadratic
517 (match entry
518 (($ <manifest-entry> name _ out _)
519 (cons entry
520 (remove (cut same-entry? <> name out)
521 result)))))
522 (manifest-entries manifest)
523 entries)))
524
525 (define (manifest-lookup manifest pattern)
526 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
527 no match.."
528 (find (entry-predicate pattern)
529 (manifest-entries manifest)))
530
531 (define (manifest-installed? manifest pattern)
532 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
533 #f otherwise."
534 (->bool (manifest-lookup manifest pattern)))
535
536 (define (manifest-matching-entries manifest patterns)
537 "Return all the entries of MANIFEST that match one of the PATTERNS."
538 (define predicates
539 (map entry-predicate patterns))
540
541 (define (matches? entry)
542 (any (lambda (pred)
543 (pred entry))
544 predicates))
545
546 (filter matches? (manifest-entries manifest)))
547
548 \f
549 ;;;
550 ;;; Manifest transactions.
551 ;;;
552
553 (define-record-type* <manifest-transaction> manifest-transaction
554 make-manifest-transaction
555 manifest-transaction?
556 (install manifest-transaction-install ; list of <manifest-entry>
557 (default '()))
558 (remove manifest-transaction-remove ; list of <manifest-pattern>
559 (default '())))
560
561 (define (manifest-transaction-install-entry entry transaction)
562 "Augment TRANSACTION's set of installed packages with ENTRY, a
563 <manifest-entry>."
564 (manifest-transaction
565 (inherit transaction)
566 (install
567 (cons entry (manifest-transaction-install transaction)))))
568
569 (define (manifest-transaction-remove-pattern pattern transaction)
570 "Add PATTERN to TRANSACTION's list of packages to remove."
571 (manifest-transaction
572 (inherit transaction)
573 (remove
574 (cons pattern (manifest-transaction-remove transaction)))))
575
576 (define (manifest-transaction-null? transaction)
577 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
578 remove software."
579 (match transaction
580 (($ <manifest-transaction> () ()) #t)
581 (($ <manifest-transaction> _ _) #f)))
582
583 (define (manifest-transaction-removal-candidate? entry transaction)
584 "Return true if ENTRY is a candidate for removal in TRANSACTION."
585 (any (lambda (pattern)
586 ((entry-predicate pattern) entry))
587 (manifest-transaction-remove transaction)))
588
589 (define (manifest-transaction-effects manifest transaction)
590 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
591 the list of packages that would be removed, installed, upgraded, or downgraded
592 when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
593 where the head is the entry being upgraded and the tail is the entry that will
594 replace it."
595 (define (manifest-entry->pattern entry)
596 (manifest-pattern
597 (name (manifest-entry-name entry))
598 (output (manifest-entry-output entry))))
599
600 (let loop ((input (manifest-transaction-install transaction))
601 (install '())
602 (upgrade '())
603 (downgrade '()))
604 (match input
605 (()
606 (let ((remove (manifest-transaction-remove transaction)))
607 (values (manifest-matching-entries manifest remove)
608 (reverse install) (reverse upgrade) (reverse downgrade))))
609 ((entry rest ...)
610 ;; Check whether installing ENTRY corresponds to the installation of a
611 ;; new package or to an upgrade.
612
613 ;; XXX: When the exact same output directory is installed, we're not
614 ;; really upgrading anything. Add a check for that case.
615 (let* ((pattern (manifest-entry->pattern entry))
616 (previous (manifest-lookup manifest pattern))
617 (newer? (and previous
618 (version>=? (manifest-entry-version entry)
619 (manifest-entry-version previous)))))
620 (loop rest
621 (if previous install (cons entry install))
622 (if (and previous newer?)
623 (alist-cons previous entry upgrade)
624 upgrade)
625 (if (and previous (not newer?))
626 (alist-cons previous entry downgrade)
627 downgrade)))))))
628
629 (define (manifest-perform-transaction manifest transaction)
630 "Perform TRANSACTION on MANIFEST and return the new manifest."
631 (let ((install (manifest-transaction-install transaction))
632 (remove (manifest-transaction-remove transaction)))
633 (manifest-add (manifest-remove manifest remove)
634 install)))
635
636 \f
637 ;;;
638 ;;; Profiles.
639 ;;;
640
641 (define (manifest-inputs manifest)
642 "Return a list of <gexp-input> objects for MANIFEST."
643 (define entry->input
644 (match-lambda
645 (($ <manifest-entry> name version output thing deps)
646 ;; THING may be a package or a file name. In the latter case, assume
647 ;; it's already valid.
648 (cons (gexp-input thing output)
649 (append-map entry->input deps)))))
650
651 (append-map entry->input (manifest-entries manifest)))
652
653 (define* (manifest-lookup-package manifest name #:optional version)
654 "Return as a monadic value the first package or store path referenced by
655 MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
656 if not found."
657 ;; Return as a monadic value the package or store path referenced by the
658 ;; manifest ENTRY, or #f if not referenced.
659 (define (entry-lookup-package entry)
660 (define (find-among-inputs inputs)
661 (find (lambda (input)
662 (and (package? input)
663 (equal? name (package-name input))
664 (if version
665 (string-prefix? version (package-version input))
666 #t)))
667 inputs))
668 (define (find-among-store-items items)
669 (find (lambda (item)
670 (let-values (((name* version*)
671 (package-name->name+version
672 (store-path-package-name item))))
673 (and (string=? name name*)
674 (if version
675 (string-prefix? version version*)
676 #t))))
677 items))
678
679 (with-monad %store-monad
680 (match (manifest-entry-item entry)
681 ((? package? package)
682 (match (cons (list (package-name package) package)
683 (package-transitive-inputs package))
684 (((labels inputs . _) ...)
685 (return (find-among-inputs inputs)))))
686 ((? string? item)
687 (mlet %store-monad ((refs (references* item)))
688 (return (find-among-store-items refs))))
689 (item
690 ;; XXX: ITEM might be a 'computed-file' or anything like that, in
691 ;; which case we don't know what to do. The fix may be to check
692 ;; references once ITEM is compiled, as proposed at
693 ;; <https://bugs.gnu.org/29927>.
694 (return #f)))))
695
696 (anym %store-monad
697 entry-lookup-package (manifest-entries manifest)))
698
699 (define (info-dir-file manifest)
700 "Return a derivation that builds the 'dir' file for all the entries of
701 MANIFEST."
702 (define texinfo ;lazy reference
703 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
704 (define gzip ;lazy reference
705 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
706
707 (define build
708 (with-imported-modules '((guix build utils))
709 #~(begin
710 (use-modules (guix build utils)
711 (srfi srfi-1) (srfi srfi-26)
712 (ice-9 ftw))
713
714 (define (info-file? file)
715 (or (string-suffix? ".info" file)
716 (string-suffix? ".info.gz" file)))
717
718 (define (info-files top)
719 (let ((infodir (string-append top "/share/info")))
720 (map (cut string-append infodir "/" <>)
721 (or (scandir infodir info-file?) '()))))
722
723 (define (install-info info)
724 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
725 (zero?
726 (system* (string-append #+texinfo "/bin/install-info") "--silent"
727 info (string-append #$output "/share/info/dir"))))
728
729 (mkdir-p (string-append #$output "/share/info"))
730 (exit (every install-info
731 (append-map info-files
732 '#$(manifest-inputs manifest)))))))
733
734 (gexp->derivation "info-dir" build
735 #:local-build? #t
736 #:substitutable? #f))
737
738 (define (ghc-package-cache-file manifest)
739 "Return a derivation that builds the GHC 'package.cache' file for all the
740 entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
741 (define ghc ;lazy reference
742 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
743
744 (define build
745 (with-imported-modules '((guix build utils))
746 #~(begin
747 (use-modules (guix build utils)
748 (srfi srfi-1) (srfi srfi-26)
749 (ice-9 ftw))
750
751 (define ghc-name-version
752 (let* ((base (basename #+ghc)))
753 (string-drop base
754 (+ 1 (string-index base #\-)))))
755
756 (define db-subdir
757 (string-append "lib/" ghc-name-version "/package.conf.d"))
758
759 (define db-dir
760 (string-append #$output "/" db-subdir))
761
762 (define (conf-files top)
763 (let ((db (string-append top "/" db-subdir)))
764 (if (file-exists? db)
765 (find-files db "\\.conf$")
766 '())))
767
768 (define (copy-conf-file conf)
769 (let ((base (basename conf)))
770 (copy-file conf (string-append db-dir "/" base))))
771
772 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
773 (for-each copy-conf-file
774 (append-map conf-files
775 (delete-duplicates
776 '#$(manifest-inputs manifest))))
777 (let ((success
778 (zero?
779 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
780 (string-append "--package-db=" db-dir)))))
781 (for-each delete-file (find-files db-dir "\\.conf$"))
782 (exit success)))))
783
784 (with-monad %store-monad
785 ;; Don't depend on GHC when there's nothing to do.
786 (if (any (cut string-prefix? "ghc" <>)
787 (map manifest-entry-name (manifest-entries manifest)))
788 (gexp->derivation "ghc-package-cache" build
789 #:local-build? #t
790 #:substitutable? #f)
791 (return #f))))
792
793 (define (ca-certificate-bundle manifest)
794 "Return a derivation that builds a single-file bundle containing the CA
795 certificates in the /etc/ssl/certs sub-directories of the packages in
796 MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
797 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
798 ;; for a discussion.
799
800 (define glibc-utf8-locales ;lazy reference
801 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
802
803 (define build
804 (with-imported-modules '((guix build utils))
805 #~(begin
806 (use-modules (guix build utils)
807 (rnrs io ports)
808 (srfi srfi-1)
809 (srfi srfi-26)
810 (ice-9 ftw)
811 (ice-9 match))
812
813 (define (pem-file? file)
814 (string-suffix? ".pem" file))
815
816 (define (ca-files top)
817 (let ((cert-dir (string-append top "/etc/ssl/certs")))
818 (map (cut string-append cert-dir "/" <>)
819 (or (scandir cert-dir pem-file?) '()))))
820
821 (define (concatenate-files files result)
822 "Make RESULT the concatenation of all of FILES."
823 (define (dump file port)
824 (display (call-with-input-file file get-string-all)
825 port)
826 (newline port)) ;required, see <https://bugs.debian.org/635570>
827
828 (call-with-output-file result
829 (lambda (port)
830 (for-each (cut dump <> port) files))))
831
832 ;; Some file names in the NSS certificates are UTF-8 encoded so
833 ;; install a UTF-8 locale.
834 (setenv "LOCPATH"
835 (string-append #+glibc-utf8-locales "/lib/locale/"
836 #+(version-major+minor
837 (package-version glibc-utf8-locales))))
838 (setlocale LC_ALL "en_US.utf8")
839
840 (match (append-map ca-files '#$(manifest-inputs manifest))
841 (()
842 ;; Since there are no CA files, just create an empty directory. Do
843 ;; not create the etc/ssl/certs sub-directory, since that would
844 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
845 ;; defined.
846 (mkdir #$output)
847 #t)
848 ((ca-files ...)
849 (let ((result (string-append #$output "/etc/ssl/certs")))
850 (mkdir-p result)
851 (concatenate-files ca-files
852 (string-append result
853 "/ca-certificates.crt"))
854 #t))))))
855
856 (gexp->derivation "ca-certificate-bundle" build
857 #:local-build? #t
858 #:substitutable? #f))
859
860 (define (glib-schemas manifest)
861 "Return a derivation that unions all schemas from manifest entries and
862 creates the Glib 'gschemas.compiled' file."
863 (define glib ; lazy reference
864 (module-ref (resolve-interface '(gnu packages glib)) 'glib))
865
866 (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
867 ;; XXX: Can't use glib-compile-schemas corresponding
868 ;; to the glib referenced by 'manifest'. Because
869 ;; '%glib' can be either a package or store path, and
870 ;; there's no way to get the "bin" output for the later.
871 (glib-compile-schemas
872 -> #~(string-append #+glib:bin
873 "/bin/glib-compile-schemas")))
874
875 (define build
876 (with-imported-modules '((guix build utils)
877 (guix build union)
878 (guix build profiles)
879 (guix search-paths)
880 (guix records))
881 #~(begin
882 (use-modules (guix build utils)
883 (guix build union)
884 (guix build profiles)
885 (srfi srfi-26))
886
887 (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
888 (schemadirs (filter file-exists?
889 (map (cut string-append <> "/share/glib-2.0/schemas")
890 '#$(manifest-inputs manifest)))))
891
892 ;; Union all the schemas.
893 (mkdir-p (string-append #$output "/share/glib-2.0"))
894 (union-build destdir schemadirs
895 #:log-port (%make-void-port "w"))
896
897 (let ((dir destdir))
898 (when (file-is-directory? dir)
899 (ensure-writable-directory dir)
900 (invoke #+glib-compile-schemas
901 (string-append "--targetdir=" dir)
902 dir)))))))
903
904 ;; Don't run the hook when there's nothing to do.
905 (if %glib
906 (gexp->derivation "glib-schemas" build
907 #:local-build? #t
908 #:substitutable? #f)
909 (return #f))))
910
911 (define (gtk-icon-themes manifest)
912 "Return a derivation that unions all icon themes from manifest entries and
913 creates the GTK+ 'icon-theme.cache' file for each theme."
914 (define gtk+ ; lazy reference
915 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
916
917 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
918 ;; XXX: Can't use gtk-update-icon-cache corresponding
919 ;; to the gtk+ referenced by 'manifest'. Because
920 ;; '%gtk+' can be either a package or store path, and
921 ;; there's no way to get the "bin" output for the later.
922 (gtk-update-icon-cache
923 -> #~(string-append #+gtk+:bin
924 "/bin/gtk-update-icon-cache")))
925
926 (define build
927 (with-imported-modules '((guix build utils)
928 (guix build union)
929 (guix build profiles)
930 (guix search-paths)
931 (guix records))
932 #~(begin
933 (use-modules (guix build utils)
934 (guix build union)
935 (guix build profiles)
936 (srfi srfi-26)
937 (ice-9 ftw))
938
939 (let* ((destdir (string-append #$output "/share/icons"))
940 (icondirs (filter file-exists?
941 (map (cut string-append <> "/share/icons")
942 '#$(manifest-inputs manifest)))))
943
944 ;; Union all the icons.
945 (mkdir-p (string-append #$output "/share"))
946 (union-build destdir icondirs
947 #:log-port (%make-void-port "w"))
948
949 ;; Update the 'icon-theme.cache' file for each icon theme.
950 (for-each
951 (lambda (theme)
952 (let ((dir (string-append destdir "/" theme)))
953 ;; Occasionally DESTDIR contains plain files, such as
954 ;; "abiword_48.png". Ignore these.
955 (when (file-is-directory? dir)
956 (ensure-writable-directory dir)
957 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
958 (scandir destdir (negate (cut member <> '("." "..")))))))))
959
960 ;; Don't run the hook when there's nothing to do.
961 (if %gtk+
962 (gexp->derivation "gtk-icon-themes" build
963 #:local-build? #t
964 #:substitutable? #f)
965 (return #f))))
966
967 (define (gtk-im-modules manifest)
968 "Return a derivation that builds the cache files for input method modules
969 for both major versions of GTK+."
970
971 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
972 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
973
974 (define (build gtk gtk-version query)
975 (let ((major (string-take gtk-version 1)))
976 (with-imported-modules '((guix build utils)
977 (guix build union)
978 (guix build profiles)
979 (guix search-paths)
980 (guix records))
981 #~(begin
982 (use-modules (guix build utils)
983 (guix build union)
984 (guix build profiles)
985 (ice-9 popen)
986 (srfi srfi-1)
987 (srfi srfi-26))
988
989 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
990 #$gtk-version))
991 (destdir (string-append #$output prefix))
992 (moddirs (cons (string-append #$gtk prefix "/immodules")
993 (filter file-exists?
994 (map (cut string-append <> prefix "/immodules")
995 '#$(manifest-inputs manifest)))))
996 (modules (append-map (cut find-files <> "\\.so$")
997 moddirs)))
998
999 ;; Generate a new immodules cache file.
1000 (mkdir-p (string-append #$output prefix))
1001 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
1002 (outfile (string-append #$output prefix
1003 "/immodules-gtk" #$major ".cache")))
1004 (dynamic-wind
1005 (const #t)
1006 (lambda ()
1007 (call-with-output-file outfile
1008 (lambda (out)
1009 (while (not (eof-object? (peek-char pipe)))
1010 (write-char (read-char pipe) out))))
1011 #t)
1012 (lambda ()
1013 (close-pipe pipe)))))))))
1014
1015 ;; Don't run the hook when there's nothing to do.
1016 (let* ((pkg-gtk+ (module-ref ; lazy reference
1017 (resolve-interface '(gnu packages gtk)) 'gtk+))
1018 (gexp #~(begin
1019 #$(if gtk+
1020 (build
1021 gtk+ "3.0.0"
1022 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
1023 ;; output of latest gtk+ package.
1024 #~(string-append
1025 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
1026 #t)
1027 #$(if gtk+-2
1028 (build
1029 gtk+-2 "2.10.0"
1030 #~(string-append
1031 #$gtk+-2 "/bin/gtk-query-immodules-2.0"))
1032 #t))))
1033 (if (or gtk+ gtk+-2)
1034 (gexp->derivation "gtk-im-modules" gexp
1035 #:local-build? #t
1036 #:substitutable? #f)
1037 (return #f)))))
1038
1039 (define (xdg-desktop-database manifest)
1040 "Return a derivation that builds the @file{mimeinfo.cache} database from
1041 desktop files. It's used to query what applications can handle a given
1042 MIME type."
1043 (define desktop-file-utils ; lazy reference
1044 (module-ref (resolve-interface '(gnu packages freedesktop))
1045 'desktop-file-utils))
1046
1047 (mlet %store-monad ((glib
1048 (manifest-lookup-package
1049 manifest "glib")))
1050 (define build
1051 (with-imported-modules '((guix build utils)
1052 (guix build union))
1053 #~(begin
1054 (use-modules (srfi srfi-26)
1055 (guix build utils)
1056 (guix build union))
1057 (let* ((destdir (string-append #$output "/share/applications"))
1058 (appdirs (filter file-exists?
1059 (map (cut string-append <>
1060 "/share/applications")
1061 '#$(manifest-inputs manifest))))
1062 (update-desktop-database (string-append
1063 #+desktop-file-utils
1064 "/bin/update-desktop-database")))
1065 (mkdir-p (string-append #$output "/share"))
1066 (union-build destdir appdirs
1067 #:log-port (%make-void-port "w"))
1068 (exit (zero? (system* update-desktop-database destdir)))))))
1069
1070 ;; Don't run the hook when 'glib' is not referenced.
1071 (if glib
1072 (gexp->derivation "xdg-desktop-database" build
1073 #:local-build? #t
1074 #:substitutable? #f)
1075 (return #f))))
1076
1077 (define (xdg-mime-database manifest)
1078 "Return a derivation that builds the @file{mime.cache} database from manifest
1079 entries. It's used to query the MIME type of a given file."
1080 (define shared-mime-info ; lazy reference
1081 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1082
1083 (mlet %store-monad ((glib
1084 (manifest-lookup-package
1085 manifest "glib")))
1086 (define build
1087 (with-imported-modules '((guix build utils)
1088 (guix build union))
1089 #~(begin
1090 (use-modules (srfi srfi-26)
1091 (guix build utils)
1092 (guix build union))
1093 (let* ((datadir (string-append #$output "/share"))
1094 (destdir (string-append datadir "/mime"))
1095 (pkgdirs (filter file-exists?
1096 (map (cut string-append <>
1097 "/share/mime/packages")
1098 (cons #+shared-mime-info
1099 '#$(manifest-inputs manifest)))))
1100 (update-mime-database (string-append
1101 #+shared-mime-info
1102 "/bin/update-mime-database")))
1103 (mkdir-p destdir)
1104 (union-build (string-append destdir "/packages") pkgdirs
1105 #:log-port (%make-void-port "w"))
1106 (setenv "XDG_DATA_HOME" datadir)
1107 (exit (zero? (system* update-mime-database destdir)))))))
1108
1109 ;; Don't run the hook when there are no GLib based applications.
1110 (if glib
1111 (gexp->derivation "xdg-mime-database" build
1112 #:local-build? #t
1113 #:substitutable? #f)
1114 (return #f))))
1115
1116 ;; Several font packages may install font files into same directory, so
1117 ;; fonts.dir and fonts.scale file should be generated here, instead of in
1118 ;; packages.
1119 (define (fonts-dir-file manifest)
1120 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
1121 files for the fonts of the @var{manifest} entries."
1122 (define mkfontscale
1123 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1124
1125 (define mkfontdir
1126 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1127
1128 (define build
1129 #~(begin
1130 (use-modules (srfi srfi-26)
1131 (guix build utils)
1132 (guix build union))
1133 (let ((fonts-dirs (filter file-exists?
1134 (map (cut string-append <>
1135 "/share/fonts")
1136 '#$(manifest-inputs manifest)))))
1137 (mkdir #$output)
1138 (if (null? fonts-dirs)
1139 (exit #t)
1140 (let* ((share-dir (string-append #$output "/share"))
1141 (fonts-dir (string-append share-dir "/fonts"))
1142 (mkfontscale (string-append #+mkfontscale
1143 "/bin/mkfontscale"))
1144 (mkfontdir (string-append #+mkfontdir
1145 "/bin/mkfontdir"))
1146 (empty-file? (lambda (filename)
1147 (call-with-ascii-input-file filename
1148 (lambda (p)
1149 (eqv? #\0 (read-char p))))))
1150 (fonts-dir-file "fonts.dir")
1151 (fonts-scale-file "fonts.scale"))
1152 (mkdir-p share-dir)
1153 ;; Create all sub-directories, because we may create fonts.dir
1154 ;; and fonts.scale files in the sub-directories.
1155 (union-build fonts-dir fonts-dirs
1156 #:log-port (%make-void-port "w")
1157 #:create-all-directories? #t)
1158 (let ((directories (find-files fonts-dir
1159 (lambda (file stat)
1160 (eq? 'directory (stat:type stat)))
1161 #:directories? #t)))
1162 (for-each (lambda (dir)
1163 (with-directory-excursion dir
1164 (when (file-exists? fonts-scale-file)
1165 (delete-file fonts-scale-file))
1166 (when (file-exists? fonts-dir-file)
1167 (delete-file fonts-dir-file))
1168 (unless (and (zero? (system* mkfontscale))
1169 (zero? (system* mkfontdir)))
1170 (exit #f))
1171 (when (and (file-exists? fonts-scale-file)
1172 (empty-file? fonts-scale-file))
1173 (delete-file fonts-scale-file))
1174 (when (and (file-exists? fonts-dir-file)
1175 (empty-file? fonts-dir-file))
1176 (delete-file fonts-dir-file))))
1177 directories)))))))
1178
1179 (gexp->derivation "fonts-dir" build
1180 #:modules '((guix build utils)
1181 (guix build union)
1182 (srfi srfi-26))
1183 #:local-build? #t
1184 #:substitutable? #f))
1185
1186 (define (manual-database manifest)
1187 "Return a derivation that builds the manual page database (\"mandb\") for
1188 the entries in MANIFEST."
1189 (define gdbm-ffi
1190 (module-ref (resolve-interface '(gnu packages guile))
1191 'guile-gdbm-ffi))
1192
1193 (define zlib
1194 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
1195
1196 (define config.scm
1197 (scheme-file "config.scm"
1198 #~(begin
1199 (define-module (guix config)
1200 #:export (%libz))
1201
1202 (define %libz
1203 #+(file-append zlib "/lib/libz")))))
1204
1205 (define modules
1206 (cons `((guix config) => ,config.scm)
1207 (delete '(guix config)
1208 (source-module-closure `((guix build utils)
1209 (guix man-db))))))
1210
1211 (define build
1212 (with-imported-modules modules
1213 (with-extensions (list gdbm-ffi) ;for (guix man-db)
1214 #~(begin
1215 (use-modules (guix man-db)
1216 (guix build utils)
1217 (srfi srfi-1)
1218 (srfi srfi-19))
1219
1220 (define (compute-entries)
1221 (append-map (lambda (directory)
1222 (let ((man (string-append directory "/share/man")))
1223 (if (directory-exists? man)
1224 (mandb-entries man)
1225 '())))
1226 '#$(manifest-inputs manifest)))
1227
1228 (define man-directory
1229 (string-append #$output "/share/man"))
1230
1231 (mkdir-p man-directory)
1232
1233 (format #t "Creating manual page database...~%")
1234 (force-output)
1235 (let* ((start (current-time))
1236 (entries (compute-entries))
1237 (_ (write-mandb-database (string-append man-directory
1238 "/index.db")
1239 entries))
1240 (duration (time-difference (current-time) start)))
1241 (format #t "~a entries processed in ~,1f s~%"
1242 (length entries)
1243 (+ (time-second duration)
1244 (* (time-nanosecond duration) (expt 10 -9))))
1245 (force-output))))))
1246
1247 (gexp->derivation "manual-database" build
1248
1249 ;; Work around GDBM 1.13 issue whereby uninitialized bytes
1250 ;; get written to disk:
1251 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
1252 #:env-vars `(("MALLOC_PERTURB_" . "1"))
1253
1254 #:local-build? #t))
1255
1256 (define %default-profile-hooks
1257 ;; This is the list of derivation-returning procedures that are called by
1258 ;; default when making a non-empty profile.
1259 (list info-dir-file
1260 manual-database
1261 fonts-dir-file
1262 ghc-package-cache-file
1263 ca-certificate-bundle
1264 glib-schemas
1265 gtk-icon-themes
1266 gtk-im-modules
1267 xdg-desktop-database
1268 xdg-mime-database))
1269
1270 (define* (profile-derivation manifest
1271 #:key
1272 (hooks %default-profile-hooks)
1273 (locales? #t)
1274 (allow-collisions? #f)
1275 (relative-symlinks? #f)
1276 system target)
1277 "Return a derivation that builds a profile (aka. 'user environment') with
1278 the given MANIFEST. The profile includes additional derivations returned by
1279 the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
1280 Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1281 entries in MANIFEST collide (for instance if there are two same-name packages
1282 with a different version number.)
1283
1284 When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
1285 a dependency on the 'glibc-utf8-locales' package.
1286
1287 When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
1288 This is one of the things to do for the result to be relocatable.
1289
1290 When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1291 are cross-built for TARGET."
1292 (mlet* %store-monad ((system (if system
1293 (return system)
1294 (current-system)))
1295 (ok? (if allow-collisions?
1296 (return #t)
1297 (check-for-collisions manifest system
1298 #:target target)))
1299 (extras (if (null? (manifest-entries manifest))
1300 (return '())
1301 (sequence %store-monad
1302 (map (lambda (hook)
1303 (hook manifest))
1304 hooks)))))
1305 (define inputs
1306 (append (filter-map (lambda (drv)
1307 (and (derivation? drv)
1308 (gexp-input drv)))
1309 extras)
1310 (manifest-inputs manifest)))
1311
1312 (define glibc-utf8-locales ;lazy reference
1313 (module-ref (resolve-interface '(gnu packages base))
1314 'glibc-utf8-locales))
1315
1316 (define set-utf8-locale
1317 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1318 ;; install a UTF-8 locale.
1319 #~(begin
1320 (setenv "LOCPATH"
1321 #$(file-append glibc-utf8-locales "/lib/locale/"
1322 (version-major+minor
1323 (package-version glibc-utf8-locales))))
1324 (setlocale LC_ALL "en_US.utf8")))
1325
1326 (define builder
1327 (with-imported-modules '((guix build profiles)
1328 (guix build union)
1329 (guix build utils)
1330 (guix search-paths)
1331 (guix records))
1332 #~(begin
1333 (use-modules (guix build profiles)
1334 (guix search-paths)
1335 (srfi srfi-1))
1336
1337 (setvbuf (current-output-port) _IOLBF)
1338 (setvbuf (current-error-port) _IOLBF)
1339
1340 #+(if locales? set-utf8-locale #t)
1341
1342 (define search-paths
1343 ;; Search paths of MANIFEST's packages, converted back to their
1344 ;; record form.
1345 (map sexp->search-path-specification
1346 (delete-duplicates
1347 '#$(map search-path-specification->sexp
1348 (append-map manifest-entry-search-paths
1349 (manifest-entries manifest))))))
1350
1351 (build-profile #$output '#$inputs
1352 #:symlink #$(if relative-symlinks?
1353 #~symlink-relative
1354 #~symlink)
1355 #:manifest '#$(manifest->gexp manifest)
1356 #:search-paths search-paths))))
1357
1358 (gexp->derivation "profile" builder
1359 #:system system
1360 #:target target
1361
1362 ;; Don't complain about _IO* on Guile 2.2.
1363 #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
1364
1365 ;; Not worth offloading.
1366 #:local-build? #t
1367
1368 ;; Disable substitution because it would trigger a
1369 ;; connection to the substitute server, which is likely
1370 ;; to have no substitute to offer.
1371 #:substitutable? #f)))
1372
1373 (define (profile-regexp profile)
1374 "Return a regular expression that matches PROFILE's name and number."
1375 (make-regexp (string-append "^" (regexp-quote (basename profile))
1376 "-([0-9]+)")))
1377
1378 (define (generation-number profile)
1379 "Return PROFILE's number or 0. An absolute file name must be used."
1380 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
1381 (basename (readlink profile))))
1382 (compose string->number (cut match:substring <> 1)))
1383 0))
1384
1385 (define (generation-numbers profile)
1386 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
1387 former profiles were found."
1388 (match (scandir (dirname profile)
1389 (cute regexp-exec (profile-regexp profile) <>))
1390 (#f ; no profile directory
1391 '(0))
1392 (() ; no profiles
1393 '(0))
1394 ((profiles ...) ; former profiles around
1395 (sort (map (compose string->number
1396 (cut match:substring <> 1)
1397 (cute regexp-exec (profile-regexp profile) <>))
1398 profiles)
1399 <))))
1400
1401 (define (profile-generations profile)
1402 "Return a list of PROFILE's generations."
1403 (let ((generations (generation-numbers profile)))
1404 (if (equal? generations '(0))
1405 '()
1406 generations)))
1407
1408 (define (relative-generation-spec->number profile spec)
1409 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
1410 may be a N, -N, or +N, where N is a number. If the spec is N, then the number
1411 returned is N. If it is -N, then the number returned is the profile's current
1412 generation number minus N. If it is +N, then the number returned is the
1413 profile's current generation number plus N. Return #f if there is no such
1414 generation."
1415 (let ((number (string->number spec)))
1416 (and number
1417 (case (string-ref spec 0)
1418 ((#\+ #\-)
1419 (relative-generation profile number))
1420 (else (if (memv number (profile-generations profile))
1421 number
1422 #f))))))
1423
1424
1425 (define* (relative-generation profile shift #:optional
1426 (current (generation-number profile)))
1427 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
1428 SHIFT is a positive or negative number.
1429 Return #f if there is no such generation."
1430 (let* ((abs-shift (abs shift))
1431 (numbers (profile-generations profile))
1432 (from-current (memq current
1433 (if (negative? shift)
1434 (reverse numbers)
1435 numbers))))
1436 (and from-current
1437 (< abs-shift (length from-current))
1438 (list-ref from-current abs-shift))))
1439
1440 (define* (previous-generation-number profile #:optional
1441 (number (generation-number profile)))
1442 "Return the number of the generation before generation NUMBER of
1443 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
1444 case when generations have been deleted (there are \"holes\")."
1445 (or (relative-generation profile -1 number)
1446 0))
1447
1448 (define (generation-file-name profile generation)
1449 "Return the file name for PROFILE's GENERATION."
1450 (format #f "~a-~a-link" profile generation))
1451
1452 (define (generation-time profile number)
1453 "Return the creation time of a generation in the UTC format."
1454 (make-time time-utc 0
1455 (stat:ctime (stat (generation-file-name profile number)))))
1456
1457 (define (link-to-empty-profile store generation)
1458 "Link GENERATION, a string, to the empty profile. An error is raised if
1459 that fails."
1460 (let* ((drv (run-with-store store
1461 (profile-derivation (manifest '())
1462 #:locales? #f)))
1463 (prof (derivation->output-path drv "out")))
1464 (build-derivations store (list drv))
1465 (switch-symlinks generation prof)))
1466
1467 (define (switch-to-generation profile number)
1468 "Atomically switch PROFILE to the generation NUMBER. Return the number of
1469 the generation that was current before switching."
1470 (let ((current (generation-number profile))
1471 (generation (generation-file-name profile number)))
1472 (cond ((not (file-exists? profile))
1473 (raise (condition (&profile-not-found-error
1474 (profile profile)))))
1475 ((not (file-exists? generation))
1476 (raise (condition (&missing-generation-error
1477 (profile profile)
1478 (generation number)))))
1479 (else
1480 (switch-symlinks profile generation)
1481 current))))
1482
1483 (define (switch-to-previous-generation profile)
1484 "Atomically switch PROFILE to the previous generation. Return the former
1485 generation number and the current one."
1486 (let ((previous (previous-generation-number profile)))
1487 (values (switch-to-generation profile previous)
1488 previous)))
1489
1490 (define (roll-back store profile)
1491 "Roll back to the previous generation of PROFILE. Return the number of the
1492 generation that was current before switching and the new generation number."
1493 (let* ((number (generation-number profile))
1494 (previous-number (previous-generation-number profile number))
1495 (previous-generation (generation-file-name profile previous-number)))
1496 (cond ((not (file-exists? profile)) ;invalid profile
1497 (raise (condition (&profile-not-found-error
1498 (profile profile)))))
1499 ((zero? number) ;empty profile
1500 (values number number))
1501 ((or (zero? previous-number) ;going to emptiness
1502 (not (file-exists? previous-generation)))
1503 (link-to-empty-profile store previous-generation)
1504 (switch-to-previous-generation profile))
1505 (else ;anything else
1506 (switch-to-previous-generation profile)))))
1507
1508 (define (delete-generation store profile number)
1509 "Delete generation with NUMBER from PROFILE. Return the file name of the
1510 generation that has been deleted, or #f if nothing was done (for instance
1511 because the NUMBER is zero.)"
1512 (define (delete-and-return)
1513 (let ((generation (generation-file-name profile number)))
1514 (delete-file generation)
1515 generation))
1516
1517 (let* ((current-number (generation-number profile))
1518 (previous-number (previous-generation-number profile number))
1519 (previous-generation (generation-file-name profile previous-number)))
1520 (cond ((zero? number) #f) ;do not delete generation 0
1521 ((and (= number current-number)
1522 (not (file-exists? previous-generation)))
1523 (link-to-empty-profile store previous-generation)
1524 (switch-to-previous-generation profile)
1525 (delete-and-return))
1526 ((= number current-number)
1527 (roll-back store profile)
1528 (delete-and-return))
1529 (else
1530 (delete-and-return)))))
1531
1532 (define %user-profile-directory
1533 (and=> (getenv "HOME")
1534 (cut string-append <> "/.guix-profile")))
1535
1536 (define %profile-directory
1537 (string-append %state-directory "/profiles/"
1538 (or (and=> (or (getenv "USER")
1539 (getenv "LOGNAME"))
1540 (cut string-append "per-user/" <>))
1541 "default")))
1542
1543 (define %current-profile
1544 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
1545 ;; coexist with Nix profiles.
1546 (string-append %profile-directory "/guix-profile"))
1547
1548 (define (canonicalize-profile profile)
1549 "If PROFILE is %USER-PROFILE-DIRECTORY, return %CURRENT-PROFILE. Otherwise
1550 return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile' as if
1551 '-p' was omitted." ; see <http://bugs.gnu.org/17939>
1552
1553 ;; Trim trailing slashes so that the basename comparison below works as
1554 ;; intended.
1555 (let ((profile (string-trim-right profile #\/)))
1556 (if (and %user-profile-directory
1557 (string=? (canonicalize-path (dirname profile))
1558 (dirname %user-profile-directory))
1559 (string=? (basename profile) (basename %user-profile-directory)))
1560 %current-profile
1561 profile)))
1562
1563 (define (user-friendly-profile profile)
1564 "Return either ~/.guix-profile if that's what PROFILE refers to, directly or
1565 indirectly, or PROFILE."
1566 (if (and %user-profile-directory
1567 (false-if-exception
1568 (string=? (readlink %user-profile-directory) profile)))
1569 %user-profile-directory
1570 profile))
1571
1572 ;;; profiles.scm ends here