packages.scm 24 KB

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