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