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