licenses: Add Free Art License 1.3.
[jackhill/guix/guix.git] / guix / channels.scm
CommitLineData
0d39a3b9 1;;; GNU Guix --- Functional package management for GNU
9272cc70 2;;; Copyright © 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
af12790b 3;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
53f21642 4;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
0d39a3b9
LC
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)
8ba7fd3c 22 #:use-module (git)
0d39a3b9 23 #:use-module (guix git)
43badf26
LC
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)
0d39a3b9
LC
29 #:use-module (guix records)
30 #:use-module (guix gexp)
5fbdc9a5 31 #:use-module (guix modules)
0d39a3b9
LC
32 #:use-module (guix discovery)
33 #:use-module (guix monads)
34 #:use-module (guix profiles)
37c0d458 35 #:use-module (guix packages)
43badf26 36 #:use-module (guix progress)
0d39a3b9 37 #:use-module (guix derivations)
f58f676b 38 #:use-module (guix combinators)
69962ab7 39 #:use-module (guix diagnostics)
8ba7fd3c 40 #:use-module (guix sets)
0d39a3b9
LC
41 #:use-module (guix store)
42 #:use-module (guix i18n)
43 #:use-module (srfi srfi-1)
af12790b 44 #:use-module (srfi srfi-2)
0d39a3b9
LC
45 #:use-module (srfi srfi-9)
46 #:use-module (srfi srfi-11)
9719e8d3 47 #:use-module (srfi srfi-26)
ab6025b5
LC
48 #:use-module (srfi srfi-34)
49 #:use-module (srfi srfi-35)
29009fdb 50 #:autoload (guix describe) (current-channels) ;XXX: circular dep
5fbdc9a5
LC
51 #:autoload (guix self) (whole-package make-config.scm)
52 #:autoload (guix inferior) (gexp->derivation-in-inferior) ;FIXME: circular dep
20507347 53 #:autoload (guix quirks) (%quirks %patches applicable-patch? apply-patch)
43badf26 54 #:use-module (ice-9 format)
0d39a3b9 55 #:use-module (ice-9 match)
ed75bdf3 56 #:use-module (ice-9 vlist)
37c0d458 57 #:use-module ((ice-9 rdelim) #:select (read-string))
43badf26 58 #:use-module ((rnrs bytevectors) #:select (bytevector=?))
0d39a3b9
LC
59 #:export (channel
60 channel?
61 channel-name
62 channel-url
63 channel-branch
64 channel-commit
43badf26 65 channel-introduction
0d39a3b9
LC
66 channel-location
67
43badf26 68 channel-introduction?
8b7d982e
LC
69 make-channel-introduction
70 channel-introduction-first-signed-commit
71 channel-introduction-first-commit-signer
43badf26 72
6577682a
LC
73 openpgp-fingerprint->bytevector
74 openpgp-fingerprint
75
16c7472b 76 %default-guix-channel
0d39a3b9 77 %default-channels
72f749dc 78 guix-channel?
0d39a3b9
LC
79
80 channel-instance?
81 channel-instance-channel
82 channel-instance-commit
83 channel-instance-checkout
84
43badf26 85 authenticate-channel
0d39a3b9 86 latest-channel-instances
fe5db4eb 87 checkout->channel-instance
030f1367 88 latest-channel-derivation
4399b1cf 89 channel-instance->sexp
c37f38bd 90 channel-instances->manifest
5fbdc9a5 91 %channel-profile-hooks
a7c714d3 92 channel-instances->derivation
872898f7 93 ensure-forward-channel-update
a7c714d3 94
8ba7fd3c 95 profile-channels
1b88b7ba 96 manifest-entry-channel
aedbc5ff 97 sexp->channel
60d72f53 98 channel->code
8ba7fd3c
LC
99
100 channel-news-entry?
101 channel-news-entry-commit
9719e8d3 102 channel-news-entry-tag
8ba7fd3c
LC
103 channel-news-entry-title
104 channel-news-entry-body
105
106 channel-news-for-commit))
0d39a3b9
LC
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))
43badf26 127 (introduction channel-introduction (default #f))
0d39a3b9
LC
128 (location channel-location
129 (default (current-source-location)) (innate)))
0d39a3b9 130
43badf26
LC
131;; Channel introductions. A "channel introduction" provides a commit/signer
132;; pair that specifies the first commit of the authentication process as well
22a96992
LC
133;; as its signer's fingerprint. Introductions are used to bootstrap trust in
134;; a channel.
43badf26 135(define-record-type <channel-introduction>
22a96992 136 (%make-channel-introduction first-signed-commit first-commit-signer)
43badf26 137 channel-introduction?
22a96992
LC
138 (first-signed-commit channel-introduction-first-signed-commit) ;hex string
139 (first-commit-signer channel-introduction-first-commit-signer)) ;bytevector
43badf26 140
8b7d982e
LC
141(define (make-channel-introduction commit signer)
142 "Return a new channel introduction: COMMIT is the introductory where
143authentication starts, and SIGNER is the OpenPGP fingerprint (a bytevector) of
144the signer of that commit."
22a96992 145 (%make-channel-introduction commit signer))
8b7d982e 146
6577682a
LC
147(define (openpgp-fingerprint->bytevector str)
148 "Convert STR, an OpenPGP fingerprint (hexadecimal string with whitespace),
149to 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),
156to 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
43badf26
LC
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
6577682a
LC
172 (openpgp-fingerprint ;mbakke
173 "BBB0 2DDF 2CEA F6A8 0D1D E643 A2A0 6DF2 A33A 54FA")))
43badf26 174
c3f6f564
LC
175(define %default-channel-url
176 ;; URL of the default 'guix' channel.
177 "https://git.savannah.gnu.org/git/guix.git")
178
16c7472b
ZZ
179(define %default-guix-channel
180 (channel
181 (name 'guix)
182 (branch "master")
183 (url %default-channel-url)
184 (introduction %guix-channel-introduction)))
185
0d39a3b9
LC
186(define %default-channels
187 ;; Default list of channels.
16c7472b 188 (list %default-guix-channel))
0d39a3b9
LC
189
190(define (guix-channel? channel)
191 "Return true if CHANNEL is the 'guix' channel."
192 (eq? 'guix (channel-name channel)))
193
c3f6f564
LC
194(define (ensure-default-introduction chan)
195 "If CHAN represents the \"official\" 'guix' channel and lacks an
196introduction, 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
0d39a3b9
LC
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
af12790b 211(define-record-type <channel-metadata>
4ae762af 212 (channel-metadata directory dependencies news-file keyring-reference url)
af12790b 213 channel-metadata?
ce5d9ec8 214 (directory channel-metadata-directory) ;string with leading slash
8ba7fd3c 215 (dependencies channel-metadata-dependencies) ;list of <channel>
43badf26 216 (news-file channel-metadata-news-file) ;string | #f
4ae762af
LC
217 (keyring-reference channel-metadata-keyring-reference) ;string
218 (url channel-metadata-url)) ;string | #f
43badf26
LC
219
220(define %default-keyring-reference
221 ;; Default value of the 'keyring-reference' field.
222 "keyring")
af12790b 223
0d39a3b9
LC
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
d774c7b1
LC
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
45b90332
LC
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
242if 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))
8ba7fd3c 246 (dependencies (or (assoc-ref properties 'dependencies) '()))
43badf26 247 (news-file (and=> (assoc-ref properties 'news-file) first))
4ae762af 248 (url (and=> (assoc-ref properties 'url) first))
43badf26
LC
249 (keyring-reference
250 (or (and=> (assoc-ref properties 'keyring-reference) first)
251 %default-keyring-reference)))
45b90332 252 (channel-metadata
8ba7fd3c 253 (cond ((not directory) "/") ;directory
ce5d9ec8
LC
254 ((string-prefix? "/" directory) directory)
255 (else (string-append "/" directory)))
8ba7fd3c 256 (map (lambda (item) ;dependencies
45b90332
LC
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)
d774c7b1
LC
266 (commit (get 'commit))
267 (introduction (and=> (get 'introduction)
268 sexp->channel-introduction))))))
8ba7fd3c 269 dependencies)
43badf26 270 news-file
4ae762af
LC
271 keyring-reference
272 url)))
45b90332
LC
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
53f21642
JN
286(define (read-channel-metadata-from-source source)
287 "Return a channel-metadata record read from channel's SOURCE/.guix-channel
ce5d9ec8
LC
288description file, or return the default channel-metadata record if that file
289doesn't exist."
45b90332
LC
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))
4ae762af 296 (channel-metadata "/" '() #f %default-keyring-reference #f)
45b90332
LC
297 (apply throw args)))))
298
299(define (channel-instance-metadata instance)
53f21642 300 "Return a channel-metadata record read from the channel INSTANCE's
ce5d9ec8 301description file or its default value."
53f21642
JN
302 (read-channel-metadata-from-source (channel-instance-checkout instance)))
303
af12790b
RW
304(define (channel-instance-dependencies instance)
305 "Return the list of channels that are declared as dependencies for the given
306channel INSTANCE."
ce5d9ec8 307 (channel-metadata-dependencies (channel-instance-metadata instance)))
af12790b 308
053b10c3
LC
309(define (apply-patches checkout commit patches)
310 "Apply the matching PATCHES to CHECKOUT, modifying files in place. The
311result is unspecified."
312 (let loop ((patches patches))
313 (match patches
314 (() #t)
20507347
LC
315 ((patch rest ...)
316 (when (applicable-patch? patch checkout commit)
317 (apply-patch patch checkout))
053b10c3
LC
318 (loop rest)))))
319
43badf26
LC
320(define commit-short-id
321 (compose (cut string-take <> 7) oid->string commit-id))
322
43badf26
LC
323(define* (authenticate-channel channel checkout commit
324 #:key (keyring-reference-prefix "origin/"))
325 "Authenticate the given COMMIT of CHANNEL, available at CHECKOUT, a
326directory containing a CHANNEL checkout. Raise an error if authentication
327fails."
838f2bdf
LC
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 \
341commits)...~%")
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
29009fdb
LC
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
43badf26
LC
361 ;; XXX: Too bad we need to re-open CHECKOUT.
362 (with-repository checkout repository
838f2bdf
LC
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)
29009fdb 371 #:authentic-commits authentic-commits
838f2bdf
LC
372 #:make-reporter make-reporter
373 #:cache-key cache-key)))
43badf26 374
053b10c3 375(define* (latest-channel-instance store channel
872898f7 376 #:key (patches %patches)
5bafc70d 377 starting-commit
a9eeeaa6 378 (authenticate? #f)
5bafc70d
LC
379 (validate-pull
380 ensure-forward-channel-update))
381 "Return the latest channel instance for CHANNEL. When STARTING-COMMIT is
382true, call VALIDATE-PULL with CHANNEL, STARTING-COMMIT, the target commit, and
a9eeeaa6 383their relation. When AUTHENTICATE? is false, CHANNEL is not authenticated."
053b10c3
LC
384 (define (dot-git? file stat)
385 (and (string=? (basename file) ".git")
386 (eq? 'directory (stat:type stat))))
387
c3f6f564
LC
388 (let-values (((channel)
389 (ensure-default-introduction channel))
390 ((checkout commit relation)
053b10c3 391 (update-cached-checkout (channel-url channel)
872898f7
LC
392 #:ref (channel-reference channel)
393 #:starting-commit starting-commit)))
5bafc70d
LC
394 (when relation
395 (validate-pull channel starting-commit commit relation))
396
a9eeeaa6
LC
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)
d51bfe24
LC
403 (raise (make-compound-condition
404 (formatted-message (G_ "channel '~a' lacks an \
ead5c461 405introduction and cannot be authenticated~%")
d51bfe24
LC
406 (channel-name channel))
407 (condition
408 (&fix-hint
409 (hint (G_ "Add the missing introduction to your
ead5c461
LC
410channels file to address the issue. Alternatively, you can pass
411@option{--disable-authentication}, at the risk of running unauthenticated and
d51bfe24 412thus potentially malicious code."))))))))
a9eeeaa6 413 (warning (G_ "channel authentication disabled~%")))
43badf26 414
053b10c3
LC
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?))))
5bafc70d 423 (channel-instance channel commit checkout))))
872898f7 424
5bafc70d 425(define (ensure-forward-channel-update channel start commit relation)
872898f7 426 "Raise an error if RELATION is not 'ancestor, meaning that START is not an
5bafc70d 427ancestor of COMMIT, unless CHANNEL specifies a commit.
872898f7
LC
428
429This 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 (_
9744cc7b
LC
435 (raise (make-compound-condition
436 (condition
437 (&message (message
438 (format #f (G_ "\
872898f7 439aborting update of channel '~a' to commit ~a, which is not a descendant of ~a")
9744cc7b 440 (channel-name channel)
5bafc70d 441 commit start))))
872898f7 442
9744cc7b
LC
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
450this downgrade."))))
451 (condition
452 (&fix-hint
453 (hint (G_ "This could indicate that the channel has
872898f7
LC
454been tampered with and is trying to force a roll-back, preventing you from
455getting the latest updates. If you think this is not the case, explicitly
9744cc7b 456allow non-forward updates."))))))))))
872898f7 457
4ae762af
LC
458(define (channel-instance-primary-url instance)
459 "Return the primary URL advertised for INSTANCE, or #f if there is no such
460information."
461 (channel-metadata-url (channel-instance-metadata instance)))
462
872898f7
LC
463(define* (latest-channel-instances store channels
464 #:key
465 (current-channels '())
a9eeeaa6 466 (authenticate? #t)
872898f7
LC
467 (validate-pull
468 ensure-forward-channel-update))
0d39a3b9 469 "Return a list of channel instances corresponding to the latest checkouts of
872898f7
LC
470CHANNELS and the channels on which they depend.
471
a9eeeaa6
LC
472When AUTHENTICATE? is true, authenticate the subset of CHANNELS that has a
473\"channel introduction\".
474
872898f7
LC
475CURRENT-CHANNELS is the list of currently used channels. It is compared
476against the newly-fetched instances of CHANNELS, and VALIDATE-PULL is called
477for each channel update and can choose to emit warnings or raise an error,
478depending on the policy it implements."
af12790b
RW
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))))))))
f58f676b 488
872898f7
LC
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
9b049de8
LC
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))
5bafc70d
LC
509 (let* ((current (current-commit (channel-name channel)))
510 (instance
511 (latest-channel-instance store channel
a9eeeaa6
LC
512 #:authenticate?
513 authenticate?
5bafc70d
LC
514 #:validate-pull
515 validate-pull
516 #:starting-commit
517 current)))
4ae762af
LC
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 \
528of ~a, which might be stale~%")
529 (channel-name channel)
530 primary-url))))
872898f7 531
9b049de8
LC
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))
f58f676b 542
9b049de8
LC
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))))
0d39a3b9 550
fe5db4eb
LC
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
555of 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
0d39a3b9
LC
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
acefa740
LC
573(define (standard-module-derivation name source core dependencies)
574 "Return a derivation that builds with CORE, a Guix instance, the Scheme
575modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
576objects. The assumption is that SOURCE contains package modules to be added
577to '%package-module-path'."
acefa740 578
53f21642 579 (let* ((metadata (read-channel-metadata-from-source source))
ce5d9ec8 580 (directory (channel-metadata-directory metadata)))
53f21642
JN
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)))
acefa740 599
ce5d9ec8 600 (let* ((subdir #$directory)
53f21642
JN
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))
acefa740 605
53f21642 606 scm)))
acefa740 607
53f21642 608 (gexp->derivation-in-inferior name build core)))
0d39a3b9 609
37c0d458
LC
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
9db88369
LC
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
631EXP, a series of monadic expressions."
632 (call-with-guile guile (lambda ()
633 (mbegin %store-monad exp ...))))
634
42a87136
LC
635(define (with-trivial-build-handler mvalue)
636 "Run MVALUE, a monadic value, with a \"trivial\" build handler installed
637that 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
316fc2ac
LC
644(define* (build-from-source instance
645 #:key core verbose? (dependencies '()))
646 "Return a derivation to build Guix from INSTANCE, using the self-build
647script contained therein. When CORE is true, build package modules under
648SOURCE 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
0d39a3b9
LC
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
316fc2ac 660 ;; know.
0d39a3b9
LC
661 (define script
662 (string-append source "/" %self-build-file))
663
664 (if (file-exists? script)
665 (let ((build (save-module-excursion
666 (lambda ()
3a8c4860
LC
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.
69962ab7 670 (parameterize ((guix-warning-port
3a8c4860 671 (%make-void-port "w")))
37c0d458
LC
672 (primitive-load script)))))
673 (guile (guile-for-source source)))
0d39a3b9
LC
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.
9db88369 680 (with-guile guile
42a87136
LC
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
316fc2ac
LC
686 (build source
687 #:verbose? verbose? #:version commit
688 #:channel-metadata (channel-instance->sexp instance)
42a87136 689 #:pull-version %pull-version))))
0d39a3b9
LC
690
691 ;; Build a set of modules that extend Guix using the standard method.
acefa740 692 (standard-module-derivation name source core dependencies)))
0d39a3b9 693
acefa740
LC
694(define* (build-channel-instance instance
695 #:optional core (dependencies '()))
0d39a3b9
LC
696 "Return, as a monadic value, the derivation for INSTANCE, a channel
697instance. DEPENDENCIES is a list of extensions providing Guile modules that
698INSTANCE depends on."
316fc2ac 699 (build-from-source instance
acefa740 700 #:core core
0d39a3b9
LC
701 #:dependencies dependencies))
702
ed75bdf3
LC
703(define (resolve-dependencies instances)
704 "Return a procedure that, given one of the elements of INSTANCES, returns
705list 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
0d39a3b9
LC
731(define (channel-instance-derivations instances)
732 "Return the list of derivations to build INSTANCES, in the same order as
733INSTANCES."
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
ed75bdf3
LC
741 (define edges
742 (resolve-dependencies instances))
743
744 (define (instance->derivation instance)
cdf68947
LC
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)))
ed75bdf3 754
ab6025b5
LC
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")))
f75243e1
LC
762 (condition
763 (&fix-hint (hint (G_ "Make sure your list of channels
764contains one channel named @code{guix} providing the core of Guix."))))
ab6025b5
LC
765 (if loc
766 (list (condition (&error-location (location loc))))
767 '())))))
768
ed75bdf3 769 (mapm %store-monad instance->derivation instances))
0d39a3b9 770
0d39a3b9
LC
771(define (whole-package-for-legacy name modules)
772 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
773modules in the old ~/.config/guix/latest style."
774 (define packages
775 (resolve-interface '(gnu packages guile)))
776
49c35bbb
LC
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
0d39a3b9
LC
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 ...))))))
49c35bbb 808 (whole-package name modules+compiled
0d39a3b9
LC
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.
b74ed909 813 (list guile-json-3 guile-git guile-bytestructures
6c46e477
LC
814 (ssh -> guile-ssh) (tls -> gnutls))
815 #:guile (default-guile))))
0d39a3b9
LC
816
817(define (old-style-guix? drv)
818 "Return true if DRV corresponds to a ~/.config/guix/latest style of
819derivation."
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)
9af75a26
LC
824 (string=? "guix-command"
825 (derivation-name
826 (derivation-input-derivation input))))
0d39a3b9
LC
827 (derivation-inputs drv))))
828
9272cc70
LC
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)
a47f16a8 839 (name ,(channel-name channel))
9272cc70
LC
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
0d39a3b9
LC
853(define (channel-instances->manifest instances)
854 "Return a profile manifest with entries for all of INSTANCES, a list of
855channel instances."
d9e6217f 856 (define (instance->entry instance drv)
9272cc70
LC
857 (let ((commit (channel-instance-commit instance))
858 (channel (channel-instance-channel instance)))
d9e6217f
LC
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
9272cc70 869 `((source ,(channel-instance->sexp instance)))))))
0d39a3b9
LC
870
871 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
d9e6217f 872 (entries -> (map instance->entry instances derivations)))
0d39a3b9 873 (return (manifest entries))))
030f1367 874
5fbdc9a5
LC
875(define (package-cache-file manifest)
876 "Build a package cache file for the instance in MANIFEST. This is meant to
877be used as a profile hook."
ccbc427f 878 (let ((profile (profile (content manifest) (hooks '()))))
5fbdc9a5
LC
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
4035fcba
LC
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
5fbdc9a5 900 #:properties '((type . profile-hook)
f674bc66
LC
901 (hook . package-cache))
902 #:local-build? #t)))
5fbdc9a5
LC
903
904(define %channel-profile-hooks
905 ;; The default channel profile hooks.
906 (cons package-cache-file %default-profile-hooks))
907
c37f38bd
LC
908(define (channel-instances->derivation instances)
909 "Return the derivation of the profile containing INSTANCES, a list of
910channel instances."
911 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
5fbdc9a5
LC
912 (profile-derivation manifest
913 #:hooks %channel-profile-hooks)))
c37f38bd 914
030f1367
LC
915(define latest-channel-instances*
916 (store-lift latest-channel-instances))
917
872898f7
LC
918(define* (latest-channel-derivation #:optional (channels %default-channels)
919 #:key
920 (current-channels '())
921 (validate-pull
922 ensure-forward-channel-update))
030f1367 923 "Return as a monadic value the derivation that builds the profile for the
872898f7
LC
924latest instances of CHANNELS. CURRENT-CHANNELS and VALIDATE-PULL are passed
925to '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)))
c37f38bd 932 (channel-instances->derivation instances)))
a7c714d3 933
9272cc70 934(define* (sexp->channel sexp #:optional (name 'channel))
a47f16a8
LC
935 "Read SEXP, a provenance sexp as created by 'channel-instance->sexp'; use
936NAME as the channel name if SEXP does not specify it. Return #f if the sexp
937does not have the expected structure."
9272cc70
LC
938 (match sexp
939 (('repository ('version 0)
940 ('url url)
941 ('branch branch)
942 ('commit commit)
943 rest ...)
a47f16a8
LC
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)))
9272cc70 951 (url url)
8f999e1a 952 (branch branch)
9272cc70
LC
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
964missing 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
a7c714d3
LC
976(define (profile-channels profile)
977 "Return the list of channels corresponding to entries in PROFILE. If
978PROFILE is not a profile created by 'guix pull', return the empty list."
9272cc70 979 (filter-map manifest-entry-channel
a7c714d3
LC
980 ;; Show most recently installed packages last.
981 (reverse
982 (manifest-entries (profile-manifest profile)))))
8ba7fd3c 983
60d72f53
LC
984(define* (channel->code channel #:key (include-introduction? #t))
985 "Return code (an sexp) to build CHANNEL. When INCLUDE-INTRODUCTION? is
986true, 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
8ba7fd3c
LC
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>
9719e8d3 1015 (channel-news-entry commit tag title body)
8ba7fd3c 1016 channel-news-entry?
9719e8d3
LC
1017 (commit channel-news-entry-commit) ;hex string | #f
1018 (tag channel-news-entry-tag) ;#f | string
8ba7fd3c
LC
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
9719e8d3 1028 (('entry ((and (or 'commit 'tag) type) commit-or-tag)
8ba7fd3c
LC
1029 ('title ((? symbol? title-tags) (? string? titles)) ...)
1030 ('body ((? symbol? body-tags) (? string? bodies)) ...)
1031 _ ...)
9719e8d3
LC
1032 (channel-news-entry (and (eq? type 'commit) commit-or-tag)
1033 (and (eq? type 'tag) commit-or-tag)
8ba7fd3c
LC
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>
1045record."
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
9719e8d3
LC
1064(define (resolve-channel-news-entry-tag repository entry)
1065 "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
1066ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
1067the field its 'tag' refers to. A 'git-error' exception is raised if the tag
1068cannot 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
8ba7fd3c
LC
1078(define* (channel-news-for-commit channel new #:optional old)
1079 "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
1080NEW. 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))
9719e8d3
LC
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
8ba7fd3c
LC
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)))
9719e8d3
LC
1106 entries))
1107 entries)))
8ba7fd3c
LC
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)))))
9db88369
LC
1116
1117;;; Local Variables:
1118;;; eval: (put 'with-guile 'scheme-indent-function 1)
1119;;; End: