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