Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / guix / scripts / package.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
4 ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (guix scripts package)
22 #:use-module (guix ui)
23 #:use-module (guix store)
24 #:use-module (guix derivations)
25 #:use-module (guix packages)
26 #:use-module (guix utils)
27 #:use-module (guix config)
28 #:use-module ((guix build utils) #:select (directory-exists? mkdir-p))
29 #:use-module (ice-9 ftw)
30 #:use-module (ice-9 format)
31 #:use-module (ice-9 match)
32 #:use-module (ice-9 regex)
33 #:use-module (ice-9 vlist)
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-11)
36 #:use-module (srfi srfi-26)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-37)
39 #:use-module (gnu packages)
40 #:use-module ((gnu packages base) #:select (guile-final))
41 #:use-module ((gnu packages bootstrap) #:select (%bootstrap-guile))
42 #:use-module (guix gnu-maintenance)
43 #:export (guix-package))
44
45 (define %store
46 (make-parameter #f))
47
48 \f
49 ;;;
50 ;;; User environment.
51 ;;;
52
53 (define %user-environment-directory
54 (and=> (getenv "HOME")
55 (cut string-append <> "/.guix-profile")))
56
57 (define %profile-directory
58 (string-append (or (getenv "NIX_STATE_DIR") %state-directory) "/profiles/"
59 (or (and=> (getenv "USER")
60 (cut string-append "per-user/" <>))
61 "default")))
62
63 (define %current-profile
64 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
65 ;; coexist with Nix profiles.
66 (string-append %profile-directory "/guix-profile"))
67
68 (define (profile-manifest profile)
69 "Return the PROFILE's manifest."
70 (let ((manifest (string-append profile "/manifest")))
71 (if (file-exists? manifest)
72 (call-with-input-file manifest read)
73 '(manifest (version 1) (packages ())))))
74
75 (define (manifest-packages manifest)
76 "Return the packages listed in MANIFEST."
77 (match manifest
78 (('manifest ('version 0)
79 ('packages ((name version output path) ...)))
80 (zip name version output path
81 (make-list (length name) '())))
82
83 ;; Version 1 adds a list of propagated inputs to the
84 ;; name/version/output/path tuples.
85 (('manifest ('version 1)
86 ('packages (packages ...)))
87 packages)
88
89 (_
90 (error "unsupported manifest format" manifest))))
91
92 (define (profile-regexp profile)
93 "Return a regular expression that matches PROFILE's name and number."
94 (make-regexp (string-append "^" (regexp-quote (basename profile))
95 "-([0-9]+)")))
96
97 (define (profile-numbers profile)
98 "Return the list of generation numbers of PROFILE, or '(0) if no
99 former profiles were found."
100 (define* (scandir name #:optional (select? (const #t))
101 (entry<? (@ (ice-9 i18n) string-locale<?)))
102 ;; XXX: Bug-fix version introduced in Guile v2.0.6-62-g139ce19.
103 (define (enter? dir stat result)
104 (and stat (string=? dir name)))
105
106 (define (visit basename result)
107 (if (select? basename)
108 (cons basename result)
109 result))
110
111 (define (leaf name stat result)
112 (and result
113 (visit (basename name) result)))
114
115 (define (down name stat result)
116 (visit "." '()))
117
118 (define (up name stat result)
119 (visit ".." result))
120
121 (define (skip name stat result)
122 ;; All the sub-directories are skipped.
123 (visit (basename name) result))
124
125 (define (error name* stat errno result)
126 (if (string=? name name*) ; top-level NAME is unreadable
127 result
128 (visit (basename name*) result)))
129
130 (and=> (file-system-fold enter? leaf down up skip error #f name lstat)
131 (lambda (files)
132 (sort files entry<?))))
133
134 (match (scandir (dirname profile)
135 (cute regexp-exec (profile-regexp profile) <>))
136 (#f ; no profile directory
137 '(0))
138 (() ; no profiles
139 '(0))
140 ((profiles ...) ; former profiles around
141 (map (compose string->number
142 (cut match:substring <> 1)
143 (cute regexp-exec (profile-regexp profile) <>))
144 profiles))))
145
146 (define (previous-profile-number profile number)
147 "Return the number of the generation before generation NUMBER of
148 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
149 case when generations have been deleted (there are \"holes\")."
150 (fold (lambda (candidate highest)
151 (if (and (< candidate number) (> candidate highest))
152 candidate
153 highest))
154 0
155 (profile-numbers profile)))
156
157 (define (profile-derivation store packages)
158 "Return a derivation that builds a profile (a user environment) with
159 all of PACKAGES, a list of name/version/output/path/deps tuples."
160 (define builder
161 `(begin
162 (use-modules (ice-9 pretty-print)
163 (guix build union))
164
165 (setvbuf (current-output-port) _IOLBF)
166 (setvbuf (current-error-port) _IOLBF)
167
168 (let ((output (assoc-ref %outputs "out"))
169 (inputs (map cdr %build-inputs)))
170 (format #t "building user environment `~a' with ~a packages...~%"
171 output (length inputs))
172 (union-build output inputs)
173 (call-with-output-file (string-append output "/manifest")
174 (lambda (p)
175 (pretty-print '(manifest (version 1)
176 (packages ,packages))
177 p))))))
178
179 (build-expression->derivation store "user-environment"
180 (%current-system)
181 builder
182 (append-map (match-lambda
183 ((name version output path deps)
184 `((,name ,path)
185 ,@deps)))
186 packages)
187 #:modules '((guix build union))))
188
189 (define (profile-number profile)
190 "Return PROFILE's number or 0. An absolute file name must be used."
191 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
192 (basename (readlink profile))))
193 (compose string->number (cut match:substring <> 1)))
194 0))
195
196 (define (roll-back profile)
197 "Roll back to the previous generation of PROFILE."
198 (let* ((number (profile-number profile))
199 (previous-number (previous-profile-number profile number))
200 (previous-profile (format #f "~a-~a-link"
201 profile previous-number))
202 (manifest (string-append previous-profile "/manifest")))
203
204 (define (switch-link)
205 ;; Atomically switch PROFILE to the previous profile.
206 (format #t (_ "switching from generation ~a to ~a~%")
207 number previous-number)
208 (switch-symlinks profile previous-profile))
209
210 (cond ((not (file-exists? profile)) ; invalid profile
211 (format (current-error-port)
212 (_ "error: profile `~a' does not exist~%")
213 profile))
214 ((zero? number) ; empty profile
215 (format (current-error-port)
216 (_ "nothing to do: already at the empty profile~%")))
217 ((or (zero? previous-number) ; going to emptiness
218 (not (file-exists? previous-profile)))
219 (let*-values (((drv-path drv)
220 (profile-derivation (%store) '()))
221 ((prof)
222 (derivation-output-path
223 (assoc-ref (derivation-outputs drv) "out"))))
224 (when (not (build-derivations (%store) (list drv-path)))
225 (leave (_ "failed to build the empty profile~%")))
226
227 (switch-symlinks previous-profile prof)
228 (switch-link)))
229 (else (switch-link))))) ; anything else
230
231 (define (find-packages-by-description rx)
232 "Search in SYNOPSIS and DESCRIPTION using RX. Return a list of
233 matching packages."
234 (define (same-location? p1 p2)
235 ;; Compare locations of two packages.
236 (equal? (package-location p1) (package-location p2)))
237
238 (delete-duplicates
239 (sort
240 (fold-packages (lambda (package result)
241 (define matches?
242 (cut regexp-exec rx <>))
243
244 (if (or (and=> (package-synopsis package)
245 (compose matches? gettext))
246 (and=> (package-description package)
247 (compose matches? gettext)))
248 (cons package result)
249 result))
250 '())
251 (lambda (p1 p2)
252 (string<? (package-name p1)
253 (package-name p2))))
254 same-location?))
255
256 (define (input->name+path input)
257 "Convert the name/package/sub-drv tuple INPUT to a name/store-path tuple."
258 (let loop ((input input))
259 (match input
260 ((name package)
261 (loop `(,name ,package "out")))
262 ((name package sub-drv)
263 (let*-values (((_ drv)
264 (package-derivation (%store) package))
265 ((out)
266 (derivation-output-path
267 (assoc-ref (derivation-outputs drv) sub-drv))))
268 `(,name ,out))))))
269
270 (define-syntax-rule (waiting exp fmt rest ...)
271 "Display the given message while EXP is being evaluated."
272 (let* ((message (format #f fmt rest ...))
273 (blank (make-string (string-length message) #\space)))
274 (display message (current-error-port))
275 (force-output (current-error-port))
276 (let ((result exp))
277 ;; Clear the line.
278 (display #\cr (current-error-port))
279 (display blank (current-error-port))
280 (display #\cr (current-error-port))
281 (force-output (current-error-port))
282 exp)))
283
284 (define (check-package-freshness package)
285 "Check whether PACKAGE has a newer version available upstream, and report
286 it."
287 ;; TODO: Automatically inject the upstream version when desired.
288
289 (catch #t
290 (lambda ()
291 (when (false-if-exception (gnu-package? package))
292 (let ((name (package-name package))
293 (full-name (package-full-name package)))
294 (match (waiting (latest-release name)
295 (_ "looking for the latest release of GNU ~a...") name)
296 ((latest-version . _)
297 (when (version>? latest-version full-name)
298 (format (current-error-port)
299 (_ "~a: note: using ~a \
300 but ~a is available upstream~%")
301 (location->string (package-location package))
302 full-name latest-version)))
303 (_ #t)))))
304 (lambda (key . args)
305 ;; Silently ignore networking errors rather than preventing
306 ;; installation.
307 (case key
308 ((getaddrinfo-error ftp-error) #f)
309 (else (apply throw key args))))))
310
311 \f
312 ;;;
313 ;;; Command-line options.
314 ;;;
315
316 (define %default-options
317 ;; Alist of default option values.
318 `((profile . ,%current-profile)))
319
320 (define (show-help)
321 (display (_ "Usage: guix package [OPTION]... PACKAGES...
322 Install, remove, or upgrade PACKAGES in a single transaction.\n"))
323 (display (_ "
324 -i, --install=PACKAGE install PACKAGE"))
325 (display (_ "
326 -e, --install-from-expression=EXP
327 install the package EXP evaluates to"))
328 (display (_ "
329 -r, --remove=PACKAGE remove PACKAGE"))
330 (display (_ "
331 -u, --upgrade=REGEXP upgrade all the installed packages matching REGEXP"))
332 (display (_ "
333 --roll-back roll back to the previous generation"))
334 (newline)
335 (display (_ "
336 -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
337 (display (_ "
338 -n, --dry-run show what would be done without actually doing it"))
339 (display (_ "
340 --bootstrap use the bootstrap Guile to build the profile"))
341 (display (_ "
342 --verbose produce verbose output"))
343 (newline)
344 (display (_ "
345 -s, --search=REGEXP search in synopsis and description using REGEXP"))
346 (display (_ "
347 -I, --list-installed[=REGEXP]
348 list installed packages matching REGEXP"))
349 (display (_ "
350 -A, --list-available[=REGEXP]
351 list available packages matching REGEXP"))
352 (newline)
353 (display (_ "
354 -h, --help display this help and exit"))
355 (display (_ "
356 -V, --version display version information and exit"))
357 (newline)
358 (show-bug-report-information))
359
360 (define %options
361 ;; Specification of the command-line options.
362 (list (option '(#\h "help") #f #f
363 (lambda args
364 (show-help)
365 (exit 0)))
366 (option '(#\V "version") #f #f
367 (lambda args
368 (show-version-and-exit "guix package")))
369
370 (option '(#\i "install") #t #f
371 (lambda (opt name arg result)
372 (alist-cons 'install arg result)))
373 (option '(#\e "install-from-expression") #t #f
374 (lambda (opt name arg result)
375 (alist-cons 'install (read/eval-package-expression arg)
376 result)))
377 (option '(#\r "remove") #t #f
378 (lambda (opt name arg result)
379 (alist-cons 'remove arg result)))
380 (option '(#\u "upgrade") #t #f
381 (lambda (opt name arg result)
382 (alist-cons 'upgrade arg result)))
383 (option '("roll-back") #f #f
384 (lambda (opt name arg result)
385 (alist-cons 'roll-back? #t result)))
386 (option '(#\p "profile") #t #f
387 (lambda (opt name arg result)
388 (alist-cons 'profile arg
389 (alist-delete 'profile result))))
390 (option '(#\n "dry-run") #f #f
391 (lambda (opt name arg result)
392 (alist-cons 'dry-run? #t result)))
393 (option '("bootstrap") #f #f
394 (lambda (opt name arg result)
395 (alist-cons 'bootstrap? #t result)))
396 (option '("verbose") #f #f
397 (lambda (opt name arg result)
398 (alist-cons 'verbose? #t result)))
399 (option '(#\s "search") #t #f
400 (lambda (opt name arg result)
401 (cons `(query search ,(or arg ""))
402 result)))
403 (option '(#\I "list-installed") #f #t
404 (lambda (opt name arg result)
405 (cons `(query list-installed ,(or arg ""))
406 result)))
407 (option '(#\A "list-available") #f #t
408 (lambda (opt name arg result)
409 (cons `(query list-available ,(or arg ""))
410 result)))))
411
412 \f
413 ;;;
414 ;;; Entry point.
415 ;;;
416
417 (define (guix-package . args)
418 (define (parse-options)
419 ;; Return the alist of option values.
420 (args-fold args %options
421 (lambda (opt name arg result)
422 (leave (_ "~A: unrecognized option~%") name))
423 (lambda (arg result)
424 (leave (_ "~A: extraneous argument~%") arg))
425 %default-options))
426
427 (define (guile-missing?)
428 ;; Return #t if %GUILE-FOR-BUILD is not available yet.
429 (let ((out (derivation-path->output-path (%guile-for-build))))
430 (not (valid-path? (%store) out))))
431
432 (define newest-available-packages
433 (memoize find-newest-available-packages))
434
435 (define (find-best-packages-by-name name version)
436 (if version
437 (find-packages-by-name name version)
438 (match (vhash-assoc name (newest-available-packages))
439 ((_ version pkgs ...) pkgs)
440 (#f '()))))
441
442 (define (find-package name)
443 ;; Find the package NAME; NAME may contain a version number and a
444 ;; sub-derivation name. If the version number is not present,
445 ;; return the preferred newest version.
446 (define request name)
447
448 (define (ensure-output p sub-drv)
449 (if (member sub-drv (package-outputs p))
450 p
451 (leave (_ "~a: error: package `~a' lacks output `~a'~%")
452 (location->string (package-location p))
453 (package-full-name p)
454 sub-drv)))
455
456 (let*-values (((name sub-drv)
457 (match (string-rindex name #\:)
458 (#f (values name "out"))
459 (colon (values (substring name 0 colon)
460 (substring name (+ 1 colon))))))
461 ((name version)
462 (package-name->name+version name)))
463 (match (find-best-packages-by-name name version)
464 ((p)
465 (list name (package-version p) sub-drv (ensure-output p sub-drv)
466 (package-transitive-propagated-inputs p)))
467 ((p p* ...)
468 (format (current-error-port)
469 (_ "warning: ambiguous package specification `~a'~%")
470 request)
471 (format (current-error-port)
472 (_ "warning: choosing ~a from ~a~%")
473 (package-full-name p)
474 (location->string (package-location p)))
475 (list name (package-version p) sub-drv (ensure-output p sub-drv)
476 (package-transitive-propagated-inputs p)))
477 (()
478 (leave (_ "~a: package not found~%") request)))))
479
480 (define (upgradeable? name current-version current-path)
481 ;; Return #t if there's a version of package NAME newer than
482 ;; CURRENT-VERSION, or if the newest available version is equal to
483 ;; CURRENT-VERSION but would have an output path different than
484 ;; CURRENT-PATH.
485 (match (vhash-assoc name (newest-available-packages))
486 ((_ candidate-version pkg . rest)
487 (case (version-compare candidate-version current-version)
488 ((>) #t)
489 ((<) #f)
490 ((=) (let ((candidate-path (derivation-path->output-path
491 (package-derivation (%store) pkg))))
492 (not (string=? current-path candidate-path))))))
493 (#f #f)))
494
495 (define (ensure-default-profile)
496 ;; Ensure the default profile symlink and directory exist.
497
498 ;; Create ~/.guix-profile if it doesn't exist yet.
499 (when (and %user-environment-directory
500 %current-profile
501 (not (false-if-exception
502 (lstat %user-environment-directory))))
503 (symlink %current-profile %user-environment-directory))
504
505 ;; Attempt to create /…/profiles/per-user/$USER if needed.
506 (unless (directory-exists? %profile-directory)
507 (catch 'system-error
508 (lambda ()
509 (mkdir-p %profile-directory))
510 (lambda args
511 ;; Often, we cannot create %PROFILE-DIRECTORY because its
512 ;; parent directory is root-owned and we're running
513 ;; unprivileged.
514 (format (current-error-port)
515 (_ "error: while creating directory `~a': ~a~%")
516 %profile-directory
517 (strerror (system-error-errno args)))
518 (format (current-error-port)
519 (_ "Please create the `~a' directory, with you as the owner.~%")
520 %profile-directory)
521 (exit 1)))))
522
523 (define (process-actions opts)
524 ;; Process any install/remove/upgrade action from OPTS.
525
526 (define dry-run? (assoc-ref opts 'dry-run?))
527 (define verbose? (assoc-ref opts 'verbose?))
528 (define profile (assoc-ref opts 'profile))
529
530 (define (canonicalize-deps deps)
531 ;; Remove duplicate entries from DEPS, a list of propagated inputs,
532 ;; where each input is a name/path tuple.
533 (define (same? d1 d2)
534 (match d1
535 ((_ path1)
536 (match d2
537 ((_ path2)
538 (string=? path1 path2))))))
539
540 (delete-duplicates (map input->name+path deps) same?))
541
542 (define (package->tuple p)
543 (let ((path (package-derivation (%store) p))
544 (deps (package-transitive-propagated-inputs p)))
545 `(,(package-name p)
546 ,(package-version p)
547
548 ;; When given a package via `-e', install the first of its
549 ;; outputs (XXX).
550 ,(car (package-outputs p))
551
552 ,path
553 ,(canonicalize-deps deps))))
554
555 (define (show-what-to-remove/install remove install dry-run?)
556 ;; Tell the user what's going to happen in high-level terms.
557 ;; TODO: Report upgrades more clearly.
558 (match remove
559 (((name version _ path _) ..1)
560 (let ((len (length name))
561 (remove (map (cut format #f " ~a-~a\t~a" <> <> <>)
562 name version path)))
563 (if dry-run?
564 (format (current-error-port)
565 (N_ "The following package would be removed:~% ~{~a~%~}~%"
566 "The following packages would be removed:~% ~{~a~%~}~%"
567 len)
568 remove)
569 (format (current-error-port)
570 (N_ "The following package will be removed:~% ~{~a~%~}~%"
571 "The following packages will be removed:~% ~{~a~%~}~%"
572 len)
573 remove))))
574 (_ #f))
575 (match install
576 (((name version _ path _) ..1)
577 (let ((len (length name))
578 (install (map (cut format #f " ~a-~a\t~a" <> <> <>)
579 name version path)))
580 (if dry-run?
581 (format (current-error-port)
582 (N_ "The following package would be installed:~% ~{~a~%~}~%"
583 "The following packages would be installed:~% ~{~a~%~}~%"
584 len)
585 install)
586 (format (current-error-port)
587 (N_ "The following package will be installed:~% ~{~a~%~}~%"
588 "The following packages will be installed:~% ~{~a~%~}~%"
589 len)
590 install))))
591 (_ #f)))
592
593 ;; First roll back if asked to.
594 (if (and (assoc-ref opts 'roll-back?) (not dry-run?))
595 (begin
596 (roll-back profile)
597 (process-actions (alist-delete 'roll-back? opts)))
598 (let* ((installed (manifest-packages (profile-manifest profile)))
599 (upgrade-regexps (filter-map (match-lambda
600 (('upgrade . regexp)
601 (make-regexp regexp))
602 (_ #f))
603 opts))
604 (upgrade (if (null? upgrade-regexps)
605 '()
606 (let ((newest (find-newest-available-packages)))
607 (filter-map (match-lambda
608 ((name version output path _)
609 (and (any (cut regexp-exec <> name)
610 upgrade-regexps)
611 (upgradeable? name version path)
612 (find-package name)))
613 (_ #f))
614 installed))))
615 (install (append
616 upgrade
617 (filter-map (match-lambda
618 (('install . (? package? p))
619 #f)
620 (('install . (? store-path?))
621 #f)
622 (('install . package)
623 (find-package package))
624 (_ #f))
625 opts)))
626 (drv (filter-map (match-lambda
627 ((name version sub-drv
628 (? package? package)
629 (deps ...))
630 (check-package-freshness package)
631 (package-derivation (%store) package))
632 (_ #f))
633 install))
634 (install* (append
635 (filter-map (match-lambda
636 (('install . (? package? p))
637 (package->tuple p))
638 (('install . (? store-path? path))
639 (let-values (((name version)
640 (package-name->name+version
641 (store-path-package-name
642 path))))
643 `(,name ,version #f ,path ())))
644 (_ #f))
645 opts)
646 (map (lambda (tuple drv)
647 (match tuple
648 ((name version sub-drv _ (deps ...))
649 (let ((output-path
650 (derivation-path->output-path
651 drv sub-drv)))
652 `(,name ,version ,sub-drv ,output-path
653 ,(canonicalize-deps deps))))))
654 install drv)))
655 (remove (filter-map (match-lambda
656 (('remove . package)
657 package)
658 (_ #f))
659 opts))
660 (remove* (filter-map (cut assoc <> installed) remove))
661 (packages (append install*
662 (fold (lambda (package result)
663 (match package
664 ((name _ ...)
665 (alist-delete name result))))
666 (fold alist-delete installed remove)
667 install*))))
668
669 (when (equal? profile %current-profile)
670 (ensure-default-profile))
671
672 (show-what-to-remove/install remove* install* dry-run?)
673 (show-what-to-build (%store) drv dry-run?)
674
675 (or dry-run?
676 (and (build-derivations (%store) drv)
677 (let* ((prof-drv (profile-derivation (%store) packages))
678 (prof (derivation-path->output-path prof-drv))
679 (old-drv (profile-derivation
680 (%store) (manifest-packages
681 (profile-manifest profile))))
682 (old-prof (derivation-path->output-path old-drv))
683 (number (profile-number profile))
684
685 ;; Always use NUMBER + 1 for the new profile,
686 ;; possibly overwriting a "previous future
687 ;; generation".
688 (name (format #f "~a-~a-link"
689 profile (+ 1 number))))
690 (if (string=? old-prof prof)
691 (when (or (pair? install) (pair? remove))
692 (format (current-error-port)
693 (_ "nothing to be done~%")))
694 (and (parameterize ((current-build-output-port
695 ;; Output something when Guile
696 ;; needs to be built.
697 (if (or verbose? (guile-missing?))
698 (current-error-port)
699 (%make-void-port "w"))))
700 (build-derivations (%store) (list prof-drv)))
701 (begin
702 (switch-symlinks name prof)
703 (switch-symlinks profile name))))))))))
704
705 (define (process-query opts)
706 ;; Process any query specified by OPTS. Return #t when a query was
707 ;; actually processed, #f otherwise.
708 (let ((profile (assoc-ref opts 'profile)))
709 (match (assoc-ref opts 'query)
710 (('list-installed regexp)
711 (let* ((regexp (and regexp (make-regexp regexp)))
712 (manifest (profile-manifest profile))
713 (installed (manifest-packages manifest)))
714 (for-each (match-lambda
715 ((name version output path _)
716 (when (or (not regexp)
717 (regexp-exec regexp name))
718 (format #t "~a\t~a\t~a\t~a~%"
719 name (or version "?") output path))))
720 installed)
721 #t))
722
723 (('list-available regexp)
724 (let* ((regexp (and regexp (make-regexp regexp)))
725 (available (fold-packages
726 (lambda (p r)
727 (let ((n (package-name p)))
728 (if regexp
729 (if (regexp-exec regexp n)
730 (cons p r)
731 r)
732 (cons p r))))
733 '())))
734 (for-each (lambda (p)
735 (format #t "~a\t~a\t~a\t~a~%"
736 (package-name p)
737 (package-version p)
738 (string-join (package-outputs p) ",")
739 (location->string (package-location p))))
740 (sort available
741 (lambda (p1 p2)
742 (string<? (package-name p1)
743 (package-name p2)))))
744 #t))
745
746 (('search regexp)
747 (let ((regexp (make-regexp regexp regexp/icase)))
748 (for-each (cute package->recutils <> (current-output-port))
749 (find-packages-by-description regexp))
750 #t))
751 (_ #f))))
752
753 (let ((opts (parse-options)))
754 (or (process-query opts)
755 (with-error-handling
756 (parameterize ((%store (open-connection)))
757 (parameterize ((%guile-for-build
758 (package-derivation (%store)
759 (if (assoc-ref opts 'bootstrap?)
760 %bootstrap-guile
761 guile-final))))
762 (process-actions opts)))))))