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