guix: qt-utils: Don't include useless inputs in wrapped variables.
[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
a514b4ab
LC
600 (define optimizations-for-level
601 ;; Guile 3.0 provides this procedure but Guile 2.2 didn't.
602 ;; Since this code may be executed by either version, we can't
603 ;; rely on its availability.
604 (or (and=> (false-if-exception
605 (resolve-interface '(system base optimize)))
606 (lambda (iface)
607 (module-ref iface 'optimizations-for-level)))
608 (const '())))
609
610 (define -O1
611 ;; Optimize for package module compilation speed.
612 (optimizations-for-level 1))
613
ce5d9ec8 614 (let* ((subdir #$directory)
53f21642 615 (source (string-append #$source subdir)))
a514b4ab
LC
616 (compile-files source go (find-files source "\\.scm$")
617 #:optimization-options (const -O1))
53f21642
JN
618 (mkdir-p (dirname scm))
619 (symlink (string-append #$source subdir) scm))
acefa740 620
53f21642 621 scm)))
acefa740 622
53f21642 623 (gexp->derivation-in-inferior name build core)))
0d39a3b9 624
37c0d458
LC
625(define* (guile-for-source source #:optional (quirks %quirks))
626 "Return the Guile package to use when building SOURCE or #f if the default
627'%guile-for-build' should be good enough."
628 (let loop ((quirks quirks))
629 (match quirks
630 (()
631 #f)
632 (((predicate . guile) rest ...)
633 (if (predicate source) (guile) (loop rest))))))
634
9db88369
LC
635(define (call-with-guile guile thunk)
636 (lambda (store)
637 (values (parameterize ((%guile-for-build
638 (if guile
639 (package-derivation store guile)
640 (%guile-for-build))))
641 (run-with-store store (thunk)))
642 store)))
643
644(define-syntax-rule (with-guile guile exp ...)
645 "Set GUILE as the '%guile-for-build' parameter for the dynamic extent of
646EXP, a series of monadic expressions."
647 (call-with-guile guile (lambda ()
648 (mbegin %store-monad exp ...))))
649
42a87136
LC
650(define (with-trivial-build-handler mvalue)
651 "Run MVALUE, a monadic value, with a \"trivial\" build handler installed
652that unconditionally resumes the continuation."
653 (lambda (store)
654 (with-build-handler (lambda (continue . _)
655 (continue #t))
656 (values (run-with-store store mvalue)
657 store))))
658
316fc2ac 659(define* (build-from-source instance
34985fb6 660 #:key core verbose? (dependencies '()) system)
316fc2ac
LC
661 "Return a derivation to build Guix from INSTANCE, using the self-build
662script contained therein. When CORE is true, build package modules under
34985fb6
CB
663SOURCE using CORE, an instance of Guix. By default, build for the current
664system, or SYSTEM if specified."
316fc2ac
LC
665 (define name
666 (symbol->string
667 (channel-name (channel-instance-channel instance))))
668 (define source
669 (channel-instance-checkout instance))
670 (define commit
671 (channel-instance-commit instance))
672
0d39a3b9
LC
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
316fc2ac 676 ;; know.
0d39a3b9
LC
677 (define script
678 (string-append source "/" %self-build-file))
679
680 (if (file-exists? script)
681 (let ((build (save-module-excursion
682 (lambda ()
3a8c4860
LC
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.
69962ab7 686 (parameterize ((guix-warning-port
3a8c4860 687 (%make-void-port "w")))
37c0d458
LC
688 (primitive-load script)))))
689 (guile (guile-for-source source)))
0d39a3b9
LC
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.
9db88369 696 (with-guile guile
42a87136
LC
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
316fc2ac
LC
702 (build source
703 #:verbose? verbose? #:version commit
34985fb6 704 #:system system
316fc2ac 705 #:channel-metadata (channel-instance->sexp instance)
42a87136 706 #:pull-version %pull-version))))
0d39a3b9
LC
707
708 ;; Build a set of modules that extend Guix using the standard method.
acefa740 709 (standard-module-derivation name source core dependencies)))
0d39a3b9 710
34985fb6 711(define* (build-channel-instance instance system
acefa740 712 #:optional core (dependencies '()))
0d39a3b9 713 "Return, as a monadic value, the derivation for INSTANCE, a channel
34985fb6
CB
714instance, for SYSTEM. DEPENDENCIES is a list of extensions providing Guile
715modules that INSTANCE depends on."
316fc2ac 716 (build-from-source instance
acefa740 717 #:core core
34985fb6
CB
718 #:dependencies dependencies
719 #:system system))
0d39a3b9 720
ed75bdf3
LC
721(define (resolve-dependencies instances)
722 "Return a procedure that, given one of the elements of INSTANCES, returns
723list of instances it depends on."
724 (define channel-instance-name
725 (compose channel-name channel-instance-channel))
726
727 (define table ;map a name to an instance
728 (fold (lambda (instance table)
729 (vhash-consq (channel-instance-name instance)
730 instance table))
731 vlist-null
732 instances))
733
734 (define edges
735 (fold (lambda (instance edges)
736 (fold (lambda (channel edges)
737 (let ((name (channel-name channel)))
738 (match (vhash-assq name table)
739 ((_ . target)
740 (vhash-consq instance target edges)))))
741 edges
742 (channel-instance-dependencies instance)))
743 vlist-null
744 instances))
745
746 (lambda (instance)
747 (vhash-foldq* cons '() instance edges)))
748
34985fb6 749(define* (channel-instance-derivations instances #:key system)
0d39a3b9 750 "Return the list of derivations to build INSTANCES, in the same order as
34985fb6 751INSTANCES. Build for the current system by default, or SYSTEM if specified."
0d39a3b9
LC
752 (define core-instance
753 ;; The 'guix' channel is treated specially: it's an implicit dependency of
754 ;; all the other channels.
755 (find (lambda (instance)
756 (guix-channel? (channel-instance-channel instance)))
757 instances))
758
ed75bdf3
LC
759 (define edges
760 (resolve-dependencies instances))
761
762 (define (instance->derivation instance)
34985fb6 763 (mlet %store-monad ((system (if system (return system) (current-system))))
cdf68947 764 (mcached (if (eq? instance core-instance)
34985fb6 765 (build-channel-instance instance system)
cdf68947
LC
766 (mlet %store-monad ((core (instance->derivation core-instance))
767 (deps (mapm %store-monad instance->derivation
768 (edges instance))))
34985fb6 769 (build-channel-instance instance system core deps)))
cdf68947
LC
770 instance
771 system)))
ed75bdf3 772
ab6025b5
LC
773 (unless core-instance
774 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
775 instances)
776 source-properties->location)))
777 (raise (apply make-compound-condition
778 (condition
779 (&message (message "'guix' channel is lacking")))
f75243e1
LC
780 (condition
781 (&fix-hint (hint (G_ "Make sure your list of channels
782contains one channel named @code{guix} providing the core of Guix."))))
ab6025b5
LC
783 (if loc
784 (list (condition (&error-location (location loc))))
785 '())))))
786
ed75bdf3 787 (mapm %store-monad instance->derivation instances))
0d39a3b9 788
0d39a3b9
LC
789(define (whole-package-for-legacy name modules)
790 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
791modules in the old ~/.config/guix/latest style."
792 (define packages
793 (resolve-interface '(gnu packages guile)))
794
49c35bbb
LC
795 (define modules+compiled
796 ;; Since MODULES contains both .scm and .go files at its root, re-bundle
797 ;; it so that it has share/guile/site and lib/guile, which is what
798 ;; 'whole-package' expects.
799 (computed-file (derivation-name modules)
800 (with-imported-modules '((guix build utils))
801 #~(begin
802 (use-modules (guix build utils))
803
804 (define version
805 (effective-version))
806 (define share
807 (string-append #$output "/share/guile/site"))
808 (define lib
809 (string-append #$output "/lib/guile/" version))
810
811 (mkdir-p share) (mkdir-p lib)
812 (symlink #$modules (string-append share "/" version))
813 (symlink #$modules (string-append lib "/site-ccache"))))))
814
0d39a3b9
LC
815 (letrec-syntax ((list (syntax-rules (->)
816 ((_)
817 '())
818 ((_ (module -> variable) rest ...)
819 (cons (module-ref (resolve-interface
820 '(gnu packages module))
821 'variable)
822 (list rest ...)))
823 ((_ variable rest ...)
824 (cons (module-ref packages 'variable)
825 (list rest ...))))))
49c35bbb 826 (whole-package name modules+compiled
0d39a3b9
LC
827
828 ;; In the "old style", %SELF-BUILD-FILE would simply return a
829 ;; derivation that builds modules. We have to infer what the
830 ;; dependencies of these modules were.
b74ed909 831 (list guile-json-3 guile-git guile-bytestructures
6c46e477
LC
832 (ssh -> guile-ssh) (tls -> gnutls))
833 #:guile (default-guile))))
0d39a3b9
LC
834
835(define (old-style-guix? drv)
836 "Return true if DRV corresponds to a ~/.config/guix/latest style of
837derivation."
838 ;; Here we rely on a gross historical fact: that derivations produced by the
839 ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
840 ;; dated May 30, 2018) did not depend on "guix-command.drv".
841 (not (find (lambda (input)
9af75a26
LC
842 (string=? "guix-command"
843 (derivation-name
844 (derivation-input-derivation input))))
0d39a3b9
LC
845 (derivation-inputs drv))))
846
9272cc70
LC
847(define (channel-instance->sexp instance)
848 "Return an sexp representation of INSTANCE, a channel instance."
849 (let* ((commit (channel-instance-commit instance))
850 (channel (channel-instance-channel instance))
851 (intro (channel-introduction channel)))
852 `(repository
853 (version 0)
854 (url ,(channel-url channel))
855 (branch ,(channel-branch channel))
856 (commit ,commit)
a47f16a8 857 (name ,(channel-name channel))
9272cc70
LC
858 ,@(if intro
859 `((introduction
860 (channel-introduction
861 (version 0)
862 (commit
863 ,(channel-introduction-first-signed-commit
864 intro))
865 (signer
866 ,(openpgp-format-fingerprint
867 (channel-introduction-first-commit-signer
868 intro))))))
869 '()))))
870
34985fb6 871(define* (channel-instances->manifest instances #:key system)
0d39a3b9 872 "Return a profile manifest with entries for all of INSTANCES, a list of
34985fb6
CB
873channel instances. By default, build for the current system, or SYSTEM if
874specified."
d9e6217f 875 (define (instance->entry instance drv)
9272cc70
LC
876 (let ((commit (channel-instance-commit instance))
877 (channel (channel-instance-channel instance)))
d9e6217f
LC
878 (manifest-entry
879 (name (symbol->string (channel-name channel)))
880 (version (string-take commit 7))
881 (item (if (guix-channel? channel)
882 (if (old-style-guix? drv)
883 (whole-package-for-legacy (string-append name "-" version)
884 drv)
885 drv)
886 drv))
887 (properties
9272cc70 888 `((source ,(channel-instance->sexp instance)))))))
0d39a3b9 889
34985fb6
CB
890 (mlet* %store-monad ((derivations (channel-instance-derivations instances
891 #:system system))
d9e6217f 892 (entries -> (map instance->entry instances derivations)))
0d39a3b9 893 (return (manifest entries))))
030f1367 894
5fbdc9a5
LC
895(define (package-cache-file manifest)
896 "Build a package cache file for the instance in MANIFEST. This is meant to
897be used as a profile hook."
ccbc427f 898 (let ((profile (profile (content manifest) (hooks '()))))
5fbdc9a5
LC
899 (define build
900 #~(begin
901 (use-modules (gnu packages))
902
903 (if (defined? 'generate-package-cache)
904 (begin
905 ;; Delegate package cache generation to the inferior.
906 (format (current-error-port)
907 "Generating package cache for '~a'...~%"
908 #$profile)
909 (generate-package-cache #$output))
910 (mkdir #$output))))
911
912 (gexp->derivation-in-inferior "guix-package-cache" build
913 profile
4035fcba
LC
914
915 ;; If the Guix in PROFILE is too old and
916 ;; lacks 'guix repl', don't build the cache
917 ;; instead of failing.
918 #:silent-failure? #t
919
5fbdc9a5 920 #:properties '((type . profile-hook)
f674bc66
LC
921 (hook . package-cache))
922 #:local-build? #t)))
5fbdc9a5
LC
923
924(define %channel-profile-hooks
925 ;; The default channel profile hooks.
926 (cons package-cache-file %default-profile-hooks))
927
c37f38bd
LC
928(define (channel-instances->derivation instances)
929 "Return the derivation of the profile containing INSTANCES, a list of
930channel instances."
931 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
5fbdc9a5
LC
932 (profile-derivation manifest
933 #:hooks %channel-profile-hooks)))
c37f38bd 934
030f1367
LC
935(define latest-channel-instances*
936 (store-lift latest-channel-instances))
937
872898f7
LC
938(define* (latest-channel-derivation #:optional (channels %default-channels)
939 #:key
940 (current-channels '())
941 (validate-pull
942 ensure-forward-channel-update))
030f1367 943 "Return as a monadic value the derivation that builds the profile for the
872898f7
LC
944latest instances of CHANNELS. CURRENT-CHANNELS and VALIDATE-PULL are passed
945to 'latest-channel-instances'."
946 (mlet %store-monad ((instances
947 (latest-channel-instances* channels
948 #:current-channels
949 current-channels
950 #:validate-pull
951 validate-pull)))
c37f38bd 952 (channel-instances->derivation instances)))
a7c714d3 953
9272cc70 954(define* (sexp->channel sexp #:optional (name 'channel))
a47f16a8
LC
955 "Read SEXP, a provenance sexp as created by 'channel-instance->sexp'; use
956NAME as the channel name if SEXP does not specify it. Return #f if the sexp
957does not have the expected structure."
9272cc70
LC
958 (match sexp
959 (('repository ('version 0)
960 ('url url)
961 ('branch branch)
962 ('commit commit)
963 rest ...)
a47f16a8
LC
964 ;; Historically channel sexps did not include the channel name. It's OK
965 ;; for channels created by 'channel-instances->manifest' because the
966 ;; entry name is the channel name, but it was missing for entries created
967 ;; by 'manifest-entry-with-provenance'.
968 (channel (name (match (assq 'name rest)
969 (#f name)
970 (('name name) name)))
9272cc70 971 (url url)
8f999e1a 972 (branch branch)
9272cc70
LC
973 (commit commit)
974 (introduction
975 (match (assq 'introduction rest)
976 (#f #f)
977 (('introduction intro)
978 (sexp->channel-introduction intro))))))
979
980 (_ #f)))
981
982(define (manifest-entry-channel entry)
983 "Return the channel ENTRY corresponds to, or #f if that information is
984missing or unreadable. ENTRY must be an entry created by
985'channel-instances->manifest', with the 'source' property."
986 (let ((name (string->symbol (manifest-entry-name entry))))
987 (match (assq-ref (manifest-entry-properties entry) 'source)
988 ((sexp)
989 (sexp->channel sexp name))
990 (_
991 ;; No channel information for this manifest entry.
992 ;; XXX: Pre-0.15.0 Guix did not provide that information,
993 ;; but there's not much we can do in that case.
994 #f))))
995
a7c714d3
LC
996(define (profile-channels profile)
997 "Return the list of channels corresponding to entries in PROFILE. If
998PROFILE is not a profile created by 'guix pull', return the empty list."
9272cc70 999 (filter-map manifest-entry-channel
a7c714d3
LC
1000 ;; Show most recently installed packages last.
1001 (reverse
1002 (manifest-entries (profile-manifest profile)))))
8ba7fd3c 1003
60d72f53
LC
1004(define* (channel->code channel #:key (include-introduction? #t))
1005 "Return code (an sexp) to build CHANNEL. When INCLUDE-INTRODUCTION? is
1006true, include its introduction, if any."
1007 (let ((intro (and include-introduction?
1008 (channel-introduction channel))))
1009 `(channel
1010 (name ',(channel-name channel))
1011 (url ,(channel-url channel))
1012 (commit ,(channel-commit channel))
1013 ,@(if intro
1014 `((introduction (make-channel-introduction
1015 ,(channel-introduction-first-signed-commit intro)
1016 (openpgp-fingerprint
1017 ,(openpgp-format-fingerprint
1018 (channel-introduction-first-commit-signer
1019 intro))))))
1020 '()))))
1021
8ba7fd3c
LC
1022\f
1023;;;
1024;;; News.
1025;;;
1026
1027;; Channel news.
1028(define-record-type <channel-news>
1029 (channel-news entries)
1030 channel-news?
1031 (entries channel-news-entries)) ;list of <channel-news-entry>
1032
1033;; News entry, associated with a specific commit of the channel.
1034(define-record-type <channel-news-entry>
9719e8d3 1035 (channel-news-entry commit tag title body)
8ba7fd3c 1036 channel-news-entry?
9719e8d3
LC
1037 (commit channel-news-entry-commit) ;hex string | #f
1038 (tag channel-news-entry-tag) ;#f | string
8ba7fd3c
LC
1039 (title channel-news-entry-title) ;list of language tag/string pairs
1040 (body channel-news-entry-body)) ;list of language tag/string pairs
1041
1042(define (sexp->channel-news-entry entry)
1043 "Return the <channel-news-entry> record corresponding to ENTRY, an sexp."
1044 (define (pair language message)
1045 (cons (symbol->string language) message))
1046
1047 (match entry
9719e8d3 1048 (('entry ((and (or 'commit 'tag) type) commit-or-tag)
8ba7fd3c
LC
1049 ('title ((? symbol? title-tags) (? string? titles)) ...)
1050 ('body ((? symbol? body-tags) (? string? bodies)) ...)
1051 _ ...)
9719e8d3
LC
1052 (channel-news-entry (and (eq? type 'commit) commit-or-tag)
1053 (and (eq? type 'tag) commit-or-tag)
8ba7fd3c
LC
1054 (map pair title-tags titles)
1055 (map pair body-tags bodies)))
1056 (_
1057 (raise (condition
1058 (&message (message "invalid channel news entry"))
1059 (&error-location
1060 (location (source-properties->location
1061 (source-properties entry)))))))))
1062
1063(define (read-channel-news port)
1064 "Read a channel news feed from PORT and return it as a <channel-news>
1065record."
1066 (match (false-if-exception (read port))
1067 (('channel-news ('version 0) entries ...)
1068 (channel-news (map sexp->channel-news-entry entries)))
1069 (('channel-news ('version version) _ ...)
1070 ;; This is an unsupported version from the future. There's nothing wrong
1071 ;; with that (the user may simply need to upgrade the 'guix' channel to
1072 ;; be able to read it), so silently ignore it.
1073 (channel-news '()))
1074 (#f
1075 (raise (condition
1076 (&message (message "syntactically invalid channel news file")))))
1077 (sexp
1078 (raise (condition
1079 (&message (message "invalid channel news file"))
1080 (&error-location
1081 (location (source-properties->location
1082 (source-properties sexp)))))))))
1083
9719e8d3
LC
1084(define (resolve-channel-news-entry-tag repository entry)
1085 "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
1086ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
1087the field its 'tag' refers to. A 'git-error' exception is raised if the tag
1088cannot be found."
1089 (if (channel-news-entry-commit entry)
1090 entry
1091 (let* ((tag (channel-news-entry-tag entry))
1092 (reference (string-append "refs/tags/" tag))
1093 (oid (reference-name->oid repository reference)))
1094 (channel-news-entry (oid->string oid) tag
1095 (channel-news-entry-title entry)
1096 (channel-news-entry-body entry)))))
1097
8ba7fd3c
LC
1098(define* (channel-news-for-commit channel new #:optional old)
1099 "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
1100NEW. When OLD is omitted or is #f, return all the news entries of CHANNEL."
1101 (catch 'git-error
1102 (lambda ()
1103 (let* ((checkout (update-cached-checkout (channel-url channel)
1104 #:ref `(commit . ,new)))
1105 (metadata (read-channel-metadata-from-source checkout))
1106 (news-file (channel-metadata-news-file metadata))
1107 (news-file (and news-file
1108 (string-append checkout "/" news-file))))
1109 (if (and news-file (file-exists? news-file))
9719e8d3
LC
1110 (with-repository checkout repository
1111 (let* ((news (call-with-input-file news-file
1112 read-channel-news))
1113 (entries (map (lambda (entry)
1114 (resolve-channel-news-entry-tag repository
1115 entry))
1116 (channel-news-entries news))))
1117 (if old
8ba7fd3c
LC
1118 (let* ((new (commit-lookup repository (string->oid new)))
1119 (old (commit-lookup repository (string->oid old)))
1120 (commits (list->set
1121 (map (compose oid->string commit-id)
1122 (commit-difference new old)))))
1123 (filter (lambda (entry)
1124 (set-contains? commits
1125 (channel-news-entry-commit entry)))
9719e8d3
LC
1126 entries))
1127 entries)))
8ba7fd3c
LC
1128 '())))
1129 (lambda (key error . rest)
1130 ;; If commit NEW or commit OLD cannot be found, then something must be
1131 ;; wrong (for example, the history of CHANNEL was rewritten and these
1132 ;; commits no longer exist upstream), so quietly return the empty list.
1133 (if (= GIT_ENOTFOUND (git-error-code error))
1134 '()
1135 (apply throw key error rest)))))
9db88369
LC
1136
1137;;; Local Variables:
1138;;; eval: (put 'with-guile 'scheme-indent-function 1)
1139;;; End: