projects/guix·blob librewolf-updates · refs

621 lines · 24.7 KiB

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