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 packages*
161 ;; Turn any package object in PACKAGES into its output path.
162 (map (match-lambda
163 ((name version output path (deps ...))
164 `(,name ,version ,output ,path
165 ,(map input->name+path deps))))
166 packages))
167
168 (define builder
169 `(begin
170 (use-modules (ice-9 pretty-print)
171 (guix build union))
172
173 (setvbuf (current-output-port) _IOLBF)
174 (setvbuf (current-error-port) _IOLBF)
175
176 (let ((output (assoc-ref %outputs "out"))
177 (inputs (map cdr %build-inputs)))
178 (format #t "building user environment `~a' with ~a packages...~%"
179 output (length inputs))
180 (union-build output inputs)
181 (call-with-output-file (string-append output "/manifest")
182 (lambda (p)
183 (pretty-print '(manifest (version 1)
184 (packages ,packages*))
185 p))))))
186
187 (define ensure-valid-input
188 ;; If a package object appears in the given input, turn it into a
189 ;; derivation path.
190 (match-lambda
191 ((name (? package? p) sub-drv ...)
192 `(,name ,(package-derivation (%store) p) ,@sub-drv))
193 (input
194 input)))
195
196 (build-expression->derivation store "user-environment"
197 (%current-system)
198 builder
199 (append-map (match-lambda
200 ((name version output path deps)
201 `((,name ,path)
202 ,@(map ensure-valid-input
203 deps))))
204 packages)
205 #:modules '((guix build union))))
206
207 (define (profile-number profile)
208 "Return PROFILE's number or 0. An absolute file name must be used."
209 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
210 (basename (readlink profile))))
211 (compose string->number (cut match:substring <> 1)))
212 0))
213
214 (define (roll-back profile)
215 "Roll back to the previous generation of PROFILE."
216 (let* ((number (profile-number profile))
217 (previous-number (previous-profile-number profile number))
218 (previous-profile (format #f "~a-~a-link"
219 profile previous-number))
220 (manifest (string-append previous-profile "/manifest")))
221
222 (define (switch-link)
223 ;; Atomically switch PROFILE to the previous profile.
224 (format #t (_ "switching from generation ~a to ~a~%")
225 number previous-number)
226 (switch-symlinks profile previous-profile))
227
228 (cond ((not (file-exists? profile)) ; invalid profile
229 (leave (_ "profile `~a' does not exist~%")
230 profile))
231 ((zero? number) ; empty profile
232 (format (current-error-port)
233 (_ "nothing to do: already at the empty profile~%")))
234 ((or (zero? previous-number) ; going to emptiness
235 (not (file-exists? previous-profile)))
236 (let*-values (((drv-path drv)
237 (profile-derivation (%store) '()))
238 ((prof)
239 (derivation-output-path
240 (assoc-ref (derivation-outputs drv) "out"))))
241 (when (not (build-derivations (%store) (list drv-path)))
242 (leave (_ "failed to build the empty profile~%")))
243
244 (switch-symlinks previous-profile prof)
245 (switch-link)))
246 (else (switch-link))))) ; anything else
247
248 (define (find-packages-by-description rx)
249 "Search in SYNOPSIS and DESCRIPTION using RX. Return a list of
250 matching packages."
251 (define (same-location? p1 p2)
252 ;; Compare locations of two packages.
253 (equal? (package-location p1) (package-location p2)))
254
255 (delete-duplicates
256 (sort
257 (fold-packages (lambda (package result)
258 (define matches?
259 (cut regexp-exec rx <>))
260
261 (if (or (and=> (package-synopsis package)
262 (compose matches? gettext))
263 (and=> (package-description package)
264 (compose matches? gettext)))
265 (cons package result)
266 result))
267 '())
268 (lambda (p1 p2)
269 (string<? (package-name p1)
270 (package-name p2))))
271 same-location?))
272
273 (define (input->name+path input)
274 "Convert the name/package/sub-drv tuple INPUT to a name/store-path tuple."
275 (let loop ((input input))
276 (match input
277 ((name (? package? package))
278 (loop `(,name ,package "out")))
279 ((name (? package? package) sub-drv)
280 `(,name ,(package-output (%store) package sub-drv)))
281 (_
282 input))))
283
284 (define %sigint-prompt
285 ;; The prompt to jump to upon SIGINT.
286 (make-prompt-tag "interruptible"))
287
288 (define (call-with-sigint-handler thunk handler)
289 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
290 number in the context of the continuation of the call to this function, and
291 return its return value."
292 (call-with-prompt %sigint-prompt
293 (lambda ()
294 (sigaction SIGINT
295 (lambda (signum)
296 (sigaction SIGINT SIG_DFL)
297 (abort-to-prompt %sigint-prompt signum)))
298 (dynamic-wind
299 (const #t)
300 thunk
301 (cut sigaction SIGINT SIG_DFL)))
302 (lambda (k signum)
303 (handler signum))))
304
305 (define-syntax-rule (waiting exp fmt rest ...)
306 "Display the given message while EXP is being evaluated."
307 (let* ((message (format #f fmt rest ...))
308 (blank (make-string (string-length message) #\space)))
309 (display message (current-error-port))
310 (force-output (current-error-port))
311 (call-with-sigint-handler
312 (lambda ()
313 (dynamic-wind
314 (const #f)
315 (lambda () exp)
316 (lambda ()
317 ;; Clear the line.
318 (display #\cr (current-error-port))
319 (display blank (current-error-port))
320 (display #\cr (current-error-port))
321 (force-output (current-error-port)))))
322 (lambda (signum)
323 (format (current-error-port) " interrupted by signal ~a~%" SIGINT)
324 #f))))
325
326 (define (check-package-freshness package)
327 "Check whether PACKAGE has a newer version available upstream, and report
328 it."
329 ;; TODO: Automatically inject the upstream version when desired.
330
331 (catch #t
332 (lambda ()
333 (when (false-if-exception (gnu-package? package))
334 (let ((name (package-name package))
335 (full-name (package-full-name package)))
336 (match (waiting (latest-release name)
337 (_ "looking for the latest release of GNU ~a...") name)
338 ((latest-version . _)
339 (when (version>? latest-version full-name)
340 (format (current-error-port)
341 (_ "~a: note: using ~a \
342 but ~a is available upstream~%")
343 (location->string (package-location package))
344 full-name latest-version)))
345 (_ #t)))))
346 (lambda (key . args)
347 ;; Silently ignore networking errors rather than preventing
348 ;; installation.
349 (case key
350 ((getaddrinfo-error ftp-error) #f)
351 (else (apply throw key args))))))
352
353 (define* (search-path-environment-variables packages profile
354 #:optional (getenv getenv))
355 "Return environment variable definitions that may be needed for the use of
356 PACKAGES in PROFILE. Use GETENV to determine the current settings and report
357 only settings not already effective."
358
359 ;; Prefer ~/.guix-profile to the real profile directory name.
360 (let ((profile (if (and %user-environment-directory
361 (false-if-exception
362 (string=? (readlink %user-environment-directory)
363 profile)))
364 %user-environment-directory
365 profile)))
366
367 ;; The search path info is not stored in the manifest. Thus, we infer the
368 ;; search paths from same-named packages found in the distro.
369
370 (define package-in-manifest->package
371 (match-lambda
372 ((name version _ ...)
373 (match (append (find-packages-by-name name version)
374 (find-packages-by-name name))
375 ((p _ ...) p)
376 (_ #f)))))
377
378 (define search-path-definition
379 (match-lambda
380 (($ <search-path-specification> variable directories separator)
381 (let ((values (or (and=> (getenv variable)
382 (cut string-tokenize* <> separator))
383 '()))
384 (directories (filter file-exists?
385 (map (cut string-append profile
386 "/" <>)
387 directories))))
388 (if (every (cut member <> values) directories)
389 #f
390 (format #f "export ~a=\"~a\""
391 variable
392 (string-join directories separator)))))))
393
394 (let* ((packages (filter-map package-in-manifest->package packages))
395 (search-paths (delete-duplicates
396 (append-map package-native-search-paths
397 packages))))
398 (filter-map search-path-definition search-paths))))
399
400 (define (display-search-paths packages profile)
401 "Display the search path environment variables that may need to be set for
402 PACKAGES, in the context of PROFILE."
403 (let ((settings (search-path-environment-variables packages profile)))
404 (unless (null? settings)
405 (format #t (_ "The following environment variable definitions may be needed:~%"))
406 (format #t "~{ ~a~%~}" settings))))
407
408 \f
409 ;;;
410 ;;; Command-line options.
411 ;;;
412
413 (define %default-options
414 ;; Alist of default option values.
415 `((profile . ,%current-profile)
416 (max-silent-time . 3600)
417 (substitutes? . #t)))
418
419 (define (show-help)
420 (display (_ "Usage: guix package [OPTION]... PACKAGES...
421 Install, remove, or upgrade PACKAGES in a single transaction.\n"))
422 (display (_ "
423 -i, --install=PACKAGE install PACKAGE"))
424 (display (_ "
425 -e, --install-from-expression=EXP
426 install the package EXP evaluates to"))
427 (display (_ "
428 -r, --remove=PACKAGE remove PACKAGE"))
429 (display (_ "
430 -u, --upgrade[=REGEXP] upgrade all the installed packages matching REGEXP"))
431 (display (_ "
432 --roll-back roll back to the previous generation"))
433 (display (_ "
434 --search-paths display needed environment variable definitions"))
435 (newline)
436 (display (_ "
437 -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
438 (display (_ "
439 -n, --dry-run show what would be done without actually doing it"))
440 (display (_ "
441 --fallback fall back to building when the substituter fails"))
442 (display (_ "
443 --no-substitutes build instead of resorting to pre-built substitutes"))
444 (display (_ "
445 --max-silent-time=SECONDS
446 mark the build as failed after SECONDS of silence"))
447 (display (_ "
448 --bootstrap use the bootstrap Guile to build the profile"))
449 (display (_ "
450 --verbose produce verbose output"))
451 (newline)
452 (display (_ "
453 -s, --search=REGEXP search in synopsis and description using REGEXP"))
454 (display (_ "
455 -I, --list-installed[=REGEXP]
456 list installed packages matching REGEXP"))
457 (display (_ "
458 -A, --list-available[=REGEXP]
459 list available packages matching REGEXP"))
460 (newline)
461 (display (_ "
462 -h, --help display this help and exit"))
463 (display (_ "
464 -V, --version display version information and exit"))
465 (newline)
466 (show-bug-report-information))
467
468 (define %options
469 ;; Specification of the command-line options.
470 (list (option '(#\h "help") #f #f
471 (lambda args
472 (show-help)
473 (exit 0)))
474 (option '(#\V "version") #f #f
475 (lambda args
476 (show-version-and-exit "guix package")))
477
478 (option '(#\i "install") #t #f
479 (lambda (opt name arg result)
480 (alist-cons 'install arg result)))
481 (option '(#\e "install-from-expression") #t #f
482 (lambda (opt name arg result)
483 (alist-cons 'install (read/eval-package-expression arg)
484 result)))
485 (option '(#\r "remove") #t #f
486 (lambda (opt name arg result)
487 (alist-cons 'remove arg result)))
488 (option '(#\u "upgrade") #f #t
489 (lambda (opt name arg result)
490 (alist-cons 'upgrade arg result)))
491 (option '("roll-back") #f #f
492 (lambda (opt name arg result)
493 (alist-cons 'roll-back? #t result)))
494 (option '("search-paths") #f #f
495 (lambda (opt name arg result)
496 (cons `(query search-paths) result)))
497 (option '(#\p "profile") #t #f
498 (lambda (opt name arg result)
499 (alist-cons 'profile arg
500 (alist-delete 'profile result))))
501 (option '(#\n "dry-run") #f #f
502 (lambda (opt name arg result)
503 (alist-cons 'dry-run? #t result)))
504 (option '("fallback") #f #f
505 (lambda (opt name arg result)
506 (alist-cons 'fallback? #t
507 (alist-delete 'fallback? result))))
508 (option '("no-substitutes") #f #f
509 (lambda (opt name arg result)
510 (alist-cons 'substitutes? #f
511 (alist-delete 'substitutes? result))))
512 (option '("max-silent-time") #t #f
513 (lambda (opt name arg result)
514 (alist-cons 'max-silent-time (string->number* arg)
515 result)))
516 (option '("bootstrap") #f #f
517 (lambda (opt name arg result)
518 (alist-cons 'bootstrap? #t result)))
519 (option '("verbose") #f #f
520 (lambda (opt name arg result)
521 (alist-cons 'verbose? #t result)))
522 (option '(#\s "search") #t #f
523 (lambda (opt name arg result)
524 (cons `(query search ,(or arg ""))
525 result)))
526 (option '(#\I "list-installed") #f #t
527 (lambda (opt name arg result)
528 (cons `(query list-installed ,(or arg ""))
529 result)))
530 (option '(#\A "list-available") #f #t
531 (lambda (opt name arg result)
532 (cons `(query list-available ,(or arg ""))
533 result)))))
534
535 \f
536 ;;;
537 ;;; Entry point.
538 ;;;
539
540 (define (guix-package . args)
541 (define (parse-options)
542 ;; Return the alist of option values.
543 (args-fold* args %options
544 (lambda (opt name arg result)
545 (leave (_ "~A: unrecognized option~%") name))
546 (lambda (arg result)
547 (leave (_ "~A: extraneous argument~%") arg))
548 %default-options))
549
550 (define (guile-missing?)
551 ;; Return #t if %GUILE-FOR-BUILD is not available yet.
552 (let ((out (derivation-path->output-path (%guile-for-build))))
553 (not (valid-path? (%store) out))))
554
555 (define newest-available-packages
556 (memoize find-newest-available-packages))
557
558 (define (find-best-packages-by-name name version)
559 (if version
560 (find-packages-by-name name version)
561 (match (vhash-assoc name (newest-available-packages))
562 ((_ version pkgs ...) pkgs)
563 (#f '()))))
564
565 (define* (find-package name #:optional (output "out"))
566 ;; Find the package NAME; NAME may contain a version number and a
567 ;; sub-derivation name. If the version number is not present,
568 ;; return the preferred newest version. If the sub-derivation name is not
569 ;; present, use OUTPUT.
570 (define request name)
571
572 (define (ensure-output p sub-drv)
573 (if (member sub-drv (package-outputs p))
574 p
575 (leave (_ "package `~a' lacks output `~a'~%")
576 (package-full-name p)
577 sub-drv)))
578
579 (let*-values (((name sub-drv)
580 (match (string-rindex name #\:)
581 (#f (values name output))
582 (colon (values (substring name 0 colon)
583 (substring name (+ 1 colon))))))
584 ((name version)
585 (package-name->name+version name)))
586 (match (find-best-packages-by-name name version)
587 ((p)
588 (list name (package-version p) sub-drv (ensure-output p sub-drv)
589 (package-transitive-propagated-inputs p)))
590 ((p p* ...)
591 (warning (_ "ambiguous package specification `~a'~%")
592 request)
593 (warning (_ "choosing ~a from ~a~%")
594 (package-full-name p)
595 (location->string (package-location p)))
596 (list name (package-version p) sub-drv (ensure-output p sub-drv)
597 (package-transitive-propagated-inputs p)))
598 (()
599 (leave (_ "~a: package not found~%") request)))))
600
601 (define (upgradeable? name current-version current-path)
602 ;; Return #t if there's a version of package NAME newer than
603 ;; CURRENT-VERSION, or if the newest available version is equal to
604 ;; CURRENT-VERSION but would have an output path different than
605 ;; CURRENT-PATH.
606 (match (vhash-assoc name (newest-available-packages))
607 ((_ candidate-version pkg . rest)
608 (case (version-compare candidate-version current-version)
609 ((>) #t)
610 ((<) #f)
611 ((=) (let ((candidate-path (derivation-path->output-path
612 (package-derivation (%store) pkg))))
613 (not (string=? current-path candidate-path))))))
614 (#f #f)))
615
616 (define (ensure-default-profile)
617 ;; Ensure the default profile symlink and directory exist and are
618 ;; writable.
619
620 (define (rtfm)
621 (format (current-error-port)
622 (_ "Try \"info '(guix) Invoking guix package'\" for \
623 more information.~%"))
624 (exit 1))
625
626 ;; Create ~/.guix-profile if it doesn't exist yet.
627 (when (and %user-environment-directory
628 %current-profile
629 (not (false-if-exception
630 (lstat %user-environment-directory))))
631 (symlink %current-profile %user-environment-directory))
632
633 (let ((s (stat %profile-directory #f)))
634 ;; Attempt to create /…/profiles/per-user/$USER if needed.
635 (unless (and s (eq? 'directory (stat:type s)))
636 (catch 'system-error
637 (lambda ()
638 (mkdir-p %profile-directory))
639 (lambda args
640 ;; Often, we cannot create %PROFILE-DIRECTORY because its
641 ;; parent directory is root-owned and we're running
642 ;; unprivileged.
643 (format (current-error-port)
644 (_ "error: while creating directory `~a': ~a~%")
645 %profile-directory
646 (strerror (system-error-errno args)))
647 (format (current-error-port)
648 (_ "Please create the `~a' directory, with you as the owner.~%")
649 %profile-directory)
650 (rtfm))))
651
652 ;; Bail out if it's not owned by the user.
653 (unless (or (not s) (= (stat:uid s) (getuid)))
654 (format (current-error-port)
655 (_ "error: directory `~a' is not owned by you~%")
656 %profile-directory)
657 (format (current-error-port)
658 (_ "Please change the owner of `~a' to user ~s.~%")
659 %profile-directory (or (getenv "USER") (getuid)))
660 (rtfm))))
661
662 (define (process-actions opts)
663 ;; Process any install/remove/upgrade action from OPTS.
664
665 (define dry-run? (assoc-ref opts 'dry-run?))
666 (define verbose? (assoc-ref opts 'verbose?))
667 (define profile (assoc-ref opts 'profile))
668
669 (define (canonicalize-deps deps)
670 ;; Remove duplicate entries from DEPS, a list of propagated inputs,
671 ;; where each input is a name/path tuple.
672 (define (same? d1 d2)
673 (match d1
674 ((_ p1)
675 (match d2
676 ((_ p2) (eq? p1 p2))
677 (_ #f)))
678 ((_ p1 out1)
679 (match d2
680 ((_ p2 out2)
681 (and (string=? out1 out2)
682 (eq? p1 p2)))
683 (_ #f)))))
684
685 (delete-duplicates deps same?))
686
687 (define (package->tuple p)
688 ;; Convert package P to a tuple.
689 ;; When given a package via `-e', install the first of its
690 ;; outputs (XXX).
691 (let* ((out (car (package-outputs p)))
692 (path (package-output (%store) p out))
693 (deps (package-transitive-propagated-inputs p)))
694 `(,(package-name p)
695 ,(package-version p)
696 ,out
697 ,path
698 ,(canonicalize-deps deps))))
699
700 (define (show-what-to-remove/install remove install dry-run?)
701 ;; Tell the user what's going to happen in high-level terms.
702 ;; TODO: Report upgrades more clearly.
703 (match remove
704 (((name version _ path _) ..1)
705 (let ((len (length name))
706 (remove (map (cut format #f " ~a-~a\t~a" <> <> <>)
707 name version path)))
708 (if dry-run?
709 (format (current-error-port)
710 (N_ "The following package would be removed:~% ~{~a~%~}~%"
711 "The following packages would be removed:~% ~{~a~%~}~%"
712 len)
713 remove)
714 (format (current-error-port)
715 (N_ "The following package will be removed:~% ~{~a~%~}~%"
716 "The following packages will be removed:~% ~{~a~%~}~%"
717 len)
718 remove))))
719 (_ #f))
720 (match install
721 (((name version output path _) ..1)
722 (let ((len (length name))
723 (install (map (cut format #f " ~a-~a\t~a\t~a" <> <> <> <>)
724 name version output path)))
725 (if dry-run?
726 (format (current-error-port)
727 (N_ "The following package would be installed:~%~{~a~%~}~%"
728 "The following packages would be installed:~%~{~a~%~}~%"
729 len)
730 install)
731 (format (current-error-port)
732 (N_ "The following package will be installed:~%~{~a~%~}~%"
733 "The following packages will be installed:~%~{~a~%~}~%"
734 len)
735 install))))
736 (_ #f)))
737
738 ;; First roll back if asked to.
739 (if (and (assoc-ref opts 'roll-back?) (not dry-run?))
740 (begin
741 (roll-back profile)
742 (process-actions (alist-delete 'roll-back? opts)))
743 (let* ((installed (manifest-packages (profile-manifest profile)))
744 (upgrade-regexps (filter-map (match-lambda
745 (('upgrade . regexp)
746 (make-regexp (or regexp "")))
747 (_ #f))
748 opts))
749 (upgrade (if (null? upgrade-regexps)
750 '()
751 (let ((newest (find-newest-available-packages)))
752 (filter-map (match-lambda
753 ((name version output path _)
754 (and (any (cut regexp-exec <> name)
755 upgrade-regexps)
756 (upgradeable? name version path)
757 (find-package name
758 (or output "out"))))
759 (_ #f))
760 installed))))
761 (install (append
762 upgrade
763 (filter-map (match-lambda
764 (('install . (? package? p))
765 #f)
766 (('install . (? store-path?))
767 #f)
768 (('install . package)
769 (find-package package))
770 (_ #f))
771 opts)))
772 (drv (filter-map (match-lambda
773 ((name version sub-drv
774 (? package? package)
775 (deps ...))
776 (check-package-freshness package)
777 (package-derivation (%store) package))
778 (_ #f))
779 install))
780 (install* (append
781 (filter-map (match-lambda
782 (('install . (? package? p))
783 (package->tuple p))
784 (('install . (? store-path? path))
785 (let-values (((name version)
786 (package-name->name+version
787 (store-path-package-name
788 path))))
789 `(,name ,version #f ,path ())))
790 (_ #f))
791 opts)
792 (map (lambda (tuple drv)
793 (match tuple
794 ((name version sub-drv _ (deps ...))
795 (let ((output-path
796 (derivation-path->output-path
797 drv sub-drv)))
798 `(,name ,version ,sub-drv ,output-path
799 ,(canonicalize-deps deps))))))
800 install drv)))
801 (remove (filter-map (match-lambda
802 (('remove . package)
803 package)
804 (_ #f))
805 opts))
806 (remove* (filter-map (cut assoc <> installed) remove))
807 (packages (append install*
808 (fold (lambda (package result)
809 (match package
810 ((name _ ...)
811 (alist-delete name result))))
812 (fold alist-delete installed remove)
813 install*))))
814
815 (when (equal? profile %current-profile)
816 (ensure-default-profile))
817
818 (show-what-to-remove/install remove* install* dry-run?)
819 (show-what-to-build (%store) drv
820 #:use-substitutes? (assoc-ref opts 'substitutes?)
821 #:dry-run? dry-run?)
822
823 (or dry-run?
824 (and (build-derivations (%store) drv)
825 (let* ((prof-drv (profile-derivation (%store) packages))
826 (prof (derivation-path->output-path prof-drv))
827 (old-drv (profile-derivation
828 (%store) (manifest-packages
829 (profile-manifest profile))))
830 (old-prof (derivation-path->output-path old-drv))
831 (number (profile-number profile))
832
833 ;; Always use NUMBER + 1 for the new profile,
834 ;; possibly overwriting a "previous future
835 ;; generation".
836 (name (format #f "~a-~a-link"
837 profile (+ 1 number))))
838 (if (string=? old-prof prof)
839 (when (or (pair? install) (pair? remove))
840 (format (current-error-port)
841 (_ "nothing to be done~%")))
842 (and (parameterize ((current-build-output-port
843 ;; Output something when Guile
844 ;; needs to be built.
845 (if (or verbose? (guile-missing?))
846 (current-error-port)
847 (%make-void-port "w"))))
848 (build-derivations (%store) (list prof-drv)))
849 (let ((count (length packages)))
850 (switch-symlinks name prof)
851 (switch-symlinks profile name)
852 (format #t (N_ "~a package in profile~%"
853 "~a packages in profile~%"
854 count)
855 count)
856 (display-search-paths packages
857 profile))))))))))
858
859 (define (process-query opts)
860 ;; Process any query specified by OPTS. Return #t when a query was
861 ;; actually processed, #f otherwise.
862 (let ((profile (assoc-ref opts 'profile)))
863 (match (assoc-ref opts 'query)
864 (('list-installed regexp)
865 (let* ((regexp (and regexp (make-regexp regexp)))
866 (manifest (profile-manifest profile))
867 (installed (manifest-packages manifest)))
868 (for-each (match-lambda
869 ((name version output path _)
870 (when (or (not regexp)
871 (regexp-exec regexp name))
872 (format #t "~a\t~a\t~a\t~a~%"
873 name (or version "?") output path))))
874 installed)
875 #t))
876
877 (('list-available regexp)
878 (let* ((regexp (and regexp (make-regexp regexp)))
879 (available (fold-packages
880 (lambda (p r)
881 (let ((n (package-name p)))
882 (if regexp
883 (if (regexp-exec regexp n)
884 (cons p r)
885 r)
886 (cons p r))))
887 '())))
888 (for-each (lambda (p)
889 (format #t "~a\t~a\t~a\t~a~%"
890 (package-name p)
891 (package-version p)
892 (string-join (package-outputs p) ",")
893 (location->string (package-location p))))
894 (sort available
895 (lambda (p1 p2)
896 (string<? (package-name p1)
897 (package-name p2)))))
898 #t))
899
900 (('search regexp)
901 (let ((regexp (make-regexp regexp regexp/icase)))
902 (for-each (cute package->recutils <> (current-output-port))
903 (find-packages-by-description regexp))
904 #t))
905
906 (('search-paths)
907 (let* ((manifest (profile-manifest profile))
908 (packages (manifest-packages manifest))
909 (settings (search-path-environment-variables packages
910 profile
911 (const #f))))
912 (format #t "~{~a~%~}" settings)
913 #t))
914
915 (_ #f))))
916
917 (let ((opts (parse-options)))
918 (or (process-query opts)
919 (with-error-handling
920 (parameterize ((%store (open-connection)))
921 (set-build-options (%store)
922 #:fallback? (assoc-ref opts 'fallback?)
923 #:use-substitutes?
924 (assoc-ref opts 'substitutes?)
925 #:max-silent-time
926 (assoc-ref opts 'max-silent-time))
927
928 (parameterize ((%guile-for-build
929 (package-derivation (%store)
930 (if (assoc-ref opts 'bootstrap?)
931 %bootstrap-guile
932 guile-final))))
933 (process-actions opts)))))))