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)
22 #:use-module (guix git)
23 #:use-module (guix records)
24 #:use-module (guix gexp)
25 #:use-module (guix modules)
26 #:use-module (guix discovery)
27 #:use-module (guix monads)
28 #:use-module (guix profiles)
29 #:use-module (guix derivations)
30 #:use-module (guix combinators)
31 #:use-module (guix diagnostics)
32 #:use-module (guix store)
33 #:use-module (guix i18n)
34 #:use-module ((guix utils)
35 #:select (source-properties->location
37 #:use-module (srfi srfi-1)
38 #:use-module (srfi srfi-2)
39 #:use-module (srfi srfi-9)
40 #:use-module (srfi srfi-11)
41 #:use-module (srfi srfi-34)
42 #:use-module (srfi srfi-35)
43 #:autoload (guix self) (whole-package make-config.scm)
44 #:autoload (guix inferior) (gexp->derivation-in-inferior) ;FIXME: circular dep
45 #:use-module (ice-9 match)
46 #:use-module (ice-9 vlist)
59 channel-instance-channel
60 channel-instance-commit
61 channel-instance-checkout
63 latest-channel-instances
64 checkout->channel-instance
65 latest-channel-derivation
66 channel-instances->manifest
67 %channel-profile-hooks
68 channel-instances->derivation))
72 ;;; This module implements "channels." A channel is usually a source of
73 ;;; package definitions. There's a special channel, the 'guix' channel, that
74 ;;; provides all of Guix, including its commands and its documentation.
75 ;;; User-defined channels are expected to typically provide a bunch of .scm
76 ;;; files meant to be added to the '%package-search-path'.
78 ;;; This module provides tools to fetch and update channels from a Git
79 ;;; repository and to build them.
83 (define-record-type* <channel> channel make-channel
87 (branch channel-branch (default "master"))
88 (commit channel-commit (default #f))
89 (location channel-location
90 (default (current-source-location)) (innate)))
92 (define %default-channels
93 ;; Default list of channels.
97 (url "https://git.savannah.gnu.org/git/guix.git"))))
99 (define (guix-channel? channel)
100 "Return true if CHANNEL is the 'guix' channel."
101 (eq? 'guix (channel-name channel)))
103 (define-record-type <channel-instance>
104 (channel-instance channel commit checkout)
106 (channel channel-instance-channel)
107 (commit channel-instance-commit)
108 (checkout channel-instance-checkout))
110 (define-record-type <channel-metadata>
111 (channel-metadata directory dependencies)
113 (directory channel-metadata-directory) ;string with leading slash
114 (dependencies channel-metadata-dependencies)) ;list of <channel>
116 (define (channel-reference channel)
117 "Return the \"reference\" for CHANNEL, an sexp suitable for
118 'latest-repository-commit'."
119 (match (channel-commit channel)
120 (#f `(branch . ,(channel-branch channel)))
121 (commit `(commit . ,(channel-commit channel)))))
123 (define (read-channel-metadata port)
124 "Read from PORT channel metadata in the format expected for the
125 '.guix-channel' file. Return a <channel-metadata> record, or raise an error
126 if valid metadata could not be read from PORT."
128 (('channel ('version 0) properties ...)
129 (let ((directory (and=> (assoc-ref properties 'directory) first))
130 (dependencies (or (assoc-ref properties 'dependencies) '())))
132 (cond ((not directory) "/")
133 ((string-prefix? "/" directory) directory)
134 (else (string-append "/" directory)))
136 (let ((get (lambda* (key #:optional default)
137 (or (and=> (assoc-ref item key) first) default))))
138 (and-let* ((name (get 'name))
140 (branch (get 'branch "master")))
145 (commit (get 'commit))))))
147 ((and ('channel ('version version) _ ...) sexp)
149 (&message (message "unsupported '.guix-channel' version"))
151 (location (source-properties->location
152 (source-properties sexp)))))))
155 (&message (message "invalid '.guix-channel' file"))
157 (location (source-properties->location
158 (source-properties sexp)))))))))
160 (define (read-channel-metadata-from-source source)
161 "Return a channel-metadata record read from channel's SOURCE/.guix-channel
162 description file, or return the default channel-metadata record if that file
166 (call-with-input-file (string-append source "/.guix-channel")
167 read-channel-metadata))
169 (if (= ENOENT (system-error-errno args))
170 (channel-metadata "/" '())
171 (apply throw args)))))
173 (define (channel-instance-metadata instance)
174 "Return a channel-metadata record read from the channel INSTANCE's
175 description file or its default value."
176 (read-channel-metadata-from-source (channel-instance-checkout instance)))
178 (define (channel-instance-dependencies instance)
179 "Return the list of channels that are declared as dependencies for the given
181 (channel-metadata-dependencies (channel-instance-metadata instance)))
183 (define* (latest-channel-instances store channels #:optional (previous-channels '()))
184 "Return a list of channel instances corresponding to the latest checkouts of
185 CHANNELS and the channels on which they depend. PREVIOUS-CHANNELS is a list
186 of previously processed channels."
187 ;; Only process channels that are unique, or that are more specific than a
188 ;; previous channel specification.
189 (define (ignore? channel others)
190 (member channel others
192 (and (eq? (channel-name a) (channel-name b))
193 (or (channel-commit b)
194 (not (or (channel-commit a)
195 (channel-commit b))))))))
197 ;; Accumulate a list of instances. A list of processed channels is also
198 ;; accumulated to decide on duplicate channel specifications.
199 (define-values (resulting-channels instances)
200 (fold2 (lambda (channel previous-channels instances)
201 (if (ignore? channel previous-channels)
202 (values previous-channels instances)
204 (format (current-error-port)
205 (G_ "Updating channel '~a' from Git repository at '~a'...~%")
206 (channel-name channel)
207 (channel-url channel))
208 (let-values (((checkout commit)
209 (latest-repository-commit store (channel-url channel)
210 #:ref (channel-reference
212 (let ((instance (channel-instance channel commit checkout)))
213 (let-values (((new-instances new-channels)
214 (latest-channel-instances
216 (channel-instance-dependencies instance)
218 (values (append (cons channel new-channels)
220 (append (cons instance new-instances)
226 (let ((instance-name (compose channel-name channel-instance-channel)))
227 ;; Remove all earlier channel specifications if they are followed by a
228 ;; more specific one.
229 (values (delete-duplicates instances
231 (eq? (instance-name a) (instance-name b))))
232 resulting-channels)))
234 (define* (checkout->channel-instance checkout
236 (url checkout) (name 'guix))
237 "Return a channel instance for CHECKOUT, which is assumed to be a checkout
238 of COMMIT at URL. Use NAME as the channel name."
239 (let* ((commit (or commit (make-string 40 #\0)))
240 (channel (channel (name name)
243 (channel-instance channel commit checkout)))
245 (define %self-build-file
246 ;; The file containing code to build Guix. This serves the same purpose as
247 ;; a makefile, and, similarly, is intended to always keep this name.
248 "build-aux/build-self.scm")
250 (define %pull-version
251 ;; This is the version of the 'guix pull' protocol. It specifies what's
252 ;; expected from %SELF-BUILD-FILE. The initial version ("0") was when we'd
253 ;; place a set of compiled Guile modules in ~/.config/guix/latest.
256 (define (standard-module-derivation name source core dependencies)
257 "Return a derivation that builds with CORE, a Guix instance, the Scheme
258 modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
259 objects. The assumption is that SOURCE contains package modules to be added
260 to '%package-module-path'."
262 (let* ((metadata (read-channel-metadata-from-source source))
263 (directory (channel-metadata-directory metadata)))
266 ;; This is code that we'll run in CORE, a Guix instance, with its own
267 ;; modules and so on. That way, we make sure these modules are built for
268 ;; the right Guile version, with the right dependencies, and that they get
269 ;; to see the right (gnu packages …) modules.
270 (with-extensions dependencies
272 (use-modules (guix build compile)
277 (string-append #$output "/lib/guile/" (effective-version)
280 (string-append #$output "/share/guile/site/"
281 (effective-version)))
283 (let* ((subdir #$directory)
284 (source (string-append #$source subdir)))
285 (compile-files source go (find-files source "\\.scm$"))
286 (mkdir-p (dirname scm))
287 (symlink (string-append #$source subdir) scm))
291 (gexp->derivation-in-inferior name build core)))
293 (define* (build-from-source name source
294 #:key core verbose? commit
296 "Return a derivation to build Guix from SOURCE, using the self-build script
297 contained therein; use COMMIT as the version string. When CORE is true, build
298 package modules under SOURCE using CORE, an instance of Guix."
299 ;; Running the self-build script makes it easier to update the build
300 ;; procedure: the self-build script of the Guix-to-be-installed contains the
301 ;; right dependencies, build procedure, etc., which the Guix-in-use may not
304 (string-append source "/" %self-build-file))
306 (if (file-exists? script)
307 (let ((build (save-module-excursion
309 ;; Disable deprecation warnings; it's OK for SCRIPT to
310 ;; use deprecated APIs and the user doesn't have to know
312 (parameterize ((guix-warning-port
313 (%make-void-port "w")))
314 (primitive-load script))))))
315 ;; BUILD must be a monadic procedure of at least one argument: the
318 ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
319 ;; the future we'll fall back to a previous version of the protocol
320 ;; when that happens.
321 (build source #:verbose? verbose? #:version commit
322 #:pull-version %pull-version))
324 ;; Build a set of modules that extend Guix using the standard method.
325 (standard-module-derivation name source core dependencies)))
327 (define* (build-channel-instance instance
328 #:optional core (dependencies '()))
329 "Return, as a monadic value, the derivation for INSTANCE, a channel
330 instance. DEPENDENCIES is a list of extensions providing Guile modules that
331 INSTANCE depends on."
332 (build-from-source (symbol->string
333 (channel-name (channel-instance-channel instance)))
334 (channel-instance-checkout instance)
335 #:commit (channel-instance-commit instance)
337 #:dependencies dependencies))
339 (define (resolve-dependencies instances)
340 "Return a procedure that, given one of the elements of INSTANCES, returns
341 list of instances it depends on."
342 (define channel-instance-name
343 (compose channel-name channel-instance-channel))
345 (define table ;map a name to an instance
346 (fold (lambda (instance table)
347 (vhash-consq (channel-instance-name instance)
353 (fold (lambda (instance edges)
354 (fold (lambda (channel edges)
355 (let ((name (channel-name channel)))
356 (match (vhash-assq name table)
358 (vhash-consq instance target edges)))))
360 (channel-instance-dependencies instance)))
365 (vhash-foldq* cons '() instance edges)))
367 (define (channel-instance-derivations instances)
368 "Return the list of derivations to build INSTANCES, in the same order as
370 (define core-instance
371 ;; The 'guix' channel is treated specially: it's an implicit dependency of
372 ;; all the other channels.
373 (find (lambda (instance)
374 (guix-channel? (channel-instance-channel instance)))
378 (resolve-dependencies instances))
380 (define (instance->derivation instance)
381 (mlet %store-monad ((system (current-system)))
382 (mcached (if (eq? instance core-instance)
383 (build-channel-instance instance)
384 (mlet %store-monad ((core (instance->derivation core-instance))
385 (deps (mapm %store-monad instance->derivation
387 (build-channel-instance instance core deps)))
391 (unless core-instance
392 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
394 source-properties->location)))
395 (raise (apply make-compound-condition
397 (&message (message "'guix' channel is lacking")))
399 (list (condition (&error-location (location loc))))
402 (mapm %store-monad instance->derivation instances))
404 (define (whole-package-for-legacy name modules)
405 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
406 modules in the old ~/.config/guix/latest style."
408 (resolve-interface '(gnu packages guile)))
410 (define modules+compiled
411 ;; Since MODULES contains both .scm and .go files at its root, re-bundle
412 ;; it so that it has share/guile/site and lib/guile, which is what
413 ;; 'whole-package' expects.
414 (computed-file (derivation-name modules)
415 (with-imported-modules '((guix build utils))
417 (use-modules (guix build utils))
422 (string-append #$output "/share/guile/site"))
424 (string-append #$output "/lib/guile/" version))
426 (mkdir-p share) (mkdir-p lib)
427 (symlink #$modules (string-append share "/" version))
428 (symlink #$modules (string-append lib "/site-ccache"))))))
430 (letrec-syntax ((list (syntax-rules (->)
433 ((_ (module -> variable) rest ...)
434 (cons (module-ref (resolve-interface
435 '(gnu packages module))
438 ((_ variable rest ...)
439 (cons (module-ref packages 'variable)
441 (whole-package name modules+compiled
443 ;; In the "old style", %SELF-BUILD-FILE would simply return a
444 ;; derivation that builds modules. We have to infer what the
445 ;; dependencies of these modules were.
446 (list guile-json guile-git guile-bytestructures
447 (ssh -> guile-ssh) (tls -> gnutls)))))
449 (define (old-style-guix? drv)
450 "Return true if DRV corresponds to a ~/.config/guix/latest style of
452 ;; Here we rely on a gross historical fact: that derivations produced by the
453 ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
454 ;; dated May 30, 2018) did not depend on "guix-command.drv".
455 (not (find (lambda (input)
456 (string=? "guix-command"
458 (derivation-input-derivation input))))
459 (derivation-inputs drv))))
461 (define (channel-instances->manifest instances)
462 "Return a profile manifest with entries for all of INSTANCES, a list of
464 (define (instance->entry instance drv)
465 (let ((commit (channel-instance-commit instance))
466 (channel (channel-instance-channel instance)))
468 (name (symbol->string (channel-name channel)))
469 (version (string-take commit 7))
470 (item (if (guix-channel? channel)
471 (if (old-style-guix? drv)
472 (whole-package-for-legacy (string-append name "-" version)
477 `((source (repository
479 (url ,(channel-url channel))
480 (branch ,(channel-branch channel))
481 (commit ,commit))))))))
483 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
484 (entries -> (map instance->entry instances derivations)))
485 (return (manifest entries))))
487 (define (package-cache-file manifest)
488 "Build a package cache file for the instance in MANIFEST. This is meant to
489 be used as a profile hook."
490 (mlet %store-monad ((profile (profile-derivation manifest
495 (use-modules (gnu packages))
497 (if (defined? 'generate-package-cache)
499 ;; Delegate package cache generation to the inferior.
500 (format (current-error-port)
501 "Generating package cache for '~a'...~%"
503 (generate-package-cache #$output))
506 (gexp->derivation-in-inferior "guix-package-cache" build
509 ;; If the Guix in PROFILE is too old and
510 ;; lacks 'guix repl', don't build the cache
511 ;; instead of failing.
514 #:properties '((type . profile-hook)
515 (hook . package-cache))
518 (define %channel-profile-hooks
519 ;; The default channel profile hooks.
520 (cons package-cache-file %default-profile-hooks))
522 (define (channel-instances->derivation instances)
523 "Return the derivation of the profile containing INSTANCES, a list of
525 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
526 (profile-derivation manifest
527 #:hooks %channel-profile-hooks)))
529 (define latest-channel-instances*
530 (store-lift latest-channel-instances))
532 (define* (latest-channel-derivation #:optional (channels %default-channels))
533 "Return as a monadic value the derivation that builds the profile for the
534 latest instances of CHANNELS."
535 (mlet %store-monad ((instances (latest-channel-instances* channels)))
536 (channel-instances->derivation instances)))