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