1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019 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 derivations)
31 #:use-module (guix combinators)
32 #:use-module (guix diagnostics)
33 #:use-module (guix sets)
34 #:use-module (guix store)
35 #:use-module (guix i18n)
36 #:use-module ((guix utils)
37 #:select (source-properties->location
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-2)
41 #:use-module (srfi srfi-9)
42 #:use-module (srfi srfi-11)
43 #:use-module (srfi srfi-26)
44 #:use-module (srfi srfi-34)
45 #:use-module (srfi srfi-35)
46 #:autoload (guix self) (whole-package make-config.scm)
47 #:autoload (guix inferior) (gexp->derivation-in-inferior) ;FIXME: circular dep
48 #:use-module (ice-9 match)
49 #:use-module (ice-9 vlist)
62 channel-instance-channel
63 channel-instance-commit
64 channel-instance-checkout
66 latest-channel-instances
67 checkout->channel-instance
68 latest-channel-derivation
69 channel-instances->manifest
70 %channel-profile-hooks
71 channel-instances->derivation
76 channel-news-entry-commit
77 channel-news-entry-tag
78 channel-news-entry-title
79 channel-news-entry-body
81 channel-news-for-commit))
85 ;;; This module implements "channels." A channel is usually a source of
86 ;;; package definitions. There's a special channel, the 'guix' channel, that
87 ;;; provides all of Guix, including its commands and its documentation.
88 ;;; User-defined channels are expected to typically provide a bunch of .scm
89 ;;; files meant to be added to the '%package-search-path'.
91 ;;; This module provides tools to fetch and update channels from a Git
92 ;;; repository and to build them.
96 (define-record-type* <channel> channel make-channel
100 (branch channel-branch (default "master"))
101 (commit channel-commit (default #f))
102 (location channel-location
103 (default (current-source-location)) (innate)))
105 (define %default-channels
106 ;; Default list of channels.
110 (url "https://git.savannah.gnu.org/git/guix.git"))))
112 (define (guix-channel? channel)
113 "Return true if CHANNEL is the 'guix' channel."
114 (eq? 'guix (channel-name channel)))
116 (define-record-type <channel-instance>
117 (channel-instance channel commit checkout)
119 (channel channel-instance-channel)
120 (commit channel-instance-commit)
121 (checkout channel-instance-checkout))
123 (define-record-type <channel-metadata>
124 (channel-metadata directory dependencies news-file)
126 (directory channel-metadata-directory) ;string with leading slash
127 (dependencies channel-metadata-dependencies) ;list of <channel>
128 (news-file channel-metadata-news-file)) ;string | #f
130 (define (channel-reference channel)
131 "Return the \"reference\" for CHANNEL, an sexp suitable for
132 'latest-repository-commit'."
133 (match (channel-commit channel)
134 (#f `(branch . ,(channel-branch channel)))
135 (commit `(commit . ,(channel-commit channel)))))
137 (define (read-channel-metadata port)
138 "Read from PORT channel metadata in the format expected for the
139 '.guix-channel' file. Return a <channel-metadata> record, or raise an error
140 if valid metadata could not be read from PORT."
142 (('channel ('version 0) properties ...)
143 (let ((directory (and=> (assoc-ref properties 'directory) first))
144 (dependencies (or (assoc-ref properties 'dependencies) '()))
145 (news-file (and=> (assoc-ref properties 'news-file) first)))
147 (cond ((not directory) "/") ;directory
148 ((string-prefix? "/" directory) directory)
149 (else (string-append "/" directory)))
150 (map (lambda (item) ;dependencies
151 (let ((get (lambda* (key #:optional default)
152 (or (and=> (assoc-ref item key) first) default))))
153 (and-let* ((name (get 'name))
155 (branch (get 'branch "master")))
160 (commit (get 'commit))))))
162 news-file))) ;news-file
163 ((and ('channel ('version version) _ ...) sexp)
165 (&message (message "unsupported '.guix-channel' version"))
167 (location (source-properties->location
168 (source-properties sexp)))))))
171 (&message (message "invalid '.guix-channel' file"))
173 (location (source-properties->location
174 (source-properties sexp)))))))))
176 (define (read-channel-metadata-from-source source)
177 "Return a channel-metadata record read from channel's SOURCE/.guix-channel
178 description file, or return the default channel-metadata record if that file
182 (call-with-input-file (string-append source "/.guix-channel")
183 read-channel-metadata))
185 (if (= ENOENT (system-error-errno args))
186 (channel-metadata "/" '() #f)
187 (apply throw args)))))
189 (define (channel-instance-metadata instance)
190 "Return a channel-metadata record read from the channel INSTANCE's
191 description file or its default value."
192 (read-channel-metadata-from-source (channel-instance-checkout instance)))
194 (define (channel-instance-dependencies instance)
195 "Return the list of channels that are declared as dependencies for the given
197 (channel-metadata-dependencies (channel-instance-metadata instance)))
199 (define* (latest-channel-instances store channels #:optional (previous-channels '()))
200 "Return a list of channel instances corresponding to the latest checkouts of
201 CHANNELS and the channels on which they depend. PREVIOUS-CHANNELS is a list
202 of previously processed channels."
203 ;; Only process channels that are unique, or that are more specific than a
204 ;; previous channel specification.
205 (define (ignore? channel others)
206 (member channel others
208 (and (eq? (channel-name a) (channel-name b))
209 (or (channel-commit b)
210 (not (or (channel-commit a)
211 (channel-commit b))))))))
213 ;; Accumulate a list of instances. A list of processed channels is also
214 ;; accumulated to decide on duplicate channel specifications.
215 (define-values (resulting-channels instances)
216 (fold2 (lambda (channel previous-channels instances)
217 (if (ignore? channel previous-channels)
218 (values previous-channels instances)
220 (format (current-error-port)
221 (G_ "Updating channel '~a' from Git repository at '~a'...~%")
222 (channel-name channel)
223 (channel-url channel))
224 (let-values (((checkout commit)
225 (latest-repository-commit store (channel-url channel)
226 #:ref (channel-reference
228 (let ((instance (channel-instance channel commit checkout)))
229 (let-values (((new-instances new-channels)
230 (latest-channel-instances
232 (channel-instance-dependencies instance)
234 (values (append (cons channel new-channels)
236 (append (cons instance new-instances)
242 (let ((instance-name (compose channel-name channel-instance-channel)))
243 ;; Remove all earlier channel specifications if they are followed by a
244 ;; more specific one.
245 (values (delete-duplicates instances
247 (eq? (instance-name a) (instance-name b))))
248 resulting-channels)))
250 (define* (checkout->channel-instance checkout
252 (url checkout) (name 'guix))
253 "Return a channel instance for CHECKOUT, which is assumed to be a checkout
254 of COMMIT at URL. Use NAME as the channel name."
255 (let* ((commit (or commit (make-string 40 #\0)))
256 (channel (channel (name name)
259 (channel-instance channel commit checkout)))
261 (define %self-build-file
262 ;; The file containing code to build Guix. This serves the same purpose as
263 ;; a makefile, and, similarly, is intended to always keep this name.
264 "build-aux/build-self.scm")
266 (define %pull-version
267 ;; This is the version of the 'guix pull' protocol. It specifies what's
268 ;; expected from %SELF-BUILD-FILE. The initial version ("0") was when we'd
269 ;; place a set of compiled Guile modules in ~/.config/guix/latest.
272 (define (standard-module-derivation name source core dependencies)
273 "Return a derivation that builds with CORE, a Guix instance, the Scheme
274 modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
275 objects. The assumption is that SOURCE contains package modules to be added
276 to '%package-module-path'."
278 (let* ((metadata (read-channel-metadata-from-source source))
279 (directory (channel-metadata-directory metadata)))
282 ;; This is code that we'll run in CORE, a Guix instance, with its own
283 ;; modules and so on. That way, we make sure these modules are built for
284 ;; the right Guile version, with the right dependencies, and that they get
285 ;; to see the right (gnu packages …) modules.
286 (with-extensions dependencies
288 (use-modules (guix build compile)
293 (string-append #$output "/lib/guile/" (effective-version)
296 (string-append #$output "/share/guile/site/"
297 (effective-version)))
299 (let* ((subdir #$directory)
300 (source (string-append #$source subdir)))
301 (compile-files source go (find-files source "\\.scm$"))
302 (mkdir-p (dirname scm))
303 (symlink (string-append #$source subdir) scm))
307 (gexp->derivation-in-inferior name build core)))
309 (define* (build-from-source name source
310 #:key core verbose? commit
312 "Return a derivation to build Guix from SOURCE, using the self-build script
313 contained therein; use COMMIT as the version string. When CORE is true, build
314 package modules under SOURCE using CORE, an instance of Guix."
315 ;; Running the self-build script makes it easier to update the build
316 ;; procedure: the self-build script of the Guix-to-be-installed contains the
317 ;; right dependencies, build procedure, etc., which the Guix-in-use may not
320 (string-append source "/" %self-build-file))
322 (if (file-exists? script)
323 (let ((build (save-module-excursion
325 ;; Disable deprecation warnings; it's OK for SCRIPT to
326 ;; use deprecated APIs and the user doesn't have to know
328 (parameterize ((guix-warning-port
329 (%make-void-port "w")))
330 (primitive-load script))))))
331 ;; BUILD must be a monadic procedure of at least one argument: the
334 ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
335 ;; the future we'll fall back to a previous version of the protocol
336 ;; when that happens.
337 (build source #:verbose? verbose? #:version commit
338 #:pull-version %pull-version))
340 ;; Build a set of modules that extend Guix using the standard method.
341 (standard-module-derivation name source core dependencies)))
343 (define* (build-channel-instance instance
344 #:optional core (dependencies '()))
345 "Return, as a monadic value, the derivation for INSTANCE, a channel
346 instance. DEPENDENCIES is a list of extensions providing Guile modules that
347 INSTANCE depends on."
348 (build-from-source (symbol->string
349 (channel-name (channel-instance-channel instance)))
350 (channel-instance-checkout instance)
351 #:commit (channel-instance-commit instance)
353 #:dependencies dependencies))
355 (define (resolve-dependencies instances)
356 "Return a procedure that, given one of the elements of INSTANCES, returns
357 list of instances it depends on."
358 (define channel-instance-name
359 (compose channel-name channel-instance-channel))
361 (define table ;map a name to an instance
362 (fold (lambda (instance table)
363 (vhash-consq (channel-instance-name instance)
369 (fold (lambda (instance edges)
370 (fold (lambda (channel edges)
371 (let ((name (channel-name channel)))
372 (match (vhash-assq name table)
374 (vhash-consq instance target edges)))))
376 (channel-instance-dependencies instance)))
381 (vhash-foldq* cons '() instance edges)))
383 (define (channel-instance-derivations instances)
384 "Return the list of derivations to build INSTANCES, in the same order as
386 (define core-instance
387 ;; The 'guix' channel is treated specially: it's an implicit dependency of
388 ;; all the other channels.
389 (find (lambda (instance)
390 (guix-channel? (channel-instance-channel instance)))
394 (resolve-dependencies instances))
396 (define (instance->derivation instance)
397 (mlet %store-monad ((system (current-system)))
398 (mcached (if (eq? instance core-instance)
399 (build-channel-instance instance)
400 (mlet %store-monad ((core (instance->derivation core-instance))
401 (deps (mapm %store-monad instance->derivation
403 (build-channel-instance instance core deps)))
407 (unless core-instance
408 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
410 source-properties->location)))
411 (raise (apply make-compound-condition
413 (&message (message "'guix' channel is lacking")))
415 (list (condition (&error-location (location loc))))
418 (mapm %store-monad instance->derivation instances))
420 (define (whole-package-for-legacy name modules)
421 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
422 modules in the old ~/.config/guix/latest style."
424 (resolve-interface '(gnu packages guile)))
426 (define modules+compiled
427 ;; Since MODULES contains both .scm and .go files at its root, re-bundle
428 ;; it so that it has share/guile/site and lib/guile, which is what
429 ;; 'whole-package' expects.
430 (computed-file (derivation-name modules)
431 (with-imported-modules '((guix build utils))
433 (use-modules (guix build utils))
438 (string-append #$output "/share/guile/site"))
440 (string-append #$output "/lib/guile/" version))
442 (mkdir-p share) (mkdir-p lib)
443 (symlink #$modules (string-append share "/" version))
444 (symlink #$modules (string-append lib "/site-ccache"))))))
446 (letrec-syntax ((list (syntax-rules (->)
449 ((_ (module -> variable) rest ...)
450 (cons (module-ref (resolve-interface
451 '(gnu packages module))
454 ((_ variable rest ...)
455 (cons (module-ref packages 'variable)
457 (whole-package name modules+compiled
459 ;; In the "old style", %SELF-BUILD-FILE would simply return a
460 ;; derivation that builds modules. We have to infer what the
461 ;; dependencies of these modules were.
462 (list guile-json guile-git guile-bytestructures
463 (ssh -> guile-ssh) (tls -> gnutls)))))
465 (define (old-style-guix? drv)
466 "Return true if DRV corresponds to a ~/.config/guix/latest style of
468 ;; Here we rely on a gross historical fact: that derivations produced by the
469 ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
470 ;; dated May 30, 2018) did not depend on "guix-command.drv".
471 (not (find (lambda (input)
472 (string=? "guix-command"
474 (derivation-input-derivation input))))
475 (derivation-inputs drv))))
477 (define (channel-instances->manifest instances)
478 "Return a profile manifest with entries for all of INSTANCES, a list of
480 (define (instance->entry instance drv)
481 (let ((commit (channel-instance-commit instance))
482 (channel (channel-instance-channel instance)))
484 (name (symbol->string (channel-name channel)))
485 (version (string-take commit 7))
486 (item (if (guix-channel? channel)
487 (if (old-style-guix? drv)
488 (whole-package-for-legacy (string-append name "-" version)
493 `((source (repository
495 (url ,(channel-url channel))
496 (branch ,(channel-branch channel))
497 (commit ,commit))))))))
499 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
500 (entries -> (map instance->entry instances derivations)))
501 (return (manifest entries))))
503 (define (package-cache-file manifest)
504 "Build a package cache file for the instance in MANIFEST. This is meant to
505 be used as a profile hook."
506 (mlet %store-monad ((profile (profile-derivation manifest
511 (use-modules (gnu packages))
513 (if (defined? 'generate-package-cache)
515 ;; Delegate package cache generation to the inferior.
516 (format (current-error-port)
517 "Generating package cache for '~a'...~%"
519 (generate-package-cache #$output))
522 (gexp->derivation-in-inferior "guix-package-cache" build
525 ;; If the Guix in PROFILE is too old and
526 ;; lacks 'guix repl', don't build the cache
527 ;; instead of failing.
530 #:properties '((type . profile-hook)
531 (hook . package-cache))
534 (define %channel-profile-hooks
535 ;; The default channel profile hooks.
536 (cons package-cache-file %default-profile-hooks))
538 (define (channel-instances->derivation instances)
539 "Return the derivation of the profile containing INSTANCES, a list of
541 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
542 (profile-derivation manifest
543 #:hooks %channel-profile-hooks)))
545 (define latest-channel-instances*
546 (store-lift latest-channel-instances))
548 (define* (latest-channel-derivation #:optional (channels %default-channels))
549 "Return as a monadic value the derivation that builds the profile for the
550 latest instances of CHANNELS."
551 (mlet %store-monad ((instances (latest-channel-instances* channels)))
552 (channel-instances->derivation instances)))
554 (define (profile-channels profile)
555 "Return the list of channels corresponding to entries in PROFILE. If
556 PROFILE is not a profile created by 'guix pull', return the empty list."
557 (filter-map (lambda (entry)
558 (match (assq 'source (manifest-entry-properties entry))
559 (('source ('repository ('version 0)
564 (channel (name (string->symbol
565 (manifest-entry-name entry)))
569 ;; No channel information for this manifest entry.
570 ;; XXX: Pre-0.15.0 Guix did not provide that information,
571 ;; but there's not much we can do in that case.
574 ;; Show most recently installed packages last.
576 (manifest-entries (profile-manifest profile)))))
584 (define-record-type <channel-news>
585 (channel-news entries)
587 (entries channel-news-entries)) ;list of <channel-news-entry>
589 ;; News entry, associated with a specific commit of the channel.
590 (define-record-type <channel-news-entry>
591 (channel-news-entry commit tag title body)
593 (commit channel-news-entry-commit) ;hex string | #f
594 (tag channel-news-entry-tag) ;#f | string
595 (title channel-news-entry-title) ;list of language tag/string pairs
596 (body channel-news-entry-body)) ;list of language tag/string pairs
598 (define (sexp->channel-news-entry entry)
599 "Return the <channel-news-entry> record corresponding to ENTRY, an sexp."
600 (define (pair language message)
601 (cons (symbol->string language) message))
604 (('entry ((and (or 'commit 'tag) type) commit-or-tag)
605 ('title ((? symbol? title-tags) (? string? titles)) ...)
606 ('body ((? symbol? body-tags) (? string? bodies)) ...)
608 (channel-news-entry (and (eq? type 'commit) commit-or-tag)
609 (and (eq? type 'tag) commit-or-tag)
610 (map pair title-tags titles)
611 (map pair body-tags bodies)))
614 (&message (message "invalid channel news entry"))
616 (location (source-properties->location
617 (source-properties entry)))))))))
619 (define (read-channel-news port)
620 "Read a channel news feed from PORT and return it as a <channel-news>
622 (match (false-if-exception (read port))
623 (('channel-news ('version 0) entries ...)
624 (channel-news (map sexp->channel-news-entry entries)))
625 (('channel-news ('version version) _ ...)
626 ;; This is an unsupported version from the future. There's nothing wrong
627 ;; with that (the user may simply need to upgrade the 'guix' channel to
628 ;; be able to read it), so silently ignore it.
632 (&message (message "syntactically invalid channel news file")))))
635 (&message (message "invalid channel news file"))
637 (location (source-properties->location
638 (source-properties sexp)))))))))
640 (define (resolve-channel-news-entry-tag repository entry)
641 "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
642 ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
643 the field its 'tag' refers to. A 'git-error' exception is raised if the tag
645 (if (channel-news-entry-commit entry)
647 (let* ((tag (channel-news-entry-tag entry))
648 (reference (string-append "refs/tags/" tag))
649 (oid (reference-name->oid repository reference)))
650 (channel-news-entry (oid->string oid) tag
651 (channel-news-entry-title entry)
652 (channel-news-entry-body entry)))))
654 (define* (channel-news-for-commit channel new #:optional old)
655 "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
656 NEW. When OLD is omitted or is #f, return all the news entries of CHANNEL."
659 (let* ((checkout (update-cached-checkout (channel-url channel)
660 #:ref `(commit . ,new)))
661 (metadata (read-channel-metadata-from-source checkout))
662 (news-file (channel-metadata-news-file metadata))
663 (news-file (and news-file
664 (string-append checkout "/" news-file))))
665 (if (and news-file (file-exists? news-file))
666 (with-repository checkout repository
667 (let* ((news (call-with-input-file news-file
669 (entries (map (lambda (entry)
670 (resolve-channel-news-entry-tag repository
672 (channel-news-entries news))))
674 (let* ((new (commit-lookup repository (string->oid new)))
675 (old (commit-lookup repository (string->oid old)))
677 (map (compose oid->string commit-id)
678 (commit-difference new old)))))
679 (filter (lambda (entry)
680 (set-contains? commits
681 (channel-news-entry-commit entry)))
685 (lambda (key error . rest)
686 ;; If commit NEW or commit OLD cannot be found, then something must be
687 ;; wrong (for example, the history of CHANNEL was rewritten and these
688 ;; commits no longer exist upstream), so quietly return the empty list.
689 (if (= GIT_ENOTFOUND (git-error-code error))
691 (apply throw key error rest)))))