Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / guix / channels.scm
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 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
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.
11 ;;;
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.
16 ;;;
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/>.
19
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 diagnostics)
31 #:use-module (guix store)
32 #:use-module (guix i18n)
33 #:use-module ((guix utils)
34 #:select (source-properties->location
35 &error-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)
46 #:export (channel
47 channel?
48 channel-name
49 channel-url
50 channel-branch
51 channel-commit
52 channel-location
53
54 %default-channels
55 guix-channel?
56
57 channel-instance?
58 channel-instance-channel
59 channel-instance-commit
60 channel-instance-checkout
61
62 latest-channel-instances
63 checkout->channel-instance
64 latest-channel-derivation
65 channel-instances->manifest
66 %channel-profile-hooks
67 channel-instances->derivation))
68
69 ;;; Commentary:
70 ;;;
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'.
76 ;;;
77 ;;; This module provides tools to fetch and update channels from a Git
78 ;;; repository and to build them.
79 ;;;
80 ;;; Code:
81
82 (define-record-type* <channel> channel make-channel
83 channel?
84 (name channel-name)
85 (url channel-url)
86 (branch channel-branch (default "master"))
87 (commit channel-commit (default #f))
88 (location channel-location
89 (default (current-source-location)) (innate)))
90
91 (define %default-channels
92 ;; Default list of channels.
93 (list (channel
94 (name 'guix)
95 (branch "master")
96 (url "https://git.savannah.gnu.org/git/guix.git"))))
97
98 (define (guix-channel? channel)
99 "Return true if CHANNEL is the 'guix' channel."
100 (eq? 'guix (channel-name channel)))
101
102 (define-record-type <channel-instance>
103 (channel-instance channel commit checkout)
104 channel-instance?
105 (channel channel-instance-channel)
106 (commit channel-instance-commit)
107 (checkout channel-instance-checkout))
108
109 (define-record-type <channel-metadata>
110 (channel-metadata version dependencies)
111 channel-metadata?
112 (version channel-metadata-version)
113 (dependencies channel-metadata-dependencies))
114
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)))))
121
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
125 file."
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) '())))
132 (channel-metadata
133 version
134 (map (lambda (item)
135 (let ((get (lambda* (key #:optional default)
136 (or (and=> (assoc-ref item key) first) default))))
137 (and-let* ((name (get 'name))
138 (url (get 'url))
139 (branch (get 'branch "master")))
140 (channel
141 (name name)
142 (branch branch)
143 (url url)
144 (commit (get 'commit))))))
145 dependencies))))))
146
147 (define (channel-instance-dependencies instance)
148 "Return the list of channels that are declared as dependencies for the given
149 channel INSTANCE."
150 (match (read-channel-metadata instance)
151 (#f '())
152 (($ <channel-metadata> version dependencies)
153 dependencies)))
154
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
163 (lambda (a b)
164 (and (eq? (channel-name a) (channel-name b))
165 (or (channel-commit b)
166 (not (or (channel-commit a)
167 (channel-commit b))))))))
168
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)
175 (begin
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
183 channel))))
184 (let ((instance (channel-instance channel commit checkout)))
185 (let-values (((new-instances new-channels)
186 (latest-channel-instances
187 store
188 (channel-instance-dependencies instance)
189 previous-channels)))
190 (values (append (cons channel new-channels)
191 previous-channels)
192 (append (cons instance new-instances)
193 instances))))))))
194 previous-channels
195 '() ;instances
196 channels))
197
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
202 (lambda (a b)
203 (eq? (instance-name a) (instance-name b))))
204 resulting-channels)))
205
206 (define* (checkout->channel-instance checkout
207 #:key commit
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)
213 (commit commit)
214 (url url))))
215 (channel-instance channel commit checkout)))
216
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")
221
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.
226 1)
227
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,
236 ;; etc.
237
238 (define build
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
244 #~(begin
245 (use-modules (guix build compile)
246 (guix build utils)
247 (srfi srfi-26))
248
249 (define go
250 (string-append #$output "/lib/guile/" (effective-version)
251 "/site-ccache"))
252 (define scm
253 (string-append #$output "/share/guile/site/"
254 (effective-version)))
255
256 (compile-files #$source go
257 (find-files #$source "\\.scm$"))
258 (mkdir-p (dirname scm))
259 (symlink #$source scm)
260 scm)))
261
262 (gexp->derivation-in-inferior name build core))
263
264 (define* (build-from-source name source
265 #:key core verbose? commit
266 (dependencies '()))
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
273 ;; be know.
274 (define script
275 (string-append source "/" %self-build-file))
276
277 (if (file-exists? script)
278 (let ((build (save-module-excursion
279 (lambda ()
280 ;; Disable deprecation warnings; it's OK for SCRIPT to
281 ;; use deprecated APIs and the user doesn't have to know
282 ;; about it.
283 (parameterize ((guix-warning-port
284 (%make-void-port "w")))
285 (primitive-load script))))))
286 ;; BUILD must be a monadic procedure of at least one argument: the
287 ;; source tree.
288 ;;
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))
294
295 ;; Build a set of modules that extend Guix using the standard method.
296 (standard-module-derivation name source core dependencies)))
297
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)
307 #:core core
308 #:dependencies dependencies))
309
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))
315
316 (define table ;map a name to an instance
317 (fold (lambda (instance table)
318 (vhash-consq (channel-instance-name instance)
319 instance table))
320 vlist-null
321 instances))
322
323 (define edges
324 (fold (lambda (instance edges)
325 (fold (lambda (channel edges)
326 (let ((name (channel-name channel)))
327 (match (vhash-assq name table)
328 ((_ . target)
329 (vhash-consq instance target edges)))))
330 edges
331 (channel-instance-dependencies instance)))
332 vlist-null
333 instances))
334
335 (lambda (instance)
336 (vhash-foldq* cons '() instance edges)))
337
338 (define (channel-instance-derivations instances)
339 "Return the list of derivations to build INSTANCES, in the same order as
340 INSTANCES."
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)))
346 instances))
347
348 (define edges
349 (resolve-dependencies instances))
350
351 (define (instance->derivation instance)
352 (mlet %store-monad ((system (current-system)))
353 (mcached (if (eq? instance core-instance)
354 (build-channel-instance instance)
355 (mlet %store-monad ((core (instance->derivation core-instance))
356 (deps (mapm %store-monad instance->derivation
357 (edges instance))))
358 (build-channel-instance instance core deps)))
359 instance
360 system)))
361
362 (unless core-instance
363 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
364 instances)
365 source-properties->location)))
366 (raise (apply make-compound-condition
367 (condition
368 (&message (message "'guix' channel is lacking")))
369 (if loc
370 (list (condition (&error-location (location loc))))
371 '())))))
372
373 (mapm %store-monad instance->derivation instances))
374
375 (define (whole-package-for-legacy name modules)
376 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
377 modules in the old ~/.config/guix/latest style."
378 (define packages
379 (resolve-interface '(gnu packages guile)))
380
381 (define modules+compiled
382 ;; Since MODULES contains both .scm and .go files at its root, re-bundle
383 ;; it so that it has share/guile/site and lib/guile, which is what
384 ;; 'whole-package' expects.
385 (computed-file (derivation-name modules)
386 (with-imported-modules '((guix build utils))
387 #~(begin
388 (use-modules (guix build utils))
389
390 (define version
391 (effective-version))
392 (define share
393 (string-append #$output "/share/guile/site"))
394 (define lib
395 (string-append #$output "/lib/guile/" version))
396
397 (mkdir-p share) (mkdir-p lib)
398 (symlink #$modules (string-append share "/" version))
399 (symlink #$modules (string-append lib "/site-ccache"))))))
400
401 (letrec-syntax ((list (syntax-rules (->)
402 ((_)
403 '())
404 ((_ (module -> variable) rest ...)
405 (cons (module-ref (resolve-interface
406 '(gnu packages module))
407 'variable)
408 (list rest ...)))
409 ((_ variable rest ...)
410 (cons (module-ref packages 'variable)
411 (list rest ...))))))
412 (whole-package name modules+compiled
413
414 ;; In the "old style", %SELF-BUILD-FILE would simply return a
415 ;; derivation that builds modules. We have to infer what the
416 ;; dependencies of these modules were.
417 (list guile-json guile-git guile-bytestructures
418 (ssh -> guile-ssh) (tls -> gnutls)))))
419
420 (define (old-style-guix? drv)
421 "Return true if DRV corresponds to a ~/.config/guix/latest style of
422 derivation."
423 ;; Here we rely on a gross historical fact: that derivations produced by the
424 ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
425 ;; dated May 30, 2018) did not depend on "guix-command.drv".
426 (not (find (lambda (input)
427 (string-suffix? "-guix-command.drv"
428 (derivation-input-path input)))
429 (derivation-inputs drv))))
430
431 (define (channel-instances->manifest instances)
432 "Return a profile manifest with entries for all of INSTANCES, a list of
433 channel instances."
434 (define (instance->entry instance drv)
435 (let ((commit (channel-instance-commit instance))
436 (channel (channel-instance-channel instance)))
437 (manifest-entry
438 (name (symbol->string (channel-name channel)))
439 (version (string-take commit 7))
440 (item (if (guix-channel? channel)
441 (if (old-style-guix? drv)
442 (whole-package-for-legacy (string-append name "-" version)
443 drv)
444 drv)
445 drv))
446 (properties
447 `((source (repository
448 (version 0)
449 (url ,(channel-url channel))
450 (branch ,(channel-branch channel))
451 (commit ,commit))))))))
452
453 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
454 (entries -> (map instance->entry instances derivations)))
455 (return (manifest entries))))
456
457 (define (package-cache-file manifest)
458 "Build a package cache file for the instance in MANIFEST. This is meant to
459 be used as a profile hook."
460 (mlet %store-monad ((profile (profile-derivation manifest
461 #:hooks '())))
462
463 (define build
464 #~(begin
465 (use-modules (gnu packages))
466
467 (if (defined? 'generate-package-cache)
468 (begin
469 ;; Delegate package cache generation to the inferior.
470 (format (current-error-port)
471 "Generating package cache for '~a'...~%"
472 #$profile)
473 (generate-package-cache #$output))
474 (mkdir #$output))))
475
476 (gexp->derivation-in-inferior "guix-package-cache" build
477 profile
478
479 ;; If the Guix in PROFILE is too old and
480 ;; lacks 'guix repl', don't build the cache
481 ;; instead of failing.
482 #:silent-failure? #t
483
484 #:properties '((type . profile-hook)
485 (hook . package-cache))
486 #:local-build? #t)))
487
488 (define %channel-profile-hooks
489 ;; The default channel profile hooks.
490 (cons package-cache-file %default-profile-hooks))
491
492 (define (channel-instances->derivation instances)
493 "Return the derivation of the profile containing INSTANCES, a list of
494 channel instances."
495 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
496 (profile-derivation manifest
497 #:hooks %channel-profile-hooks)))
498
499 (define latest-channel-instances*
500 (store-lift latest-channel-instances))
501
502 (define* (latest-channel-derivation #:optional (channels %default-channels))
503 "Return as a monadic value the derivation that builds the profile for the
504 latest instances of CHANNELS."
505 (mlet %store-monad ((instances (latest-channel-instances* channels)))
506 (channel-instances->derivation instances)))