Merge branch 'master' into staging
[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 deprecation)
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 ((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
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 (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
356 (edges instance))))
357 (build-channel-instance instance core deps)))
358 instance))
359
360 (unless core-instance
361 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
362 instances)
363 source-properties->location)))
364 (raise (apply make-compound-condition
365 (condition
366 (&message (message "'guix' channel is lacking")))
367 (if loc
368 (list (condition (&error-location (location loc))))
369 '())))))
370
371 (mapm %store-monad instance->derivation instances))
372
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."
376 (define packages
377 (resolve-interface '(gnu packages guile)))
378
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))
385 #~(begin
386 (use-modules (guix build utils))
387
388 (define version
389 (effective-version))
390 (define share
391 (string-append #$output "/share/guile/site"))
392 (define lib
393 (string-append #$output "/lib/guile/" version))
394
395 (mkdir-p share) (mkdir-p lib)
396 (symlink #$modules (string-append share "/" version))
397 (symlink #$modules (string-append lib "/site-ccache"))))))
398
399 (letrec-syntax ((list (syntax-rules (->)
400 ((_)
401 '())
402 ((_ (module -> variable) rest ...)
403 (cons (module-ref (resolve-interface
404 '(gnu packages module))
405 'variable)
406 (list rest ...)))
407 ((_ variable rest ...)
408 (cons (module-ref packages 'variable)
409 (list rest ...))))))
410 (whole-package name modules+compiled
411
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)))))
417
418 (define (old-style-guix? drv)
419 "Return true if DRV corresponds to a ~/.config/guix/latest style of
420 derivation."
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))))
428
429 (define (channel-instances->manifest instances)
430 "Return a profile manifest with entries for all of INSTANCES, a list of
431 channel instances."
432 (define instance->entry
433 (match-lambda
434 ((instance drv)
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)
445 drv)
446 drv)
447 drv))
448 (properties
449 `((source (repository
450 (version 0)
451 (url ,(channel-url channel))
452 (branch ,(channel-branch channel))
453 (commit ,commit))))))))))))
454
455 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
456 (entries (mapm %store-monad instance->entry
457 (zip instances derivations))))
458 (return (manifest entries))))
459
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
464 #:hooks '())))
465
466 (define build
467 #~(begin
468 (use-modules (gnu packages))
469
470 (if (defined? 'generate-package-cache)
471 (begin
472 ;; Delegate package cache generation to the inferior.
473 (format (current-error-port)
474 "Generating package cache for '~a'...~%"
475 #$profile)
476 (generate-package-cache #$output))
477 (mkdir #$output))))
478
479 (gexp->derivation-in-inferior "guix-package-cache" build
480 profile
481
482 ;; If the Guix in PROFILE is too old and
483 ;; lacks 'guix repl', don't build the cache
484 ;; instead of failing.
485 #:silent-failure? #t
486
487 #:properties '((type . profile-hook)
488 (hook . package-cache))
489 #:local-build? #t)))
490
491 (define %channel-profile-hooks
492 ;; The default channel profile hooks.
493 (cons package-cache-file %default-profile-hooks))
494
495 (define (channel-instances->derivation instances)
496 "Return the derivation of the profile containing INSTANCES, a list of
497 channel instances."
498 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
499 (profile-derivation manifest
500 #:hooks %channel-profile-hooks)))
501
502 (define latest-channel-instances*
503 (store-lift latest-channel-instances))
504
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)))