packages.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 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 discovery)
  27. #:use-module (guix memoization)
  28. #:use-module ((guix build utils)
  29. #:select ((package-name->name+version
  30. . hyphen-separated-name->name+version)
  31. mkdir-p))
  32. #:autoload (guix profiles) (packages->manifest)
  33. #:use-module (guix describe)
  34. #:use-module (guix deprecation)
  35. #:use-module (ice-9 vlist)
  36. #:use-module (ice-9 match)
  37. #:autoload (ice-9 binary-ports) (put-bytevector)
  38. #:autoload (system base compile) (compile)
  39. #:use-module (srfi srfi-1)
  40. #:use-module (srfi srfi-11)
  41. #:use-module (srfi srfi-26)
  42. #:use-module (srfi srfi-34)
  43. #:use-module (srfi srfi-35)
  44. #:use-module (srfi srfi-39)
  45. #:export (search-patch
  46. search-patches
  47. search-auxiliary-file
  48. %patch-path
  49. %auxiliary-files-path
  50. %package-module-path
  51. %default-package-module-path
  52. fold-packages
  53. fold-available-packages
  54. find-newest-available-packages
  55. find-packages-by-name
  56. find-package-locations
  57. find-best-packages-by-name
  58. specification->package
  59. specification->package+output
  60. specification->location
  61. specifications->manifest
  62. generate-package-cache))
  63. ;;; Commentary:
  64. ;;;
  65. ;;; General utilities for the software distribution---i.e., the modules under
  66. ;;; (gnu packages ...).
  67. ;;;
  68. ;;; Code:
  69. ;; By default, we store patches and auxiliary files
  70. ;; alongside Guile modules. This is so that these extra files can be
  71. ;; found without requiring a special setup, such as a specific
  72. ;; installation directory and an extra environment variable. One
  73. ;; advantage of this setup is that everything just works in an
  74. ;; auto-compilation setting.
  75. (define %auxiliary-files-path
  76. (make-parameter
  77. (map (cut string-append <> "/gnu/packages/aux-files")
  78. %load-path)))
  79. (define (search-auxiliary-file file-name)
  80. "Search the auxiliary FILE-NAME. Return #f if not found."
  81. (search-path (%auxiliary-files-path) file-name))
  82. (define (search-patch file-name)
  83. "Search the patch FILE-NAME. Raise an error if not found."
  84. (or (search-path (%patch-path) file-name)
  85. (raise (condition
  86. (&message (message (format #f (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. (define (generate-package-cache directory)
  337. "Generate under DIRECTORY a cache of all the available packages.
  338. The primary purpose of the cache is to speed up package lookup by name such
  339. that we don't have to traverse and load all the package modules, thereby also
  340. reducing the memory footprint."
  341. (define cache-file
  342. (string-append directory %package-cache-file))
  343. (define (expand-cache module symbol variable result+seen)
  344. (match (false-if-exception (variable-ref variable))
  345. ((? package? package)
  346. (match result+seen
  347. ((result . seen)
  348. (if (or (vhash-assq package seen)
  349. (hidden-package? package))
  350. (cons result seen)
  351. (cons (cons `#(,(package-name package)
  352. ,(package-version package)
  353. ,(module-name module)
  354. ,symbol
  355. ,(package-outputs package)
  356. ,(->bool (supported-package? package))
  357. ,(->bool (package-superseded package))
  358. ,@(let ((loc (package-location package)))
  359. (if loc
  360. `(,(location-file loc)
  361. ,(location-line loc)
  362. ,(location-column loc))
  363. '(#f #f #f))))
  364. result)
  365. (vhash-consq package #t seen))))))
  366. (_
  367. result+seen)))
  368. (define exp
  369. (first
  370. (fold-module-public-variables* expand-cache
  371. (cons '() vlist-null)
  372. (all-modules (%package-module-path)
  373. #:warn
  374. warn-about-load-error))))
  375. (mkdir-p (dirname cache-file))
  376. (call-with-output-file cache-file
  377. (lambda (port)
  378. ;; Store the cache as a '.go' file. This makes loading fast and reduces
  379. ;; heap usage since some of the static data is directly mmapped.
  380. (put-bytevector port
  381. (compile `'(,@exp)
  382. #:to 'bytecode
  383. #:opts '(#:to-file? #t)))))
  384. cache-file)
  385. (define %sigint-prompt
  386. ;; The prompt to jump to upon SIGINT.
  387. (make-prompt-tag "interruptible"))
  388. (define (call-with-sigint-handler thunk handler)
  389. "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
  390. number in the context of the continuation of the call to this function, and
  391. return its return value."
  392. (call-with-prompt %sigint-prompt
  393. (lambda ()
  394. (sigaction SIGINT
  395. (lambda (signum)
  396. (sigaction SIGINT SIG_DFL)
  397. (abort-to-prompt %sigint-prompt signum)))
  398. (dynamic-wind
  399. (const #t)
  400. thunk
  401. (cut sigaction SIGINT SIG_DFL)))
  402. (lambda (k signum)
  403. (handler signum))))
  404. ;;;
  405. ;;; Package specification.
  406. ;;;
  407. (define* (%find-package spec name version)
  408. (match (find-best-packages-by-name name version)
  409. ((pkg . pkg*)
  410. (unless (null? pkg*)
  411. (warning (G_ "ambiguous package specification `~a'~%") spec)
  412. (warning (G_ "choosing ~a@~a from ~a~%")
  413. (package-name pkg) (package-version pkg)
  414. (location->string (package-location pkg))))
  415. (match (package-superseded pkg)
  416. ((? package? new)
  417. (info (G_ "package '~a' has been superseded by '~a'~%")
  418. (package-name pkg) (package-name new))
  419. new)
  420. (#f
  421. pkg)))
  422. (x
  423. (if version
  424. (leave (G_ "~A: package not found for version ~a~%") name version)
  425. (leave (G_ "~A: unknown package~%") name)))))
  426. (define (specification->package spec)
  427. "Return a package matching SPEC. SPEC may be a package name, or a package
  428. name followed by an at-sign and a version number. If the version number is not
  429. present, return the preferred newest version."
  430. (let-values (((name version) (package-name->name+version spec)))
  431. (%find-package spec name version)))
  432. (define (specification->location spec)
  433. "Return the location of the highest-numbered package matching SPEC, a
  434. specification such as \"guile@2\" or \"emacs\"."
  435. (let-values (((name version) (package-name->name+version spec)))
  436. (match (find-package-locations name version)
  437. (()
  438. (if version
  439. (leave (G_ "~A: package not found for version ~a~%") name version)
  440. (leave (G_ "~A: unknown package~%") name)))
  441. (lst
  442. (let* ((highest (match lst (((version . _) _ ...) version)))
  443. (locations (take-while (match-lambda
  444. ((version . location)
  445. (string=? version highest)))
  446. lst)))
  447. (match locations
  448. (((version . location) . rest)
  449. (unless (null? rest)
  450. (warning (G_ "ambiguous package specification `~a'~%") spec)
  451. (warning (G_ "choosing ~a@~a from ~a~%")
  452. name version
  453. (location->string location)))
  454. location)))))))
  455. (define* (specification->package+output spec #:optional (output "out"))
  456. "Return the package and output specified by SPEC, or #f and #f; SPEC may
  457. optionally contain a version number and an output name, as in these examples:
  458. guile
  459. guile@2.0.9
  460. guile:debug
  461. guile@2.0.9:debug
  462. If SPEC does not specify a version number, return the preferred newest
  463. version; if SPEC does not specify an output, return OUTPUT.
  464. When OUTPUT is false and SPEC does not specify any output, return #f as the
  465. output."
  466. (let-values (((name version sub-drv)
  467. (package-specification->name+version+output spec output)))
  468. (match (%find-package spec name version)
  469. (#f
  470. (values #f #f))
  471. (package
  472. (if (or (and (not output) (not sub-drv))
  473. (member sub-drv (package-outputs package)))
  474. (values package sub-drv)
  475. (leave (G_ "package `~a' lacks output `~a'~%")
  476. (package-full-name package)
  477. sub-drv))))))
  478. (define (specifications->manifest specs)
  479. "Given SPECS, a list of specifications such as \"emacs@25.2\" or
  480. \"guile:debug\", return a profile manifest."
  481. ;; This procedure exists mostly so users of 'guix package -m' don't have to
  482. ;; fiddle with multiple-value returns.
  483. (packages->manifest
  484. (map (compose list specification->package+output) specs)))