gnu: r-igraph: Move to (gnu packages cran).
[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 git-authenticate)
25 #:use-module ((guix openpgp)
26 #:select (openpgp-public-key-fingerprint
27 openpgp-format-fingerprint))
28 #:use-module (guix base16)
29 #:use-module (guix records)
30 #:use-module (guix gexp)
31 #:use-module (guix modules)
32 #:use-module (guix discovery)
33 #:use-module (guix monads)
34 #:use-module (guix profiles)
35 #:use-module (guix packages)
36 #:use-module (guix progress)
37 #:use-module (guix derivations)
38 #:use-module (guix combinators)
39 #:use-module (guix diagnostics)
40 #:use-module (guix sets)
41 #:use-module (guix store)
42 #:use-module (guix i18n)
43 #:use-module (srfi srfi-1)
44 #:use-module (srfi srfi-2)
45 #:use-module (srfi srfi-9)
46 #:use-module (srfi srfi-11)
47 #:use-module (srfi srfi-26)
48 #:use-module (srfi srfi-34)
49 #:use-module (srfi srfi-35)
50 #:autoload (guix self) (whole-package make-config.scm)
51 #:autoload (guix inferior) (gexp->derivation-in-inferior) ;FIXME: circular dep
52 #:autoload (guix quirks) (%quirks %patches applicable-patch? apply-patch)
53 #:use-module (ice-9 format)
54 #:use-module (ice-9 match)
55 #:use-module (ice-9 vlist)
56 #:use-module ((ice-9 rdelim) #:select (read-string))
57 #:use-module ((rnrs bytevectors) #:select (bytevector=?))
58 #:export (channel
59 channel?
60 channel-name
61 channel-url
62 channel-branch
63 channel-commit
64 channel-introduction
65 channel-location
66
67 channel-introduction?
68 make-channel-introduction
69 channel-introduction-first-signed-commit
70 channel-introduction-first-commit-signer
71
72 openpgp-fingerprint->bytevector
73 openpgp-fingerprint
74
75 %default-channels
76 guix-channel?
77
78 channel-instance?
79 channel-instance-channel
80 channel-instance-commit
81 channel-instance-checkout
82
83 authenticate-channel
84 latest-channel-instances
85 checkout->channel-instance
86 latest-channel-derivation
87 channel-instances->manifest
88 %channel-profile-hooks
89 channel-instances->derivation
90 ensure-forward-channel-update
91
92 profile-channels
93
94 channel-news-entry?
95 channel-news-entry-commit
96 channel-news-entry-tag
97 channel-news-entry-title
98 channel-news-entry-body
99
100 channel-news-for-commit))
101
102 ;;; Commentary:
103 ;;;
104 ;;; This module implements "channels." A channel is usually a source of
105 ;;; package definitions. There's a special channel, the 'guix' channel, that
106 ;;; provides all of Guix, including its commands and its documentation.
107 ;;; User-defined channels are expected to typically provide a bunch of .scm
108 ;;; files meant to be added to the '%package-search-path'.
109 ;;;
110 ;;; This module provides tools to fetch and update channels from a Git
111 ;;; repository and to build them.
112 ;;;
113 ;;; Code:
114
115 (define-record-type* <channel> channel make-channel
116 channel?
117 (name channel-name)
118 (url channel-url)
119 (branch channel-branch (default "master"))
120 (commit channel-commit (default #f))
121 (introduction channel-introduction (default #f))
122 (location channel-location
123 (default (current-source-location)) (innate)))
124
125 ;; Channel introductions. A "channel introduction" provides a commit/signer
126 ;; pair that specifies the first commit of the authentication process as well
127 ;; as its signer's fingerprint. Introductions are used to bootstrap trust in
128 ;; a channel.
129 (define-record-type <channel-introduction>
130 (%make-channel-introduction first-signed-commit first-commit-signer)
131 channel-introduction?
132 (first-signed-commit channel-introduction-first-signed-commit) ;hex string
133 (first-commit-signer channel-introduction-first-commit-signer)) ;bytevector
134
135 (define (make-channel-introduction commit signer)
136 "Return a new channel introduction: COMMIT is the introductory where
137 authentication starts, and SIGNER is the OpenPGP fingerprint (a bytevector) of
138 the signer of that commit."
139 (%make-channel-introduction commit signer))
140
141 (define (openpgp-fingerprint->bytevector str)
142 "Convert STR, an OpenPGP fingerprint (hexadecimal string with whitespace),
143 to the corresponding bytevector."
144 (base16-string->bytevector
145 (string-downcase (string-filter char-set:hex-digit str))))
146
147 (define-syntax openpgp-fingerprint
148 (lambda (s)
149 "Convert STR, an OpenPGP fingerprint (hexadecimal string with whitespace),
150 to the corresponding bytevector."
151 (syntax-case s ()
152 ((_ str)
153 (string? (syntax->datum #'str))
154 (openpgp-fingerprint->bytevector (syntax->datum #'str)))
155 ((_ str)
156 #'(openpgp-fingerprint->bytevector str)))))
157
158 (define %guix-channel-introduction
159 ;; Introduction of the official 'guix channel. The chosen commit is the
160 ;; first one that introduces '.guix-authorizations' on the 'staging'
161 ;; branch that was eventually merged in 'master'. Any branch starting
162 ;; before that commit cannot be merged or it will be rejected by 'guix pull'
163 ;; & co.
164 (make-channel-introduction
165 "9edb3f66fd807b096b48283debdcddccfea34bad" ;2020-05-26
166 (openpgp-fingerprint ;mbakke
167 "BBB0 2DDF 2CEA F6A8 0D1D E643 A2A0 6DF2 A33A 54FA")))
168
169 (define %default-channel-url
170 ;; URL of the default 'guix' channel.
171 "https://git.savannah.gnu.org/git/guix.git")
172
173 (define %default-channels
174 ;; Default list of channels.
175 (list (channel
176 (name 'guix)
177 (branch "master")
178 (url %default-channel-url)
179 (introduction %guix-channel-introduction))))
180
181 (define (guix-channel? channel)
182 "Return true if CHANNEL is the 'guix' channel."
183 (eq? 'guix (channel-name channel)))
184
185 (define (ensure-default-introduction chan)
186 "If CHAN represents the \"official\" 'guix' channel and lacks an
187 introduction, add it."
188 (if (and (guix-channel? chan)
189 (not (channel-introduction chan))
190 (string=? (channel-url chan) %default-channel-url))
191 (channel (inherit chan)
192 (introduction %guix-channel-introduction))
193 chan))
194
195 (define-record-type <channel-instance>
196 (channel-instance channel commit checkout)
197 channel-instance?
198 (channel channel-instance-channel)
199 (commit channel-instance-commit)
200 (checkout channel-instance-checkout))
201
202 (define-record-type <channel-metadata>
203 (channel-metadata directory dependencies news-file keyring-reference url)
204 channel-metadata?
205 (directory channel-metadata-directory) ;string with leading slash
206 (dependencies channel-metadata-dependencies) ;list of <channel>
207 (news-file channel-metadata-news-file) ;string | #f
208 (keyring-reference channel-metadata-keyring-reference) ;string
209 (url channel-metadata-url)) ;string | #f
210
211 (define %default-keyring-reference
212 ;; Default value of the 'keyring-reference' field.
213 "keyring")
214
215 (define (channel-reference channel)
216 "Return the \"reference\" for CHANNEL, an sexp suitable for
217 'latest-repository-commit'."
218 (match (channel-commit channel)
219 (#f `(branch . ,(channel-branch channel)))
220 (commit `(commit . ,(channel-commit channel)))))
221
222 (define sexp->channel-introduction
223 (match-lambda
224 (('channel-introduction ('version 0)
225 ('commit commit) ('signer signer)
226 _ ...)
227 (make-channel-introduction commit (openpgp-fingerprint signer)))
228 (x #f)))
229
230 (define (read-channel-metadata port)
231 "Read from PORT channel metadata in the format expected for the
232 '.guix-channel' file. Return a <channel-metadata> record, or raise an error
233 if valid metadata could not be read from PORT."
234 (match (read port)
235 (('channel ('version 0) properties ...)
236 (let ((directory (and=> (assoc-ref properties 'directory) first))
237 (dependencies (or (assoc-ref properties 'dependencies) '()))
238 (news-file (and=> (assoc-ref properties 'news-file) first))
239 (url (and=> (assoc-ref properties 'url) first))
240 (keyring-reference
241 (or (and=> (assoc-ref properties 'keyring-reference) first)
242 %default-keyring-reference)))
243 (channel-metadata
244 (cond ((not directory) "/") ;directory
245 ((string-prefix? "/" directory) directory)
246 (else (string-append "/" directory)))
247 (map (lambda (item) ;dependencies
248 (let ((get (lambda* (key #:optional default)
249 (or (and=> (assoc-ref item key) first) default))))
250 (and-let* ((name (get 'name))
251 (url (get 'url))
252 (branch (get 'branch "master")))
253 (channel
254 (name name)
255 (branch branch)
256 (url url)
257 (commit (get 'commit))
258 (introduction (and=> (get 'introduction)
259 sexp->channel-introduction))))))
260 dependencies)
261 news-file
262 keyring-reference
263 url)))
264 ((and ('channel ('version version) _ ...) sexp)
265 (raise (condition
266 (&message (message "unsupported '.guix-channel' version"))
267 (&error-location
268 (location (source-properties->location
269 (source-properties sexp)))))))
270 (sexp
271 (raise (condition
272 (&message (message "invalid '.guix-channel' file"))
273 (&error-location
274 (location (source-properties->location
275 (source-properties sexp)))))))))
276
277 (define (read-channel-metadata-from-source source)
278 "Return a channel-metadata record read from channel's SOURCE/.guix-channel
279 description file, or return the default channel-metadata record if that file
280 doesn't exist."
281 (catch 'system-error
282 (lambda ()
283 (call-with-input-file (string-append source "/.guix-channel")
284 read-channel-metadata))
285 (lambda args
286 (if (= ENOENT (system-error-errno args))
287 (channel-metadata "/" '() #f %default-keyring-reference #f)
288 (apply throw args)))))
289
290 (define (channel-instance-metadata instance)
291 "Return a channel-metadata record read from the channel INSTANCE's
292 description file or its default value."
293 (read-channel-metadata-from-source (channel-instance-checkout instance)))
294
295 (define (channel-instance-dependencies instance)
296 "Return the list of channels that are declared as dependencies for the given
297 channel INSTANCE."
298 (channel-metadata-dependencies (channel-instance-metadata instance)))
299
300 (define (apply-patches checkout commit patches)
301 "Apply the matching PATCHES to CHECKOUT, modifying files in place. The
302 result is unspecified."
303 (let loop ((patches patches))
304 (match patches
305 (() #t)
306 ((patch rest ...)
307 (when (applicable-patch? patch checkout commit)
308 (apply-patch patch checkout))
309 (loop rest)))))
310
311 (define commit-short-id
312 (compose (cut string-take <> 7) oid->string commit-id))
313
314 (define* (authenticate-channel channel checkout commit
315 #:key (keyring-reference-prefix "origin/"))
316 "Authenticate the given COMMIT of CHANNEL, available at CHECKOUT, a
317 directory containing a CHANNEL checkout. Raise an error if authentication
318 fails."
319 (define intro
320 (channel-introduction channel))
321
322 (define cache-key
323 (string-append "channels/" (symbol->string (channel-name channel))))
324
325 (define keyring-reference
326 (channel-metadata-keyring-reference
327 (read-channel-metadata-from-source checkout)))
328
329 (define (make-reporter start-commit end-commit commits)
330 (format (current-error-port)
331 (G_ "Authenticating channel '~a', commits ~a to ~a (~h new \
332 commits)...~%")
333 (channel-name channel)
334 (commit-short-id start-commit)
335 (commit-short-id end-commit)
336 (length commits))
337
338 (progress-reporter/bar (length commits)))
339
340 ;; XXX: Too bad we need to re-open CHECKOUT.
341 (with-repository checkout repository
342 (authenticate-repository repository
343 (string->oid
344 (channel-introduction-first-signed-commit intro))
345 (channel-introduction-first-commit-signer intro)
346 #:end (string->oid commit)
347 #:keyring-reference
348 (string-append keyring-reference-prefix
349 keyring-reference)
350 #:make-reporter make-reporter
351 #:cache-key cache-key)))
352
353 (define* (latest-channel-instance store channel
354 #:key (patches %patches)
355 starting-commit
356 (authenticate? #f)
357 (validate-pull
358 ensure-forward-channel-update))
359 "Return the latest channel instance for CHANNEL. When STARTING-COMMIT is
360 true, call VALIDATE-PULL with CHANNEL, STARTING-COMMIT, the target commit, and
361 their relation. When AUTHENTICATE? is false, CHANNEL is not authenticated."
362 (define (dot-git? file stat)
363 (and (string=? (basename file) ".git")
364 (eq? 'directory (stat:type stat))))
365
366 (let-values (((channel)
367 (ensure-default-introduction channel))
368 ((checkout commit relation)
369 (update-cached-checkout (channel-url channel)
370 #:ref (channel-reference channel)
371 #:starting-commit starting-commit)))
372 (when relation
373 (validate-pull channel starting-commit commit relation))
374
375 (if authenticate?
376 (if (channel-introduction channel)
377 (authenticate-channel channel checkout commit)
378 ;; TODO: Warn for all the channels once the authentication interface
379 ;; is public.
380 (when (guix-channel? channel)
381 (raise (make-compound-condition
382 (formatted-message (G_ "channel '~a' lacks an \
383 introduction and cannot be authenticated~%")
384 (channel-name channel))
385 (condition
386 (&fix-hint
387 (hint (G_ "Add the missing introduction to your
388 channels file to address the issue. Alternatively, you can pass
389 @option{--disable-authentication}, at the risk of running unauthenticated and
390 thus potentially malicious code."))))))))
391 (warning (G_ "channel authentication disabled~%")))
392
393 (when (guix-channel? channel)
394 ;; Apply the relevant subset of PATCHES directly in CHECKOUT. This is
395 ;; safe to do because 'switch-to-ref' eventually does a hard reset.
396 (apply-patches checkout commit patches))
397
398 (let* ((name (url+commit->name (channel-url channel) commit))
399 (checkout (add-to-store store name #t "sha256" checkout
400 #:select? (negate dot-git?))))
401 (channel-instance channel commit checkout))))
402
403 (define (ensure-forward-channel-update channel start commit relation)
404 "Raise an error if RELATION is not 'ancestor, meaning that START is not an
405 ancestor of COMMIT, unless CHANNEL specifies a commit.
406
407 This procedure implements a channel update policy meant to be used as a
408 #:validate-pull argument."
409 (match relation
410 ('ancestor #t)
411 ('self #t)
412 (_
413 (raise (make-compound-condition
414 (condition
415 (&message (message
416 (format #f (G_ "\
417 aborting update of channel '~a' to commit ~a, which is not a descendant of ~a")
418 (channel-name channel)
419 commit start))))
420
421 ;; If the user asked for a specific commit, they might want
422 ;; that to happen nevertheless, so tell them about the
423 ;; relevant 'guix pull' option.
424 (if (channel-commit channel)
425 (condition
426 (&fix-hint
427 (hint (G_ "Use @option{--allow-downgrades} to force
428 this downgrade."))))
429 (condition
430 (&fix-hint
431 (hint (G_ "This could indicate that the channel has
432 been tampered with and is trying to force a roll-back, preventing you from
433 getting the latest updates. If you think this is not the case, explicitly
434 allow non-forward updates."))))))))))
435
436 (define (channel-instance-primary-url instance)
437 "Return the primary URL advertised for INSTANCE, or #f if there is no such
438 information."
439 (channel-metadata-url (channel-instance-metadata instance)))
440
441 (define* (latest-channel-instances store channels
442 #:key
443 (current-channels '())
444 (authenticate? #t)
445 (validate-pull
446 ensure-forward-channel-update))
447 "Return a list of channel instances corresponding to the latest checkouts of
448 CHANNELS and the channels on which they depend.
449
450 When AUTHENTICATE? is true, authenticate the subset of CHANNELS that has a
451 \"channel introduction\".
452
453 CURRENT-CHANNELS is the list of currently used channels. It is compared
454 against the newly-fetched instances of CHANNELS, and VALIDATE-PULL is called
455 for each channel update and can choose to emit warnings or raise an error,
456 depending on the policy it implements."
457 ;; Only process channels that are unique, or that are more specific than a
458 ;; previous channel specification.
459 (define (ignore? channel others)
460 (member channel others
461 (lambda (a b)
462 (and (eq? (channel-name a) (channel-name b))
463 (or (channel-commit b)
464 (not (or (channel-commit a)
465 (channel-commit b))))))))
466
467 (define (current-commit name)
468 ;; Return the current commit for channel NAME.
469 (any (lambda (channel)
470 (and (eq? (channel-name channel) name)
471 (channel-commit channel)))
472 current-channels))
473
474 (let loop ((channels channels)
475 (previous-channels '()))
476 ;; Accumulate a list of instances. A list of processed channels is also
477 ;; accumulated to decide on duplicate channel specifications.
478 (define-values (resulting-channels instances)
479 (fold2 (lambda (channel previous-channels instances)
480 (if (ignore? channel previous-channels)
481 (values previous-channels instances)
482 (begin
483 (format (current-error-port)
484 (G_ "Updating channel '~a' from Git repository at '~a'...~%")
485 (channel-name channel)
486 (channel-url channel))
487 (let* ((current (current-commit (channel-name channel)))
488 (instance
489 (latest-channel-instance store channel
490 #:authenticate?
491 authenticate?
492 #:validate-pull
493 validate-pull
494 #:starting-commit
495 current)))
496 (when authenticate?
497 ;; CHANNEL is authenticated so we can trust the
498 ;; primary URL advertised in its metadata and warn
499 ;; about possibly stale mirrors.
500 (let ((primary-url (channel-instance-primary-url
501 instance)))
502 (unless (or (not primary-url)
503 (channel-commit channel)
504 (string=? primary-url (channel-url channel)))
505 (warning (G_ "pulled channel '~a' from a mirror \
506 of ~a, which might be stale~%")
507 (channel-name channel)
508 primary-url))))
509
510 (let-values (((new-instances new-channels)
511 (loop (channel-instance-dependencies instance)
512 previous-channels)))
513 (values (append (cons channel new-channels)
514 previous-channels)
515 (append (cons instance new-instances)
516 instances)))))))
517 previous-channels
518 '() ;instances
519 channels))
520
521 (let ((instance-name (compose channel-name channel-instance-channel)))
522 ;; Remove all earlier channel specifications if they are followed by a
523 ;; more specific one.
524 (values (delete-duplicates instances
525 (lambda (a b)
526 (eq? (instance-name a) (instance-name b))))
527 resulting-channels))))
528
529 (define* (checkout->channel-instance checkout
530 #:key commit
531 (url checkout) (name 'guix))
532 "Return a channel instance for CHECKOUT, which is assumed to be a checkout
533 of COMMIT at URL. Use NAME as the channel name."
534 (let* ((commit (or commit (make-string 40 #\0)))
535 (channel (channel (name name)
536 (commit commit)
537 (url url))))
538 (channel-instance channel commit checkout)))
539
540 (define %self-build-file
541 ;; The file containing code to build Guix. This serves the same purpose as
542 ;; a makefile, and, similarly, is intended to always keep this name.
543 "build-aux/build-self.scm")
544
545 (define %pull-version
546 ;; This is the version of the 'guix pull' protocol. It specifies what's
547 ;; expected from %SELF-BUILD-FILE. The initial version ("0") was when we'd
548 ;; place a set of compiled Guile modules in ~/.config/guix/latest.
549 1)
550
551 (define (standard-module-derivation name source core dependencies)
552 "Return a derivation that builds with CORE, a Guix instance, the Scheme
553 modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
554 objects. The assumption is that SOURCE contains package modules to be added
555 to '%package-module-path'."
556
557 (let* ((metadata (read-channel-metadata-from-source source))
558 (directory (channel-metadata-directory metadata)))
559
560 (define build
561 ;; This is code that we'll run in CORE, a Guix instance, with its own
562 ;; modules and so on. That way, we make sure these modules are built for
563 ;; the right Guile version, with the right dependencies, and that they get
564 ;; to see the right (gnu packages …) modules.
565 (with-extensions dependencies
566 #~(begin
567 (use-modules (guix build compile)
568 (guix build utils)
569 (srfi srfi-26))
570
571 (define go
572 (string-append #$output "/lib/guile/" (effective-version)
573 "/site-ccache"))
574 (define scm
575 (string-append #$output "/share/guile/site/"
576 (effective-version)))
577
578 (let* ((subdir #$directory)
579 (source (string-append #$source subdir)))
580 (compile-files source go (find-files source "\\.scm$"))
581 (mkdir-p (dirname scm))
582 (symlink (string-append #$source subdir) scm))
583
584 scm)))
585
586 (gexp->derivation-in-inferior name build core)))
587
588 (define* (guile-for-source source #:optional (quirks %quirks))
589 "Return the Guile package to use when building SOURCE or #f if the default
590 '%guile-for-build' should be good enough."
591 (let loop ((quirks quirks))
592 (match quirks
593 (()
594 #f)
595 (((predicate . guile) rest ...)
596 (if (predicate source) (guile) (loop rest))))))
597
598 (define (call-with-guile guile thunk)
599 (lambda (store)
600 (values (parameterize ((%guile-for-build
601 (if guile
602 (package-derivation store guile)
603 (%guile-for-build))))
604 (run-with-store store (thunk)))
605 store)))
606
607 (define-syntax-rule (with-guile guile exp ...)
608 "Set GUILE as the '%guile-for-build' parameter for the dynamic extent of
609 EXP, a series of monadic expressions."
610 (call-with-guile guile (lambda ()
611 (mbegin %store-monad exp ...))))
612
613 (define (with-trivial-build-handler mvalue)
614 "Run MVALUE, a monadic value, with a \"trivial\" build handler installed
615 that unconditionally resumes the continuation."
616 (lambda (store)
617 (with-build-handler (lambda (continue . _)
618 (continue #t))
619 (values (run-with-store store mvalue)
620 store))))
621
622 (define* (build-from-source name source
623 #:key core verbose? commit
624 (dependencies '()))
625 "Return a derivation to build Guix from SOURCE, using the self-build script
626 contained therein; use COMMIT as the version string. When CORE is true, build
627 package modules under SOURCE using CORE, an instance of Guix."
628 ;; Running the self-build script makes it easier to update the build
629 ;; procedure: the self-build script of the Guix-to-be-installed contains the
630 ;; right dependencies, build procedure, etc., which the Guix-in-use may not
631 ;; be know.
632 (define script
633 (string-append source "/" %self-build-file))
634
635 (if (file-exists? script)
636 (let ((build (save-module-excursion
637 (lambda ()
638 ;; Disable deprecation warnings; it's OK for SCRIPT to
639 ;; use deprecated APIs and the user doesn't have to know
640 ;; about it.
641 (parameterize ((guix-warning-port
642 (%make-void-port "w")))
643 (primitive-load script)))))
644 (guile (guile-for-source source)))
645 ;; BUILD must be a monadic procedure of at least one argument: the
646 ;; source tree.
647 ;;
648 ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
649 ;; the future we'll fall back to a previous version of the protocol
650 ;; when that happens.
651 (with-guile guile
652 ;; BUILD is usually quite costly. Install a "trivial" build handler
653 ;; so we don't bounce an outer build-accumulator handler that could
654 ;; cause us to redo half of the BUILD computation several times just
655 ;; to realize it gives the same result.
656 (with-trivial-build-handler
657 (build source #:verbose? verbose? #:version commit
658 #:pull-version %pull-version))))
659
660 ;; Build a set of modules that extend Guix using the standard method.
661 (standard-module-derivation name source core dependencies)))
662
663 (define* (build-channel-instance instance
664 #:optional core (dependencies '()))
665 "Return, as a monadic value, the derivation for INSTANCE, a channel
666 instance. DEPENDENCIES is a list of extensions providing Guile modules that
667 INSTANCE depends on."
668 (build-from-source (symbol->string
669 (channel-name (channel-instance-channel instance)))
670 (channel-instance-checkout instance)
671 #:commit (channel-instance-commit instance)
672 #:core core
673 #:dependencies dependencies))
674
675 (define (resolve-dependencies instances)
676 "Return a procedure that, given one of the elements of INSTANCES, returns
677 list of instances it depends on."
678 (define channel-instance-name
679 (compose channel-name channel-instance-channel))
680
681 (define table ;map a name to an instance
682 (fold (lambda (instance table)
683 (vhash-consq (channel-instance-name instance)
684 instance table))
685 vlist-null
686 instances))
687
688 (define edges
689 (fold (lambda (instance edges)
690 (fold (lambda (channel edges)
691 (let ((name (channel-name channel)))
692 (match (vhash-assq name table)
693 ((_ . target)
694 (vhash-consq instance target edges)))))
695 edges
696 (channel-instance-dependencies instance)))
697 vlist-null
698 instances))
699
700 (lambda (instance)
701 (vhash-foldq* cons '() instance edges)))
702
703 (define (channel-instance-derivations instances)
704 "Return the list of derivations to build INSTANCES, in the same order as
705 INSTANCES."
706 (define core-instance
707 ;; The 'guix' channel is treated specially: it's an implicit dependency of
708 ;; all the other channels.
709 (find (lambda (instance)
710 (guix-channel? (channel-instance-channel instance)))
711 instances))
712
713 (define edges
714 (resolve-dependencies instances))
715
716 (define (instance->derivation instance)
717 (mlet %store-monad ((system (current-system)))
718 (mcached (if (eq? instance core-instance)
719 (build-channel-instance instance)
720 (mlet %store-monad ((core (instance->derivation core-instance))
721 (deps (mapm %store-monad instance->derivation
722 (edges instance))))
723 (build-channel-instance instance core deps)))
724 instance
725 system)))
726
727 (unless core-instance
728 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
729 instances)
730 source-properties->location)))
731 (raise (apply make-compound-condition
732 (condition
733 (&message (message "'guix' channel is lacking")))
734 (condition
735 (&fix-hint (hint (G_ "Make sure your list of channels
736 contains one channel named @code{guix} providing the core of Guix."))))
737 (if loc
738 (list (condition (&error-location (location loc))))
739 '())))))
740
741 (mapm %store-monad instance->derivation instances))
742
743 (define (whole-package-for-legacy name modules)
744 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
745 modules in the old ~/.config/guix/latest style."
746 (define packages
747 (resolve-interface '(gnu packages guile)))
748
749 (define modules+compiled
750 ;; Since MODULES contains both .scm and .go files at its root, re-bundle
751 ;; it so that it has share/guile/site and lib/guile, which is what
752 ;; 'whole-package' expects.
753 (computed-file (derivation-name modules)
754 (with-imported-modules '((guix build utils))
755 #~(begin
756 (use-modules (guix build utils))
757
758 (define version
759 (effective-version))
760 (define share
761 (string-append #$output "/share/guile/site"))
762 (define lib
763 (string-append #$output "/lib/guile/" version))
764
765 (mkdir-p share) (mkdir-p lib)
766 (symlink #$modules (string-append share "/" version))
767 (symlink #$modules (string-append lib "/site-ccache"))))))
768
769 (letrec-syntax ((list (syntax-rules (->)
770 ((_)
771 '())
772 ((_ (module -> variable) rest ...)
773 (cons (module-ref (resolve-interface
774 '(gnu packages module))
775 'variable)
776 (list rest ...)))
777 ((_ variable rest ...)
778 (cons (module-ref packages 'variable)
779 (list rest ...))))))
780 (whole-package name modules+compiled
781
782 ;; In the "old style", %SELF-BUILD-FILE would simply return a
783 ;; derivation that builds modules. We have to infer what the
784 ;; dependencies of these modules were.
785 (list guile-json-3 guile-git guile-bytestructures
786 (ssh -> guile-ssh) (tls -> gnutls)))))
787
788 (define (old-style-guix? drv)
789 "Return true if DRV corresponds to a ~/.config/guix/latest style of
790 derivation."
791 ;; Here we rely on a gross historical fact: that derivations produced by the
792 ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
793 ;; dated May 30, 2018) did not depend on "guix-command.drv".
794 (not (find (lambda (input)
795 (string=? "guix-command"
796 (derivation-name
797 (derivation-input-derivation input))))
798 (derivation-inputs drv))))
799
800 (define (channel-instances->manifest instances)
801 "Return a profile manifest with entries for all of INSTANCES, a list of
802 channel instances."
803 (define (instance->entry instance drv)
804 (let* ((commit (channel-instance-commit instance))
805 (channel (channel-instance-channel instance))
806 (intro (channel-introduction channel)))
807 (manifest-entry
808 (name (symbol->string (channel-name channel)))
809 (version (string-take commit 7))
810 (item (if (guix-channel? channel)
811 (if (old-style-guix? drv)
812 (whole-package-for-legacy (string-append name "-" version)
813 drv)
814 drv)
815 drv))
816 (properties
817 `((source (repository
818 (version 0)
819 (url ,(channel-url channel))
820 (branch ,(channel-branch channel))
821 (commit ,commit)
822 ,@(if intro
823 `((introduction
824 (channel-introduction
825 (version 0)
826 (commit
827 ,(channel-introduction-first-signed-commit
828 intro))
829 (signer
830 ,(openpgp-format-fingerprint
831 (channel-introduction-first-commit-signer
832 intro))))))
833 '()))))))))
834
835 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
836 (entries -> (map instance->entry instances derivations)))
837 (return (manifest entries))))
838
839 (define (package-cache-file manifest)
840 "Build a package cache file for the instance in MANIFEST. This is meant to
841 be used as a profile hook."
842 (let ((profile (profile (content manifest) (hooks '()))))
843 (define build
844 #~(begin
845 (use-modules (gnu packages))
846
847 (if (defined? 'generate-package-cache)
848 (begin
849 ;; Delegate package cache generation to the inferior.
850 (format (current-error-port)
851 "Generating package cache for '~a'...~%"
852 #$profile)
853 (generate-package-cache #$output))
854 (mkdir #$output))))
855
856 (gexp->derivation-in-inferior "guix-package-cache" build
857 profile
858
859 ;; If the Guix in PROFILE is too old and
860 ;; lacks 'guix repl', don't build the cache
861 ;; instead of failing.
862 #:silent-failure? #t
863
864 #:properties '((type . profile-hook)
865 (hook . package-cache))
866 #:local-build? #t)))
867
868 (define %channel-profile-hooks
869 ;; The default channel profile hooks.
870 (cons package-cache-file %default-profile-hooks))
871
872 (define (channel-instances->derivation instances)
873 "Return the derivation of the profile containing INSTANCES, a list of
874 channel instances."
875 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
876 (profile-derivation manifest
877 #:hooks %channel-profile-hooks)))
878
879 (define latest-channel-instances*
880 (store-lift latest-channel-instances))
881
882 (define* (latest-channel-derivation #:optional (channels %default-channels)
883 #:key
884 (current-channels '())
885 (validate-pull
886 ensure-forward-channel-update))
887 "Return as a monadic value the derivation that builds the profile for the
888 latest instances of CHANNELS. CURRENT-CHANNELS and VALIDATE-PULL are passed
889 to 'latest-channel-instances'."
890 (mlet %store-monad ((instances
891 (latest-channel-instances* channels
892 #:current-channels
893 current-channels
894 #:validate-pull
895 validate-pull)))
896 (channel-instances->derivation instances)))
897
898 (define (profile-channels profile)
899 "Return the list of channels corresponding to entries in PROFILE. If
900 PROFILE is not a profile created by 'guix pull', return the empty list."
901 (filter-map (lambda (entry)
902 (match (assq 'source (manifest-entry-properties entry))
903 (('source ('repository ('version 0)
904 ('url url)
905 ('branch branch)
906 ('commit commit)
907 rest ...))
908 (channel (name (string->symbol
909 (manifest-entry-name entry)))
910 (url url)
911 (commit commit)
912 (introduction
913 (match (assq 'introduction rest)
914 (#f #f)
915 (('introduction intro)
916 (sexp->channel-introduction intro))))))
917
918 ;; No channel information for this manifest entry.
919 ;; XXX: Pre-0.15.0 Guix did not provide that information,
920 ;; but there's not much we can do in that case.
921 (_ #f)))
922
923 ;; Show most recently installed packages last.
924 (reverse
925 (manifest-entries (profile-manifest profile)))))
926
927 \f
928 ;;;
929 ;;; News.
930 ;;;
931
932 ;; Channel news.
933 (define-record-type <channel-news>
934 (channel-news entries)
935 channel-news?
936 (entries channel-news-entries)) ;list of <channel-news-entry>
937
938 ;; News entry, associated with a specific commit of the channel.
939 (define-record-type <channel-news-entry>
940 (channel-news-entry commit tag title body)
941 channel-news-entry?
942 (commit channel-news-entry-commit) ;hex string | #f
943 (tag channel-news-entry-tag) ;#f | string
944 (title channel-news-entry-title) ;list of language tag/string pairs
945 (body channel-news-entry-body)) ;list of language tag/string pairs
946
947 (define (sexp->channel-news-entry entry)
948 "Return the <channel-news-entry> record corresponding to ENTRY, an sexp."
949 (define (pair language message)
950 (cons (symbol->string language) message))
951
952 (match entry
953 (('entry ((and (or 'commit 'tag) type) commit-or-tag)
954 ('title ((? symbol? title-tags) (? string? titles)) ...)
955 ('body ((? symbol? body-tags) (? string? bodies)) ...)
956 _ ...)
957 (channel-news-entry (and (eq? type 'commit) commit-or-tag)
958 (and (eq? type 'tag) commit-or-tag)
959 (map pair title-tags titles)
960 (map pair body-tags bodies)))
961 (_
962 (raise (condition
963 (&message (message "invalid channel news entry"))
964 (&error-location
965 (location (source-properties->location
966 (source-properties entry)))))))))
967
968 (define (read-channel-news port)
969 "Read a channel news feed from PORT and return it as a <channel-news>
970 record."
971 (match (false-if-exception (read port))
972 (('channel-news ('version 0) entries ...)
973 (channel-news (map sexp->channel-news-entry entries)))
974 (('channel-news ('version version) _ ...)
975 ;; This is an unsupported version from the future. There's nothing wrong
976 ;; with that (the user may simply need to upgrade the 'guix' channel to
977 ;; be able to read it), so silently ignore it.
978 (channel-news '()))
979 (#f
980 (raise (condition
981 (&message (message "syntactically invalid channel news file")))))
982 (sexp
983 (raise (condition
984 (&message (message "invalid channel news file"))
985 (&error-location
986 (location (source-properties->location
987 (source-properties sexp)))))))))
988
989 (define (resolve-channel-news-entry-tag repository entry)
990 "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
991 ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
992 the field its 'tag' refers to. A 'git-error' exception is raised if the tag
993 cannot be found."
994 (if (channel-news-entry-commit entry)
995 entry
996 (let* ((tag (channel-news-entry-tag entry))
997 (reference (string-append "refs/tags/" tag))
998 (oid (reference-name->oid repository reference)))
999 (channel-news-entry (oid->string oid) tag
1000 (channel-news-entry-title entry)
1001 (channel-news-entry-body entry)))))
1002
1003 (define* (channel-news-for-commit channel new #:optional old)
1004 "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
1005 NEW. When OLD is omitted or is #f, return all the news entries of CHANNEL."
1006 (catch 'git-error
1007 (lambda ()
1008 (let* ((checkout (update-cached-checkout (channel-url channel)
1009 #:ref `(commit . ,new)))
1010 (metadata (read-channel-metadata-from-source checkout))
1011 (news-file (channel-metadata-news-file metadata))
1012 (news-file (and news-file
1013 (string-append checkout "/" news-file))))
1014 (if (and news-file (file-exists? news-file))
1015 (with-repository checkout repository
1016 (let* ((news (call-with-input-file news-file
1017 read-channel-news))
1018 (entries (map (lambda (entry)
1019 (resolve-channel-news-entry-tag repository
1020 entry))
1021 (channel-news-entries news))))
1022 (if old
1023 (let* ((new (commit-lookup repository (string->oid new)))
1024 (old (commit-lookup repository (string->oid old)))
1025 (commits (list->set
1026 (map (compose oid->string commit-id)
1027 (commit-difference new old)))))
1028 (filter (lambda (entry)
1029 (set-contains? commits
1030 (channel-news-entry-commit entry)))
1031 entries))
1032 entries)))
1033 '())))
1034 (lambda (key error . rest)
1035 ;; If commit NEW or commit OLD cannot be found, then something must be
1036 ;; wrong (for example, the history of CHANNEL was rewritten and these
1037 ;; commits no longer exist upstream), so quietly return the empty list.
1038 (if (= GIT_ENOTFOUND (git-error-code error))
1039 '()
1040 (apply throw key error rest)))))
1041
1042 ;;; Local Variables:
1043 ;;; eval: (put 'with-guile 'scheme-indent-function 1)
1044 ;;; End: