Revert "gnu: nss: Update to 3.45 [security fixes]."
[guix.git] / guix / channels.scm
blob415246cbd1f7d44d455f8854f6e04b5b0921c92c
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>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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
36                           &error-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)
47   #:export (channel
48             channel?
49             channel-name
50             channel-url
51             channel-branch
52             channel-commit
53             channel-location
55             %default-channels
56             guix-channel?
58             channel-instance?
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))
70 ;;; Commentary:
71 ;;;
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'.
77 ;;;
78 ;;; This module provides tools to fetch and update channels from a Git
79 ;;; repository and to build them.
80 ;;;
81 ;;; Code:
83 (define-record-type* <channel> channel make-channel
84   channel?
85   (name      channel-name)
86   (url       channel-url)
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.
94   (list (channel
95          (name 'guix)
96          (branch "master")
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)
105   channel-instance?
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)
112   channel-metadata?
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."
127   (match (read port)
128     (('channel ('version 0) properties ...)
129      (let ((directory    (and=> (assoc-ref properties 'directory) first))
130            (dependencies (or (assoc-ref properties 'dependencies) '())))
131        (channel-metadata
132         (cond ((not directory) "/")
133               ((string-prefix? "/" directory) directory)
134               (else (string-append "/" directory)))
135         (map (lambda (item)
136                (let ((get (lambda* (key #:optional default)
137                             (or (and=> (assoc-ref item key) first) default))))
138                  (and-let* ((name (get 'name))
139                             (url (get 'url))
140                             (branch (get 'branch "master")))
141                    (channel
142                     (name name)
143                     (branch branch)
144                     (url url)
145                     (commit (get 'commit))))))
146              dependencies))))
147     ((and ('channel ('version version) _ ...) sexp)
148      (raise (condition
149              (&message (message "unsupported '.guix-channel' version"))
150              (&error-location
151               (location (source-properties->location
152                          (source-properties sexp)))))))
153     (sexp
154      (raise (condition
155              (&message (message "invalid '.guix-channel' file"))
156              (&error-location
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
163 doesn't exist."
164   (catch 'system-error
165     (lambda ()
166       (call-with-input-file (string-append source "/.guix-channel")
167         read-channel-metadata))
168     (lambda args
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
180 channel INSTANCE."
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
191             (lambda (a b)
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)
203                  (begin
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
211                                                                   channel))))
212                      (let ((instance (channel-instance channel commit checkout)))
213                        (let-values (((new-instances new-channels)
214                                      (latest-channel-instances
215                                       store
216                                       (channel-instance-dependencies instance)
217                                       previous-channels)))
218                          (values (append (cons channel new-channels)
219                                          previous-channels)
220                                  (append (cons instance new-instances)
221                                          instances))))))))
222            previous-channels
223            '()                                    ;instances
224            channels))
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
230                                (lambda (a b)
231                                  (eq? (instance-name a) (instance-name b))))
232             resulting-channels)))
234 (define* (checkout->channel-instance checkout
235                                      #:key commit
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)
241                            (commit commit)
242                            (url url))))
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.
254   1)
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)))
265     (define build
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
271         #~(begin
272             (use-modules (guix build compile)
273                          (guix build utils)
274                          (srfi srfi-26))
276             (define go
277               (string-append #$output "/lib/guile/" (effective-version)
278                              "/site-ccache"))
279             (define scm
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))
289             scm)))
291     (gexp->derivation-in-inferior name build core)))
293 (define* (build-from-source name source
294                             #:key core verbose? commit
295                             (dependencies '()))
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
302   ;; be know.
303   (define script
304     (string-append source "/" %self-build-file))
306   (if (file-exists? script)
307       (let ((build (save-module-excursion
308                     (lambda ()
309                       ;; Disable deprecation warnings; it's OK for SCRIPT to
310                       ;; use deprecated APIs and the user doesn't have to know
311                       ;; about it.
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
316         ;; source tree.
317         ;;
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)
336                      #:core core
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)
348                          instance table))
349           vlist-null
350           instances))
352   (define edges
353     (fold (lambda (instance edges)
354             (fold (lambda (channel edges)
355                     (let ((name (channel-name channel)))
356                       (match (vhash-assq name table)
357                         ((_ . target)
358                          (vhash-consq instance target edges)))))
359                   edges
360                   (channel-instance-dependencies instance)))
361           vlist-null
362           instances))
364   (lambda (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
369 INSTANCES."
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)))
375           instances))
377   (define edges
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
386                                                    (edges instance))))
387                      (build-channel-instance instance core deps)))
388                instance
389                system)))
391   (unless core-instance
392     (let ((loc (and=> (any (compose channel-location channel-instance-channel)
393                            instances)
394                       source-properties->location)))
395       (raise (apply make-compound-condition
396                     (condition
397                      (&message (message "'guix' channel is lacking")))
398                     (if loc
399                         (list (condition (&error-location (location loc))))
400                         '())))))
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."
407   (define packages
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))
416                      #~(begin
417                          (use-modules (guix build utils))
419                          (define version
420                            (effective-version))
421                          (define share
422                            (string-append #$output "/share/guile/site"))
423                          (define lib
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 (->)
431                           ((_)
432                            '())
433                           ((_ (module -> variable) rest ...)
434                            (cons (module-ref (resolve-interface
435                                               '(gnu packages module))
436                                              'variable)
437                                  (list rest ...)))
438                           ((_ variable rest ...)
439                            (cons (module-ref packages 'variable)
440                                  (list rest ...))))))
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
451 derivation."
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"
457                          (derivation-name
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
463 channel instances."
464   (define (instance->entry instance drv)
465     (let ((commit  (channel-instance-commit instance))
466           (channel (channel-instance-channel instance)))
467       (manifest-entry
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)
473                                                 drv)
474                       drv)
475                   drv))
476         (properties
477          `((source (repository
478                     (version 0)
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
491                                                    #:hooks '())))
493     (define build
494       #~(begin
495           (use-modules (gnu packages))
497           (if (defined? 'generate-package-cache)
498               (begin
499                 ;; Delegate package cache generation to the inferior.
500                 (format (current-error-port)
501                         "Generating package cache for '~a'...~%"
502                         #$profile)
503                 (generate-package-cache #$output))
504               (mkdir #$output))))
506     (gexp->derivation-in-inferior "guix-package-cache" build
507                                   profile
509                                   ;; If the Guix in PROFILE is too old and
510                                   ;; lacks 'guix repl', don't build the cache
511                                   ;; instead of failing.
512                                   #:silent-failure? #t
514                                   #:properties '((type . profile-hook)
515                                                  (hook . package-cache))
516                                   #:local-build? #t)))
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
524 channel instances."
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)))