packages: Remove 'find-newest-available-packages'.
[jackhill/guix/guix.git] / gnu / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
e2a903c8 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
c2868b1e 3;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
7d193ec3 4;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
96eaa55f 5;;; Copyright © 2016, 2017 Alex Kost <alezost@gmail.com>
fad155d4 6;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
6b1891b0 7;;;
233e7676 8;;; This file is part of GNU Guix.
6b1891b0 9;;;
233e7676 10;;; GNU Guix is free software; you can redistribute it and/or modify it
6b1891b0
LC
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;;;
233e7676 15;;; GNU Guix is distributed in the hope that it will be useful, but
6b1891b0
LC
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
233e7676 21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
6b1891b0 22
59a43334 23(define-module (gnu packages)
6b1891b0 24 #:use-module (guix packages)
98eb8cbe 25 #:use-module (guix ui)
800cdeef 26 #:use-module (guix utils)
cd903ef7 27 #:use-module (guix discovery)
f9704f17 28 #:use-module (guix memoization)
95cd4971
LC
29 #:use-module ((guix build utils)
30 #:select ((package-name->name+version
31 . hyphen-separated-name->name+version)))
c08ea55e 32 #:autoload (guix profiles) (packages->manifest)
fe634eaf 33 #:use-module (guix describe)
c2868b1e 34 #:use-module (ice-9 vlist)
dc5669cd 35 #:use-module (ice-9 match)
6b1891b0 36 #:use-module (srfi srfi-1)
5e3b388b 37 #:use-module (srfi srfi-11)
6b1891b0 38 #:use-module (srfi srfi-26)
dbab5150
LC
39 #:use-module (srfi srfi-34)
40 #:use-module (srfi srfi-35)
800cdeef
LC
41 #:use-module (srfi srfi-39)
42 #:export (search-patch
25897079 43 search-patches
96eaa55f 44 search-auxiliary-file
ac5aa288 45 search-bootstrap-binary
0492f4a2 46 %patch-path
96eaa55f 47 %auxiliary-files-path
0b3651bc 48 %bootstrap-binaries-path
c107b541 49 %package-module-path
fe634eaf 50 %default-package-module-path
7d193ec3 51
ba326ce4 52 fold-packages
7d193ec3 53
dc5669cd 54 find-packages-by-name
3f26bfc1 55 find-best-packages-by-name
7d193ec3 56
84189ebc 57 specification->package
c08ea55e
LC
58 specification->package+output
59 specifications->manifest))
6b1891b0
LC
60
61;;; Commentary:
62;;;
63;;; General utilities for the software distribution---i.e., the modules under
59a43334 64;;; (gnu packages ...).
6b1891b0
LC
65;;;
66;;; Code:
67
96eaa55f
AK
68;; By default, we store patches, auxiliary files and bootstrap binaries
69;; alongside Guile modules. This is so that these extra files can be
70;; found without requiring a special setup, such as a specific
71;; installation directory and an extra environment variable. One
72;; advantage of this setup is that everything just works in an
73;; auto-compilation setting.
a9f60c42 74
a9f60c42 75(define %bootstrap-binaries-path
ac5aa288 76 (make-parameter
1ffa7090 77 (map (cut string-append <> "/gnu/packages/bootstrap")
0b3651bc 78 %load-path)))
ac5aa288 79
96eaa55f
AK
80(define %auxiliary-files-path
81 (make-parameter
82 (map (cut string-append <> "/gnu/packages/aux-files")
83 %load-path)))
84
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
800cdeef 89(define (search-patch file-name)
dbab5150
LC
90 "Search the patch FILE-NAME. Raise an error if not found."
91 (or (search-path (%patch-path) file-name)
92 (raise (condition
69daee23 93 (&message (message (format #f (G_ "~a: patch not found")
dbab5150 94 file-name)))))))
800cdeef 95
25897079
AK
96(define-syntax-rule (search-patches file-name ...)
97 "Return the list of absolute file names corresponding to each
98FILE-NAME found in %PATCH-PATH."
99 (list (search-patch file-name) ...))
100
ac5aa288 101(define (search-bootstrap-binary file-name system)
dfba5489
LC
102 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
103found."
104 (or (search-path (%bootstrap-binaries-path)
105 (string-append system "/" file-name))
106 (raise (condition
107 (&message
108 (message
69daee23 109 (format #f (G_ "could not find bootstrap binary '~a' \
dfba5489
LC
110for system '~a'")
111 file-name system)))))))
ac5aa288 112
84836a57 113(define %distro-root-directory
eaae07ec
LC
114 ;; Absolute file name of the module hierarchy. Since (gnu packages …) might
115 ;; live in a directory different from (guix), try to get the best match.
116 (letrec-syntax ((dirname* (syntax-rules ()
117 ((_ file)
118 (dirname file))
119 ((_ file head tail ...)
120 (dirname (dirname* file tail ...)))))
121 (try (syntax-rules ()
122 ((_ (file things ...) rest ...)
123 (match (search-path %load-path file)
124 (#f
125 (try rest ...))
126 (absolute
127 (dirname* absolute things ...))))
128 ((_)
129 #f))))
130 (try ("gnu/packages/base.scm" gnu/ packages/)
131 ("gnu/packages.scm" gnu/)
132 ("guix.scm"))))
6b1891b0 133
fe634eaf
LC
134(define %default-package-module-path
135 ;; Default search path for package modules.
136 `((,%distro-root-directory . "gnu/packages")))
137
c107b541
LC
138(define %package-module-path
139 ;; Search path for package modules. Each item must be either a directory
140 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
141 ;; to narrow the search.
8689901f
LC
142 (let* ((not-colon (char-set-complement (char-set #\:)))
143 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
fe634eaf
LC
144 not-colon))
145 (channels (package-path-entries)))
146 ;; Automatically add channels and items from $GUIX_PACKAGE_PATH to Guile's
147 ;; search path. For historical reasons, $GUIX_PACKAGE_PATH goes to the
148 ;; front; channels go to the back so that they don't override Guix' own
149 ;; modules.
150 (set! %load-path
151 (append environment %load-path channels))
152 (set! %load-compiled-path
153 (append environment %load-compiled-path channels))
8689901f
LC
154
155 (make-parameter
fe634eaf
LC
156 (append environment
157 %default-package-module-path
158 channels))))
c107b541 159
ee06af5b
LC
160(define %patch-path
161 ;; Define it after '%package-module-path' so that '%load-path' contains user
162 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
163 (make-parameter
164 (map (lambda (directory)
165 (if (string=? directory %distro-root-directory)
166 (string-append directory "/gnu/packages/patches")
167 directory))
168 %load-path)))
169
5c5ae46c
LC
170(define* (fold-packages proc init
171 #:optional
3c0128b0
LC
172 (modules (all-modules (%package-module-path)
173 #:warn
174 warn-about-load-error))
96dc8f35 175 #:key (select? (negate hidden-package?)))
5c5ae46c 176 "Call (PROC PACKAGE RESULT) for each available package defined in one of
96dc8f35
LC
177MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
178is guaranteed to never traverse the same package twice."
cd903ef7 179 (fold-module-public-variables (lambda (object result)
96dc8f35 180 (if (and (package? object) (select? object))
cd903ef7
LC
181 (proc object result)
182 result))
183 init
5c5ae46c 184 modules))
ba326ce4 185
9ffc1c00
LC
186(define find-packages-by-name
187 (let ((packages (delay
188 (fold-packages (lambda (p r)
189 (vhash-cons (package-name p) p r))
724311a2
LC
190 vlist-null)))
191 (version>? (lambda (p1 p2)
192 (version>? (package-version p1) (package-version p2)))))
9ffc1c00
LC
193 (lambda* (name #:optional version)
194 "Return the list of packages with the given NAME. If VERSION is not #f,
724311a2
LC
195then only return packages whose version is prefixed by VERSION, sorted in
196decreasing version order."
197 (let ((matching (sort (vhash-fold* cons '() name (force packages))
198 version>?)))
9ffc1c00
LC
199 (if version
200 (filter (lambda (package)
348987d3 201 (version-prefix? version (package-version package)))
9ffc1c00
LC
202 matching)
203 matching)))))
dc5669cd 204
3f26bfc1
LC
205(define (find-best-packages-by-name name version)
206 "If version is #f, return the list of packages named NAME with the highest
207version numbers; otherwise, return the list of packages named NAME and at
208VERSION."
209 (if version
210 (find-packages-by-name name version)
e2a903c8
LC
211 (match (find-packages-by-name name)
212 (()
213 '())
214 ((matches ...)
215 ;; Return the subset of MATCHES with the higher version number.
216 (let ((highest (package-version (first matches))))
217 (take-while (lambda (p)
218 (string=? (package-version p) highest))
219 matches))))))
7d193ec3
EB
220
221\f
4ea44419
AK
222(define %sigint-prompt
223 ;; The prompt to jump to upon SIGINT.
224 (make-prompt-tag "interruptible"))
225
226(define (call-with-sigint-handler thunk handler)
227 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
228number in the context of the continuation of the call to this function, and
229return its return value."
230 (call-with-prompt %sigint-prompt
231 (lambda ()
232 (sigaction SIGINT
233 (lambda (signum)
234 (sigaction SIGINT SIG_DFL)
235 (abort-to-prompt %sigint-prompt signum)))
236 (dynamic-wind
237 (const #t)
238 thunk
239 (cut sigaction SIGINT SIG_DFL)))
240 (lambda (k signum)
241 (handler signum))))
242
fad155d4
ML
243\f
244;;;
245;;; Package specification.
246;;;
247
e30c2be1 248(define* (%find-package spec name version)
fad155d4
ML
249 (match (find-best-packages-by-name name version)
250 ((pkg . pkg*)
251 (unless (null? pkg*)
69daee23
LC
252 (warning (G_ "ambiguous package specification `~a'~%") spec)
253 (warning (G_ "choosing ~a@~a from ~a~%")
d75e8f36 254 (package-name pkg) (package-version pkg)
fad155d4 255 (location->string (package-location pkg))))
01afdab8
LC
256 (match (package-superseded pkg)
257 ((? package? new)
69daee23 258 (info (G_ "package '~a' has been superseded by '~a'~%")
01afdab8
LC
259 (package-name pkg) (package-name new))
260 new)
261 (#f
262 pkg)))
e465d9e1 263 (x
fad155d4 264 (if version
69daee23
LC
265 (leave (G_ "~A: package not found for version ~a~%") name version)
266 (leave (G_ "~A: unknown package~%") name)))))
fad155d4 267
5e3b388b
CR
268(define (specification->package spec)
269 "Return a package matching SPEC. SPEC may be a package name, or a package
1b846da8 270name followed by an at-sign and a version number. If the version number is not
5e3b388b 271present, return the preferred newest version."
fad155d4
ML
272 (let-values (((name version) (package-name->name+version spec)))
273 (%find-package spec name version)))
84189ebc
LC
274
275(define* (specification->package+output spec #:optional (output "out"))
276 "Return the package and output specified by SPEC, or #f and #f; SPEC may
277optionally contain a version number and an output name, as in these examples:
278
279 guile
1b846da8 280 guile@2.0.9
84189ebc 281 guile:debug
1b846da8 282 guile@2.0.9:debug
84189ebc
LC
283
284If SPEC does not specify a version number, return the preferred newest
285version; if SPEC does not specify an output, return OUTPUT."
84189ebc
LC
286 (let-values (((name version sub-drv)
287 (package-specification->name+version+output spec output)))
fad155d4
ML
288 (match (%find-package spec name version)
289 (#f
290 (values #f #f))
291 (package
292 (if (member sub-drv (package-outputs package))
293 (values package sub-drv)
69daee23 294 (leave (G_ "package `~a' lacks output `~a'~%")
fad155d4
ML
295 (package-full-name package)
296 sub-drv))))))
c08ea55e
LC
297
298(define (specifications->manifest specs)
299 "Given SPECS, a list of specifications such as \"emacs@25.2\" or
300\"guile:debug\", return a profile manifest."
301 ;; This procedure exists mostly so users of 'guix package -m' don't have to
302 ;; fiddle with multiple-value returns.
303 (packages->manifest
304 (map (compose list specification->package+output) specs)))