channels: 'latest-channel-instances' guards against non-forward updates.
[jackhill/guix/guix.git] / guix / channels.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
4 ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (guix channels)
22 #:use-module (git)
23 #:use-module (guix git)
24 #:use-module (guix records)
25 #:use-module (guix gexp)
26 #:use-module (guix modules)
27 #:use-module (guix discovery)
28 #:use-module (guix monads)
29 #:use-module (guix profiles)
30 #:use-module (guix packages)
31 #:use-module (guix derivations)
32 #:use-module (guix combinators)
33 #:use-module (guix diagnostics)
34 #:use-module (guix sets)
35 #:use-module (guix store)
36 #:use-module (guix i18n)
37 #:use-module ((guix utils)
38 #:select (source-properties->location
39 &error-location
40 &fix-hint))
41 #:use-module (srfi srfi-1)
42 #:use-module (srfi srfi-2)
43 #:use-module (srfi srfi-9)
44 #:use-module (srfi srfi-11)
45 #:use-module (srfi srfi-26)
46 #:use-module (srfi srfi-34)
47 #:use-module (srfi srfi-35)
48 #:autoload (guix self) (whole-package make-config.scm)
49 #:autoload (guix inferior) (gexp->derivation-in-inferior) ;FIXME: circular dep
50 #:autoload (guix quirks) (%quirks %patches applicable-patch? apply-patch)
51 #:use-module (ice-9 match)
52 #:use-module (ice-9 vlist)
53 #:use-module ((ice-9 rdelim) #:select (read-string))
54 #:export (channel
55 channel?
56 channel-name
57 channel-url
58 channel-branch
59 channel-commit
60 channel-location
61
62 %default-channels
63 guix-channel?
64
65 channel-instance?
66 channel-instance-channel
67 channel-instance-commit
68 channel-instance-checkout
69
70 latest-channel-instances
71 checkout->channel-instance
72 latest-channel-derivation
73 channel-instances->manifest
74 %channel-profile-hooks
75 channel-instances->derivation
76 ensure-forward-channel-update
77
78 profile-channels
79
80 channel-news-entry?
81 channel-news-entry-commit
82 channel-news-entry-tag
83 channel-news-entry-title
84 channel-news-entry-body
85
86 channel-news-for-commit))
87
88 ;;; Commentary:
89 ;;;
90 ;;; This module implements "channels." A channel is usually a source of
91 ;;; package definitions. There's a special channel, the 'guix' channel, that
92 ;;; provides all of Guix, including its commands and its documentation.
93 ;;; User-defined channels are expected to typically provide a bunch of .scm
94 ;;; files meant to be added to the '%package-search-path'.
95 ;;;
96 ;;; This module provides tools to fetch and update channels from a Git
97 ;;; repository and to build them.
98 ;;;
99 ;;; Code:
100
101 (define-record-type* <channel> channel make-channel
102 channel?
103 (name channel-name)
104 (url channel-url)
105 (branch channel-branch (default "master"))
106 (commit channel-commit (default #f))
107 (location channel-location
108 (default (current-source-location)) (innate)))
109
110 (define %default-channels
111 ;; Default list of channels.
112 (list (channel
113 (name 'guix)
114 (branch "master")
115 (url "https://git.savannah.gnu.org/git/guix.git"))))
116
117 (define (guix-channel? channel)
118 "Return true if CHANNEL is the 'guix' channel."
119 (eq? 'guix (channel-name channel)))
120
121 (define-record-type <channel-instance>
122 (channel-instance channel commit checkout)
123 channel-instance?
124 (channel channel-instance-channel)
125 (commit channel-instance-commit)
126 (checkout channel-instance-checkout))
127
128 (define-record-type <channel-metadata>
129 (channel-metadata directory dependencies news-file)
130 channel-metadata?
131 (directory channel-metadata-directory) ;string with leading slash
132 (dependencies channel-metadata-dependencies) ;list of <channel>
133 (news-file channel-metadata-news-file)) ;string | #f
134
135 (define (channel-reference channel)
136 "Return the \"reference\" for CHANNEL, an sexp suitable for
137 'latest-repository-commit'."
138 (match (channel-commit channel)
139 (#f `(branch . ,(channel-branch channel)))
140 (commit `(commit . ,(channel-commit channel)))))
141
142 (define (read-channel-metadata port)
143 "Read from PORT channel metadata in the format expected for the
144 '.guix-channel' file. Return a <channel-metadata> record, or raise an error
145 if valid metadata could not be read from PORT."
146 (match (read port)
147 (('channel ('version 0) properties ...)
148 (let ((directory (and=> (assoc-ref properties 'directory) first))
149 (dependencies (or (assoc-ref properties 'dependencies) '()))
150 (news-file (and=> (assoc-ref properties 'news-file) first)))
151 (channel-metadata
152 (cond ((not directory) "/") ;directory
153 ((string-prefix? "/" directory) directory)
154 (else (string-append "/" directory)))
155 (map (lambda (item) ;dependencies
156 (let ((get (lambda* (key #:optional default)
157 (or (and=> (assoc-ref item key) first) default))))
158 (and-let* ((name (get 'name))
159 (url (get 'url))
160 (branch (get 'branch "master")))
161 (channel
162 (name name)
163 (branch branch)
164 (url url)
165 (commit (get 'commit))))))
166 dependencies)
167 news-file))) ;news-file
168 ((and ('channel ('version version) _ ...) sexp)
169 (raise (condition
170 (&message (message "unsupported '.guix-channel' version"))
171 (&error-location
172 (location (source-properties->location
173 (source-properties sexp)))))))
174 (sexp
175 (raise (condition
176 (&message (message "invalid '.guix-channel' file"))
177 (&error-location
178 (location (source-properties->location
179 (source-properties sexp)))))))))
180
181 (define (read-channel-metadata-from-source source)
182 "Return a channel-metadata record read from channel's SOURCE/.guix-channel
183 description file, or return the default channel-metadata record if that file
184 doesn't exist."
185 (catch 'system-error
186 (lambda ()
187 (call-with-input-file (string-append source "/.guix-channel")
188 read-channel-metadata))
189 (lambda args
190 (if (= ENOENT (system-error-errno args))
191 (channel-metadata "/" '() #f)
192 (apply throw args)))))
193
194 (define (channel-instance-metadata instance)
195 "Return a channel-metadata record read from the channel INSTANCE's
196 description file or its default value."
197 (read-channel-metadata-from-source (channel-instance-checkout instance)))
198
199 (define (channel-instance-dependencies instance)
200 "Return the list of channels that are declared as dependencies for the given
201 channel INSTANCE."
202 (channel-metadata-dependencies (channel-instance-metadata instance)))
203
204 (define (apply-patches checkout commit patches)
205 "Apply the matching PATCHES to CHECKOUT, modifying files in place. The
206 result is unspecified."
207 (let loop ((patches patches))
208 (match patches
209 (() #t)
210 ((patch rest ...)
211 (when (applicable-patch? patch checkout commit)
212 (apply-patch patch checkout))
213 (loop rest)))))
214
215 (define* (latest-channel-instance store channel
216 #:key (patches %patches)
217 starting-commit)
218 "Return two values: the latest channel instance for CHANNEL, and its
219 relation to STARTING-COMMIT when provided."
220 (define (dot-git? file stat)
221 (and (string=? (basename file) ".git")
222 (eq? 'directory (stat:type stat))))
223
224 (let-values (((checkout commit relation)
225 (update-cached-checkout (channel-url channel)
226 #:ref (channel-reference channel)
227 #:starting-commit starting-commit)))
228 (when (guix-channel? channel)
229 ;; Apply the relevant subset of PATCHES directly in CHECKOUT. This is
230 ;; safe to do because 'switch-to-ref' eventually does a hard reset.
231 (apply-patches checkout commit patches))
232
233 (let* ((name (url+commit->name (channel-url channel) commit))
234 (checkout (add-to-store store name #t "sha256" checkout
235 #:select? (negate dot-git?))))
236 (values (channel-instance channel commit checkout)
237 relation))))
238
239 (define (ensure-forward-channel-update channel start instance relation)
240 "Raise an error if RELATION is not 'ancestor, meaning that START is not an
241 ancestor of the commit in INSTANCE, unless CHANNEL specifies a commit.
242
243 This procedure implements a channel update policy meant to be used as a
244 #:validate-pull argument."
245 (match relation
246 ('ancestor #t)
247 ('self #t)
248 (_
249 (raise (apply make-compound-condition
250 (condition
251 (&message (message
252 (format #f (G_ "\
253 aborting update of channel '~a' to commit ~a, which is not a descendant of ~a")
254 (channel-name channel)
255 (channel-instance-commit instance)
256 start))))
257
258 ;; Don't show the hint when the user explicitly specified a
259 ;; commit in CHANNEL.
260 (if (channel-commit channel)
261 '()
262 (list (condition
263 (&fix-hint
264 (hint (G_ "This could indicate that the channel has
265 been tampered with and is trying to force a roll-back, preventing you from
266 getting the latest updates. If you think this is not the case, explicitly
267 allow non-forward updates.")))))))))))
268
269 (define* (latest-channel-instances store channels
270 #:key
271 (current-channels '())
272 (validate-pull
273 ensure-forward-channel-update))
274 "Return a list of channel instances corresponding to the latest checkouts of
275 CHANNELS and the channels on which they depend.
276
277 CURRENT-CHANNELS is the list of currently used channels. It is compared
278 against the newly-fetched instances of CHANNELS, and VALIDATE-PULL is called
279 for each channel update and can choose to emit warnings or raise an error,
280 depending on the policy it implements."
281 ;; Only process channels that are unique, or that are more specific than a
282 ;; previous channel specification.
283 (define (ignore? channel others)
284 (member channel others
285 (lambda (a b)
286 (and (eq? (channel-name a) (channel-name b))
287 (or (channel-commit b)
288 (not (or (channel-commit a)
289 (channel-commit b))))))))
290
291 (define (current-commit name)
292 ;; Return the current commit for channel NAME.
293 (any (lambda (channel)
294 (and (eq? (channel-name channel) name)
295 (channel-commit channel)))
296 current-channels))
297
298 (let loop ((channels channels)
299 (previous-channels '()))
300 ;; Accumulate a list of instances. A list of processed channels is also
301 ;; accumulated to decide on duplicate channel specifications.
302 (define-values (resulting-channels instances)
303 (fold2 (lambda (channel previous-channels instances)
304 (if (ignore? channel previous-channels)
305 (values previous-channels instances)
306 (begin
307 (format (current-error-port)
308 (G_ "Updating channel '~a' from Git repository at '~a'...~%")
309 (channel-name channel)
310 (channel-url channel))
311 (let*-values (((current)
312 (current-commit (channel-name channel)))
313 ((instance relation)
314 (latest-channel-instance store channel
315 #:starting-commit
316 current)))
317 (when relation
318 (validate-pull channel current instance relation))
319
320 (let-values (((new-instances new-channels)
321 (loop (channel-instance-dependencies instance)
322 previous-channels)))
323 (values (append (cons channel new-channels)
324 previous-channels)
325 (append (cons instance new-instances)
326 instances)))))))
327 previous-channels
328 '() ;instances
329 channels))
330
331 (let ((instance-name (compose channel-name channel-instance-channel)))
332 ;; Remove all earlier channel specifications if they are followed by a
333 ;; more specific one.
334 (values (delete-duplicates instances
335 (lambda (a b)
336 (eq? (instance-name a) (instance-name b))))
337 resulting-channels))))
338
339 (define* (checkout->channel-instance checkout
340 #:key commit
341 (url checkout) (name 'guix))
342 "Return a channel instance for CHECKOUT, which is assumed to be a checkout
343 of COMMIT at URL. Use NAME as the channel name."
344 (let* ((commit (or commit (make-string 40 #\0)))
345 (channel (channel (name name)
346 (commit commit)
347 (url url))))
348 (channel-instance channel commit checkout)))
349
350 (define %self-build-file
351 ;; The file containing code to build Guix. This serves the same purpose as
352 ;; a makefile, and, similarly, is intended to always keep this name.
353 "build-aux/build-self.scm")
354
355 (define %pull-version
356 ;; This is the version of the 'guix pull' protocol. It specifies what's
357 ;; expected from %SELF-BUILD-FILE. The initial version ("0") was when we'd
358 ;; place a set of compiled Guile modules in ~/.config/guix/latest.
359 1)
360
361 (define (standard-module-derivation name source core dependencies)
362 "Return a derivation that builds with CORE, a Guix instance, the Scheme
363 modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
364 objects. The assumption is that SOURCE contains package modules to be added
365 to '%package-module-path'."
366
367 (let* ((metadata (read-channel-metadata-from-source source))
368 (directory (channel-metadata-directory metadata)))
369
370 (define build
371 ;; This is code that we'll run in CORE, a Guix instance, with its own
372 ;; modules and so on. That way, we make sure these modules are built for
373 ;; the right Guile version, with the right dependencies, and that they get
374 ;; to see the right (gnu packages …) modules.
375 (with-extensions dependencies
376 #~(begin
377 (use-modules (guix build compile)
378 (guix build utils)
379 (srfi srfi-26))
380
381 (define go
382 (string-append #$output "/lib/guile/" (effective-version)
383 "/site-ccache"))
384 (define scm
385 (string-append #$output "/share/guile/site/"
386 (effective-version)))
387
388 (let* ((subdir #$directory)
389 (source (string-append #$source subdir)))
390 (compile-files source go (find-files source "\\.scm$"))
391 (mkdir-p (dirname scm))
392 (symlink (string-append #$source subdir) scm))
393
394 scm)))
395
396 (gexp->derivation-in-inferior name build core)))
397
398 (define* (guile-for-source source #:optional (quirks %quirks))
399 "Return the Guile package to use when building SOURCE or #f if the default
400 '%guile-for-build' should be good enough."
401 (let loop ((quirks quirks))
402 (match quirks
403 (()
404 #f)
405 (((predicate . guile) rest ...)
406 (if (predicate source) (guile) (loop rest))))))
407
408 (define (call-with-guile guile thunk)
409 (lambda (store)
410 (values (parameterize ((%guile-for-build
411 (if guile
412 (package-derivation store guile)
413 (%guile-for-build))))
414 (run-with-store store (thunk)))
415 store)))
416
417 (define-syntax-rule (with-guile guile exp ...)
418 "Set GUILE as the '%guile-for-build' parameter for the dynamic extent of
419 EXP, a series of monadic expressions."
420 (call-with-guile guile (lambda ()
421 (mbegin %store-monad exp ...))))
422
423 (define (with-trivial-build-handler mvalue)
424 "Run MVALUE, a monadic value, with a \"trivial\" build handler installed
425 that unconditionally resumes the continuation."
426 (lambda (store)
427 (with-build-handler (lambda (continue . _)
428 (continue #t))
429 (values (run-with-store store mvalue)
430 store))))
431
432 (define* (build-from-source name source
433 #:key core verbose? commit
434 (dependencies '()))
435 "Return a derivation to build Guix from SOURCE, using the self-build script
436 contained therein; use COMMIT as the version string. When CORE is true, build
437 package modules under SOURCE using CORE, an instance of Guix."
438 ;; Running the self-build script makes it easier to update the build
439 ;; procedure: the self-build script of the Guix-to-be-installed contains the
440 ;; right dependencies, build procedure, etc., which the Guix-in-use may not
441 ;; be know.
442 (define script
443 (string-append source "/" %self-build-file))
444
445 (if (file-exists? script)
446 (let ((build (save-module-excursion
447 (lambda ()
448 ;; Disable deprecation warnings; it's OK for SCRIPT to
449 ;; use deprecated APIs and the user doesn't have to know
450 ;; about it.
451 (parameterize ((guix-warning-port
452 (%make-void-port "w")))
453 (primitive-load script)))))
454 (guile (guile-for-source source)))
455 ;; BUILD must be a monadic procedure of at least one argument: the
456 ;; source tree.
457 ;;
458 ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
459 ;; the future we'll fall back to a previous version of the protocol
460 ;; when that happens.
461 (with-guile guile
462 ;; BUILD is usually quite costly. Install a "trivial" build handler
463 ;; so we don't bounce an outer build-accumulator handler that could
464 ;; cause us to redo half of the BUILD computation several times just
465 ;; to realize it gives the same result.
466 (with-trivial-build-handler
467 (build source #:verbose? verbose? #:version commit
468 #:pull-version %pull-version))))
469
470 ;; Build a set of modules that extend Guix using the standard method.
471 (standard-module-derivation name source core dependencies)))
472
473 (define* (build-channel-instance instance
474 #:optional core (dependencies '()))
475 "Return, as a monadic value, the derivation for INSTANCE, a channel
476 instance. DEPENDENCIES is a list of extensions providing Guile modules that
477 INSTANCE depends on."
478 (build-from-source (symbol->string
479 (channel-name (channel-instance-channel instance)))
480 (channel-instance-checkout instance)
481 #:commit (channel-instance-commit instance)
482 #:core core
483 #:dependencies dependencies))
484
485 (define (resolve-dependencies instances)
486 "Return a procedure that, given one of the elements of INSTANCES, returns
487 list of instances it depends on."
488 (define channel-instance-name
489 (compose channel-name channel-instance-channel))
490
491 (define table ;map a name to an instance
492 (fold (lambda (instance table)
493 (vhash-consq (channel-instance-name instance)
494 instance table))
495 vlist-null
496 instances))
497
498 (define edges
499 (fold (lambda (instance edges)
500 (fold (lambda (channel edges)
501 (let ((name (channel-name channel)))
502 (match (vhash-assq name table)
503 ((_ . target)
504 (vhash-consq instance target edges)))))
505 edges
506 (channel-instance-dependencies instance)))
507 vlist-null
508 instances))
509
510 (lambda (instance)
511 (vhash-foldq* cons '() instance edges)))
512
513 (define (channel-instance-derivations instances)
514 "Return the list of derivations to build INSTANCES, in the same order as
515 INSTANCES."
516 (define core-instance
517 ;; The 'guix' channel is treated specially: it's an implicit dependency of
518 ;; all the other channels.
519 (find (lambda (instance)
520 (guix-channel? (channel-instance-channel instance)))
521 instances))
522
523 (define edges
524 (resolve-dependencies instances))
525
526 (define (instance->derivation instance)
527 (mlet %store-monad ((system (current-system)))
528 (mcached (if (eq? instance core-instance)
529 (build-channel-instance instance)
530 (mlet %store-monad ((core (instance->derivation core-instance))
531 (deps (mapm %store-monad instance->derivation
532 (edges instance))))
533 (build-channel-instance instance core deps)))
534 instance
535 system)))
536
537 (unless core-instance
538 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
539 instances)
540 source-properties->location)))
541 (raise (apply make-compound-condition
542 (condition
543 (&message (message "'guix' channel is lacking")))
544 (condition
545 (&fix-hint (hint (G_ "Make sure your list of channels
546 contains one channel named @code{guix} providing the core of Guix."))))
547 (if loc
548 (list (condition (&error-location (location loc))))
549 '())))))
550
551 (mapm %store-monad instance->derivation instances))
552
553 (define (whole-package-for-legacy name modules)
554 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
555 modules in the old ~/.config/guix/latest style."
556 (define packages
557 (resolve-interface '(gnu packages guile)))
558
559 (define modules+compiled
560 ;; Since MODULES contains both .scm and .go files at its root, re-bundle
561 ;; it so that it has share/guile/site and lib/guile, which is what
562 ;; 'whole-package' expects.
563 (computed-file (derivation-name modules)
564 (with-imported-modules '((guix build utils))
565 #~(begin
566 (use-modules (guix build utils))
567
568 (define version
569 (effective-version))
570 (define share
571 (string-append #$output "/share/guile/site"))
572 (define lib
573 (string-append #$output "/lib/guile/" version))
574
575 (mkdir-p share) (mkdir-p lib)
576 (symlink #$modules (string-append share "/" version))
577 (symlink #$modules (string-append lib "/site-ccache"))))))
578
579 (letrec-syntax ((list (syntax-rules (->)
580 ((_)
581 '())
582 ((_ (module -> variable) rest ...)
583 (cons (module-ref (resolve-interface
584 '(gnu packages module))
585 'variable)
586 (list rest ...)))
587 ((_ variable rest ...)
588 (cons (module-ref packages 'variable)
589 (list rest ...))))))
590 (whole-package name modules+compiled
591
592 ;; In the "old style", %SELF-BUILD-FILE would simply return a
593 ;; derivation that builds modules. We have to infer what the
594 ;; dependencies of these modules were.
595 (list guile-json-3 guile-git guile-bytestructures
596 (ssh -> guile-ssh) (tls -> gnutls)))))
597
598 (define (old-style-guix? drv)
599 "Return true if DRV corresponds to a ~/.config/guix/latest style of
600 derivation."
601 ;; Here we rely on a gross historical fact: that derivations produced by the
602 ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
603 ;; dated May 30, 2018) did not depend on "guix-command.drv".
604 (not (find (lambda (input)
605 (string=? "guix-command"
606 (derivation-name
607 (derivation-input-derivation input))))
608 (derivation-inputs drv))))
609
610 (define (channel-instances->manifest instances)
611 "Return a profile manifest with entries for all of INSTANCES, a list of
612 channel instances."
613 (define (instance->entry instance drv)
614 (let ((commit (channel-instance-commit instance))
615 (channel (channel-instance-channel instance)))
616 (manifest-entry
617 (name (symbol->string (channel-name channel)))
618 (version (string-take commit 7))
619 (item (if (guix-channel? channel)
620 (if (old-style-guix? drv)
621 (whole-package-for-legacy (string-append name "-" version)
622 drv)
623 drv)
624 drv))
625 (properties
626 `((source (repository
627 (version 0)
628 (url ,(channel-url channel))
629 (branch ,(channel-branch channel))
630 (commit ,commit))))))))
631
632 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
633 (entries -> (map instance->entry instances derivations)))
634 (return (manifest entries))))
635
636 (define (package-cache-file manifest)
637 "Build a package cache file for the instance in MANIFEST. This is meant to
638 be used as a profile hook."
639 (let ((profile (profile (content manifest) (hooks '()))))
640 (define build
641 #~(begin
642 (use-modules (gnu packages))
643
644 (if (defined? 'generate-package-cache)
645 (begin
646 ;; Delegate package cache generation to the inferior.
647 (format (current-error-port)
648 "Generating package cache for '~a'...~%"
649 #$profile)
650 (generate-package-cache #$output))
651 (mkdir #$output))))
652
653 (gexp->derivation-in-inferior "guix-package-cache" build
654 profile
655
656 ;; If the Guix in PROFILE is too old and
657 ;; lacks 'guix repl', don't build the cache
658 ;; instead of failing.
659 #:silent-failure? #t
660
661 #:properties '((type . profile-hook)
662 (hook . package-cache))
663 #:local-build? #t)))
664
665 (define %channel-profile-hooks
666 ;; The default channel profile hooks.
667 (cons package-cache-file %default-profile-hooks))
668
669 (define (channel-instances->derivation instances)
670 "Return the derivation of the profile containing INSTANCES, a list of
671 channel instances."
672 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
673 (profile-derivation manifest
674 #:hooks %channel-profile-hooks)))
675
676 (define latest-channel-instances*
677 (store-lift latest-channel-instances))
678
679 (define* (latest-channel-derivation #:optional (channels %default-channels)
680 #:key
681 (current-channels '())
682 (validate-pull
683 ensure-forward-channel-update))
684 "Return as a monadic value the derivation that builds the profile for the
685 latest instances of CHANNELS. CURRENT-CHANNELS and VALIDATE-PULL are passed
686 to 'latest-channel-instances'."
687 (mlet %store-monad ((instances
688 (latest-channel-instances* channels
689 #:current-channels
690 current-channels
691 #:validate-pull
692 validate-pull)))
693 (channel-instances->derivation instances)))
694
695 (define (profile-channels profile)
696 "Return the list of channels corresponding to entries in PROFILE. If
697 PROFILE is not a profile created by 'guix pull', return the empty list."
698 (filter-map (lambda (entry)
699 (match (assq 'source (manifest-entry-properties entry))
700 (('source ('repository ('version 0)
701 ('url url)
702 ('branch branch)
703 ('commit commit)
704 _ ...))
705 (channel (name (string->symbol
706 (manifest-entry-name entry)))
707 (url url)
708 (commit commit)))
709
710 ;; No channel information for this manifest entry.
711 ;; XXX: Pre-0.15.0 Guix did not provide that information,
712 ;; but there's not much we can do in that case.
713 (_ #f)))
714
715 ;; Show most recently installed packages last.
716 (reverse
717 (manifest-entries (profile-manifest profile)))))
718
719 \f
720 ;;;
721 ;;; News.
722 ;;;
723
724 ;; Channel news.
725 (define-record-type <channel-news>
726 (channel-news entries)
727 channel-news?
728 (entries channel-news-entries)) ;list of <channel-news-entry>
729
730 ;; News entry, associated with a specific commit of the channel.
731 (define-record-type <channel-news-entry>
732 (channel-news-entry commit tag title body)
733 channel-news-entry?
734 (commit channel-news-entry-commit) ;hex string | #f
735 (tag channel-news-entry-tag) ;#f | string
736 (title channel-news-entry-title) ;list of language tag/string pairs
737 (body channel-news-entry-body)) ;list of language tag/string pairs
738
739 (define (sexp->channel-news-entry entry)
740 "Return the <channel-news-entry> record corresponding to ENTRY, an sexp."
741 (define (pair language message)
742 (cons (symbol->string language) message))
743
744 (match entry
745 (('entry ((and (or 'commit 'tag) type) commit-or-tag)
746 ('title ((? symbol? title-tags) (? string? titles)) ...)
747 ('body ((? symbol? body-tags) (? string? bodies)) ...)
748 _ ...)
749 (channel-news-entry (and (eq? type 'commit) commit-or-tag)
750 (and (eq? type 'tag) commit-or-tag)
751 (map pair title-tags titles)
752 (map pair body-tags bodies)))
753 (_
754 (raise (condition
755 (&message (message "invalid channel news entry"))
756 (&error-location
757 (location (source-properties->location
758 (source-properties entry)))))))))
759
760 (define (read-channel-news port)
761 "Read a channel news feed from PORT and return it as a <channel-news>
762 record."
763 (match (false-if-exception (read port))
764 (('channel-news ('version 0) entries ...)
765 (channel-news (map sexp->channel-news-entry entries)))
766 (('channel-news ('version version) _ ...)
767 ;; This is an unsupported version from the future. There's nothing wrong
768 ;; with that (the user may simply need to upgrade the 'guix' channel to
769 ;; be able to read it), so silently ignore it.
770 (channel-news '()))
771 (#f
772 (raise (condition
773 (&message (message "syntactically invalid channel news file")))))
774 (sexp
775 (raise (condition
776 (&message (message "invalid channel news file"))
777 (&error-location
778 (location (source-properties->location
779 (source-properties sexp)))))))))
780
781 (define (resolve-channel-news-entry-tag repository entry)
782 "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
783 ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
784 the field its 'tag' refers to. A 'git-error' exception is raised if the tag
785 cannot be found."
786 (if (channel-news-entry-commit entry)
787 entry
788 (let* ((tag (channel-news-entry-tag entry))
789 (reference (string-append "refs/tags/" tag))
790 (oid (reference-name->oid repository reference)))
791 (channel-news-entry (oid->string oid) tag
792 (channel-news-entry-title entry)
793 (channel-news-entry-body entry)))))
794
795 (define* (channel-news-for-commit channel new #:optional old)
796 "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
797 NEW. When OLD is omitted or is #f, return all the news entries of CHANNEL."
798 (catch 'git-error
799 (lambda ()
800 (let* ((checkout (update-cached-checkout (channel-url channel)
801 #:ref `(commit . ,new)))
802 (metadata (read-channel-metadata-from-source checkout))
803 (news-file (channel-metadata-news-file metadata))
804 (news-file (and news-file
805 (string-append checkout "/" news-file))))
806 (if (and news-file (file-exists? news-file))
807 (with-repository checkout repository
808 (let* ((news (call-with-input-file news-file
809 read-channel-news))
810 (entries (map (lambda (entry)
811 (resolve-channel-news-entry-tag repository
812 entry))
813 (channel-news-entries news))))
814 (if old
815 (let* ((new (commit-lookup repository (string->oid new)))
816 (old (commit-lookup repository (string->oid old)))
817 (commits (list->set
818 (map (compose oid->string commit-id)
819 (commit-difference new old)))))
820 (filter (lambda (entry)
821 (set-contains? commits
822 (channel-news-entry-commit entry)))
823 entries))
824 entries)))
825 '())))
826 (lambda (key error . rest)
827 ;; If commit NEW or commit OLD cannot be found, then something must be
828 ;; wrong (for example, the history of CHANNEL was rewritten and these
829 ;; commits no longer exist upstream), so quietly return the empty list.
830 (if (= GIT_ENOTFOUND (git-error-code error))
831 '()
832 (apply throw key error rest)))))
833
834 ;;; Local Variables:
835 ;;; eval: (put 'with-guile 'scheme-indent-function 1)
836 ;;; End: