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>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
21 (define-module (guix channels)
23 #:use-module (guix git)
24 #:use-module (guix records)
25 #:use-module (guix gexp)
26 #:use-module (guix modules)
27 #:use-module (guix discovery)
28 #:use-module (guix monads)
29 #:use-module (guix profiles)
30 #:use-module (guix packages)
31 #:use-module (guix derivations)
32 #:use-module (guix combinators)
33 #:use-module (guix diagnostics)
34 #:use-module (guix sets)
35 #:use-module (guix store)
36 #:use-module (guix i18n)
37 #:use-module ((guix utils)
38 #:select (source-properties->location
41 #:use-module (srfi srfi-1)
42 #:use-module (srfi srfi-2)
43 #:use-module (srfi srfi-9)
44 #:use-module (srfi srfi-11)
45 #:use-module (srfi srfi-26)
46 #:use-module (srfi srfi-34)
47 #:use-module (srfi srfi-35)
48 #:autoload (guix self) (whole-package make-config.scm)
49 #:autoload (guix inferior) (gexp->derivation-in-inferior) ;FIXME: circular dep
50 #:autoload (guix quirks) (%quirks %patches applicable-patch? apply-patch)
51 #:use-module (ice-9 match)
52 #:use-module (ice-9 vlist)
53 #:use-module ((ice-9 rdelim) #:select (read-string))
66 channel-instance-channel
67 channel-instance-commit
68 channel-instance-checkout
70 latest-channel-instances
71 checkout->channel-instance
72 latest-channel-derivation
73 channel-instances->manifest
74 %channel-profile-hooks
75 channel-instances->derivation
80 channel-news-entry-commit
81 channel-news-entry-tag
82 channel-news-entry-title
83 channel-news-entry-body
85 channel-news-for-commit))
89 ;;; This module implements "channels." A channel is usually a source of
90 ;;; package definitions. There's a special channel, the 'guix' channel, that
91 ;;; provides all of Guix, including its commands and its documentation.
92 ;;; User-defined channels are expected to typically provide a bunch of .scm
93 ;;; files meant to be added to the '%package-search-path'.
95 ;;; This module provides tools to fetch and update channels from a Git
96 ;;; repository and to build them.
100 (define-record-type* <channel> channel make-channel
104 (branch channel-branch (default "master"))
105 (commit channel-commit (default #f))
106 (location channel-location
107 (default (current-source-location)) (innate)))
109 (define %default-channels
110 ;; Default list of channels.
114 (url "https://git.savannah.gnu.org/git/guix.git"))))
116 (define (guix-channel? channel)
117 "Return true if CHANNEL is the 'guix' channel."
118 (eq? 'guix (channel-name channel)))
120 (define-record-type <channel-instance>
121 (channel-instance channel commit checkout)
123 (channel channel-instance-channel)
124 (commit channel-instance-commit)
125 (checkout channel-instance-checkout))
127 (define-record-type <channel-metadata>
128 (channel-metadata directory dependencies news-file)
130 (directory channel-metadata-directory) ;string with leading slash
131 (dependencies channel-metadata-dependencies) ;list of <channel>
132 (news-file channel-metadata-news-file)) ;string | #f
134 (define (channel-reference channel)
135 "Return the \"reference\" for CHANNEL, an sexp suitable for
136 'latest-repository-commit'."
137 (match (channel-commit channel)
138 (#f `(branch . ,(channel-branch channel)))
139 (commit `(commit . ,(channel-commit channel)))))
141 (define (read-channel-metadata port)
142 "Read from PORT channel metadata in the format expected for the
143 '.guix-channel' file. Return a <channel-metadata> record, or raise an error
144 if valid metadata could not be read from PORT."
146 (('channel ('version 0) properties ...)
147 (let ((directory (and=> (assoc-ref properties 'directory) first))
148 (dependencies (or (assoc-ref properties 'dependencies) '()))
149 (news-file (and=> (assoc-ref properties 'news-file) first)))
151 (cond ((not directory) "/") ;directory
152 ((string-prefix? "/" directory) directory)
153 (else (string-append "/" directory)))
154 (map (lambda (item) ;dependencies
155 (let ((get (lambda* (key #:optional default)
156 (or (and=> (assoc-ref item key) first) default))))
157 (and-let* ((name (get 'name))
159 (branch (get 'branch "master")))
164 (commit (get 'commit))))))
166 news-file))) ;news-file
167 ((and ('channel ('version version) _ ...) sexp)
169 (&message (message "unsupported '.guix-channel' version"))
171 (location (source-properties->location
172 (source-properties sexp)))))))
175 (&message (message "invalid '.guix-channel' file"))
177 (location (source-properties->location
178 (source-properties sexp)))))))))
180 (define (read-channel-metadata-from-source source)
181 "Return a channel-metadata record read from channel's SOURCE/.guix-channel
182 description file, or return the default channel-metadata record if that file
186 (call-with-input-file (string-append source "/.guix-channel")
187 read-channel-metadata))
189 (if (= ENOENT (system-error-errno args))
190 (channel-metadata "/" '() #f)
191 (apply throw args)))))
193 (define (channel-instance-metadata instance)
194 "Return a channel-metadata record read from the channel INSTANCE's
195 description file or its default value."
196 (read-channel-metadata-from-source (channel-instance-checkout instance)))
198 (define (channel-instance-dependencies instance)
199 "Return the list of channels that are declared as dependencies for the given
201 (channel-metadata-dependencies (channel-instance-metadata instance)))
203 (define (apply-patches checkout commit patches)
204 "Apply the matching PATCHES to CHECKOUT, modifying files in place. The
205 result is unspecified."
206 (let loop ((patches patches))
210 (when (applicable-patch? patch checkout commit)
211 (apply-patch patch checkout))
214 (define* (latest-channel-instance store channel
215 #:key (patches %patches))
216 "Return the latest channel instance for CHANNEL."
217 (define (dot-git? file stat)
218 (and (string=? (basename file) ".git")
219 (eq? 'directory (stat:type stat))))
221 (let-values (((checkout commit)
222 (update-cached-checkout (channel-url channel)
223 #:ref (channel-reference channel))))
224 (when (guix-channel? channel)
225 ;; Apply the relevant subset of PATCHES directly in CHECKOUT. This is
226 ;; safe to do because 'switch-to-ref' eventually does a hard reset.
227 (apply-patches checkout commit patches))
229 (let* ((name (url+commit->name (channel-url channel) commit))
230 (checkout (add-to-store store name #t "sha256" checkout
231 #:select? (negate dot-git?))))
232 (channel-instance channel commit checkout))))
234 (define* (latest-channel-instances store channels #:optional (previous-channels '()))
235 "Return a list of channel instances corresponding to the latest checkouts of
236 CHANNELS and the channels on which they depend. PREVIOUS-CHANNELS is a list
237 of previously processed channels."
238 ;; Only process channels that are unique, or that are more specific than a
239 ;; previous channel specification.
240 (define (ignore? channel others)
241 (member channel others
243 (and (eq? (channel-name a) (channel-name b))
244 (or (channel-commit b)
245 (not (or (channel-commit a)
246 (channel-commit b))))))))
248 ;; Accumulate a list of instances. A list of processed channels is also
249 ;; accumulated to decide on duplicate channel specifications.
250 (define-values (resulting-channels instances)
251 (fold2 (lambda (channel previous-channels instances)
252 (if (ignore? channel previous-channels)
253 (values previous-channels instances)
255 (format (current-error-port)
256 (G_ "Updating channel '~a' from Git repository at '~a'...~%")
257 (channel-name channel)
258 (channel-url channel))
259 (let ((instance (latest-channel-instance store channel)))
260 (let-values (((new-instances new-channels)
261 (latest-channel-instances
263 (channel-instance-dependencies instance)
265 (values (append (cons channel new-channels)
267 (append (cons instance new-instances)
273 (let ((instance-name (compose channel-name channel-instance-channel)))
274 ;; Remove all earlier channel specifications if they are followed by a
275 ;; more specific one.
276 (values (delete-duplicates instances
278 (eq? (instance-name a) (instance-name b))))
279 resulting-channels)))
281 (define* (checkout->channel-instance checkout
283 (url checkout) (name 'guix))
284 "Return a channel instance for CHECKOUT, which is assumed to be a checkout
285 of COMMIT at URL. Use NAME as the channel name."
286 (let* ((commit (or commit (make-string 40 #\0)))
287 (channel (channel (name name)
290 (channel-instance channel commit checkout)))
292 (define %self-build-file
293 ;; The file containing code to build Guix. This serves the same purpose as
294 ;; a makefile, and, similarly, is intended to always keep this name.
295 "build-aux/build-self.scm")
297 (define %pull-version
298 ;; This is the version of the 'guix pull' protocol. It specifies what's
299 ;; expected from %SELF-BUILD-FILE. The initial version ("0") was when we'd
300 ;; place a set of compiled Guile modules in ~/.config/guix/latest.
303 (define (standard-module-derivation name source core dependencies)
304 "Return a derivation that builds with CORE, a Guix instance, the Scheme
305 modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
306 objects. The assumption is that SOURCE contains package modules to be added
307 to '%package-module-path'."
309 (let* ((metadata (read-channel-metadata-from-source source))
310 (directory (channel-metadata-directory metadata)))
313 ;; This is code that we'll run in CORE, a Guix instance, with its own
314 ;; modules and so on. That way, we make sure these modules are built for
315 ;; the right Guile version, with the right dependencies, and that they get
316 ;; to see the right (gnu packages …) modules.
317 (with-extensions dependencies
319 (use-modules (guix build compile)
324 (string-append #$output "/lib/guile/" (effective-version)
327 (string-append #$output "/share/guile/site/"
328 (effective-version)))
330 (let* ((subdir #$directory)
331 (source (string-append #$source subdir)))
332 (compile-files source go (find-files source "\\.scm$"))
333 (mkdir-p (dirname scm))
334 (symlink (string-append #$source subdir) scm))
338 (gexp->derivation-in-inferior name build core)))
340 (define* (guile-for-source source #:optional (quirks %quirks))
341 "Return the Guile package to use when building SOURCE or #f if the default
342 '%guile-for-build' should be good enough."
343 (let loop ((quirks quirks))
347 (((predicate . guile) rest ...)
348 (if (predicate source) (guile) (loop rest))))))
350 (define (call-with-guile guile thunk)
352 (values (parameterize ((%guile-for-build
354 (package-derivation store guile)
355 (%guile-for-build))))
356 (run-with-store store (thunk)))
359 (define-syntax-rule (with-guile guile exp ...)
360 "Set GUILE as the '%guile-for-build' parameter for the dynamic extent of
361 EXP, a series of monadic expressions."
362 (call-with-guile guile (lambda ()
363 (mbegin %store-monad exp ...))))
365 (define (with-trivial-build-handler mvalue)
366 "Run MVALUE, a monadic value, with a \"trivial\" build handler installed
367 that unconditionally resumes the continuation."
369 (with-build-handler (lambda (continue . _)
371 (values (run-with-store store mvalue)
374 (define* (build-from-source name source
375 #:key core verbose? commit
377 "Return a derivation to build Guix from SOURCE, using the self-build script
378 contained therein; use COMMIT as the version string. When CORE is true, build
379 package modules under SOURCE using CORE, an instance of Guix."
380 ;; Running the self-build script makes it easier to update the build
381 ;; procedure: the self-build script of the Guix-to-be-installed contains the
382 ;; right dependencies, build procedure, etc., which the Guix-in-use may not
385 (string-append source "/" %self-build-file))
387 (if (file-exists? script)
388 (let ((build (save-module-excursion
390 ;; Disable deprecation warnings; it's OK for SCRIPT to
391 ;; use deprecated APIs and the user doesn't have to know
393 (parameterize ((guix-warning-port
394 (%make-void-port "w")))
395 (primitive-load script)))))
396 (guile (guile-for-source source)))
397 ;; BUILD must be a monadic procedure of at least one argument: the
400 ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
401 ;; the future we'll fall back to a previous version of the protocol
402 ;; when that happens.
404 ;; BUILD is usually quite costly. Install a "trivial" build handler
405 ;; so we don't bounce an outer build-accumulator handler that could
406 ;; cause us to redo half of the BUILD computation several times just
407 ;; to realize it gives the same result.
408 (with-trivial-build-handler
409 (build source #:verbose? verbose? #:version commit
410 #:pull-version %pull-version))))
412 ;; Build a set of modules that extend Guix using the standard method.
413 (standard-module-derivation name source core dependencies)))
415 (define* (build-channel-instance instance
416 #:optional core (dependencies '()))
417 "Return, as a monadic value, the derivation for INSTANCE, a channel
418 instance. DEPENDENCIES is a list of extensions providing Guile modules that
419 INSTANCE depends on."
420 (build-from-source (symbol->string
421 (channel-name (channel-instance-channel instance)))
422 (channel-instance-checkout instance)
423 #:commit (channel-instance-commit instance)
425 #:dependencies dependencies))
427 (define (resolve-dependencies instances)
428 "Return a procedure that, given one of the elements of INSTANCES, returns
429 list of instances it depends on."
430 (define channel-instance-name
431 (compose channel-name channel-instance-channel))
433 (define table ;map a name to an instance
434 (fold (lambda (instance table)
435 (vhash-consq (channel-instance-name instance)
441 (fold (lambda (instance edges)
442 (fold (lambda (channel edges)
443 (let ((name (channel-name channel)))
444 (match (vhash-assq name table)
446 (vhash-consq instance target edges)))))
448 (channel-instance-dependencies instance)))
453 (vhash-foldq* cons '() instance edges)))
455 (define (channel-instance-derivations instances)
456 "Return the list of derivations to build INSTANCES, in the same order as
458 (define core-instance
459 ;; The 'guix' channel is treated specially: it's an implicit dependency of
460 ;; all the other channels.
461 (find (lambda (instance)
462 (guix-channel? (channel-instance-channel instance)))
466 (resolve-dependencies instances))
468 (define (instance->derivation instance)
469 (mlet %store-monad ((system (current-system)))
470 (mcached (if (eq? instance core-instance)
471 (build-channel-instance instance)
472 (mlet %store-monad ((core (instance->derivation core-instance))
473 (deps (mapm %store-monad instance->derivation
475 (build-channel-instance instance core deps)))
479 (unless core-instance
480 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
482 source-properties->location)))
483 (raise (apply make-compound-condition
485 (&message (message "'guix' channel is lacking")))
487 (&fix-hint (hint (G_ "Make sure your list of channels
488 contains one channel named @code{guix} providing the core of Guix."))))
490 (list (condition (&error-location (location loc))))
493 (mapm %store-monad instance->derivation instances))
495 (define (whole-package-for-legacy name modules)
496 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
497 modules in the old ~/.config/guix/latest style."
499 (resolve-interface '(gnu packages guile)))
501 (define modules+compiled
502 ;; Since MODULES contains both .scm and .go files at its root, re-bundle
503 ;; it so that it has share/guile/site and lib/guile, which is what
504 ;; 'whole-package' expects.
505 (computed-file (derivation-name modules)
506 (with-imported-modules '((guix build utils))
508 (use-modules (guix build utils))
513 (string-append #$output "/share/guile/site"))
515 (string-append #$output "/lib/guile/" version))
517 (mkdir-p share) (mkdir-p lib)
518 (symlink #$modules (string-append share "/" version))
519 (symlink #$modules (string-append lib "/site-ccache"))))))
521 (letrec-syntax ((list (syntax-rules (->)
524 ((_ (module -> variable) rest ...)
525 (cons (module-ref (resolve-interface
526 '(gnu packages module))
529 ((_ variable rest ...)
530 (cons (module-ref packages 'variable)
532 (whole-package name modules+compiled
534 ;; In the "old style", %SELF-BUILD-FILE would simply return a
535 ;; derivation that builds modules. We have to infer what the
536 ;; dependencies of these modules were.
537 (list guile-json-3 guile-git guile-bytestructures
538 (ssh -> guile-ssh) (tls -> gnutls)))))
540 (define (old-style-guix? drv)
541 "Return true if DRV corresponds to a ~/.config/guix/latest style of
543 ;; Here we rely on a gross historical fact: that derivations produced by the
544 ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
545 ;; dated May 30, 2018) did not depend on "guix-command.drv".
546 (not (find (lambda (input)
547 (string=? "guix-command"
549 (derivation-input-derivation input))))
550 (derivation-inputs drv))))
552 (define (channel-instances->manifest instances)
553 "Return a profile manifest with entries for all of INSTANCES, a list of
555 (define (instance->entry instance drv)
556 (let ((commit (channel-instance-commit instance))
557 (channel (channel-instance-channel instance)))
559 (name (symbol->string (channel-name channel)))
560 (version (string-take commit 7))
561 (item (if (guix-channel? channel)
562 (if (old-style-guix? drv)
563 (whole-package-for-legacy (string-append name "-" version)
568 `((source (repository
570 (url ,(channel-url channel))
571 (branch ,(channel-branch channel))
572 (commit ,commit))))))))
574 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
575 (entries -> (map instance->entry instances derivations)))
576 (return (manifest entries))))
578 (define (package-cache-file manifest)
579 "Build a package cache file for the instance in MANIFEST. This is meant to
580 be used as a profile hook."
581 (let ((profile (profile (content manifest) (hooks '()))))
584 (use-modules (gnu packages))
586 (if (defined? 'generate-package-cache)
588 ;; Delegate package cache generation to the inferior.
589 (format (current-error-port)
590 "Generating package cache for '~a'...~%"
592 (generate-package-cache #$output))
595 (gexp->derivation-in-inferior "guix-package-cache" build
598 ;; If the Guix in PROFILE is too old and
599 ;; lacks 'guix repl', don't build the cache
600 ;; instead of failing.
603 #:properties '((type . profile-hook)
604 (hook . package-cache))
607 (define %channel-profile-hooks
608 ;; The default channel profile hooks.
609 (cons package-cache-file %default-profile-hooks))
611 (define (channel-instances->derivation instances)
612 "Return the derivation of the profile containing INSTANCES, a list of
614 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
615 (profile-derivation manifest
616 #:hooks %channel-profile-hooks)))
618 (define latest-channel-instances*
619 (store-lift latest-channel-instances))
621 (define* (latest-channel-derivation #:optional (channels %default-channels))
622 "Return as a monadic value the derivation that builds the profile for the
623 latest instances of CHANNELS."
624 (mlet %store-monad ((instances (latest-channel-instances* channels)))
625 (channel-instances->derivation instances)))
627 (define (profile-channels profile)
628 "Return the list of channels corresponding to entries in PROFILE. If
629 PROFILE is not a profile created by 'guix pull', return the empty list."
630 (filter-map (lambda (entry)
631 (match (assq 'source (manifest-entry-properties entry))
632 (('source ('repository ('version 0)
637 (channel (name (string->symbol
638 (manifest-entry-name entry)))
642 ;; No channel information for this manifest entry.
643 ;; XXX: Pre-0.15.0 Guix did not provide that information,
644 ;; but there's not much we can do in that case.
647 ;; Show most recently installed packages last.
649 (manifest-entries (profile-manifest profile)))))
657 (define-record-type <channel-news>
658 (channel-news entries)
660 (entries channel-news-entries)) ;list of <channel-news-entry>
662 ;; News entry, associated with a specific commit of the channel.
663 (define-record-type <channel-news-entry>
664 (channel-news-entry commit tag title body)
666 (commit channel-news-entry-commit) ;hex string | #f
667 (tag channel-news-entry-tag) ;#f | string
668 (title channel-news-entry-title) ;list of language tag/string pairs
669 (body channel-news-entry-body)) ;list of language tag/string pairs
671 (define (sexp->channel-news-entry entry)
672 "Return the <channel-news-entry> record corresponding to ENTRY, an sexp."
673 (define (pair language message)
674 (cons (symbol->string language) message))
677 (('entry ((and (or 'commit 'tag) type) commit-or-tag)
678 ('title ((? symbol? title-tags) (? string? titles)) ...)
679 ('body ((? symbol? body-tags) (? string? bodies)) ...)
681 (channel-news-entry (and (eq? type 'commit) commit-or-tag)
682 (and (eq? type 'tag) commit-or-tag)
683 (map pair title-tags titles)
684 (map pair body-tags bodies)))
687 (&message (message "invalid channel news entry"))
689 (location (source-properties->location
690 (source-properties entry)))))))))
692 (define (read-channel-news port)
693 "Read a channel news feed from PORT and return it as a <channel-news>
695 (match (false-if-exception (read port))
696 (('channel-news ('version 0) entries ...)
697 (channel-news (map sexp->channel-news-entry entries)))
698 (('channel-news ('version version) _ ...)
699 ;; This is an unsupported version from the future. There's nothing wrong
700 ;; with that (the user may simply need to upgrade the 'guix' channel to
701 ;; be able to read it), so silently ignore it.
705 (&message (message "syntactically invalid channel news file")))))
708 (&message (message "invalid channel news file"))
710 (location (source-properties->location
711 (source-properties sexp)))))))))
713 (define (resolve-channel-news-entry-tag repository entry)
714 "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
715 ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
716 the field its 'tag' refers to. A 'git-error' exception is raised if the tag
718 (if (channel-news-entry-commit entry)
720 (let* ((tag (channel-news-entry-tag entry))
721 (reference (string-append "refs/tags/" tag))
722 (oid (reference-name->oid repository reference)))
723 (channel-news-entry (oid->string oid) tag
724 (channel-news-entry-title entry)
725 (channel-news-entry-body entry)))))
727 (define* (channel-news-for-commit channel new #:optional old)
728 "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
729 NEW. When OLD is omitted or is #f, return all the news entries of CHANNEL."
732 (let* ((checkout (update-cached-checkout (channel-url channel)
733 #:ref `(commit . ,new)))
734 (metadata (read-channel-metadata-from-source checkout))
735 (news-file (channel-metadata-news-file metadata))
736 (news-file (and news-file
737 (string-append checkout "/" news-file))))
738 (if (and news-file (file-exists? news-file))
739 (with-repository checkout repository
740 (let* ((news (call-with-input-file news-file
742 (entries (map (lambda (entry)
743 (resolve-channel-news-entry-tag repository
745 (channel-news-entries news))))
747 (let* ((new (commit-lookup repository (string->oid new)))
748 (old (commit-lookup repository (string->oid old)))
750 (map (compose oid->string commit-id)
751 (commit-difference new old)))))
752 (filter (lambda (entry)
753 (set-contains? commits
754 (channel-news-entry-commit entry)))
758 (lambda (key error . rest)
759 ;; If commit NEW or commit OLD cannot be found, then something must be
760 ;; wrong (for example, the history of CHANNEL was rewritten and these
761 ;; commits no longer exist upstream), so quietly return the empty list.
762 (if (= GIT_ENOTFOUND (git-error-code error))
764 (apply throw key error rest)))))
767 ;;; eval: (put 'with-guile 'scheme-indent-function 1)