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>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix channels)
21 #:use-module (guix git)
22 #:use-module (guix records)
23 #:use-module (guix gexp)
24 #:use-module (guix modules)
25 #:use-module (guix discovery)
26 #:use-module (guix monads)
27 #:use-module (guix profiles)
28 #:use-module (guix derivations)
29 #:use-module (guix combinators)
30 #:use-module (guix deprecation)
31 #:use-module (guix store)
32 #:use-module (guix i18n)
33 #:use-module ((guix utils)
34 #:select (source-properties->location
36 #:use-module (srfi srfi-1)
37 #:use-module (srfi srfi-2)
38 #:use-module (srfi srfi-9)
39 #:use-module (srfi srfi-11)
40 #:use-module (srfi srfi-34)
41 #:use-module (srfi srfi-35)
42 #:autoload (guix self) (whole-package make-config.scm)
43 #:autoload (guix inferior) (gexp->derivation-in-inferior) ;FIXME: circular dep
44 #:use-module (ice-9 match)
45 #:use-module (ice-9 vlist)
58 channel-instance-channel
59 channel-instance-commit
60 channel-instance-checkout
62 latest-channel-instances
63 checkout->channel-instance
64 latest-channel-derivation
65 channel-instances->manifest
66 %channel-profile-hooks
67 channel-instances->derivation))
71 ;;; This module implements "channels." A channel is usually a source of
72 ;;; package definitions. There's a special channel, the 'guix' channel, that
73 ;;; provides all of Guix, including its commands and its documentation.
74 ;;; User-defined channels are expected to typically provide a bunch of .scm
75 ;;; files meant to be added to the '%package-search-path'.
77 ;;; This module provides tools to fetch and update channels from a Git
78 ;;; repository and to build them.
82 (define-record-type* <channel> channel make-channel
86 (branch channel-branch (default "master"))
87 (commit channel-commit (default #f))
88 (location channel-location
89 (default (current-source-location)) (innate)))
91 (define %default-channels
92 ;; Default list of channels.
96 (url "https://git.savannah.gnu.org/git/guix.git"))))
98 (define (guix-channel? channel)
99 "Return true if CHANNEL is the 'guix' channel."
100 (eq? 'guix (channel-name channel)))
102 (define-record-type <channel-instance>
103 (channel-instance channel commit checkout)
105 (channel channel-instance-channel)
106 (commit channel-instance-commit)
107 (checkout channel-instance-checkout))
109 (define-record-type <channel-metadata>
110 (channel-metadata version dependencies)
112 (version channel-metadata-version)
113 (dependencies channel-metadata-dependencies))
115 (define (channel-reference channel)
116 "Return the \"reference\" for CHANNEL, an sexp suitable for
117 'latest-repository-commit'."
118 (match (channel-commit channel)
119 (#f `(branch . ,(channel-branch channel)))
120 (commit `(commit . ,(channel-commit channel)))))
122 (define (read-channel-metadata instance)
123 "Return a channel-metadata record read from the channel INSTANCE's
124 description file, or return #F if the channel instance does not include the
126 (let* ((source (channel-instance-checkout instance))
127 (meta-file (string-append source "/.guix-channel")))
128 (and (file-exists? meta-file)
129 (and-let* ((raw (call-with-input-file meta-file read))
130 (version (and=> (assoc-ref raw 'version) first))
131 (dependencies (or (assoc-ref raw 'dependencies) '())))
135 (let ((get (lambda* (key #:optional default)
136 (or (and=> (assoc-ref item key) first) default))))
137 (and-let* ((name (get 'name))
139 (branch (get 'branch "master")))
144 (commit (get 'commit))))))
147 (define (channel-instance-dependencies instance)
148 "Return the list of channels that are declared as dependencies for the given
150 (match (read-channel-metadata instance)
152 (($ <channel-metadata> version dependencies)
155 (define* (latest-channel-instances store channels #:optional (previous-channels '()))
156 "Return a list of channel instances corresponding to the latest checkouts of
157 CHANNELS and the channels on which they depend. PREVIOUS-CHANNELS is a list
158 of previously processed channels."
159 ;; Only process channels that are unique, or that are more specific than a
160 ;; previous channel specification.
161 (define (ignore? channel others)
162 (member channel others
164 (and (eq? (channel-name a) (channel-name b))
165 (or (channel-commit b)
166 (not (or (channel-commit a)
167 (channel-commit b))))))))
169 ;; Accumulate a list of instances. A list of processed channels is also
170 ;; accumulated to decide on duplicate channel specifications.
171 (define-values (resulting-channels instances)
172 (fold2 (lambda (channel previous-channels instances)
173 (if (ignore? channel previous-channels)
174 (values previous-channels instances)
176 (format (current-error-port)
177 (G_ "Updating channel '~a' from Git repository at '~a'...~%")
178 (channel-name channel)
179 (channel-url channel))
180 (let-values (((checkout commit)
181 (latest-repository-commit store (channel-url channel)
182 #:ref (channel-reference
184 (let ((instance (channel-instance channel commit checkout)))
185 (let-values (((new-instances new-channels)
186 (latest-channel-instances
188 (channel-instance-dependencies instance)
190 (values (append (cons channel new-channels)
192 (append (cons instance new-instances)
198 (let ((instance-name (compose channel-name channel-instance-channel)))
199 ;; Remove all earlier channel specifications if they are followed by a
200 ;; more specific one.
201 (values (delete-duplicates instances
203 (eq? (instance-name a) (instance-name b))))
204 resulting-channels)))
206 (define* (checkout->channel-instance checkout
208 (url checkout) (name 'guix))
209 "Return a channel instance for CHECKOUT, which is assumed to be a checkout
210 of COMMIT at URL. Use NAME as the channel name."
211 (let* ((commit (or commit (make-string 40 #\0)))
212 (channel (channel (name name)
215 (channel-instance channel commit checkout)))
217 (define %self-build-file
218 ;; The file containing code to build Guix. This serves the same purpose as
219 ;; a makefile, and, similarly, is intended to always keep this name.
220 "build-aux/build-self.scm")
222 (define %pull-version
223 ;; This is the version of the 'guix pull' protocol. It specifies what's
224 ;; expected from %SELF-BUILD-FILE. The initial version ("0") was when we'd
225 ;; place a set of compiled Guile modules in ~/.config/guix/latest.
228 (define (standard-module-derivation name source core dependencies)
229 "Return a derivation that builds with CORE, a Guix instance, the Scheme
230 modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
231 objects. The assumption is that SOURCE contains package modules to be added
232 to '%package-module-path'."
233 ;; FIXME: We should load, say SOURCE/.guix-channel.scm, which would allow
234 ;; channel publishers to specify things such as the sub-directory where .scm
235 ;; files live, files to exclude from the channel, preferred substitute URLs,
239 ;; This is code that we'll run in CORE, a Guix instance, with its own
240 ;; modules and so on. That way, we make sure these modules are built for
241 ;; the right Guile version, with the right dependencies, and that they get
242 ;; to see the right (gnu packages …) modules.
243 (with-extensions dependencies
245 (use-modules (guix build compile)
250 (string-append #$output "/lib/guile/" (effective-version)
253 (string-append #$output "/share/guile/site/"
254 (effective-version)))
256 (compile-files #$source go
257 (find-files #$source "\\.scm$"))
258 (mkdir-p (dirname scm))
259 (symlink #$source scm)
262 (gexp->derivation-in-inferior name build core))
264 (define* (build-from-source name source
265 #:key core verbose? commit
267 "Return a derivation to build Guix from SOURCE, using the self-build script
268 contained therein; use COMMIT as the version string. When CORE is true, build
269 package modules under SOURCE using CORE, an instance of Guix."
270 ;; Running the self-build script makes it easier to update the build
271 ;; procedure: the self-build script of the Guix-to-be-installed contains the
272 ;; right dependencies, build procedure, etc., which the Guix-in-use may not
275 (string-append source "/" %self-build-file))
277 (if (file-exists? script)
278 (let ((build (save-module-excursion
280 ;; Disable deprecation warnings; it's OK for SCRIPT to
281 ;; use deprecated APIs and the user doesn't have to know
283 (parameterize ((deprecation-warning-port
284 (%make-void-port "w")))
285 (primitive-load script))))))
286 ;; BUILD must be a monadic procedure of at least one argument: the
289 ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
290 ;; the future we'll fall back to a previous version of the protocol
291 ;; when that happens.
292 (build source #:verbose? verbose? #:version commit
293 #:pull-version %pull-version))
295 ;; Build a set of modules that extend Guix using the standard method.
296 (standard-module-derivation name source core dependencies)))
298 (define* (build-channel-instance instance
299 #:optional core (dependencies '()))
300 "Return, as a monadic value, the derivation for INSTANCE, a channel
301 instance. DEPENDENCIES is a list of extensions providing Guile modules that
302 INSTANCE depends on."
303 (build-from-source (symbol->string
304 (channel-name (channel-instance-channel instance)))
305 (channel-instance-checkout instance)
306 #:commit (channel-instance-commit instance)
308 #:dependencies dependencies))
310 (define (resolve-dependencies instances)
311 "Return a procedure that, given one of the elements of INSTANCES, returns
312 list of instances it depends on."
313 (define channel-instance-name
314 (compose channel-name channel-instance-channel))
316 (define table ;map a name to an instance
317 (fold (lambda (instance table)
318 (vhash-consq (channel-instance-name instance)
324 (fold (lambda (instance edges)
325 (fold (lambda (channel edges)
326 (let ((name (channel-name channel)))
327 (match (vhash-assq name table)
329 (vhash-consq instance target edges)))))
331 (channel-instance-dependencies instance)))
336 (vhash-foldq* cons '() instance edges)))
338 (define (channel-instance-derivations instances)
339 "Return the list of derivations to build INSTANCES, in the same order as
341 (define core-instance
342 ;; The 'guix' channel is treated specially: it's an implicit dependency of
343 ;; all the other channels.
344 (find (lambda (instance)
345 (guix-channel? (channel-instance-channel instance)))
349 (resolve-dependencies instances))
351 (define (instance->derivation instance)
352 (mcached (if (eq? instance core-instance)
353 (build-channel-instance instance)
354 (mlet %store-monad ((core (instance->derivation core-instance))
355 (deps (mapm %store-monad instance->derivation
357 (build-channel-instance instance core deps)))
360 (unless core-instance
361 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
363 source-properties->location)))
364 (raise (apply make-compound-condition
366 (&message (message "'guix' channel is lacking")))
368 (list (condition (&error-location (location loc))))
371 (mapm %store-monad instance->derivation instances))
373 (define (whole-package-for-legacy name modules)
374 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
375 modules in the old ~/.config/guix/latest style."
377 (resolve-interface '(gnu packages guile)))
379 (define modules+compiled
380 ;; Since MODULES contains both .scm and .go files at its root, re-bundle
381 ;; it so that it has share/guile/site and lib/guile, which is what
382 ;; 'whole-package' expects.
383 (computed-file (derivation-name modules)
384 (with-imported-modules '((guix build utils))
386 (use-modules (guix build utils))
391 (string-append #$output "/share/guile/site"))
393 (string-append #$output "/lib/guile/" version))
395 (mkdir-p share) (mkdir-p lib)
396 (symlink #$modules (string-append share "/" version))
397 (symlink #$modules (string-append lib "/site-ccache"))))))
399 (letrec-syntax ((list (syntax-rules (->)
402 ((_ (module -> variable) rest ...)
403 (cons (module-ref (resolve-interface
404 '(gnu packages module))
407 ((_ variable rest ...)
408 (cons (module-ref packages 'variable)
410 (whole-package name modules+compiled
412 ;; In the "old style", %SELF-BUILD-FILE would simply return a
413 ;; derivation that builds modules. We have to infer what the
414 ;; dependencies of these modules were.
415 (list guile-json guile-git guile-bytestructures
416 (ssh -> guile-ssh) (tls -> gnutls)))))
418 (define (old-style-guix? drv)
419 "Return true if DRV corresponds to a ~/.config/guix/latest style of
421 ;; Here we rely on a gross historical fact: that derivations produced by the
422 ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
423 ;; dated May 30, 2018) did not depend on "guix-command.drv".
424 (not (find (lambda (input)
425 (string-suffix? "-guix-command.drv"
426 (derivation-input-path input)))
427 (derivation-inputs drv))))
429 (define (channel-instances->manifest instances)
430 "Return a profile manifest with entries for all of INSTANCES, a list of
432 (define instance->entry
435 (let ((commit (channel-instance-commit instance))
436 (channel (channel-instance-channel instance)))
437 (with-monad %store-monad
438 (return (manifest-entry
439 (name (symbol->string (channel-name channel)))
440 (version (string-take commit 7))
441 (item (if (guix-channel? channel)
442 (if (old-style-guix? drv)
443 (whole-package-for-legacy
444 (string-append name "-" version)
449 `((source (repository
451 (url ,(channel-url channel))
452 (branch ,(channel-branch channel))
453 (commit ,commit))))))))))))
455 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
456 (entries (mapm %store-monad instance->entry
457 (zip instances derivations))))
458 (return (manifest entries))))
460 (define (package-cache-file manifest)
461 "Build a package cache file for the instance in MANIFEST. This is meant to
462 be used as a profile hook."
463 (mlet %store-monad ((profile (profile-derivation manifest
468 (use-modules (gnu packages))
470 (if (defined? 'generate-package-cache)
472 ;; Delegate package cache generation to the inferior.
473 (format (current-error-port)
474 "Generating package cache for '~a'...~%"
476 (generate-package-cache #$output))
479 (gexp->derivation-in-inferior "guix-package-cache" build
482 ;; If the Guix in PROFILE is too old and
483 ;; lacks 'guix repl', don't build the cache
484 ;; instead of failing.
487 #:properties '((type . profile-hook)
488 (hook . package-cache))
491 (define %channel-profile-hooks
492 ;; The default channel profile hooks.
493 (cons package-cache-file %default-profile-hooks))
495 (define (channel-instances->derivation instances)
496 "Return the derivation of the profile containing INSTANCES, a list of
498 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
499 (profile-derivation manifest
500 #:hooks %channel-profile-hooks)))
502 (define latest-channel-instances*
503 (store-lift latest-channel-instances))
505 (define* (latest-channel-derivation #:optional (channels %default-channels))
506 "Return as a monadic value the derivation that builds the profile for the
507 latest instances of CHANNELS."
508 (mlet %store-monad ((instances (latest-channel-instances* channels)))
509 (channel-instances->derivation instances)))