packages.scm 23 KB

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