profiles: Add lowerable <profile> record type.
[jackhill/guix/guix.git] / guix / profiles.scm
CommitLineData
cc4ecc2d 1;;; GNU Guix --- Functional package management for GNU
25af35fa 2;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
cc4ecc2d 3;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
9eb5a449 4;;; Copyright © 2014, 2016 Alex Kost <alezost@gmail.com>
536c3ee4 5;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
b04af0ec 6;;; Copyright © 2015 Sou Bunnbu <iyzsong@gmail.com>
743497b5 7;;; Copyright © 2016, 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
9008debc 8;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
0a5ce0d1 9;;; Copyright © 2017 Huang Ying <huang.ying.caritas@gmail.com>
a0b87ef8 10;;; Copyright © 2017 Maxim Cournoyer <maxim.cournoyer@gmail.com>
723f5b1d 11;;; Copyright © 2019 Kyle Meyer <kyle@kyleam.com>
e51de343 12;;; Copyright © 2019 Mathieu Othacehe <m.othacehe@gmail.com>
5c79f238 13;;; Copyright © 2020 Danny Milosavljevic <dannym@scratchpost.org>
cc4ecc2d
LC
14;;;
15;;; This file is part of GNU Guix.
16;;;
17;;; GNU Guix is free software; you can redistribute it and/or modify it
18;;; under the terms of the GNU General Public License as published by
19;;; the Free Software Foundation; either version 3 of the License, or (at
20;;; your option) any later version.
21;;;
22;;; GNU Guix is distributed in the hope that it will be useful, but
23;;; WITHOUT ANY WARRANTY; without even the implied warranty of
24;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25;;; GNU General Public License for more details.
26;;;
27;;; You should have received a copy of the GNU General Public License
28;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
29
30(define-module (guix profiles)
efcb4441 31 #:use-module ((guix config) #:select (%state-directory))
97425486
LC
32 #:use-module ((guix utils) #:hide (package-name->name+version))
33 #:use-module ((guix build utils)
77dcfb4c
LC
34 #:select (package-name->name+version mkdir-p))
35 #:use-module (guix i18n)
cc4ecc2d 36 #:use-module (guix records)
cc4ecc2d 37 #:use-module (guix packages)
e89431bf
LC
38 #:use-module (guix derivations)
39 #:use-module (guix search-paths)
a54c94a4 40 #:use-module (guix gexp)
b8396f96 41 #:use-module (guix modules)
79ee406d 42 #:use-module (guix monads)
e87f0591 43 #:use-module (guix store)
a654dc4b
LC
44 #:use-module (guix sets)
45 #:use-module (ice-9 vlist)
cc4ecc2d
LC
46 #:use-module (ice-9 match)
47 #:use-module (ice-9 regex)
48 #:use-module (ice-9 ftw)
343745c8 49 #:use-module (ice-9 format)
cc4ecc2d
LC
50 #:use-module (srfi srfi-1)
51 #:use-module (srfi srfi-9)
79601521 52 #:use-module (srfi srfi-11)
cc4ecc2d
LC
53 #:use-module (srfi srfi-19)
54 #:use-module (srfi srfi-26)
c0c018f1
AK
55 #:use-module (srfi srfi-34)
56 #:use-module (srfi srfi-35)
57 #:export (&profile-error
58 profile-error?
59 profile-error-profile
60 &profile-not-found-error
61 profile-not-found-error?
fdc4f665 62 &profile-collision-error
a654dc4b
LC
63 profile-collision-error?
64 profile-collision-error-entry
65 profile-collision-error-conflict
c0c018f1
AK
66 &missing-generation-error
67 missing-generation-error?
68 missing-generation-error-generation
487cbb01
LC
69 &unmatched-pattern-error
70 unmatched-pattern-error?
71 unmatched-pattern-error-pattern
72 unmatched-pattern-error-manifest
c0c018f1
AK
73
74 manifest make-manifest
cc4ecc2d
LC
75 manifest?
76 manifest-entries
a654dc4b 77 manifest-transitive-entries
cc4ecc2d
LC
78
79 <manifest-entry> ; FIXME: eventually make it internal
80 manifest-entry
81 manifest-entry?
82 manifest-entry-name
83 manifest-entry-version
84 manifest-entry-output
a54c94a4 85 manifest-entry-item
cc4ecc2d 86 manifest-entry-dependencies
dedb17ad 87 manifest-entry-search-paths
b3a00885 88 manifest-entry-parent
f6f2346f 89 manifest-entry-properties
190ddfe2 90 lower-manifest-entry
cc4ecc2d 91
a357849f
LC
92 manifest-entry=?
93
a2078770
LC
94 manifest-pattern
95 manifest-pattern?
03763d64
LC
96 manifest-pattern-name
97 manifest-pattern-version
98 manifest-pattern-output
a2078770 99
ce30a0eb 100 concatenate-manifests
8a705ae4 101 map-manifest-entries
cc4ecc2d 102 manifest-remove
f7554030 103 manifest-add
ef8993e2 104 manifest-lookup
cc4ecc2d 105 manifest-installed?
a2078770 106 manifest-matching-entries
f03df3ee 107 manifest-search-paths
cc4ecc2d 108
343745c8
AK
109 manifest-transaction
110 manifest-transaction?
111 manifest-transaction-install
112 manifest-transaction-remove
c8c25704
LC
113 manifest-transaction-install-entry
114 manifest-transaction-remove-pattern
115 manifest-transaction-null?
6d382339 116 manifest-transaction-removal-candidate?
343745c8 117 manifest-perform-transaction
79601521 118 manifest-transaction-effects
343745c8 119
cc4ecc2d 120 profile-manifest
462f5cca 121 package->manifest-entry
8404ed5c 122 packages->manifest
849a1b81 123 ca-certificate-bundle
aa46a028 124 %default-profile-hooks
cc4ecc2d 125 profile-derivation
78d55b70 126 profile-search-paths
06d45f45 127
ef674a24
LC
128 profile
129 profile?
130 profile-name
131 profile-content
132 profile-hooks
133 profile-locales?
134 profile-allow-collisions?
135 profile-relative-symlinks?
136
cc4ecc2d 137 generation-number
c872b952 138 generation-profile
cc4ecc2d 139 generation-numbers
f452e8ff 140 profile-generations
9008debc 141 relative-generation-spec->number
3ccde087 142 relative-generation
cc4ecc2d
LC
143 previous-generation-number
144 generation-time
06d45f45
LC
145 generation-file-name
146 switch-to-generation
147 roll-back
efcb4441
LC
148 delete-generation
149
150 %user-profile-directory
151 %profile-directory
152 %current-profile
77dcfb4c 153 ensure-profile-directory
efcb4441 154 canonicalize-profile
5c79f238
DM
155 user-friendly-profile
156
157 linux-module-database))
cc4ecc2d
LC
158
159;;; Commentary:
160;;;
161;;; Tools to create and manipulate profiles---i.e., the representation of a
162;;; set of installed packages.
163;;;
164;;; Code:
165
166\f
c0c018f1
AK
167;;;
168;;; Condition types.
169;;;
170
171(define-condition-type &profile-error &error
172 profile-error?
173 (profile profile-error-profile))
174
175(define-condition-type &profile-not-found-error &profile-error
176 profile-not-found-error?)
177
a654dc4b
LC
178(define-condition-type &profile-collision-error &error
179 profile-collision-error?
180 (entry profile-collision-error-entry) ;<manifest-entry>
181 (conflict profile-collision-error-conflict)) ;<manifest-entry>
182
487cbb01
LC
183(define-condition-type &unmatched-pattern-error &error
184 unmatched-pattern-error?
185 (pattern unmatched-pattern-error-pattern) ;<manifest-pattern>
186 (manifest unmatched-pattern-error-manifest)) ;<manifest>
187
c0c018f1
AK
188(define-condition-type &missing-generation-error &profile-error
189 missing-generation-error?
190 (generation missing-generation-error-generation))
191
192\f
cc4ecc2d
LC
193;;;
194;;; Manifests.
195;;;
196
197(define-record-type <manifest>
198 (manifest entries)
199 manifest?
200 (entries manifest-entries)) ; list of <manifest-entry>
201
202;; Convenient alias, to avoid name clashes.
203(define make-manifest manifest)
204
205(define-record-type* <manifest-entry> manifest-entry
206 make-manifest-entry
207 manifest-entry?
208 (name manifest-entry-name) ; string
209 (version manifest-entry-version) ; string
210 (output manifest-entry-output ; string
211 (default "out"))
3636b1c7 212 (item manifest-entry-item) ; package | file-like | store path
55b4715f 213 (dependencies manifest-entry-dependencies ; <manifest-entry>*
dedb17ad
LC
214 (default '()))
215 (search-paths manifest-entry-search-paths ; search-path-specification*
b3a00885
LC
216 (default '()))
217 (parent manifest-entry-parent ; promise (#f | <manifest-entry>)
f6f2346f
LC
218 (default (delay #f)))
219 (properties manifest-entry-properties ; list of symbol/value pairs
220 (default '())))
cc4ecc2d 221
a2078770
LC
222(define-record-type* <manifest-pattern> manifest-pattern
223 make-manifest-pattern
224 manifest-pattern?
225 (name manifest-pattern-name) ; string
226 (version manifest-pattern-version ; string | #f
227 (default #f))
228 (output manifest-pattern-output ; string | #f
229 (default "out")))
230
a357849f
LC
231(define (list=? = lst1 lst2)
232 "Return true if LST1 and LST2 have the same length and their elements are
233pairwise equal per =."
234 (match lst1
235 (()
236 (null? lst2))
237 ((head1 . tail1)
238 (match lst2
239 ((head2 . tail2)
240 (and (= head1 head2) (list=? = tail1 tail2)))
241 (()
242 #f)))))
243
244(define (manifest-entry=? entry1 entry2)
245 "Return true if ENTRY1 is equivalent to ENTRY2, ignoring their 'properties'
246field."
247 (match entry1
248 (($ <manifest-entry> name1 version1 output1 item1 dependencies1 paths1)
249 (match entry2
250 (($ <manifest-entry> name2 version2 output2 item2 dependencies2 paths2)
251 (and (string=? name1 name2)
252 (string=? version1 version2)
253 (string=? output1 output2)
254 (equal? item1 item2) ;XXX: could be <package> vs. store item
255 (equal? paths1 paths2)
256 (list=? manifest-entry=? dependencies1 dependencies2)))))))
257
2e2b5ad7
LC
258(define (manifest-transitive-entries manifest)
259 "Return the entries of MANIFEST along with their propagated inputs,
260recursively."
261 (let loop ((entries (manifest-entries manifest))
262 (result '())
263 (visited (set))) ;compare with 'equal?'
264 (match entries
265 (()
266 (reverse result))
267 ((head . tail)
268 (if (set-contains? visited head)
269 (loop tail result visited)
270 (loop (append (manifest-entry-dependencies head)
271 tail)
272 (cons head result)
273 (set-insert head visited)))))))
274
cc4ecc2d
LC
275(define (profile-manifest profile)
276 "Return the PROFILE's manifest."
277 (let ((file (string-append profile "/manifest")))
278 (if (file-exists? file)
279 (call-with-input-file file read-manifest)
280 (manifest '()))))
281
a654dc4b
LC
282(define (manifest-entry-lookup manifest)
283 "Return a lookup procedure for the entries of MANIFEST. The lookup
284procedure takes two arguments: the entry name and output."
285 (define mapping
286 (let loop ((entries (manifest-entries manifest))
287 (mapping vlist-null))
288 (fold (lambda (entry result)
289 (vhash-cons (cons (manifest-entry-name entry)
290 (manifest-entry-output entry))
291 entry
292 (loop (manifest-entry-dependencies entry)
293 result)))
294 mapping
295 entries)))
296
297 (lambda (name output)
298 (match (vhash-assoc (cons name output) mapping)
299 ((_ . entry) entry)
300 (#f #f))))
301
302(define* (lower-manifest-entry entry system #:key target)
303 "Lower ENTRY for SYSTEM and TARGET such that its 'item' field is a store
304file name."
1a9a373e
LC
305 (define (recurse entry)
306 (mapm/accumulate-builds (lambda (entry)
307 (lower-manifest-entry entry system
308 #:target target))
309 (manifest-entry-dependencies entry)))
310
a654dc4b
LC
311 (let ((item (manifest-entry-item entry)))
312 (if (string? item)
313 (with-monad %store-monad
314 (return entry))
315 (mlet %store-monad ((drv (lower-object item system
316 #:target target))
1a9a373e 317 (dependencies (recurse entry))
a654dc4b
LC
318 (output -> (manifest-entry-output entry)))
319 (return (manifest-entry
320 (inherit entry)
1a9a373e
LC
321 (item (derivation->output-path drv output))
322 (dependencies dependencies)))))))
a654dc4b
LC
323
324(define* (check-for-collisions manifest system #:key target)
325 "Check whether the entries of MANIFEST conflict with one another; raise a
326'&profile-collision-error' when a conflict is encountered."
327 (define lookup
328 (manifest-entry-lookup manifest))
329
25af35fa
LC
330 (define candidates
331 (filter-map (lambda (entry)
332 (let ((other (lookup (manifest-entry-name entry)
333 (manifest-entry-output entry))))
334 (and other (list entry other))))
335 (manifest-transitive-entries manifest)))
336
337 (define lower-pair
338 (match-lambda
339 ((first second)
340 (mlet %store-monad ((first (lower-manifest-entry first system
341 #:target target))
342 (second (lower-manifest-entry second system
343 #:target target)))
344 (return (list first second))))))
345
346 ;; Start by lowering CANDIDATES "in parallel".
347 (mlet %store-monad ((lst (mapm/accumulate-builds lower-pair candidates)))
a654dc4b 348 (foldm %store-monad
25af35fa
LC
349 (lambda (entries result)
350 (match entries
351 ((first second)
352 (if (string=? (manifest-entry-item first)
353 (manifest-entry-item second))
354 (return result)
355 (raise (condition
356 (&profile-collision-error
357 (entry first)
358 (conflict second))))))))
a654dc4b 359 #t
25af35fa 360 lst)))
a654dc4b 361
b3a00885 362(define* (package->manifest-entry package #:optional (output "out")
2b73d828
LC
363 #:key (parent (delay #f))
364 (properties '()))
9e90fc77 365 "Return a manifest entry for the OUTPUT of package PACKAGE."
b3a00885
LC
366 ;; For each dependency, keep a promise pointing to its "parent" entry.
367 (letrec* ((deps (map (match-lambda
368 ((label package)
369 (package->manifest-entry package
370 #:parent (delay entry)))
371 ((label package output)
372 (package->manifest-entry package output
373 #:parent (delay entry))))
374 (package-propagated-inputs package)))
375 (entry (manifest-entry
376 (name (package-name package))
377 (version (package-version package))
378 (output output)
379 (item package)
380 (dependencies (delete-duplicates deps))
381 (search-paths
382 (package-transitive-native-search-paths package))
2b73d828
LC
383 (parent parent)
384 (properties properties))))
b3a00885 385 entry))
462f5cca 386
8404ed5c
DT
387(define (packages->manifest packages)
388 "Return a list of manifest entries, one for each item listed in PACKAGES.
389Elements of PACKAGES can be either package objects or package/string tuples
390denoting a specific output of a package."
811b21fb
LC
391 (define inferiors-loaded?
392 ;; This hack allows us to provide seamless integration for inferior
393 ;; packages while not having a hard dependency on (guix inferior).
394 (resolve-module '(guix inferior) #f #f #:ensure #f))
395
396 (define (inferior->entry)
397 (module-ref (resolve-interface '(guix inferior))
398 'inferior-package->manifest-entry))
399
8404ed5c
DT
400 (manifest
401 (map (match-lambda
723f5b1d 402 (((? package? package) output)
811b21fb
LC
403 (package->manifest-entry package output))
404 ((? package? package)
405 (package->manifest-entry package))
406 ((thing output)
407 (if inferiors-loaded?
408 ((inferior->entry) thing output)
409 (throw 'wrong-type-arg 'packages->manifest
410 "Wrong package object: ~S" (list thing) (list thing))))
411 (thing
412 (if inferiors-loaded?
413 ((inferior->entry) thing)
414 (throw 'wrong-type-arg 'packages->manifest
415 "Wrong package object: ~S" (list thing) (list thing)))))
8404ed5c
DT
416 packages)))
417
a54c94a4
LC
418(define (manifest->gexp manifest)
419 "Return a representation of MANIFEST as a gexp."
420 (define (entry->gexp entry)
cc4ecc2d 421 (match entry
dedb17ad 422 (($ <manifest-entry> name version output (? string? path)
f6f2346f 423 (deps ...) (search-paths ...) _ (properties ...))
dedb17ad 424 #~(#$name #$version #$output #$path
55b4715f 425 (propagated-inputs #$(map entry->gexp deps))
dedb17ad 426 (search-paths #$(map search-path-specification->sexp
f6f2346f
LC
427 search-paths))
428 (properties . #$properties)))
3636b1c7 429 (($ <manifest-entry> name version output package
f6f2346f 430 (deps ...) (search-paths ...) _ (properties ...))
a54c94a4 431 #~(#$name #$version #$output
dedb17ad 432 (ungexp package (or output "out"))
55b4715f 433 (propagated-inputs #$(map entry->gexp deps))
dedb17ad 434 (search-paths #$(map search-path-specification->sexp
f6f2346f
LC
435 search-paths))
436 (properties . #$properties)))))
cc4ecc2d
LC
437
438 (match manifest
439 (($ <manifest> (entries ...))
55b4715f 440 #~(manifest (version 3)
a54c94a4 441 (packages #$(map entry->gexp entries))))))
cc4ecc2d 442
dedb17ad
LC
443(define (find-package name version)
444 "Return a package from the distro matching NAME and possibly VERSION. This
445procedure is here for backward-compatibility and will eventually vanish."
446 (define find-best-packages-by-name ;break abstractions
447 (module-ref (resolve-interface '(gnu packages))
448 'find-best-packages-by-name))
449
450 ;; Use 'find-best-packages-by-name' and not 'find-packages-by-name'; the
451 ;; former traverses the module tree only once and then allows for efficient
452 ;; access via a vhash.
453 (match (find-best-packages-by-name name version)
454 ((p _ ...) p)
455 (_
456 (match (find-best-packages-by-name name #f)
457 ((p _ ...) p)
458 (_ #f)))))
459
cc4ecc2d
LC
460(define (sexp->manifest sexp)
461 "Parse SEXP as a manifest."
dedb17ad
LC
462 (define (infer-search-paths name version)
463 ;; Infer the search path specifications for NAME-VERSION by looking up a
464 ;; same-named package in the distro. Useful for the old manifest formats
465 ;; that did not store search path info.
466 (let ((package (find-package name version)))
467 (if package
468 (package-native-search-paths package)
469 '())))
470
b3a00885 471 (define (infer-dependency item parent)
55b4715f
LC
472 ;; Return a <manifest-entry> for ITEM.
473 (let-values (((name version)
474 (package-name->name+version
475 (store-path-package-name item))))
476 (manifest-entry
477 (name name)
478 (version version)
b3a00885
LC
479 (item item)
480 (parent parent))))
481
482 (define* (sexp->manifest-entry sexp #:optional (parent (delay #f)))
483 (match sexp
484 ((name version output path
485 ('propagated-inputs deps)
486 ('search-paths search-paths)
487 extra-stuff ...)
488 ;; For each of DEPS, keep a promise pointing to ENTRY.
489 (letrec* ((deps* (map (cut sexp->manifest-entry <> (delay entry))
490 deps))
491 (entry (manifest-entry
492 (name name)
493 (version version)
494 (output output)
495 (item path)
496 (dependencies deps*)
497 (search-paths (map sexp->search-path-specification
498 search-paths))
f6f2346f
LC
499 (parent parent)
500 (properties (or (assoc-ref extra-stuff 'properties)
501 '())))))
b3a00885 502 entry))))
55b4715f 503
cc4ecc2d
LC
504 (match sexp
505 (('manifest ('version 0)
506 ('packages ((name version output path) ...)))
507 (manifest
508 (map (lambda (name version output path)
509 (manifest-entry
55b4715f
LC
510 (name name)
511 (version version)
512 (output output)
513 (item path)
514 (search-paths (infer-search-paths name version))))
cc4ecc2d
LC
515 name version output path)))
516
517 ;; Version 1 adds a list of propagated inputs to the
518 ;; name/version/output/path tuples.
519 (('manifest ('version 1)
520 ('packages ((name version output path deps) ...)))
521 (manifest
522 (map (lambda (name version output path deps)
d34736c5
LC
523 ;; Up to Guix 0.7 included, dependencies were listed as ("gmp"
524 ;; "/gnu/store/...-gmp") for instance. Discard the 'label' in
525 ;; such lists.
526 (let ((deps (match deps
527 (((labels directories) ...)
528 directories)
529 ((directories ...)
530 directories))))
b3a00885
LC
531 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
532 deps))
533 (entry (manifest-entry
534 (name name)
535 (version version)
536 (output output)
537 (item path)
538 (dependencies deps*)
539 (search-paths
540 (infer-search-paths name version)))))
541 entry)))
cc4ecc2d
LC
542 name version output path deps)))
543
dedb17ad
LC
544 ;; Version 2 adds search paths and is slightly more verbose.
545 (('manifest ('version 2 minor-version ...)
546 ('packages ((name version output path
547 ('propagated-inputs deps)
548 ('search-paths search-paths)
549 extra-stuff ...)
550 ...)))
551 (manifest
552 (map (lambda (name version output path deps search-paths)
b3a00885
LC
553 (letrec* ((deps* (map (cute infer-dependency <> (delay entry))
554 deps))
555 (entry (manifest-entry
556 (name name)
557 (version version)
558 (output output)
559 (item path)
560 (dependencies deps*)
561 (search-paths
562 (map sexp->search-path-specification
563 search-paths)))))
564 entry))
dedb17ad 565 name version output path deps search-paths)))
55b4715f
LC
566
567 ;; Version 3 represents DEPS as full-blown manifest entries.
568 (('manifest ('version 3 minor-version ...)
569 ('packages (entries ...)))
b3a00885 570 (manifest (map sexp->manifest-entry entries)))
cc4ecc2d 571 (_
88aab8e3
LC
572 (raise (condition
573 (&message (message "unsupported manifest format")))))))
cc4ecc2d
LC
574
575(define (read-manifest port)
576 "Return the packages listed in MANIFEST."
577 (sexp->manifest (read port)))
578
ce30a0eb
LC
579(define (concatenate-manifests lst)
580 "Concatenate the manifests listed in LST and return the resulting manifest."
581 (manifest (append-map manifest-entries lst)))
582
8a705ae4
LC
583(define (map-manifest-entries proc manifest)
584 "Apply PROC to all the entries of MANIFEST and return a new manifest."
585 (make-manifest
586 (map proc (manifest-entries manifest))))
587
a2078770
LC
588(define (entry-predicate pattern)
589 "Return a procedure that returns #t when passed a manifest entry that
590matches NAME/OUTPUT/VERSION. OUTPUT and VERSION may be #f, in which case they
591are ignored."
592 (match pattern
593 (($ <manifest-pattern> name version output)
594 (match-lambda
595 (($ <manifest-entry> entry-name entry-version entry-output)
596 (and (string=? entry-name name)
597 (or (not entry-output) (not output)
598 (string=? entry-output output))
599 (or (not version)
600 (string=? entry-version version))))))))
601
602(define (manifest-remove manifest patterns)
603 "Remove entries for each of PATTERNS from MANIFEST. Each item in PATTERNS
604must be a manifest-pattern."
605 (define (remove-entry pattern lst)
606 (remove (entry-predicate pattern) lst))
607
608 (make-manifest (fold remove-entry
cc4ecc2d 609 (manifest-entries manifest)
a2078770 610 patterns)))
cc4ecc2d 611
f7554030
AK
612(define (manifest-add manifest entries)
613 "Add a list of manifest ENTRIES to MANIFEST and return new manifest.
614Remove MANIFEST entries that have the same name and output as ENTRIES."
615 (define (same-entry? entry name output)
616 (match entry
435603a1 617 (($ <manifest-entry> entry-name _ entry-output _)
f7554030
AK
618 (and (equal? name entry-name)
619 (equal? output entry-output)))))
620
621 (make-manifest
435603a1
LC
622 (fold (lambda (entry result) ;XXX: quadratic
623 (match entry
624 (($ <manifest-entry> name _ out _)
625 (cons entry
626 (remove (cut same-entry? <> name out)
627 result)))))
628 (manifest-entries manifest)
629 entries)))
f7554030 630
ef8993e2
LC
631(define (manifest-lookup manifest pattern)
632 "Return the first item of MANIFEST that matches PATTERN, or #f if there is
633no match.."
634 (find (entry-predicate pattern)
635 (manifest-entries manifest)))
636
a2078770
LC
637(define (manifest-installed? manifest pattern)
638 "Return #t if MANIFEST has an entry matching PATTERN (a manifest-pattern),
639#f otherwise."
ef8993e2 640 (->bool (manifest-lookup manifest pattern)))
cc4ecc2d 641
a2078770 642(define (manifest-matching-entries manifest patterns)
487cbb01
LC
643 "Return all the entries of MANIFEST that match one of the PATTERNS. Raise
644an '&unmatched-pattern-error' if none of the entries of MANIFEST matches one
645of PATTERNS."
646 (fold-right (lambda (pattern matches)
647 (match (filter (entry-predicate pattern)
648 (manifest-entries manifest))
649 (()
650 (raise (condition
651 (&unmatched-pattern-error
652 (pattern pattern)
653 (manifest manifest)))))
654 (lst
655 (append lst matches))))
656 '()
657 patterns))
a2078770 658
f03df3ee
LC
659(define (manifest-search-paths manifest)
660 "Return the list of search path specifications that apply to MANIFEST,
661including the search path specification for $PATH."
662 (delete-duplicates
663 (cons $PATH
664 (append-map manifest-entry-search-paths
665 (manifest-entries manifest)))))
666
cc4ecc2d 667\f
343745c8
AK
668;;;
669;;; Manifest transactions.
670;;;
671
672(define-record-type* <manifest-transaction> manifest-transaction
673 make-manifest-transaction
674 manifest-transaction?
675 (install manifest-transaction-install ; list of <manifest-entry>
676 (default '()))
677 (remove manifest-transaction-remove ; list of <manifest-pattern>
678 (default '())))
679
c8c25704
LC
680(define (manifest-transaction-install-entry entry transaction)
681 "Augment TRANSACTION's set of installed packages with ENTRY, a
682<manifest-entry>."
683 (manifest-transaction
684 (inherit transaction)
685 (install
686 (cons entry (manifest-transaction-install transaction)))))
687
688(define (manifest-transaction-remove-pattern pattern transaction)
689 "Add PATTERN to TRANSACTION's list of packages to remove."
690 (manifest-transaction
691 (inherit transaction)
692 (remove
693 (cons pattern (manifest-transaction-remove transaction)))))
694
695(define (manifest-transaction-null? transaction)
696 "Return true if TRANSACTION has no effect---i.e., it neither installs nor
697remove software."
698 (match transaction
699 (($ <manifest-transaction> () ()) #t)
700 (($ <manifest-transaction> _ _) #f)))
701
6d382339
LC
702(define (manifest-transaction-removal-candidate? entry transaction)
703 "Return true if ENTRY is a candidate for removal in TRANSACTION."
704 (any (lambda (pattern)
705 ((entry-predicate pattern) entry))
706 (manifest-transaction-remove transaction)))
707
79601521 708(define (manifest-transaction-effects manifest transaction)
46b23e1a
LC
709 "Compute the effect of applying TRANSACTION to MANIFEST. Return 4 values:
710the list of packages that would be removed, installed, upgraded, or downgraded
711when applying TRANSACTION to MANIFEST. Upgrades are represented as pairs
712where the head is the entry being upgraded and the tail is the entry that will
713replace it."
79601521
LC
714 (define (manifest-entry->pattern entry)
715 (manifest-pattern
716 (name (manifest-entry-name entry))
717 (output (manifest-entry-output entry))))
718
46b23e1a
LC
719 (let loop ((input (manifest-transaction-install transaction))
720 (install '())
721 (upgrade '())
722 (downgrade '()))
79601521
LC
723 (match input
724 (()
725 (let ((remove (manifest-transaction-remove transaction)))
726 (values (manifest-matching-entries manifest remove)
46b23e1a 727 (reverse install) (reverse upgrade) (reverse downgrade))))
79601521
LC
728 ((entry rest ...)
729 ;; Check whether installing ENTRY corresponds to the installation of a
730 ;; new package or to an upgrade.
731
732 ;; XXX: When the exact same output directory is installed, we're not
733 ;; really upgrading anything. Add a check for that case.
734 (let* ((pattern (manifest-entry->pattern entry))
46b23e1a
LC
735 (previous (manifest-lookup manifest pattern))
736 (newer? (and previous
3bea13bb
LC
737 (version>=? (manifest-entry-version entry)
738 (manifest-entry-version previous)))))
79601521 739 (loop rest
ef8993e2 740 (if previous install (cons entry install))
46b23e1a 741 (if (and previous newer?)
ef8993e2 742 (alist-cons previous entry upgrade)
46b23e1a
LC
743 upgrade)
744 (if (and previous (not newer?))
745 (alist-cons previous entry downgrade)
746 downgrade)))))))
79601521 747
343745c8 748(define (manifest-perform-transaction manifest transaction)
c8c25704 749 "Perform TRANSACTION on MANIFEST and return the new manifest."
343745c8
AK
750 (let ((install (manifest-transaction-install transaction))
751 (remove (manifest-transaction-remove transaction)))
752 (manifest-add (manifest-remove manifest remove)
753 install)))
754
343745c8 755\f
cc4ecc2d
LC
756;;;
757;;; Profiles.
758;;;
759
79ee406d 760(define (manifest-inputs manifest)
b4a4bec0 761 "Return a list of <gexp-input> objects for MANIFEST."
55b4715f
LC
762 (define entry->input
763 (match-lambda
764 (($ <manifest-entry> name version output thing deps)
765 ;; THING may be a package or a file name. In the latter case, assume
766 ;; it's already valid.
767 (cons (gexp-input thing output)
768 (append-map entry->input deps)))))
769
770 (append-map entry->input (manifest-entries manifest)))
79ee406d 771
2c9f4786 772(define* (manifest-lookup-package manifest name #:optional version)
d72d7833 773 "Return as a monadic value the first package or store path referenced by
2c9f4786
RW
774MANIFEST that is named NAME and optionally has the given VERSION prefix, or #f
775if not found."
d72d7833
SB
776 ;; Return as a monadic value the package or store path referenced by the
777 ;; manifest ENTRY, or #f if not referenced.
778 (define (entry-lookup-package entry)
779 (define (find-among-inputs inputs)
780 (find (lambda (input)
781 (and (package? input)
2c9f4786
RW
782 (equal? name (package-name input))
783 (if version
784 (string-prefix? version (package-version input))
785 #t)))
d72d7833
SB
786 inputs))
787 (define (find-among-store-items items)
788 (find (lambda (item)
054f60cd 789 (let-values (((name* version*)
2c9f4786
RW
790 (package-name->name+version
791 (store-path-package-name item))))
054f60cd 792 (and (string=? name name*)
2c9f4786 793 (if version
054f60cd 794 (string-prefix? version version*)
2c9f4786 795 #t))))
d72d7833
SB
796 items))
797
d72d7833
SB
798 (with-monad %store-monad
799 (match (manifest-entry-item entry)
800 ((? package? package)
963521a3
SB
801 (match (cons (list (package-name package) package)
802 (package-transitive-inputs package))
d72d7833
SB
803 (((labels inputs . _) ...)
804 (return (find-among-inputs inputs)))))
805 ((? string? item)
806 (mlet %store-monad ((refs (references* item)))
3636b1c7
LC
807 (return (find-among-store-items refs))))
808 (item
809 ;; XXX: ITEM might be a 'computed-file' or anything like that, in
810 ;; which case we don't know what to do. The fix may be to check
811 ;; references once ITEM is compiled, as proposed at
812 ;; <https://bugs.gnu.org/29927>.
813 (return #f)))))
d72d7833
SB
814
815 (anym %store-monad
816 entry-lookup-package (manifest-entries manifest)))
817
79ee406d
LC
818(define (info-dir-file manifest)
819 "Return a derivation that builds the 'dir' file for all the entries of
820MANIFEST."
2f0556ae
LC
821 (define texinfo ;lazy reference
822 (module-ref (resolve-interface '(gnu packages texinfo)) 'texinfo))
823 (define gzip ;lazy reference
824 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
5b0c648a
LC
825 (define glibc-utf8-locales ;lazy reference
826 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
2f0556ae 827
79ee406d 828 (define build
99b231de
LC
829 (with-imported-modules '((guix build utils))
830 #~(begin
831 (use-modules (guix build utils)
832 (srfi srfi-1) (srfi srfi-26)
833 (ice-9 ftw))
834
835 (define (info-file? file)
836 (or (string-suffix? ".info" file)
837 (string-suffix? ".info.gz" file)))
838
839 (define (info-files top)
840 (let ((infodir (string-append top "/share/info")))
841 (map (cut string-append infodir "/" <>)
842 (or (scandir infodir info-file?) '()))))
843
5b0c648a
LC
844 (define (info-file-language file)
845 (let* ((base (if (string-suffix? ".gz" file)
846 (basename file ".info.gz")
847 (basename file ".info")))
848 (dot (string-rindex base #\.)))
849 (if dot
850 (string-drop base (+ 1 dot))
851 "en")))
852
99b231de 853 (define (install-info info)
5b0c648a
LC
854 (let ((language (info-file-language info)))
855 ;; We need to choose a valid locale for $LANGUAGE to be honored.
856 (setenv "LC_ALL" "en_US.utf8")
857 (setenv "LANGUAGE" language)
858 (zero?
859 (system* #+(file-append texinfo "/bin/install-info")
860 "--silent" info
861 (apply string-append #$output "/share/info/dir"
862 (if (string=? "en" language)
863 '("")
864 `("." ,language)))))))
865
866 (setenv "PATH" (string-append #+gzip "/bin")) ;for info.gz files
867 (setenv "GUIX_LOCPATH"
868 #+(file-append glibc-utf8-locales "/lib/locale"))
99b231de
LC
869
870 (mkdir-p (string-append #$output "/share/info"))
871 (exit (every install-info
872 (append-map info-files
873 '#$(manifest-inputs manifest)))))))
79ee406d 874
aa46a028 875 (gexp->derivation "info-dir" build
a7a4fd9a 876 #:local-build? #t
80eebee9
RW
877 #:substitutable? #f
878 #:properties
879 `((type . profile-hook)
880 (hook . info-dir))))
79ee406d 881
042bc828
FB
882(define (ghc-package-cache-file manifest)
883 "Return a derivation that builds the GHC 'package.cache' file for all the
aa46a028 884entries of MANIFEST, or #f if MANIFEST does not have any GHC packages."
99b231de 885 (define ghc ;lazy reference
042bc828
FB
886 (module-ref (resolve-interface '(gnu packages haskell)) 'ghc))
887
888 (define build
99b231de
LC
889 (with-imported-modules '((guix build utils))
890 #~(begin
891 (use-modules (guix build utils)
892 (srfi srfi-1) (srfi srfi-26)
893 (ice-9 ftw))
894
895 (define ghc-name-version
896 (let* ((base (basename #+ghc)))
897 (string-drop base
898 (+ 1 (string-index base #\-)))))
899
900 (define db-subdir
901 (string-append "lib/" ghc-name-version "/package.conf.d"))
902
903 (define db-dir
904 (string-append #$output "/" db-subdir))
905
906 (define (conf-files top)
907 (let ((db (string-append top "/" db-subdir)))
908 (if (file-exists? db)
909 (find-files db "\\.conf$")
910 '())))
911
912 (define (copy-conf-file conf)
913 (let ((base (basename conf)))
914 (copy-file conf (string-append db-dir "/" base))))
915
916 (system* (string-append #+ghc "/bin/ghc-pkg") "init" db-dir)
917 (for-each copy-conf-file
918 (append-map conf-files
919 (delete-duplicates
920 '#$(manifest-inputs manifest))))
921 (let ((success
922 (zero?
923 (system* (string-append #+ghc "/bin/ghc-pkg") "recache"
924 (string-append "--package-db=" db-dir)))))
925 (for-each delete-file (find-files db-dir "\\.conf$"))
926 (exit success)))))
042bc828 927
07eaecfa
LC
928 (with-monad %store-monad
929 ;; Don't depend on GHC when there's nothing to do.
930 (if (any (cut string-prefix? "ghc" <>)
931 (map manifest-entry-name (manifest-entries manifest)))
932 (gexp->derivation "ghc-package-cache" build
a7a4fd9a 933 #:local-build? #t
80eebee9
RW
934 #:substitutable? #f
935 #:properties
936 `((type . profile-hook)
937 (hook . ghc-package-cache)))
07eaecfa 938 (return #f))))
042bc828 939
536c3ee4
MW
940(define (ca-certificate-bundle manifest)
941 "Return a derivation that builds a single-file bundle containing the CA
942certificates in the /etc/ssl/certs sub-directories of the packages in
943MANIFEST. Single-file bundles are required by programs such as Git and Lynx."
944 ;; See <http://lists.gnu.org/archive/html/guix-devel/2015-02/msg00429.html>
945 ;; for a discussion.
946
947 (define glibc-utf8-locales ;lazy reference
948 (module-ref (resolve-interface '(gnu packages base)) 'glibc-utf8-locales))
949
950 (define build
99b231de
LC
951 (with-imported-modules '((guix build utils))
952 #~(begin
953 (use-modules (guix build utils)
954 (rnrs io ports)
955 (srfi srfi-1)
956 (srfi srfi-26)
957 (ice-9 ftw)
958 (ice-9 match))
959
960 (define (pem-file? file)
961 (string-suffix? ".pem" file))
962
963 (define (ca-files top)
964 (let ((cert-dir (string-append top "/etc/ssl/certs")))
965 (map (cut string-append cert-dir "/" <>)
966 (or (scandir cert-dir pem-file?) '()))))
967
968 (define (concatenate-files files result)
969 "Make RESULT the concatenation of all of FILES."
970 (define (dump file port)
971 (display (call-with-input-file file get-string-all)
972 port)
973 (newline port)) ;required, see <https://bugs.debian.org/635570>
974
975 (call-with-output-file result
976 (lambda (port)
977 (for-each (cut dump <> port) files))))
978
979 ;; Some file names in the NSS certificates are UTF-8 encoded so
980 ;; install a UTF-8 locale.
981 (setenv "LOCPATH"
982 (string-append #+glibc-utf8-locales "/lib/locale/"
c6bc8e22
MB
983 #+(version-major+minor
984 (package-version glibc-utf8-locales))))
99b231de
LC
985 (setlocale LC_ALL "en_US.utf8")
986
987 (match (append-map ca-files '#$(manifest-inputs manifest))
988 (()
989 ;; Since there are no CA files, just create an empty directory. Do
990 ;; not create the etc/ssl/certs sub-directory, since that would
991 ;; wrongfully lead to a message about 'SSL_CERT_DIR' needing to be
992 ;; defined.
993 (mkdir #$output)
994 #t)
995 ((ca-files ...)
996 (let ((result (string-append #$output "/etc/ssl/certs")))
997 (mkdir-p result)
998 (concatenate-files ca-files
999 (string-append result
1000 "/ca-certificates.crt"))
1001 #t))))))
536c3ee4 1002
aa46a028 1003 (gexp->derivation "ca-certificate-bundle" build
a7a4fd9a 1004 #:local-build? #t
80eebee9
RW
1005 #:substitutable? #f
1006 #:properties
1007 `((type . profile-hook)
1008 (hook . ca-certificate-bundle))))
aa46a028 1009
de136f3e
DM
1010(define (glib-schemas manifest)
1011 "Return a derivation that unions all schemas from manifest entries and
1012creates the Glib 'gschemas.compiled' file."
1013 (define glib ; lazy reference
1014 (module-ref (resolve-interface '(gnu packages glib)) 'glib))
1015
1016 (mlet %store-monad ((%glib (manifest-lookup-package manifest "glib"))
1017 ;; XXX: Can't use glib-compile-schemas corresponding
1018 ;; to the glib referenced by 'manifest'. Because
1019 ;; '%glib' can be either a package or store path, and
1020 ;; there's no way to get the "bin" output for the later.
1021 (glib-compile-schemas
1022 -> #~(string-append #+glib:bin
1023 "/bin/glib-compile-schemas")))
1024
1025 (define build
1026 (with-imported-modules '((guix build utils)
1027 (guix build union)
1028 (guix build profiles)
1029 (guix search-paths)
1030 (guix records))
1031 #~(begin
1032 (use-modules (guix build utils)
1033 (guix build union)
1034 (guix build profiles)
1035 (srfi srfi-26))
1036
1037 (let* ((destdir (string-append #$output "/share/glib-2.0/schemas"))
1038 (schemadirs (filter file-exists?
1039 (map (cut string-append <> "/share/glib-2.0/schemas")
1040 '#$(manifest-inputs manifest)))))
1041
1042 ;; Union all the schemas.
1043 (mkdir-p (string-append #$output "/share/glib-2.0"))
1044 (union-build destdir schemadirs
1045 #:log-port (%make-void-port "w"))
1046
1047 (let ((dir destdir))
1048 (when (file-is-directory? dir)
1049 (ensure-writable-directory dir)
1050 (invoke #+glib-compile-schemas
1051 (string-append "--targetdir=" dir)
1052 dir)))))))
1053
1054 ;; Don't run the hook when there's nothing to do.
1055 (if %glib
1056 (gexp->derivation "glib-schemas" build
1057 #:local-build? #t
80eebee9
RW
1058 #:substitutable? #f
1059 #:properties
1060 `((type . profile-hook)
1061 (hook . glib-schemas)))
de136f3e
DM
1062 (return #f))))
1063
b04af0ec
SB
1064(define (gtk-icon-themes manifest)
1065 "Return a derivation that unions all icon themes from manifest entries and
1066creates the GTK+ 'icon-theme.cache' file for each theme."
d1fb4af6
SB
1067 (define gtk+ ; lazy reference
1068 (module-ref (resolve-interface '(gnu packages gtk)) 'gtk+))
1069
1070 (mlet %store-monad ((%gtk+ (manifest-lookup-package manifest "gtk+"))
1071 ;; XXX: Can't use gtk-update-icon-cache corresponding
1072 ;; to the gtk+ referenced by 'manifest'. Because
1073 ;; '%gtk+' can be either a package or store path, and
1074 ;; there's no way to get the "bin" output for the later.
1075 (gtk-update-icon-cache
1076 -> #~(string-append #+gtk+:bin
1077 "/bin/gtk-update-icon-cache")))
1078
b04af0ec 1079 (define build
99b231de
LC
1080 (with-imported-modules '((guix build utils)
1081 (guix build union)
1082 (guix build profiles)
1083 (guix search-paths)
1084 (guix records))
1085 #~(begin
1086 (use-modules (guix build utils)
1087 (guix build union)
1088 (guix build profiles)
1089 (srfi srfi-26)
1090 (ice-9 ftw))
1091
1092 (let* ((destdir (string-append #$output "/share/icons"))
1093 (icondirs (filter file-exists?
1094 (map (cut string-append <> "/share/icons")
d1fb4af6 1095 '#$(manifest-inputs manifest)))))
99b231de
LC
1096
1097 ;; Union all the icons.
1098 (mkdir-p (string-append #$output "/share"))
1099 (union-build destdir icondirs
1100 #:log-port (%make-void-port "w"))
1101
1102 ;; Update the 'icon-theme.cache' file for each icon theme.
1103 (for-each
1104 (lambda (theme)
1105 (let ((dir (string-append destdir "/" theme)))
1106 ;; Occasionally DESTDIR contains plain files, such as
1107 ;; "abiword_48.png". Ignore these.
1108 (when (file-is-directory? dir)
1109 (ensure-writable-directory dir)
d1fb4af6 1110 (system* #+gtk-update-icon-cache "-t" dir "--quiet"))))
99b231de 1111 (scandir destdir (negate (cut member <> '("." "..")))))))))
b04af0ec
SB
1112
1113 ;; Don't run the hook when there's nothing to do.
d1fb4af6 1114 (if %gtk+
b04af0ec 1115 (gexp->derivation "gtk-icon-themes" build
a7a4fd9a 1116 #:local-build? #t
80eebee9
RW
1117 #:substitutable? #f
1118 #:properties
1119 `((type . profile-hook)
1120 (hook . gtk-icon-themes)))
b04af0ec
SB
1121 (return #f))))
1122
7ddc1780
RW
1123(define (gtk-im-modules manifest)
1124 "Return a derivation that builds the cache files for input method modules
1125for both major versions of GTK+."
1126
1127 (mlet %store-monad ((gtk+ (manifest-lookup-package manifest "gtk+" "3"))
1128 (gtk+-2 (manifest-lookup-package manifest "gtk+" "2")))
1129
06d7d119 1130 (define (build gtk gtk-version query)
7ddc1780
RW
1131 (let ((major (string-take gtk-version 1)))
1132 (with-imported-modules '((guix build utils)
1133 (guix build union)
1134 (guix build profiles)
1135 (guix search-paths)
1136 (guix records))
1137 #~(begin
1138 (use-modules (guix build utils)
1139 (guix build union)
1140 (guix build profiles)
1141 (ice-9 popen)
1142 (srfi srfi-1)
1143 (srfi srfi-26))
1144
1145 (let* ((prefix (string-append "/lib/gtk-" #$major ".0/"
1146 #$gtk-version))
7ddc1780
RW
1147 (destdir (string-append #$output prefix))
1148 (moddirs (cons (string-append #$gtk prefix "/immodules")
1149 (filter file-exists?
1150 (map (cut string-append <> prefix "/immodules")
1151 '#$(manifest-inputs manifest)))))
1152 (modules (append-map (cut find-files <> "\\.so$")
1153 moddirs)))
1154
1155 ;; Generate a new immodules cache file.
1156 (mkdir-p (string-append #$output prefix))
06d7d119 1157 (let ((pipe (apply open-pipe* OPEN_READ #$query modules))
7ddc1780
RW
1158 (outfile (string-append #$output prefix
1159 "/immodules-gtk" #$major ".cache")))
1160 (dynamic-wind
1161 (const #t)
1162 (lambda ()
1163 (call-with-output-file outfile
1164 (lambda (out)
1165 (while (not (eof-object? (peek-char pipe)))
1166 (write-char (read-char pipe) out))))
1167 #t)
1168 (lambda ()
1169 (close-pipe pipe)))))))))
1170
1171 ;; Don't run the hook when there's nothing to do.
06d7d119
YH
1172 (let* ((pkg-gtk+ (module-ref ; lazy reference
1173 (resolve-interface '(gnu packages gtk)) 'gtk+))
1174 (gexp #~(begin
1175 #$(if gtk+
1176 (build
1177 gtk+ "3.0.0"
1178 ;; Use 'gtk-query-immodules-3.0' from the 'bin'
1179 ;; output of latest gtk+ package.
1180 #~(string-append
1181 #$pkg-gtk+:bin "/bin/gtk-query-immodules-3.0"))
1182 #t)
1183 #$(if gtk+-2
1184 (build
1185 gtk+-2 "2.10.0"
1186 #~(string-append
1187 #$gtk+-2 "/bin/gtk-query-immodules-2.0"))
1188 #t))))
7ddc1780
RW
1189 (if (or gtk+ gtk+-2)
1190 (gexp->derivation "gtk-im-modules" gexp
1191 #:local-build? #t
80eebee9
RW
1192 #:substitutable? #f
1193 #:properties
1194 `((type . profile-hook)
1195 (hook . gtk-im-modules)))
7ddc1780
RW
1196 (return #f)))))
1197
5c79f238
DM
1198(define (linux-module-database manifest)
1199 "Return a derivation that unites all the kernel modules of the manifest
1200and creates the dependency graph of all these kernel modules.
1201
1202This is meant to be used as a profile hook."
1203 (define kmod ; lazy reference
1204 (module-ref (resolve-interface '(gnu packages linux)) 'kmod))
1205 (define build
1206 (with-imported-modules
1207 (source-module-closure '((guix build utils)
1208 (gnu build linux-modules)))
1209 #~(begin
1210 (use-modules (ice-9 ftw)
1211 (ice-9 match)
1212 (srfi srfi-1) ; append-map
1213 (gnu build linux-modules))
1214 (let* ((inputs '#$(manifest-inputs manifest))
1215 (module-directories
1216 (map (lambda (directory)
1217 (string-append directory "/lib/modules"))
1218 inputs))
1219 (directory-entries
1220 (lambda (directory)
1221 (scandir directory (lambda (basename)
1222 (not
1223 (string-prefix? "." basename))))))
1224 ;; Note: Should usually result in one entry.
1225 (versions (delete-duplicates
1226 (append-map directory-entries
1227 module-directories))))
1228 (match versions
1229 ((version)
1230 (let ((old-path (getenv "PATH")))
1231 (setenv "PATH" #+(file-append kmod "/bin"))
1232 (make-linux-module-directory inputs version #$output)
1233 (setenv "PATH" old-path)))
1234 (_ (error "Specified Linux kernel and Linux kernel modules
1235are not all of the same version")))))))
1236 (gexp->derivation "linux-module-database" build
1237 #:local-build? #t
1238 #:substitutable? #f
1239 #:properties
1240 `((type . profile-hook)
1241 (hook . linux-module-database))))
1242
842cb820
SB
1243(define (xdg-desktop-database manifest)
1244 "Return a derivation that builds the @file{mimeinfo.cache} database from
1245desktop files. It's used to query what applications can handle a given
1246MIME type."
85cfbd46
SB
1247 (define desktop-file-utils ; lazy reference
1248 (module-ref (resolve-interface '(gnu packages freedesktop))
1249 'desktop-file-utils))
1250
1251 (mlet %store-monad ((glib
d72d7833 1252 (manifest-lookup-package
85cfbd46 1253 manifest "glib")))
d72d7833 1254 (define build
99b231de
LC
1255 (with-imported-modules '((guix build utils)
1256 (guix build union))
1257 #~(begin
1258 (use-modules (srfi srfi-26)
1259 (guix build utils)
1260 (guix build union))
1261 (let* ((destdir (string-append #$output "/share/applications"))
1262 (appdirs (filter file-exists?
1263 (map (cut string-append <>
1264 "/share/applications")
1265 '#$(manifest-inputs manifest))))
1266 (update-desktop-database (string-append
1267 #+desktop-file-utils
1268 "/bin/update-desktop-database")))
1269 (mkdir-p (string-append #$output "/share"))
1270 (union-build destdir appdirs
1271 #:log-port (%make-void-port "w"))
1272 (exit (zero? (system* update-desktop-database destdir)))))))
842cb820 1273
85cfbd46
SB
1274 ;; Don't run the hook when 'glib' is not referenced.
1275 (if glib
d72d7833 1276 (gexp->derivation "xdg-desktop-database" build
d72d7833 1277 #:local-build? #t
80eebee9
RW
1278 #:substitutable? #f
1279 #:properties
1280 `((type . profile-hook)
1281 (hook . xdg-desktop-database)))
d72d7833 1282 (return #f))))
842cb820 1283
6c06b1fd
SB
1284(define (xdg-mime-database manifest)
1285 "Return a derivation that builds the @file{mime.cache} database from manifest
1286entries. It's used to query the MIME type of a given file."
801d316b
SB
1287 (define shared-mime-info ; lazy reference
1288 (module-ref (resolve-interface '(gnu packages gnome)) 'shared-mime-info))
1289
1290 (mlet %store-monad ((glib
d72d7833 1291 (manifest-lookup-package
801d316b 1292 manifest "glib")))
d72d7833 1293 (define build
99b231de
LC
1294 (with-imported-modules '((guix build utils)
1295 (guix build union))
1296 #~(begin
1297 (use-modules (srfi srfi-26)
1298 (guix build utils)
1299 (guix build union))
1300 (let* ((datadir (string-append #$output "/share"))
1301 (destdir (string-append datadir "/mime"))
1302 (pkgdirs (filter file-exists?
1303 (map (cut string-append <>
1304 "/share/mime/packages")
801d316b
SB
1305 (cons #+shared-mime-info
1306 '#$(manifest-inputs manifest)))))
99b231de
LC
1307 (update-mime-database (string-append
1308 #+shared-mime-info
1309 "/bin/update-mime-database")))
1310 (mkdir-p destdir)
1311 (union-build (string-append destdir "/packages") pkgdirs
1312 #:log-port (%make-void-port "w"))
1313 (setenv "XDG_DATA_HOME" datadir)
1314 (exit (zero? (system* update-mime-database destdir)))))))
d72d7833 1315
801d316b
SB
1316 ;; Don't run the hook when there are no GLib based applications.
1317 (if glib
d72d7833 1318 (gexp->derivation "xdg-mime-database" build
d72d7833 1319 #:local-build? #t
80eebee9
RW
1320 #:substitutable? #f
1321 #:properties
1322 `((type . profile-hook)
1323 (hook . xdg-mime-database)))
d72d7833 1324 (return #f))))
6c06b1fd 1325
0a5ce0d1
HY
1326;; Several font packages may install font files into same directory, so
1327;; fonts.dir and fonts.scale file should be generated here, instead of in
1328;; packages.
9eb5a449
AK
1329(define (fonts-dir-file manifest)
1330 "Return a derivation that builds the @file{fonts.dir} and @file{fonts.scale}
0a5ce0d1 1331files for the fonts of the @var{manifest} entries."
9eb5a449
AK
1332 (define mkfontscale
1333 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontscale))
1334
1335 (define mkfontdir
1336 (module-ref (resolve-interface '(gnu packages xorg)) 'mkfontdir))
1337
1338 (define build
1339 #~(begin
1340 (use-modules (srfi srfi-26)
1341 (guix build utils)
1342 (guix build union))
0a5ce0d1
HY
1343 (let ((fonts-dirs (filter file-exists?
1344 (map (cut string-append <>
1345 "/share/fonts")
1346 '#$(manifest-inputs manifest)))))
9eb5a449 1347 (mkdir #$output)
0a5ce0d1 1348 (if (null? fonts-dirs)
9eb5a449 1349 (exit #t)
0a5ce0d1
HY
1350 (let* ((share-dir (string-append #$output "/share"))
1351 (fonts-dir (string-append share-dir "/fonts"))
9eb5a449
AK
1352 (mkfontscale (string-append #+mkfontscale
1353 "/bin/mkfontscale"))
1354 (mkfontdir (string-append #+mkfontdir
0a5ce0d1
HY
1355 "/bin/mkfontdir"))
1356 (empty-file? (lambda (filename)
1357 (call-with-ascii-input-file filename
1358 (lambda (p)
1359 (eqv? #\0 (read-char p))))))
1360 (fonts-dir-file "fonts.dir")
1361 (fonts-scale-file "fonts.scale"))
1362 (mkdir-p share-dir)
1363 ;; Create all sub-directories, because we may create fonts.dir
1364 ;; and fonts.scale files in the sub-directories.
1365 (union-build fonts-dir fonts-dirs
1366 #:log-port (%make-void-port "w")
1367 #:create-all-directories? #t)
1368 (let ((directories (find-files fonts-dir
1369 (lambda (file stat)
1370 (eq? 'directory (stat:type stat)))
1371 #:directories? #t)))
1372 (for-each (lambda (dir)
1373 (with-directory-excursion dir
1374 (when (file-exists? fonts-scale-file)
1375 (delete-file fonts-scale-file))
1376 (when (file-exists? fonts-dir-file)
1377 (delete-file fonts-dir-file))
1378 (unless (and (zero? (system* mkfontscale))
1379 (zero? (system* mkfontdir)))
1380 (exit #f))
32b7506c
RW
1381 (when (and (file-exists? fonts-scale-file)
1382 (empty-file? fonts-scale-file))
0a5ce0d1 1383 (delete-file fonts-scale-file))
32b7506c
RW
1384 (when (and (file-exists? fonts-dir-file)
1385 (empty-file? fonts-dir-file))
0a5ce0d1
HY
1386 (delete-file fonts-dir-file))))
1387 directories)))))))
9eb5a449
AK
1388
1389 (gexp->derivation "fonts-dir" build
1390 #:modules '((guix build utils)
0a5ce0d1
HY
1391 (guix build union)
1392 (srfi srfi-26))
9eb5a449 1393 #:local-build? #t
80eebee9
RW
1394 #:substitutable? #f
1395 #:properties
1396 `((type . profile-hook)
1397 (hook . fonts-dir))))
9eb5a449 1398
a0b87ef8
MC
1399(define (manual-database manifest)
1400 "Return a derivation that builds the manual page database (\"mandb\") for
1401the entries in MANIFEST."
b8396f96
LC
1402 (define gdbm-ffi
1403 (module-ref (resolve-interface '(gnu packages guile))
1404 'guile-gdbm-ffi))
1405
1406 (define zlib
1407 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
1408
1409 (define config.scm
1410 (scheme-file "config.scm"
1411 #~(begin
88d9eccc 1412 (define-module #$'(guix config) ;placate Geiser
b8396f96
LC
1413 #:export (%libz))
1414
1415 (define %libz
1416 #+(file-append zlib "/lib/libz")))))
1417
1418 (define modules
1419 (cons `((guix config) => ,config.scm)
1420 (delete '(guix config)
1421 (source-module-closure `((guix build utils)
1422 (guix man-db))))))
a0b87ef8
MC
1423
1424 (define build
b8396f96 1425 (with-imported-modules modules
331ac4cc
LC
1426 (with-extensions (list gdbm-ffi) ;for (guix man-db)
1427 #~(begin
1428 (use-modules (guix man-db)
1429 (guix build utils)
ef4b5f2f 1430 (ice-9 threads)
331ac4cc
LC
1431 (srfi srfi-1)
1432 (srfi srfi-19))
1433
ef4b5f2f
AB
1434 (define (print-string msg)
1435 (display msg)
1436 (force-output))
1437
1438 (define-syntax-rule (print fmt args ...)
1439 ;; Build up the string and display it at once.
1440 (print-string (format #f fmt args ...)))
1441
1442 (define (compute-entry directory count total)
1443 (print "\r[~3d/~3d] building list of man-db entries..."
1444 count total)
1445 (let ((man (string-append directory "/share/man")))
1446 (if (directory-exists? man)
1447 (mandb-entries man)
1448 '())))
1449
331ac4cc 1450 (define (compute-entries)
f6fe7da3
LC
1451 ;; This is the most expensive part (I/O and CPU, due to
1452 ;; decompression), so report progress as we traverse INPUTS.
ef4b5f2f
AB
1453 ;; Cap at 4 threads because we don't see any speedup beyond that
1454 ;; on an SSD laptop.
1455 (let* ((inputs '#$(manifest-inputs manifest))
1456 (total (length inputs))
1457 (threads (min (parallel-job-count) 4)))
1458 (concatenate
1459 (n-par-map threads compute-entry inputs
1460 (iota total 1)
1461 (make-list total total)))))
331ac4cc
LC
1462
1463 (define man-directory
1464 (string-append #$output "/share/man"))
1465
1466 (mkdir-p man-directory)
1467
1468 (format #t "Creating manual page database...~%")
1469 (force-output)
1470 (let* ((start (current-time))
1471 (entries (compute-entries))
1472 (_ (write-mandb-database (string-append man-directory
1473 "/index.db")
1474 entries))
1475 (duration (time-difference (current-time) start)))
f6fe7da3 1476 (newline)
331ac4cc
LC
1477 (format #t "~a entries processed in ~,1f s~%"
1478 (length entries)
1479 (+ (time-second duration)
1480 (* (time-nanosecond duration) (expt 10 -9))))
1481 (force-output))))))
a0b87ef8
MC
1482
1483 (gexp->derivation "manual-database" build
b8396f96
LC
1484
1485 ;; Work around GDBM 1.13 issue whereby uninitialized bytes
1486 ;; get written to disk:
1487 ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=29654#23>.
1488 #:env-vars `(("MALLOC_PERTURB_" . "1"))
1489
80eebee9
RW
1490 #:local-build? #t
1491 #:properties
1492 `((type . profile-hook)
1493 (hook . manual-database))))
a0b87ef8 1494
743497b5
RW
1495(define (texlive-configuration manifest)
1496 "Return a derivation that builds a TeXlive configuration for the entries in
1497MANIFEST."
1498 (define entry->texlive-input
1499 (match-lambda
1500 (($ <manifest-entry> name version output thing deps)
1501 (if (string-prefix? "texlive-" name)
1502 (cons (gexp-input thing output)
1503 (append-map entry->texlive-input deps))
1504 '()))))
1505 (define build
1506 (with-imported-modules '((guix build utils)
1507 (guix build union))
1508 #~(begin
1509 (use-modules (guix build utils)
1510 (guix build union))
1511
1512 ;; Build a modifiable union of all texlive inputs. We do this so
1513 ;; that TeX live can resolve the parent and grandparent directories
1514 ;; correctly. There might be a more elegant way to accomplish this.
1515 (union-build #$output
1516 '#$(append-map entry->texlive-input
1517 (manifest-entries manifest))
1518 #:create-all-directories? #t
1519 #:log-port (%make-void-port "w"))
cf22e99f
CB
1520 (let ((texmf.cnf (string-append
1521 #$output
1522 "/share/texmf-dist/web2c/texmf.cnf")))
1523 (when (file-exists? texmf.cnf)
1524 (substitute* texmf.cnf
1525 (("^TEXMFROOT = .*")
1526 (string-append "TEXMFROOT = " #$output "/share\n"))
1527 (("^TEXMF = .*")
1528 "TEXMF = $TEXMFROOT/share/texmf-dist\n"))))
743497b5
RW
1529 #t)))
1530
1531 (with-monad %store-monad
1532 (if (any (cut string-prefix? "texlive-" <>)
1533 (map manifest-entry-name (manifest-entries manifest)))
1534 (gexp->derivation "texlive-configuration" build
1535 #:substitutable? #f
1536 #:local-build? #t
1537 #:properties
1538 `((type . profile-hook)
1539 (hook . texlive-configuration)))
1540 (return #f))))
1541
aa46a028
LC
1542(define %default-profile-hooks
1543 ;; This is the list of derivation-returning procedures that are called by
1544 ;; default when making a non-empty profile.
1545 (list info-dir-file
a0b87ef8 1546 manual-database
9eb5a449 1547 fonts-dir-file
aa46a028 1548 ghc-package-cache-file
b04af0ec 1549 ca-certificate-bundle
de136f3e 1550 glib-schemas
842cb820 1551 gtk-icon-themes
7ddc1780 1552 gtk-im-modules
743497b5 1553 texlive-configuration
6c06b1fd
SB
1554 xdg-desktop-database
1555 xdg-mime-database))
536c3ee4
MW
1556
1557(define* (profile-derivation manifest
1558 #:key
416f7f4f 1559 (name "profile")
e5f04c2d 1560 (hooks %default-profile-hooks)
a6562c7e 1561 (locales? #t)
afd06f60 1562 (allow-collisions? #f)
e00ade3f 1563 (relative-symlinks? #f)
176febe3 1564 system target)
79ee406d 1565 "Return a derivation that builds a profile (aka. 'user environment') with
aa46a028 1566the given MANIFEST. The profile includes additional derivations returned by
a6562c7e 1567the monadic procedures listed in HOOKS--such as an Info 'dir' file, etc.
afd06f60
LC
1568Unless ALLOW-COLLISIONS? is true, a '&profile-collision-error' is raised if
1569entries in MANIFEST collide (for instance if there are two same-name packages
1570with a different version number.)
a6562c7e
LC
1571
1572When LOCALES? is true, the build is performed under a UTF-8 locale; this adds
176febe3
LC
1573a dependency on the 'glibc-utf8-locales' package.
1574
e00ade3f
LC
1575When RELATIVE-SYMLINKS? is true, use relative file names for symlink targets.
1576This is one of the things to do for the result to be relocatable.
1577
176febe3
LC
1578When TARGET is true, it must be a GNU triplet, and the packages in MANIFEST
1579are cross-built for TARGET."
a654dc4b
LC
1580 (mlet* %store-monad ((system (if system
1581 (return system)
1582 (current-system)))
e51de343
MO
1583 (target (if target
1584 (return target)
1585 (current-target-system)))
afd06f60
LC
1586 (ok? (if allow-collisions?
1587 (return #t)
1588 (check-for-collisions manifest system
1589 #:target target)))
a654dc4b
LC
1590 (extras (if (null? (manifest-entries manifest))
1591 (return '())
25af35fa
LC
1592 (mapm/accumulate-builds (lambda (hook)
1593 (hook manifest))
1594 hooks))))
79ee406d 1595 (define inputs
eeae0b3c
SB
1596 (append (filter-map (lambda (drv)
1597 (and (derivation? drv)
1598 (gexp-input drv)))
07eaecfa 1599 extras)
536c3ee4 1600 (manifest-inputs manifest)))
79ee406d 1601
1af0860e
LC
1602 (define glibc-utf8-locales ;lazy reference
1603 (module-ref (resolve-interface '(gnu packages base))
1604 'glibc-utf8-locales))
1605
a6562c7e
LC
1606 (define set-utf8-locale
1607 ;; Some file names (e.g., in 'nss-certs') are UTF-8 encoded so
1608 ;; install a UTF-8 locale.
1609 #~(begin
1610 (setenv "LOCPATH"
1611 #$(file-append glibc-utf8-locales "/lib/locale/"
c6bc8e22
MB
1612 (version-major+minor
1613 (package-version glibc-utf8-locales))))
a6562c7e
LC
1614 (setlocale LC_ALL "en_US.utf8")))
1615
79ee406d 1616 (define builder
99b231de
LC
1617 (with-imported-modules '((guix build profiles)
1618 (guix build union)
1619 (guix build utils)
1620 (guix search-paths)
1621 (guix records))
1622 #~(begin
1623 (use-modules (guix build profiles)
1624 (guix search-paths)
1625 (srfi srfi-1))
1626
1627 (setvbuf (current-output-port) _IOLBF)
1628 (setvbuf (current-error-port) _IOLBF)
1629
a6562c7e 1630 #+(if locales? set-utf8-locale #t)
1af0860e 1631
99b231de
LC
1632 (define search-paths
1633 ;; Search paths of MANIFEST's packages, converted back to their
1634 ;; record form.
1635 (map sexp->search-path-specification
1636 (delete-duplicates
1637 '#$(map search-path-specification->sexp
f03df3ee 1638 (manifest-search-paths manifest)))))
99b231de
LC
1639
1640 (build-profile #$output '#$inputs
e00ade3f
LC
1641 #:symlink #$(if relative-symlinks?
1642 #~symlink-relative
1643 #~symlink)
99b231de
LC
1644 #:manifest '#$(manifest->gexp manifest)
1645 #:search-paths search-paths))))
79ee406d 1646
416f7f4f 1647 (gexp->derivation name builder
40d71e44 1648 #:system system
176febe3 1649 #:target target
a7a4fd9a 1650
cbb76780
LC
1651 ;; Don't complain about _IO* on Guile 2.2.
1652 #:env-vars '(("GUILE_WARN_DEPRECATED" . "no"))
1653
a7a4fd9a
LC
1654 ;; Not worth offloading.
1655 #:local-build? #t
1656
1657 ;; Disable substitution because it would trigger a
1658 ;; connection to the substitute server, which is likely
1659 ;; to have no substitute to offer.
e7570ec2
LC
1660 #:substitutable? #f
1661
1662 #:properties `((type . profile)
1663 (profile
1664 (count
1665 . ,(length
1666 (manifest-entries manifest))))))))
cc4ecc2d 1667
ef674a24
LC
1668;; Declarative profile.
1669(define-record-type* <profile> profile make-profile
1670 profile?
1671 (name profile-name (default "profile")) ;string
1672 (content profile-content) ;<manifest>
1673 (hooks profile-hooks ;list of procedures
1674 (default %default-profile-hooks))
1675 (locales? profile-locales? ;Boolean
1676 (default #t))
1677 (allow-collisions? profile-allow-collisions? ;Boolean
1678 (default #f))
1679 (relative-symlinks? profile-relative-symlinks? ;Boolean
1680 (default #f)))
1681
1682(define-gexp-compiler (profile-compiler (profile <profile>) system target)
1683 "Compile PROFILE to a derivation."
1684 (match profile
1685 (($ <profile> name manifest hooks
1686 locales? allow-collisions? relative-symlinks?)
1687 (profile-derivation manifest
1688 #:name name
1689 #:hooks hooks
1690 #:locales? locales?
1691 #:allow-collisions? allow-collisions?
1692 #:relative-symlinks? relative-symlinks?
1693 #:system system #:target target))))
1694
78d55b70
LC
1695(define* (profile-search-paths profile
1696 #:optional (manifest (profile-manifest profile))
1697 #:key (getenv (const #f)))
1698 "Read the manifest of PROFILE and evaluate the values of search path
1699environment variables required by PROFILE; return a list of
1700specification/value pairs. If MANIFEST is not #f, it is assumed to be the
1701manifest of PROFILE, which avoids rereading it.
1702
1703Use GETENV to determine the current settings and report only settings not
1704already effective."
1705 (evaluate-search-paths (manifest-search-paths manifest)
1706 (list profile) getenv))
1707
cc4ecc2d
LC
1708(define (profile-regexp profile)
1709 "Return a regular expression that matches PROFILE's name and number."
1710 (make-regexp (string-append "^" (regexp-quote (basename profile))
1711 "-([0-9]+)")))
1712
1713(define (generation-number profile)
1714 "Return PROFILE's number or 0. An absolute file name must be used."
1715 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
1716 (basename (readlink profile))))
1717 (compose string->number (cut match:substring <> 1)))
1718 0))
1719
c872b952
LC
1720(define %profile-generation-rx
1721 ;; Regexp that matches profile generation.
1722 (make-regexp "(.*)-([0-9]+)-link$"))
1723
1724(define (generation-profile file)
1725 "If FILE is a profile generation GC root such as \"guix-profile-42-link\",
1726return its corresponding profile---e.g., \"guix-profile\". Otherwise return
1727#f."
1728 (match (regexp-exec %profile-generation-rx file)
1729 (#f #f)
1730 (m (let ((profile (match:substring m 1)))
1731 (and (file-exists? (string-append profile "/manifest"))
1732 profile)))))
1733
cc4ecc2d
LC
1734(define (generation-numbers profile)
1735 "Return the sorted list of generation numbers of PROFILE, or '(0) if no
1736former profiles were found."
cc4ecc2d
LC
1737 (match (scandir (dirname profile)
1738 (cute regexp-exec (profile-regexp profile) <>))
1739 (#f ; no profile directory
1740 '(0))
1741 (() ; no profiles
1742 '(0))
1743 ((profiles ...) ; former profiles around
1744 (sort (map (compose string->number
1745 (cut match:substring <> 1)
1746 (cute regexp-exec (profile-regexp profile) <>))
1747 profiles)
1748 <))))
1749
f452e8ff
AK
1750(define (profile-generations profile)
1751 "Return a list of PROFILE's generations."
1752 (let ((generations (generation-numbers profile)))
1753 (if (equal? generations '(0))
1754 '()
1755 generations)))
1756
9008debc
CM
1757(define (relative-generation-spec->number profile spec)
1758 "Return PROFILE's generation specified by SPEC, which is a string. The SPEC
1759may be a N, -N, or +N, where N is a number. If the spec is N, then the number
1760returned is N. If it is -N, then the number returned is the profile's current
1761generation number minus N. If it is +N, then the number returned is the
1762profile's current generation number plus N. Return #f if there is no such
1763generation."
1764 (let ((number (string->number spec)))
1765 (and number
1766 (case (string-ref spec 0)
1767 ((#\+ #\-)
1768 (relative-generation profile number))
1769 (else (if (memv number (profile-generations profile))
1770 number
1771 #f))))))
1772
1773
3ccde087
AK
1774(define* (relative-generation profile shift #:optional
1775 (current (generation-number profile)))
1776 "Return PROFILE's generation shifted from the CURRENT generation by SHIFT.
1777SHIFT is a positive or negative number.
1778Return #f if there is no such generation."
1779 (let* ((abs-shift (abs shift))
1780 (numbers (profile-generations profile))
1781 (from-current (memq current
1782 (if (negative? shift)
1783 (reverse numbers)
1784 numbers))))
1785 (and from-current
1786 (< abs-shift (length from-current))
1787 (list-ref from-current abs-shift))))
1788
1789(define* (previous-generation-number profile #:optional
1790 (number (generation-number profile)))
cc4ecc2d
LC
1791 "Return the number of the generation before generation NUMBER of
1792PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
1793case when generations have been deleted (there are \"holes\")."
3ccde087
AK
1794 (or (relative-generation profile -1 number)
1795 0))
cc4ecc2d
LC
1796
1797(define (generation-file-name profile generation)
1798 "Return the file name for PROFILE's GENERATION."
1799 (format #f "~a-~a-link" profile generation))
1800
1801(define (generation-time profile number)
1802 "Return the creation time of a generation in the UTC format."
1803 (make-time time-utc 0
1804 (stat:ctime (stat (generation-file-name profile number)))))
1805
06d45f45
LC
1806(define (link-to-empty-profile store generation)
1807 "Link GENERATION, a string, to the empty profile. An error is raised if
1808that fails."
1809 (let* ((drv (run-with-store store
a6562c7e
LC
1810 (profile-derivation (manifest '())
1811 #:locales? #f)))
06d45f45
LC
1812 (prof (derivation->output-path drv "out")))
1813 (build-derivations store (list drv))
1814 (switch-symlinks generation prof)))
1815
1816(define (switch-to-generation profile number)
1817 "Atomically switch PROFILE to the generation NUMBER. Return the number of
1818the generation that was current before switching."
1819 (let ((current (generation-number profile))
1820 (generation (generation-file-name profile number)))
1821 (cond ((not (file-exists? profile))
1822 (raise (condition (&profile-not-found-error
1823 (profile profile)))))
1824 ((not (file-exists? generation))
1825 (raise (condition (&missing-generation-error
1826 (profile profile)
1827 (generation number)))))
1828 (else
bc6e291e 1829 (switch-symlinks profile (basename generation))
06d45f45
LC
1830 current))))
1831
1832(define (switch-to-previous-generation profile)
1833 "Atomically switch PROFILE to the previous generation. Return the former
1834generation number and the current one."
1835 (let ((previous (previous-generation-number profile)))
1836 (values (switch-to-generation profile previous)
1837 previous)))
1838
1839(define (roll-back store profile)
1840 "Roll back to the previous generation of PROFILE. Return the number of the
1841generation that was current before switching and the new generation number."
1842 (let* ((number (generation-number profile))
1843 (previous-number (previous-generation-number profile number))
1844 (previous-generation (generation-file-name profile previous-number)))
1845 (cond ((not (file-exists? profile)) ;invalid profile
1846 (raise (condition (&profile-not-found-error
1847 (profile profile)))))
1848 ((zero? number) ;empty profile
1849 (values number number))
1850 ((or (zero? previous-number) ;going to emptiness
1851 (not (file-exists? previous-generation)))
1852 (link-to-empty-profile store previous-generation)
1853 (switch-to-previous-generation profile))
1854 (else ;anything else
1855 (switch-to-previous-generation profile)))))
1856
1857(define (delete-generation store profile number)
1858 "Delete generation with NUMBER from PROFILE. Return the file name of the
1859generation that has been deleted, or #f if nothing was done (for instance
1860because the NUMBER is zero.)"
1861 (define (delete-and-return)
1862 (let ((generation (generation-file-name profile number)))
1863 (delete-file generation)
1864 generation))
1865
1866 (let* ((current-number (generation-number profile))
1867 (previous-number (previous-generation-number profile number))
1868 (previous-generation (generation-file-name profile previous-number)))
1869 (cond ((zero? number) #f) ;do not delete generation 0
1870 ((and (= number current-number)
1871 (not (file-exists? previous-generation)))
1872 (link-to-empty-profile store previous-generation)
1873 (switch-to-previous-generation profile)
1874 (delete-and-return))
1875 ((= number current-number)
1876 (roll-back store profile)
1877 (delete-and-return))
1878 (else
1879 (delete-and-return)))))
1880
efcb4441
LC
1881(define %user-profile-directory
1882 (and=> (getenv "HOME")
1883 (cut string-append <> "/.guix-profile")))
1884
1885(define %profile-directory
1886 (string-append %state-directory "/profiles/"
1887 (or (and=> (or (getenv "USER")
c20ba183
LC
1888 (getenv "LOGNAME")
1889 (false-if-exception
1890 (passwd:name (getpwuid (getuid)))))
efcb4441
LC
1891 (cut string-append "per-user/" <>))
1892 "default")))
1893
1894(define %current-profile
1895 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
1896 ;; coexist with Nix profiles.
1897 (string-append %profile-directory "/guix-profile"))
1898
77dcfb4c 1899(define (ensure-profile-directory)
81c580c8
LC
1900 "Attempt to create /…/profiles/per-user/$USER if needed. Nowadays this is
1901taken care of by the daemon."
77dcfb4c
LC
1902 (let ((s (stat %profile-directory #f)))
1903 (unless (and s (eq? 'directory (stat:type s)))
1904 (catch 'system-error
1905 (lambda ()
1906 (mkdir-p %profile-directory))
1907 (lambda args
1908 ;; Often, we cannot create %PROFILE-DIRECTORY because its
1909 ;; parent directory is root-owned and we're running
1910 ;; unprivileged.
1911 (raise (condition
1912 (&message
1913 (message
1914 (format #f
1915 (G_ "while creating directory `~a': ~a")
1916 %profile-directory
1917 (strerror (system-error-errno args)))))
1918 (&fix-hint
1919 (hint
1920 (format #f (G_ "Please create the @file{~a} directory, \
1921with you as the owner.")
1922 %profile-directory))))))))
1923
1924 ;; Bail out if it's not owned by the user.
1925 (unless (or (not s) (= (stat:uid s) (getuid)))
1926 (raise (condition
1927 (&message
1928 (message
1929 (format #f (G_ "directory `~a' is not owned by you")
1930 %profile-directory)))
1931 (&fix-hint
1932 (hint
1933 (format #f (G_ "Please change the owner of @file{~a} \
1934to user ~s.")
1935 %profile-directory (or (getenv "USER")
1936 (getenv "LOGNAME")
1937 (getuid))))))))))
1938
efcb4441 1939(define (canonicalize-profile profile)
50c72ecd
LC
1940 "If PROFILE points to a profile in %PROFILE-DIRECTORY, return that.
1941Otherwise return PROFILE unchanged. The goal is to treat '-p ~/.guix-profile'
1942as if '-p' was omitted." ; see <http://bugs.gnu.org/17939>
1943 ;; Trim trailing slashes so 'readlink' can do its job.
efcb4441 1944 (let ((profile (string-trim-right profile #\/)))
50c72ecd
LC
1945 (catch 'system-error
1946 (lambda ()
1947 (let ((target (readlink profile)))
1948 (if (string=? (dirname target) %profile-directory)
1949 target
1950 profile)))
1951 (const profile))))
efcb4441 1952
1c795c4f
LC
1953(define %known-shorthand-profiles
1954 ;; Known shorthand forms for profiles that the user manipulates.
1955 (list (string-append (config-directory #:ensure? #f) "/current")
1956 %user-profile-directory))
1957
efcb4441 1958(define (user-friendly-profile profile)
1c795c4f
LC
1959 "Return either ~/.guix-profile or ~/.config/guix/current if that's what
1960PROFILE refers to, directly or indirectly, or PROFILE."
1961 (or (find (lambda (shorthand)
1962 (and shorthand
1963 (let ((target (false-if-exception
1964 (readlink shorthand))))
1965 (and target (string=? target profile)))))
1966 %known-shorthand-profiles)
efcb4441
LC
1967 profile))
1968
cc4ecc2d 1969;;; profiles.scm ends here