WIP: bees service
[jackhill/guix/guix.git] / gnu / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
6a7c4636 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
c2868b1e 3;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
7d193ec3 4;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
96eaa55f 5;;; Copyright © 2016, 2017 Alex Kost <alezost@gmail.com>
fad155d4 6;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
6b1891b0 7;;;
233e7676 8;;; This file is part of GNU Guix.
6b1891b0 9;;;
233e7676 10;;; GNU Guix is free software; you can redistribute it and/or modify it
6b1891b0
LC
11;;; under the terms of the GNU General Public License as published by
12;;; the Free Software Foundation; either version 3 of the License, or (at
13;;; your option) any later version.
14;;;
233e7676 15;;; GNU Guix is distributed in the hope that it will be useful, but
6b1891b0
LC
16;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;;; GNU General Public License for more details.
19;;;
20;;; You should have received a copy of the GNU General Public License
233e7676 21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
6b1891b0 22
59a43334 23(define-module (gnu packages)
6b1891b0 24 #:use-module (guix packages)
98eb8cbe 25 #:use-module (guix ui)
800cdeef 26 #:use-module (guix utils)
d51bfe24 27 #:use-module (guix diagnostics)
cd903ef7 28 #:use-module (guix discovery)
f9704f17 29 #:use-module (guix memoization)
95cd4971
LC
30 #:use-module ((guix build utils)
31 #:select ((package-name->name+version
5fbdc9a5
LC
32 . hyphen-separated-name->name+version)
33 mkdir-p))
6a7c4636 34 #:use-module (guix profiles)
fe634eaf 35 #:use-module (guix describe)
f2bf0407 36 #:use-module (guix deprecation)
c2868b1e 37 #:use-module (ice-9 vlist)
dc5669cd 38 #:use-module (ice-9 match)
6a7c4636 39 #:use-module (ice-9 binary-ports)
5fbdc9a5 40 #:autoload (system base compile) (compile)
6b1891b0 41 #:use-module (srfi srfi-1)
5e3b388b 42 #:use-module (srfi srfi-11)
6b1891b0 43 #:use-module (srfi srfi-26)
dbab5150
LC
44 #:use-module (srfi srfi-34)
45 #:use-module (srfi srfi-35)
800cdeef
LC
46 #:use-module (srfi srfi-39)
47 #:export (search-patch
25897079 48 search-patches
96eaa55f 49 search-auxiliary-file
0492f4a2 50 %patch-path
96eaa55f 51 %auxiliary-files-path
c107b541 52 %package-module-path
fe634eaf 53 %default-package-module-path
7d193ec3 54
ba326ce4 55 fold-packages
0ea939fb 56 fold-available-packages
7d193ec3 57
f2bf0407 58 find-newest-available-packages
dc5669cd 59 find-packages-by-name
ee8099f5 60 find-package-locations
3f26bfc1 61 find-best-packages-by-name
7d193ec3 62
84189ebc 63 specification->package
c08ea55e 64 specification->package+output
ee8099f5 65 specification->location
5fbdc9a5
LC
66 specifications->manifest
67
68 generate-package-cache))
6b1891b0
LC
69
70;;; Commentary:
71;;;
72;;; General utilities for the software distribution---i.e., the modules under
59a43334 73;;; (gnu packages ...).
6b1891b0
LC
74;;;
75;;; Code:
76
1ba0b1e6 77;; By default, we store patches and auxiliary files
96eaa55f
AK
78;; alongside Guile modules. This is so that these extra files can be
79;; found without requiring a special setup, such as a specific
80;; installation directory and an extra environment variable. One
81;; advantage of this setup is that everything just works in an
82;; auto-compilation setting.
a9f60c42 83
96eaa55f
AK
84(define %auxiliary-files-path
85 (make-parameter
86 (map (cut string-append <> "/gnu/packages/aux-files")
87 %load-path)))
88
89(define (search-auxiliary-file file-name)
90 "Search the auxiliary FILE-NAME. Return #f if not found."
91 (search-path (%auxiliary-files-path) file-name))
92
800cdeef 93(define (search-patch file-name)
dbab5150
LC
94 "Search the patch FILE-NAME. Raise an error if not found."
95 (or (search-path (%patch-path) file-name)
d51bfe24
LC
96 (raise (formatted-message (G_ "~a: patch not found")
97 file-name))))
800cdeef 98
25897079
AK
99(define-syntax-rule (search-patches file-name ...)
100 "Return the list of absolute file names corresponding to each
101FILE-NAME found in %PATCH-PATH."
102 (list (search-patch file-name) ...))
103
84836a57 104(define %distro-root-directory
eaae07ec
LC
105 ;; Absolute file name of the module hierarchy. Since (gnu packages …) might
106 ;; live in a directory different from (guix), try to get the best match.
107 (letrec-syntax ((dirname* (syntax-rules ()
108 ((_ file)
109 (dirname file))
110 ((_ file head tail ...)
111 (dirname (dirname* file tail ...)))))
112 (try (syntax-rules ()
113 ((_ (file things ...) rest ...)
114 (match (search-path %load-path file)
115 (#f
116 (try rest ...))
117 (absolute
118 (dirname* absolute things ...))))
119 ((_)
120 #f))))
121 (try ("gnu/packages/base.scm" gnu/ packages/)
122 ("gnu/packages.scm" gnu/)
123 ("guix.scm"))))
6b1891b0 124
fe634eaf
LC
125(define %default-package-module-path
126 ;; Default search path for package modules.
127 `((,%distro-root-directory . "gnu/packages")))
128
5fbdc9a5
LC
129(define (cache-is-authoritative?)
130 "Return true if the pre-computed package cache is authoritative. It is not
131authoritative when entries have been added via GUIX_PACKAGE_PATH or '-L'
132flags."
133 (equal? (%package-module-path)
134 (append %default-package-module-path
135 (package-path-entries))))
136
c107b541
LC
137(define %package-module-path
138 ;; Search path for package modules. Each item must be either a directory
139 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
140 ;; to narrow the search.
bfc9c339
LC
141 (let*-values (((not-colon)
142 (char-set-complement (char-set #\:)))
143 ((environment)
144 (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
145 not-colon))
146 ((channels-scm channels-go)
147 (package-path-entries)))
fe634eaf
LC
148 ;; Automatically add channels and items from $GUIX_PACKAGE_PATH to Guile's
149 ;; search path. For historical reasons, $GUIX_PACKAGE_PATH goes to the
150 ;; front; channels go to the back so that they don't override Guix' own
151 ;; modules.
152 (set! %load-path
bfc9c339 153 (append environment %load-path channels-scm))
fe634eaf 154 (set! %load-compiled-path
bfc9c339 155 (append environment %load-compiled-path channels-go))
8689901f
LC
156
157 (make-parameter
fe634eaf
LC
158 (append environment
159 %default-package-module-path
bfc9c339 160 channels-scm))))
c107b541 161
ee06af5b
LC
162(define %patch-path
163 ;; Define it after '%package-module-path' so that '%load-path' contains user
164 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
165 (make-parameter
166 (map (lambda (directory)
167 (if (string=? directory %distro-root-directory)
168 (string-append directory "/gnu/packages/patches")
169 directory))
170 %load-path)))
171
f2bf0407
LC
172;; This procedure is used by Emacs-Guix up to 0.5.1.1, so keep it for now.
173;; See <https://github.com/alezost/guix.el/issues/30>.
174(define-deprecated find-newest-available-packages
175 find-packages-by-name
176 (mlambda ()
177 "Return a vhash keyed by package names, and with
178associated values of the form
179
180 (newest-version newest-package ...)
181
182where the preferred package is listed first."
183 (fold-packages (lambda (p r)
184 (let ((name (package-name p))
185 (version (package-version p)))
186 (match (vhash-assoc name r)
187 ((_ newest-so-far . pkgs)
188 (case (version-compare version newest-so-far)
189 ((>) (vhash-cons name `(,version ,p) r))
190 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
191 ((<) r)))
192 (#f (vhash-cons name `(,version ,p) r)))))
193 vlist-null)))
194
0ea939fb
LC
195(define (fold-available-packages proc init)
196 "Fold PROC over the list of available packages. For each available package,
197PROC is called along these lines:
198
199 (PROC NAME VERSION RESULT
200 #:outputs OUTPUTS
201 #:location LOCATION
202 …)
203
204PROC can use #:allow-other-keys to ignore the bits it's not interested in.
205When a package cache is available, this procedure does not actually load any
206package module."
207 (define cache
208 (load-package-cache (current-profile)))
209
210 (if (and cache (cache-is-authoritative?))
211 (vhash-fold (lambda (name vector result)
212 (match vector
213 (#(name version module symbol outputs
214 supported? deprecated?
215 file line column)
216 (proc name version result
217 #:outputs outputs
218 #:location (and file
219 (location file line column))
220 #:supported? supported?
221 #:deprecated? deprecated?))))
222 init
223 cache)
224 (fold-packages (lambda (package result)
225 (proc (package-name package)
226 (package-version package)
227 result
228 #:outputs (package-outputs package)
229 #:location (package-location package)
230 #:supported?
88da0115 231 (->bool (supported-package? package))
0ea939fb
LC
232 #:deprecated?
233 (->bool
234 (package-superseded package))))
235 init)))
236
5c5ae46c
LC
237(define* (fold-packages proc init
238 #:optional
3c0128b0
LC
239 (modules (all-modules (%package-module-path)
240 #:warn
241 warn-about-load-error))
96dc8f35 242 #:key (select? (negate hidden-package?)))
5c5ae46c 243 "Call (PROC PACKAGE RESULT) for each available package defined in one of
96dc8f35
LC
244MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
245is guaranteed to never traverse the same package twice."
cd903ef7 246 (fold-module-public-variables (lambda (object result)
96dc8f35 247 (if (and (package? object) (select? object))
cd903ef7
LC
248 (proc object result)
249 result))
250 init
5c5ae46c 251 modules))
ba326ce4 252
5fbdc9a5
LC
253(define %package-cache-file
254 ;; Location of the package cache.
255 "/lib/guix/package.cache")
256
257(define load-package-cache
258 (mlambda (profile)
259 "Attempt to load the package cache. On success return a vhash keyed by
260package names. Return #f on failure."
261 (match profile
262 (#f #f)
263 (profile
264 (catch 'system-error
265 (lambda ()
266 (define lst
267 (load-compiled (string-append profile %package-cache-file)))
268 (fold (lambda (item vhash)
269 (match item
270 (#(name version module symbol outputs
271 supported? deprecated?
272 file line column)
273 (vhash-cons name item vhash))))
274 vlist-null
275 lst))
276 (lambda args
277 (if (= ENOENT (system-error-errno args))
278 #f
279 (apply throw args))))))))
280
281(define find-packages-by-name/direct ;bypass the cache
9ffc1c00
LC
282 (let ((packages (delay
283 (fold-packages (lambda (p r)
284 (vhash-cons (package-name p) p r))
724311a2
LC
285 vlist-null)))
286 (version>? (lambda (p1 p2)
287 (version>? (package-version p1) (package-version p2)))))
9ffc1c00
LC
288 (lambda* (name #:optional version)
289 "Return the list of packages with the given NAME. If VERSION is not #f,
724311a2
LC
290then only return packages whose version is prefixed by VERSION, sorted in
291decreasing version order."
292 (let ((matching (sort (vhash-fold* cons '() name (force packages))
293 version>?)))
9ffc1c00
LC
294 (if version
295 (filter (lambda (package)
348987d3 296 (version-prefix? version (package-version package)))
9ffc1c00
LC
297 matching)
298 matching)))))
dc5669cd 299
5fbdc9a5
LC
300(define (cache-lookup cache name)
301 "Lookup package NAME in CACHE. Return a list sorted in increasing version
302order."
303 (define (package-version<? v1 v2)
304 (version>? (vector-ref v2 1) (vector-ref v1 1)))
305
306 (sort (vhash-fold* cons '() name cache)
307 package-version<?))
308
309(define* (find-packages-by-name name #:optional version)
310 "Return the list of packages with the given NAME. If VERSION is not #f,
311then only return packages whose version is prefixed by VERSION, sorted in
312decreasing version order."
313 (define cache
314 (load-package-cache (current-profile)))
315
316 (if (and (cache-is-authoritative?) cache)
317 (match (cache-lookup cache name)
318 (#f #f)
319 ((#(_ versions modules symbols _ _ _ _ _ _) ...)
320 (fold (lambda (version* module symbol result)
321 (if (or (not version)
322 (version-prefix? version version*))
323 (cons (module-ref (resolve-interface module)
324 symbol)
325 result)
326 result))
327 '()
328 versions modules symbols)))
329 (find-packages-by-name/direct name version)))
330
ee8099f5
LC
331(define* (find-package-locations name #:optional version)
332 "Return a list of version/location pairs corresponding to each package
333matching NAME and VERSION."
334 (define cache
335 (load-package-cache (current-profile)))
336
337 (if (and cache (cache-is-authoritative?))
338 (match (cache-lookup cache name)
339 (#f '())
340 ((#(name versions modules symbols outputs
341 supported? deprecated?
342 files lines columns) ...)
343 (fold (lambda (version* file line column result)
344 (if (and file
345 (or (not version)
346 (version-prefix? version version*)))
347 (alist-cons version* (location file line column)
348 result)
349 result))
350 '()
351 versions files lines columns)))
352 (map (lambda (package)
353 (cons (package-version package) (package-location package)))
354 (find-packages-by-name/direct name version))))
3f26bfc1
LC
355
356(define (find-best-packages-by-name name version)
357 "If version is #f, return the list of packages named NAME with the highest
358version numbers; otherwise, return the list of packages named NAME and at
359VERSION."
360 (if version
361 (find-packages-by-name name version)
e2a903c8
LC
362 (match (find-packages-by-name name)
363 (()
364 '())
365 ((matches ...)
366 ;; Return the subset of MATCHES with the higher version number.
367 (let ((highest (package-version (first matches))))
368 (take-while (lambda (p)
369 (string=? (package-version p) highest))
370 matches))))))
7d193ec3 371
886a7607
LC
372;; Prevent Guile 3 from inlining this procedure so we can mock it in tests.
373(set! find-best-packages-by-name find-best-packages-by-name)
374
5fbdc9a5
LC
375(define (generate-package-cache directory)
376 "Generate under DIRECTORY a cache of all the available packages.
377
378The primary purpose of the cache is to speed up package lookup by name such
379that we don't have to traverse and load all the package modules, thereby also
380reducing the memory footprint."
381 (define cache-file
382 (string-append directory %package-cache-file))
383
a127e52f
LC
384 (define expand-cache
385 (match-lambda*
386 (((module symbol variable) (result . seen))
387 (let ((package (variable-ref variable)))
388 (if (or (vhash-assq package seen)
389 (hidden-package? package))
390 (cons result seen)
391 (cons (cons `#(,(package-name package)
392 ,(package-version package)
393 ,(module-name module)
394 ,symbol
395 ,(package-outputs package)
396 ,(->bool (supported-package? package))
397 ,(->bool (package-superseded package))
398 ,@(let ((loc (package-location package)))
399 (if loc
400 `(,(location-file loc)
401 ,(location-line loc)
402 ,(location-column loc))
403 '(#f #f #f))))
404 result)
405 (vhash-consq package #t seen)))))))
406
407 (define entry-key
408 (match-lambda
409 ((module symbol variable)
410 (let ((value (variable-ref variable)))
411 (string-append (package-name value) (package-version value)
412 (object->string module)
413 (symbol->string symbol))))))
414
415 (define (entry<? a b)
416 (string<? (entry-key a) (entry-key b)))
417
418 (define variables
419 ;; First sort variables so that 'expand-cache' later dismisses
420 ;; already-seen package objects in a deterministic fashion.
421 (sort
422 (fold-module-public-variables* (lambda (module symbol variable lst)
423 (let ((value (false-if-exception
424 (variable-ref variable))))
425 (if (package? value)
426 (cons (list module symbol variable)
427 lst)
428 lst)))
429 '()
36754eee
LC
430 (all-modules (%package-module-path)
431 #:warn
a127e52f
LC
432 warn-about-load-error))
433 entry<?))
434
435 (define exp
436 (first (fold expand-cache (cons '() vlist-null) variables)))
5fbdc9a5
LC
437
438 (mkdir-p (dirname cache-file))
439 (call-with-output-file cache-file
440 (lambda (port)
441 ;; Store the cache as a '.go' file. This makes loading fast and reduces
442 ;; heap usage since some of the static data is directly mmapped.
443 (put-bytevector port
444 (compile `'(,@exp)
445 #:to 'bytecode
446 #:opts '(#:to-file? #t)))))
447 cache-file)
7d193ec3
EB
448
449\f
4ea44419
AK
450(define %sigint-prompt
451 ;; The prompt to jump to upon SIGINT.
452 (make-prompt-tag "interruptible"))
453
454(define (call-with-sigint-handler thunk handler)
455 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
456number in the context of the continuation of the call to this function, and
457return its return value."
458 (call-with-prompt %sigint-prompt
459 (lambda ()
460 (sigaction SIGINT
461 (lambda (signum)
462 (sigaction SIGINT SIG_DFL)
463 (abort-to-prompt %sigint-prompt signum)))
464 (dynamic-wind
465 (const #t)
466 thunk
467 (cut sigaction SIGINT SIG_DFL)))
468 (lambda (k signum)
469 (handler signum))))
470
fad155d4
ML
471\f
472;;;
473;;; Package specification.
474;;;
475
e30c2be1 476(define* (%find-package spec name version)
fad155d4
ML
477 (match (find-best-packages-by-name name version)
478 ((pkg . pkg*)
479 (unless (null? pkg*)
69daee23
LC
480 (warning (G_ "ambiguous package specification `~a'~%") spec)
481 (warning (G_ "choosing ~a@~a from ~a~%")
d75e8f36 482 (package-name pkg) (package-version pkg)
fad155d4 483 (location->string (package-location pkg))))
01afdab8
LC
484 (match (package-superseded pkg)
485 ((? package? new)
69daee23 486 (info (G_ "package '~a' has been superseded by '~a'~%")
01afdab8
LC
487 (package-name pkg) (package-name new))
488 new)
489 (#f
490 pkg)))
e465d9e1 491 (x
fad155d4 492 (if version
69daee23
LC
493 (leave (G_ "~A: package not found for version ~a~%") name version)
494 (leave (G_ "~A: unknown package~%") name)))))
fad155d4 495
5e3b388b
CR
496(define (specification->package spec)
497 "Return a package matching SPEC. SPEC may be a package name, or a package
1b846da8 498name followed by an at-sign and a version number. If the version number is not
5e3b388b 499present, return the preferred newest version."
fad155d4
ML
500 (let-values (((name version) (package-name->name+version spec)))
501 (%find-package spec name version)))
84189ebc 502
ee8099f5
LC
503(define (specification->location spec)
504 "Return the location of the highest-numbered package matching SPEC, a
505specification such as \"guile@2\" or \"emacs\"."
506 (let-values (((name version) (package-name->name+version spec)))
507 (match (find-package-locations name version)
508 (()
509 (if version
510 (leave (G_ "~A: package not found for version ~a~%") name version)
511 (leave (G_ "~A: unknown package~%") name)))
512 (lst
513 (let* ((highest (match lst (((version . _) _ ...) version)))
514 (locations (take-while (match-lambda
515 ((version . location)
516 (string=? version highest)))
517 lst)))
518 (match locations
519 (((version . location) . rest)
520 (unless (null? rest)
521 (warning (G_ "ambiguous package specification `~a'~%") spec)
522 (warning (G_ "choosing ~a@~a from ~a~%")
523 name version
524 (location->string location)))
525 location)))))))
526
84189ebc
LC
527(define* (specification->package+output spec #:optional (output "out"))
528 "Return the package and output specified by SPEC, or #f and #f; SPEC may
529optionally contain a version number and an output name, as in these examples:
530
531 guile
1b846da8 532 guile@2.0.9
84189ebc 533 guile:debug
1b846da8 534 guile@2.0.9:debug
84189ebc
LC
535
536If SPEC does not specify a version number, return the preferred newest
066eeae1
LC
537version; if SPEC does not specify an output, return OUTPUT.
538
539When OUTPUT is false and SPEC does not specify any output, return #f as the
540output."
84189ebc
LC
541 (let-values (((name version sub-drv)
542 (package-specification->name+version+output spec output)))
fad155d4
ML
543 (match (%find-package spec name version)
544 (#f
545 (values #f #f))
546 (package
066eeae1
LC
547 (if (or (and (not output) (not sub-drv))
548 (member sub-drv (package-outputs package)))
fad155d4 549 (values package sub-drv)
69daee23 550 (leave (G_ "package `~a' lacks output `~a'~%")
fad155d4
ML
551 (package-full-name package)
552 sub-drv))))))
c08ea55e
LC
553
554(define (specifications->manifest specs)
555 "Given SPECS, a list of specifications such as \"emacs@25.2\" or
556\"guile:debug\", return a profile manifest."
557 ;; This procedure exists mostly so users of 'guix package -m' don't have to
558 ;; fiddle with multiple-value returns.
559 (packages->manifest
560 (map (compose list specification->package+output) specs)))