| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
| 3 | ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org> |
| 4 | ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org> |
| 5 | ;;; Copyright © 2016, 2017 Alex Kost <alezost@gmail.com> |
| 6 | ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org> |
| 7 | ;;; |
| 8 | ;;; This file is part of GNU Guix. |
| 9 | ;;; |
| 10 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
| 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 | ;;; |
| 15 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
| 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 |
| 21 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | (define-module (gnu packages) |
| 24 | #:use-module (guix packages) |
| 25 | #:use-module (guix ui) |
| 26 | #:use-module (guix utils) |
| 27 | #:use-module (guix discovery) |
| 28 | #:use-module (guix memoization) |
| 29 | #:use-module ((guix build utils) |
| 30 | #:select ((package-name->name+version |
| 31 | . hyphen-separated-name->name+version) |
| 32 | mkdir-p)) |
| 33 | #:use-module (guix profiles) |
| 34 | #:use-module (guix describe) |
| 35 | #:use-module (guix deprecation) |
| 36 | #:use-module (ice-9 vlist) |
| 37 | #:use-module (ice-9 match) |
| 38 | #:use-module (ice-9 binary-ports) |
| 39 | #:autoload (system base compile) (compile) |
| 40 | #:use-module (srfi srfi-1) |
| 41 | #:use-module (srfi srfi-11) |
| 42 | #:use-module (srfi srfi-26) |
| 43 | #:use-module (srfi srfi-34) |
| 44 | #:use-module (srfi srfi-35) |
| 45 | #:use-module (srfi srfi-39) |
| 46 | #:export (search-patch |
| 47 | search-patches |
| 48 | search-auxiliary-file |
| 49 | %patch-path |
| 50 | %auxiliary-files-path |
| 51 | %package-module-path |
| 52 | %default-package-module-path |
| 53 | |
| 54 | fold-packages |
| 55 | fold-available-packages |
| 56 | |
| 57 | find-newest-available-packages |
| 58 | find-packages-by-name |
| 59 | find-package-locations |
| 60 | find-best-packages-by-name |
| 61 | |
| 62 | specification->package |
| 63 | specification->package+output |
| 64 | specification->location |
| 65 | specifications->manifest |
| 66 | |
| 67 | generate-package-cache)) |
| 68 | |
| 69 | ;;; Commentary: |
| 70 | ;;; |
| 71 | ;;; General utilities for the software distribution---i.e., the modules under |
| 72 | ;;; (gnu packages ...). |
| 73 | ;;; |
| 74 | ;;; Code: |
| 75 | |
| 76 | ;; By default, we store patches and auxiliary files |
| 77 | ;; alongside Guile modules. This is so that these extra files can be |
| 78 | ;; found without requiring a special setup, such as a specific |
| 79 | ;; installation directory and an extra environment variable. One |
| 80 | ;; advantage of this setup is that everything just works in an |
| 81 | ;; auto-compilation setting. |
| 82 | |
| 83 | (define %auxiliary-files-path |
| 84 | (make-parameter |
| 85 | (map (cut string-append <> "/gnu/packages/aux-files") |
| 86 | %load-path))) |
| 87 | |
| 88 | (define (search-auxiliary-file file-name) |
| 89 | "Search the auxiliary FILE-NAME. Return #f if not found." |
| 90 | (search-path (%auxiliary-files-path) file-name)) |
| 91 | |
| 92 | (define (search-patch file-name) |
| 93 | "Search the patch FILE-NAME. Raise an error if not found." |
| 94 | (or (search-path (%patch-path) file-name) |
| 95 | (raise (condition |
| 96 | (&message (message (format #f (G_ "~a: patch not found") |
| 97 | file-name))))))) |
| 98 | |
| 99 | (define-syntax-rule (search-patches file-name ...) |
| 100 | "Return the list of absolute file names corresponding to each |
| 101 | FILE-NAME found in %PATCH-PATH." |
| 102 | (list (search-patch file-name) ...)) |
| 103 | |
| 104 | (define %distro-root-directory |
| 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")))) |
| 124 | |
| 125 | (define %default-package-module-path |
| 126 | ;; Default search path for package modules. |
| 127 | `((,%distro-root-directory . "gnu/packages"))) |
| 128 | |
| 129 | (define (cache-is-authoritative?) |
| 130 | "Return true if the pre-computed package cache is authoritative. It is not |
| 131 | authoritative when entries have been added via GUIX_PACKAGE_PATH or '-L' |
| 132 | flags." |
| 133 | (equal? (%package-module-path) |
| 134 | (append %default-package-module-path |
| 135 | (package-path-entries)))) |
| 136 | |
| 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. |
| 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))) |
| 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 |
| 153 | (append environment %load-path channels-scm)) |
| 154 | (set! %load-compiled-path |
| 155 | (append environment %load-compiled-path channels-go)) |
| 156 | |
| 157 | (make-parameter |
| 158 | (append environment |
| 159 | %default-package-module-path |
| 160 | channels-scm)))) |
| 161 | |
| 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 | |
| 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 |
| 178 | associated values of the form |
| 179 | |
| 180 | (newest-version newest-package ...) |
| 181 | |
| 182 | where 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 | |
| 195 | (define (fold-available-packages proc init) |
| 196 | "Fold PROC over the list of available packages. For each available package, |
| 197 | PROC is called along these lines: |
| 198 | |
| 199 | (PROC NAME VERSION RESULT |
| 200 | #:outputs OUTPUTS |
| 201 | #:location LOCATION |
| 202 | …) |
| 203 | |
| 204 | PROC can use #:allow-other-keys to ignore the bits it's not interested in. |
| 205 | When a package cache is available, this procedure does not actually load any |
| 206 | package 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? |
| 231 | (->bool (supported-package? package)) |
| 232 | #:deprecated? |
| 233 | (->bool |
| 234 | (package-superseded package)))) |
| 235 | init))) |
| 236 | |
| 237 | (define* (fold-packages proc init |
| 238 | #:optional |
| 239 | (modules (all-modules (%package-module-path) |
| 240 | #:warn |
| 241 | warn-about-load-error)) |
| 242 | #:key (select? (negate hidden-package?))) |
| 243 | "Call (PROC PACKAGE RESULT) for each available package defined in one of |
| 244 | MODULES that matches SELECT?, using INIT as the initial value of RESULT. It |
| 245 | is guaranteed to never traverse the same package twice." |
| 246 | (fold-module-public-variables (lambda (object result) |
| 247 | (if (and (package? object) (select? object)) |
| 248 | (proc object result) |
| 249 | result)) |
| 250 | init |
| 251 | modules)) |
| 252 | |
| 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 |
| 260 | package 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 |
| 282 | (let ((packages (delay |
| 283 | (fold-packages (lambda (p r) |
| 284 | (vhash-cons (package-name p) p r)) |
| 285 | vlist-null))) |
| 286 | (version>? (lambda (p1 p2) |
| 287 | (version>? (package-version p1) (package-version p2))))) |
| 288 | (lambda* (name #:optional version) |
| 289 | "Return the list of packages with the given NAME. If VERSION is not #f, |
| 290 | then only return packages whose version is prefixed by VERSION, sorted in |
| 291 | decreasing version order." |
| 292 | (let ((matching (sort (vhash-fold* cons '() name (force packages)) |
| 293 | version>?))) |
| 294 | (if version |
| 295 | (filter (lambda (package) |
| 296 | (version-prefix? version (package-version package))) |
| 297 | matching) |
| 298 | matching))))) |
| 299 | |
| 300 | (define (cache-lookup cache name) |
| 301 | "Lookup package NAME in CACHE. Return a list sorted in increasing version |
| 302 | order." |
| 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, |
| 311 | then only return packages whose version is prefixed by VERSION, sorted in |
| 312 | decreasing 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 | |
| 331 | (define* (find-package-locations name #:optional version) |
| 332 | "Return a list of version/location pairs corresponding to each package |
| 333 | matching 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)))) |
| 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 |
| 358 | version numbers; otherwise, return the list of packages named NAME and at |
| 359 | VERSION." |
| 360 | (if version |
| 361 | (find-packages-by-name name version) |
| 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)))))) |
| 371 | |
| 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 | |
| 375 | (define (generate-package-cache directory) |
| 376 | "Generate under DIRECTORY a cache of all the available packages. |
| 377 | |
| 378 | The primary purpose of the cache is to speed up package lookup by name such |
| 379 | that we don't have to traverse and load all the package modules, thereby also |
| 380 | reducing the memory footprint." |
| 381 | (define cache-file |
| 382 | (string-append directory %package-cache-file)) |
| 383 | |
| 384 | (define (expand-cache module symbol variable result+seen) |
| 385 | (match (false-if-exception (variable-ref variable)) |
| 386 | ((? package? package) |
| 387 | (match result+seen |
| 388 | ((result . seen) |
| 389 | (if (or (vhash-assq package seen) |
| 390 | (hidden-package? package)) |
| 391 | (cons result seen) |
| 392 | (cons (cons `#(,(package-name package) |
| 393 | ,(package-version package) |
| 394 | ,(module-name module) |
| 395 | ,symbol |
| 396 | ,(package-outputs package) |
| 397 | ,(->bool (supported-package? package)) |
| 398 | ,(->bool (package-superseded package)) |
| 399 | ,@(let ((loc (package-location package))) |
| 400 | (if loc |
| 401 | `(,(location-file loc) |
| 402 | ,(location-line loc) |
| 403 | ,(location-column loc)) |
| 404 | '(#f #f #f)))) |
| 405 | result) |
| 406 | (vhash-consq package #t seen)))))) |
| 407 | (_ |
| 408 | result+seen))) |
| 409 | |
| 410 | (define exp |
| 411 | (first |
| 412 | (fold-module-public-variables* expand-cache |
| 413 | (cons '() vlist-null) |
| 414 | (all-modules (%package-module-path) |
| 415 | #:warn |
| 416 | warn-about-load-error)))) |
| 417 | |
| 418 | (mkdir-p (dirname cache-file)) |
| 419 | (call-with-output-file cache-file |
| 420 | (lambda (port) |
| 421 | ;; Store the cache as a '.go' file. This makes loading fast and reduces |
| 422 | ;; heap usage since some of the static data is directly mmapped. |
| 423 | (put-bytevector port |
| 424 | (compile `'(,@exp) |
| 425 | #:to 'bytecode |
| 426 | #:opts '(#:to-file? #t))))) |
| 427 | cache-file) |
| 428 | |
| 429 | \f |
| 430 | (define %sigint-prompt |
| 431 | ;; The prompt to jump to upon SIGINT. |
| 432 | (make-prompt-tag "interruptible")) |
| 433 | |
| 434 | (define (call-with-sigint-handler thunk handler) |
| 435 | "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal |
| 436 | number in the context of the continuation of the call to this function, and |
| 437 | return its return value." |
| 438 | (call-with-prompt %sigint-prompt |
| 439 | (lambda () |
| 440 | (sigaction SIGINT |
| 441 | (lambda (signum) |
| 442 | (sigaction SIGINT SIG_DFL) |
| 443 | (abort-to-prompt %sigint-prompt signum))) |
| 444 | (dynamic-wind |
| 445 | (const #t) |
| 446 | thunk |
| 447 | (cut sigaction SIGINT SIG_DFL))) |
| 448 | (lambda (k signum) |
| 449 | (handler signum)))) |
| 450 | |
| 451 | \f |
| 452 | ;;; |
| 453 | ;;; Package specification. |
| 454 | ;;; |
| 455 | |
| 456 | (define* (%find-package spec name version) |
| 457 | (match (find-best-packages-by-name name version) |
| 458 | ((pkg . pkg*) |
| 459 | (unless (null? pkg*) |
| 460 | (warning (G_ "ambiguous package specification `~a'~%") spec) |
| 461 | (warning (G_ "choosing ~a@~a from ~a~%") |
| 462 | (package-name pkg) (package-version pkg) |
| 463 | (location->string (package-location pkg)))) |
| 464 | (match (package-superseded pkg) |
| 465 | ((? package? new) |
| 466 | (info (G_ "package '~a' has been superseded by '~a'~%") |
| 467 | (package-name pkg) (package-name new)) |
| 468 | new) |
| 469 | (#f |
| 470 | pkg))) |
| 471 | (x |
| 472 | (if version |
| 473 | (leave (G_ "~A: package not found for version ~a~%") name version) |
| 474 | (leave (G_ "~A: unknown package~%") name))))) |
| 475 | |
| 476 | (define (specification->package spec) |
| 477 | "Return a package matching SPEC. SPEC may be a package name, or a package |
| 478 | name followed by an at-sign and a version number. If the version number is not |
| 479 | present, return the preferred newest version." |
| 480 | (let-values (((name version) (package-name->name+version spec))) |
| 481 | (%find-package spec name version))) |
| 482 | |
| 483 | (define (specification->location spec) |
| 484 | "Return the location of the highest-numbered package matching SPEC, a |
| 485 | specification such as \"guile@2\" or \"emacs\"." |
| 486 | (let-values (((name version) (package-name->name+version spec))) |
| 487 | (match (find-package-locations name version) |
| 488 | (() |
| 489 | (if version |
| 490 | (leave (G_ "~A: package not found for version ~a~%") name version) |
| 491 | (leave (G_ "~A: unknown package~%") name))) |
| 492 | (lst |
| 493 | (let* ((highest (match lst (((version . _) _ ...) version))) |
| 494 | (locations (take-while (match-lambda |
| 495 | ((version . location) |
| 496 | (string=? version highest))) |
| 497 | lst))) |
| 498 | (match locations |
| 499 | (((version . location) . rest) |
| 500 | (unless (null? rest) |
| 501 | (warning (G_ "ambiguous package specification `~a'~%") spec) |
| 502 | (warning (G_ "choosing ~a@~a from ~a~%") |
| 503 | name version |
| 504 | (location->string location))) |
| 505 | location))))))) |
| 506 | |
| 507 | (define* (specification->package+output spec #:optional (output "out")) |
| 508 | "Return the package and output specified by SPEC, or #f and #f; SPEC may |
| 509 | optionally contain a version number and an output name, as in these examples: |
| 510 | |
| 511 | guile |
| 512 | guile@2.0.9 |
| 513 | guile:debug |
| 514 | guile@2.0.9:debug |
| 515 | |
| 516 | If SPEC does not specify a version number, return the preferred newest |
| 517 | version; if SPEC does not specify an output, return OUTPUT. |
| 518 | |
| 519 | When OUTPUT is false and SPEC does not specify any output, return #f as the |
| 520 | output." |
| 521 | (let-values (((name version sub-drv) |
| 522 | (package-specification->name+version+output spec output))) |
| 523 | (match (%find-package spec name version) |
| 524 | (#f |
| 525 | (values #f #f)) |
| 526 | (package |
| 527 | (if (or (and (not output) (not sub-drv)) |
| 528 | (member sub-drv (package-outputs package))) |
| 529 | (values package sub-drv) |
| 530 | (leave (G_ "package `~a' lacks output `~a'~%") |
| 531 | (package-full-name package) |
| 532 | sub-drv)))))) |
| 533 | |
| 534 | (define (specifications->manifest specs) |
| 535 | "Given SPECS, a list of specifications such as \"emacs@25.2\" or |
| 536 | \"guile:debug\", return a profile manifest." |
| 537 | ;; This procedure exists mostly so users of 'guix package -m' don't have to |
| 538 | ;; fiddle with multiple-value returns. |
| 539 | (packages->manifest |
| 540 | (map (compose list specification->package+output) specs))) |