packages.scm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. ;;;
  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. (define-module (gnu packages)
  23. #:use-module (guix packages)
  24. #:use-module (guix ui)
  25. #:use-module (guix utils)
  26. #:use-module (guix diagnostics)
  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. cache-is-authoritative?
  54. fold-packages
  55. fold-available-packages
  56. find-newest-available-packages
  57. find-packages-by-name
  58. find-package-locations
  59. find-best-packages-by-name
  60. specification->package
  61. specification->package+output
  62. specification->location
  63. specifications->manifest
  64. package-unique-version-prefix
  65. generate-package-cache))
  66. ;;; Commentary:
  67. ;;;
  68. ;;; General utilities for the software distribution---i.e., the modules under
  69. ;;; (gnu packages ...).
  70. ;;;
  71. ;;; Code:
  72. ;; By default, we store patches and auxiliary files
  73. ;; alongside Guile modules. This is so that these extra files can be
  74. ;; found without requiring a special setup, such as a specific
  75. ;; installation directory and an extra environment variable. One
  76. ;; advantage of this setup is that everything just works in an
  77. ;; auto-compilation setting.
  78. (define %auxiliary-files-path
  79. (make-parameter
  80. (map (cut string-append <> "/gnu/packages/aux-files")
  81. %load-path)))
  82. (define (search-auxiliary-file file-name)
  83. "Search the auxiliary FILE-NAME. Return #f if not found."
  84. (search-path (%auxiliary-files-path) file-name))
  85. (define (search-patch file-name)
  86. "Search the patch FILE-NAME. Raise an error if not found."
  87. (or (search-path (%patch-path) file-name)
  88. (raise (formatted-message (G_ "~a: patch not found")
  89. file-name))))
  90. (define-syntax-rule (search-patches file-name ...)
  91. "Return the list of absolute file names corresponding to each
  92. FILE-NAME found in %PATCH-PATH."
  93. (list (search-patch file-name) ...))
  94. (define %distro-root-directory
  95. ;; Absolute file name of the module hierarchy. Since (gnu packages …) might
  96. ;; live in a directory different from (guix), try to get the best match.
  97. (letrec-syntax ((dirname* (syntax-rules ()
  98. ((_ file)
  99. (dirname file))
  100. ((_ file head tail ...)
  101. (dirname (dirname* file tail ...)))))
  102. (try (syntax-rules ()
  103. ((_ (file things ...) rest ...)
  104. (match (search-path %load-path file)
  105. (#f
  106. (try rest ...))
  107. (absolute
  108. (dirname* absolute things ...))))
  109. ((_)
  110. #f))))
  111. (try ("gnu/packages/base.scm" gnu/ packages/)
  112. ("gnu/packages.scm" gnu/)
  113. ("guix.scm"))))
  114. (define %default-package-module-path
  115. ;; Default search path for package modules.
  116. `((,%distro-root-directory . "gnu/packages")))
  117. (define (cache-is-authoritative?)
  118. "Return true if the pre-computed package cache is authoritative. It is not
  119. authoritative when entries have been added via GUIX_PACKAGE_PATH or '-L'
  120. flags."
  121. (equal? (%package-module-path)
  122. (append %default-package-module-path
  123. (package-path-entries))))
  124. (define %package-module-path
  125. ;; Search path for package modules. Each item must be either a directory
  126. ;; name or a pair whose car is a directory and whose cdr is a sub-directory
  127. ;; to narrow the search.
  128. (let*-values (((not-colon)
  129. (char-set-complement (char-set #\:)))
  130. ((environment)
  131. (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
  132. not-colon))
  133. ((channels-scm channels-go)
  134. (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. (put-bytevector port
  402. (compile `'(,@exp)
  403. #:to 'bytecode
  404. #:opts '(#:to-file? #t)))))
  405. cache-file)
  406. (define %sigint-prompt
  407. ;; The prompt to jump to upon SIGINT.
  408. (make-prompt-tag "interruptible"))
  409. (define (call-with-sigint-handler thunk handler)
  410. "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
  411. number in the context of the continuation of the call to this function, and
  412. return its return value."
  413. (call-with-prompt %sigint-prompt
  414. (lambda ()
  415. (sigaction SIGINT
  416. (lambda (signum)
  417. (sigaction SIGINT SIG_DFL)
  418. (abort-to-prompt %sigint-prompt signum)))
  419. (dynamic-wind
  420. (const #t)
  421. thunk
  422. (cut sigaction SIGINT SIG_DFL)))
  423. (lambda (k signum)
  424. (handler signum))))
  425. ;;;
  426. ;;; Package specification.
  427. ;;;
  428. (define* (%find-package spec name version)
  429. (match (find-best-packages-by-name name version)
  430. ((pkg . pkg*)
  431. (unless (null? pkg*)
  432. (warning (G_ "ambiguous package specification `~a'~%") spec)
  433. (warning (G_ "choosing ~a@~a from ~a~%")
  434. (package-name pkg) (package-version pkg)
  435. (location->string (package-location pkg))))
  436. (match (package-superseded pkg)
  437. ((? package? new)
  438. (info (G_ "package '~a' has been superseded by '~a'~%")
  439. (package-name pkg) (package-name new))
  440. new)
  441. (#f
  442. pkg)))
  443. (x
  444. (if version
  445. (leave (G_ "~A: package not found for version ~a~%") name version)
  446. (leave (G_ "~A: unknown package~%") name)))))
  447. (define (specification->package spec)
  448. "Return a package matching SPEC. SPEC may be a package name, or a package
  449. name followed by an at-sign and a version number. If the version number is not
  450. present, return the preferred newest version."
  451. (let-values (((name version) (package-name->name+version spec)))
  452. (%find-package spec name version)))
  453. (define (specification->location spec)
  454. "Return the location of the highest-numbered package matching SPEC, a
  455. specification such as \"guile@2\" or \"emacs\"."
  456. (let-values (((name version) (package-name->name+version spec)))
  457. (match (find-package-locations name version)
  458. (()
  459. (if version
  460. (leave (G_ "~A: package not found for version ~a~%") name version)
  461. (leave (G_ "~A: unknown package~%") name)))
  462. (lst
  463. (let* ((highest (match lst (((version . _) _ ...) version)))
  464. (locations (take-while (match-lambda
  465. ((version . location)
  466. (string=? version highest)))
  467. lst)))
  468. (match locations
  469. (((version . location) . rest)
  470. (unless (null? rest)
  471. (warning (G_ "ambiguous package specification `~a'~%") spec)
  472. (warning (G_ "choosing ~a@~a from ~a~%")
  473. name version
  474. (location->string location)))
  475. location)))))))
  476. (define* (specification->package+output spec #:optional (output "out"))
  477. "Return the package and output specified by SPEC, or #f and #f; SPEC may
  478. optionally contain a version number and an output name, as in these examples:
  479. guile
  480. guile@2.0.9
  481. guile:debug
  482. guile@2.0.9:debug
  483. If SPEC does not specify a version number, return the preferred newest
  484. version; if SPEC does not specify an output, return OUTPUT.
  485. When OUTPUT is false and SPEC does not specify any output, return #f as the
  486. output."
  487. (let-values (((name version sub-drv)
  488. (package-specification->name+version+output spec output)))
  489. (match (%find-package spec name version)
  490. (#f
  491. (values #f #f))
  492. (package
  493. (if (or (and (not output) (not sub-drv))
  494. (member sub-drv (package-outputs package)))
  495. (values package sub-drv)
  496. (leave (G_ "package `~a' lacks output `~a'~%")
  497. (package-full-name package)
  498. sub-drv))))))
  499. (define (specifications->manifest specs)
  500. "Given SPECS, a list of specifications such as \"emacs@25.2\" or
  501. \"guile:debug\", return a profile manifest."
  502. ;; This procedure exists mostly so users of 'guix package -m' don't have to
  503. ;; fiddle with multiple-value returns.
  504. (packages->manifest
  505. (map (compose list specification->package+output) specs)))
  506. (define (package-unique-version-prefix name version)
  507. "Search among all the versions of package NAME that are available, and
  508. return the shortest unambiguous version prefix to designate VERSION. If only
  509. one version of the package is available, return the empty string."
  510. (match (map package-version (find-packages-by-name name))
  511. ((_)
  512. ;; A single version of NAME is available, so do not specify the version
  513. ;; number, even if the available version doesn't match VERSION.
  514. "")
  515. (versions
  516. ;; If VERSION is the latest version, don't specify any version.
  517. ;; Otherwise return the shortest unique version prefix. Note that this
  518. ;; is based on the currently available packages so the result may vary
  519. ;; over time.
  520. (if (every (cut version>? version <>)
  521. (delete version versions))
  522. ""
  523. (version-unique-prefix version versions)))))