packages.scm 22 KB

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