package: Make sure the profile directory is owned by the user.
[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 (substitutes? . #t)))
417
418 (define (show-help)
419 (display (_ "Usage: guix package [OPTION]... PACKAGES...
420 Install, remove, or upgrade PACKAGES in a single transaction.\n"))
421 (display (_ "
422 -i, --install=PACKAGE install PACKAGE"))
423 (display (_ "
424 -e, --install-from-expression=EXP
425 install the package EXP evaluates to"))
426 (display (_ "
427 -r, --remove=PACKAGE remove PACKAGE"))
428 (display (_ "
429 -u, --upgrade[=REGEXP] upgrade all the installed packages matching REGEXP"))
430 (display (_ "
431 --roll-back roll back to the previous generation"))
432 (display (_ "
433 --search-paths display needed environment variable definitions"))
434 (newline)
435 (display (_ "
436 -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
437 (display (_ "
438 -n, --dry-run show what would be done without actually doing it"))
439 (display (_ "
440 --no-substitutes build instead of resorting to pre-built substitutes"))
441 (display (_ "
442 --bootstrap use the bootstrap Guile to build the profile"))
443 (display (_ "
444 --verbose produce verbose output"))
445 (newline)
446 (display (_ "
447 -s, --search=REGEXP search in synopsis and description using REGEXP"))
448 (display (_ "
449 -I, --list-installed[=REGEXP]
450 list installed packages matching REGEXP"))
451 (display (_ "
452 -A, --list-available[=REGEXP]
453 list available packages matching REGEXP"))
454 (newline)
455 (display (_ "
456 -h, --help display this help and exit"))
457 (display (_ "
458 -V, --version display version information and exit"))
459 (newline)
460 (show-bug-report-information))
461
462 (define %options
463 ;; Specification of the command-line options.
464 (list (option '(#\h "help") #f #f
465 (lambda args
466 (show-help)
467 (exit 0)))
468 (option '(#\V "version") #f #f
469 (lambda args
470 (show-version-and-exit "guix package")))
471
472 (option '(#\i "install") #t #f
473 (lambda (opt name arg result)
474 (alist-cons 'install arg result)))
475 (option '(#\e "install-from-expression") #t #f
476 (lambda (opt name arg result)
477 (alist-cons 'install (read/eval-package-expression arg)
478 result)))
479 (option '(#\r "remove") #t #f
480 (lambda (opt name arg result)
481 (alist-cons 'remove arg result)))
482 (option '(#\u "upgrade") #f #t
483 (lambda (opt name arg result)
484 (alist-cons 'upgrade arg result)))
485 (option '("roll-back") #f #f
486 (lambda (opt name arg result)
487 (alist-cons 'roll-back? #t result)))
488 (option '("search-paths") #f #f
489 (lambda (opt name arg result)
490 (cons `(query search-paths) result)))
491 (option '(#\p "profile") #t #f
492 (lambda (opt name arg result)
493 (alist-cons 'profile arg
494 (alist-delete 'profile result))))
495 (option '(#\n "dry-run") #f #f
496 (lambda (opt name arg result)
497 (alist-cons 'dry-run? #t result)))
498 (option '("no-substitutes") #f #f
499 (lambda (opt name arg result)
500 (alist-cons 'substitutes? #f
501 (alist-delete 'substitutes? result))))
502 (option '("bootstrap") #f #f
503 (lambda (opt name arg result)
504 (alist-cons 'bootstrap? #t result)))
505 (option '("verbose") #f #f
506 (lambda (opt name arg result)
507 (alist-cons 'verbose? #t result)))
508 (option '(#\s "search") #t #f
509 (lambda (opt name arg result)
510 (cons `(query search ,(or arg ""))
511 result)))
512 (option '(#\I "list-installed") #f #t
513 (lambda (opt name arg result)
514 (cons `(query list-installed ,(or arg ""))
515 result)))
516 (option '(#\A "list-available") #f #t
517 (lambda (opt name arg result)
518 (cons `(query list-available ,(or arg ""))
519 result)))))
520
521 \f
522 ;;;
523 ;;; Entry point.
524 ;;;
525
526 (define (guix-package . args)
527 (define (parse-options)
528 ;; Return the alist of option values.
529 (args-fold* args %options
530 (lambda (opt name arg result)
531 (leave (_ "~A: unrecognized option~%") name))
532 (lambda (arg result)
533 (leave (_ "~A: extraneous argument~%") arg))
534 %default-options))
535
536 (define (guile-missing?)
537 ;; Return #t if %GUILE-FOR-BUILD is not available yet.
538 (let ((out (derivation-path->output-path (%guile-for-build))))
539 (not (valid-path? (%store) out))))
540
541 (define newest-available-packages
542 (memoize find-newest-available-packages))
543
544 (define (find-best-packages-by-name name version)
545 (if version
546 (find-packages-by-name name version)
547 (match (vhash-assoc name (newest-available-packages))
548 ((_ version pkgs ...) pkgs)
549 (#f '()))))
550
551 (define* (find-package name #:optional (output "out"))
552 ;; Find the package NAME; NAME may contain a version number and a
553 ;; sub-derivation name. If the version number is not present,
554 ;; return the preferred newest version. If the sub-derivation name is not
555 ;; present, use OUTPUT.
556 (define request name)
557
558 (define (ensure-output p sub-drv)
559 (if (member sub-drv (package-outputs p))
560 p
561 (leave (_ "package `~a' lacks output `~a'~%")
562 (package-full-name p)
563 sub-drv)))
564
565 (let*-values (((name sub-drv)
566 (match (string-rindex name #\:)
567 (#f (values name output))
568 (colon (values (substring name 0 colon)
569 (substring name (+ 1 colon))))))
570 ((name version)
571 (package-name->name+version name)))
572 (match (find-best-packages-by-name name version)
573 ((p)
574 (list name (package-version p) sub-drv (ensure-output p sub-drv)
575 (package-transitive-propagated-inputs p)))
576 ((p p* ...)
577 (warning (_ "ambiguous package specification `~a'~%")
578 request)
579 (warning (_ "choosing ~a from ~a~%")
580 (package-full-name p)
581 (location->string (package-location p)))
582 (list name (package-version p) sub-drv (ensure-output p sub-drv)
583 (package-transitive-propagated-inputs p)))
584 (()
585 (leave (_ "~a: package not found~%") request)))))
586
587 (define (upgradeable? name current-version current-path)
588 ;; Return #t if there's a version of package NAME newer than
589 ;; CURRENT-VERSION, or if the newest available version is equal to
590 ;; CURRENT-VERSION but would have an output path different than
591 ;; CURRENT-PATH.
592 (match (vhash-assoc name (newest-available-packages))
593 ((_ candidate-version pkg . rest)
594 (case (version-compare candidate-version current-version)
595 ((>) #t)
596 ((<) #f)
597 ((=) (let ((candidate-path (derivation-path->output-path
598 (package-derivation (%store) pkg))))
599 (not (string=? current-path candidate-path))))))
600 (#f #f)))
601
602 (define (ensure-default-profile)
603 ;; Ensure the default profile symlink and directory exist and are
604 ;; writable.
605
606 (define (rtfm)
607 (format (current-error-port)
608 (_ "Try \"info '(guix) Invoking guix package'\" for \
609 more information.~%"))
610 (exit 1))
611
612 ;; Create ~/.guix-profile if it doesn't exist yet.
613 (when (and %user-environment-directory
614 %current-profile
615 (not (false-if-exception
616 (lstat %user-environment-directory))))
617 (symlink %current-profile %user-environment-directory))
618
619 (let ((s (stat %profile-directory #f)))
620 ;; Attempt to create /…/profiles/per-user/$USER if needed.
621 (unless (and s (eq? 'directory (stat:type s)))
622 (catch 'system-error
623 (lambda ()
624 (mkdir-p %profile-directory))
625 (lambda args
626 ;; Often, we cannot create %PROFILE-DIRECTORY because its
627 ;; parent directory is root-owned and we're running
628 ;; unprivileged.
629 (format (current-error-port)
630 (_ "error: while creating directory `~a': ~a~%")
631 %profile-directory
632 (strerror (system-error-errno args)))
633 (format (current-error-port)
634 (_ "Please create the `~a' directory, with you as the owner.~%")
635 %profile-directory)
636 (rtfm))))
637
638 ;; Bail out if it's not owned by the user.
639 (unless (= (stat:uid s) (getuid))
640 (format (current-error-port)
641 (_ "error: directory `~a' is not owned by you~%")
642 %profile-directory)
643 (format (current-error-port)
644 (_ "Please change the owner of `~a' to user ~s.~%")
645 %profile-directory (or (getenv "USER") (getuid)))
646 (rtfm))))
647
648 (define (process-actions opts)
649 ;; Process any install/remove/upgrade action from OPTS.
650
651 (define dry-run? (assoc-ref opts 'dry-run?))
652 (define verbose? (assoc-ref opts 'verbose?))
653 (define profile (assoc-ref opts 'profile))
654
655 (define (canonicalize-deps deps)
656 ;; Remove duplicate entries from DEPS, a list of propagated inputs,
657 ;; where each input is a name/path tuple.
658 (define (same? d1 d2)
659 (match d1
660 ((_ p1)
661 (match d2
662 ((_ p2) (eq? p1 p2))
663 (_ #f)))
664 ((_ p1 out1)
665 (match d2
666 ((_ p2 out2)
667 (and (string=? out1 out2)
668 (eq? p1 p2)))
669 (_ #f)))))
670
671 (delete-duplicates deps same?))
672
673 (define (package->tuple p)
674 ;; Convert package P to a tuple.
675 ;; When given a package via `-e', install the first of its
676 ;; outputs (XXX).
677 (let* ((out (car (package-outputs p)))
678 (path (package-output (%store) p out))
679 (deps (package-transitive-propagated-inputs p)))
680 `(,(package-name p)
681 ,(package-version p)
682 ,out
683 ,path
684 ,(canonicalize-deps deps))))
685
686 (define (show-what-to-remove/install remove install dry-run?)
687 ;; Tell the user what's going to happen in high-level terms.
688 ;; TODO: Report upgrades more clearly.
689 (match remove
690 (((name version _ path _) ..1)
691 (let ((len (length name))
692 (remove (map (cut format #f " ~a-~a\t~a" <> <> <>)
693 name version path)))
694 (if dry-run?
695 (format (current-error-port)
696 (N_ "The following package would be removed:~% ~{~a~%~}~%"
697 "The following packages would be removed:~% ~{~a~%~}~%"
698 len)
699 remove)
700 (format (current-error-port)
701 (N_ "The following package will be removed:~% ~{~a~%~}~%"
702 "The following packages will be removed:~% ~{~a~%~}~%"
703 len)
704 remove))))
705 (_ #f))
706 (match install
707 (((name version output path _) ..1)
708 (let ((len (length name))
709 (install (map (cut format #f " ~a-~a\t~a\t~a" <> <> <> <>)
710 name version output path)))
711 (if dry-run?
712 (format (current-error-port)
713 (N_ "The following package would be installed:~%~{~a~%~}~%"
714 "The following packages would be installed:~%~{~a~%~}~%"
715 len)
716 install)
717 (format (current-error-port)
718 (N_ "The following package will be installed:~%~{~a~%~}~%"
719 "The following packages will be installed:~%~{~a~%~}~%"
720 len)
721 install))))
722 (_ #f)))
723
724 ;; First roll back if asked to.
725 (if (and (assoc-ref opts 'roll-back?) (not dry-run?))
726 (begin
727 (roll-back profile)
728 (process-actions (alist-delete 'roll-back? opts)))
729 (let* ((installed (manifest-packages (profile-manifest profile)))
730 (upgrade-regexps (filter-map (match-lambda
731 (('upgrade . regexp)
732 (make-regexp (or regexp "")))
733 (_ #f))
734 opts))
735 (upgrade (if (null? upgrade-regexps)
736 '()
737 (let ((newest (find-newest-available-packages)))
738 (filter-map (match-lambda
739 ((name version output path _)
740 (and (any (cut regexp-exec <> name)
741 upgrade-regexps)
742 (upgradeable? name version path)
743 (find-package name
744 (or output "out"))))
745 (_ #f))
746 installed))))
747 (install (append
748 upgrade
749 (filter-map (match-lambda
750 (('install . (? package? p))
751 #f)
752 (('install . (? store-path?))
753 #f)
754 (('install . package)
755 (find-package package))
756 (_ #f))
757 opts)))
758 (drv (filter-map (match-lambda
759 ((name version sub-drv
760 (? package? package)
761 (deps ...))
762 (check-package-freshness package)
763 (package-derivation (%store) package))
764 (_ #f))
765 install))
766 (install* (append
767 (filter-map (match-lambda
768 (('install . (? package? p))
769 (package->tuple p))
770 (('install . (? store-path? path))
771 (let-values (((name version)
772 (package-name->name+version
773 (store-path-package-name
774 path))))
775 `(,name ,version #f ,path ())))
776 (_ #f))
777 opts)
778 (map (lambda (tuple drv)
779 (match tuple
780 ((name version sub-drv _ (deps ...))
781 (let ((output-path
782 (derivation-path->output-path
783 drv sub-drv)))
784 `(,name ,version ,sub-drv ,output-path
785 ,(canonicalize-deps deps))))))
786 install drv)))
787 (remove (filter-map (match-lambda
788 (('remove . package)
789 package)
790 (_ #f))
791 opts))
792 (remove* (filter-map (cut assoc <> installed) remove))
793 (packages (append install*
794 (fold (lambda (package result)
795 (match package
796 ((name _ ...)
797 (alist-delete name result))))
798 (fold alist-delete installed remove)
799 install*))))
800
801 (when (equal? profile %current-profile)
802 (ensure-default-profile))
803
804 (show-what-to-remove/install remove* install* dry-run?)
805 (show-what-to-build (%store) drv
806 #:use-substitutes? (assoc-ref opts 'substitutes?)
807 #:dry-run? dry-run?)
808
809 (or dry-run?
810 (and (build-derivations (%store) drv)
811 (let* ((prof-drv (profile-derivation (%store) packages))
812 (prof (derivation-path->output-path prof-drv))
813 (old-drv (profile-derivation
814 (%store) (manifest-packages
815 (profile-manifest profile))))
816 (old-prof (derivation-path->output-path old-drv))
817 (number (profile-number profile))
818
819 ;; Always use NUMBER + 1 for the new profile,
820 ;; possibly overwriting a "previous future
821 ;; generation".
822 (name (format #f "~a-~a-link"
823 profile (+ 1 number))))
824 (if (string=? old-prof prof)
825 (when (or (pair? install) (pair? remove))
826 (format (current-error-port)
827 (_ "nothing to be done~%")))
828 (and (parameterize ((current-build-output-port
829 ;; Output something when Guile
830 ;; needs to be built.
831 (if (or verbose? (guile-missing?))
832 (current-error-port)
833 (%make-void-port "w"))))
834 (build-derivations (%store) (list prof-drv)))
835 (begin
836 (switch-symlinks name prof)
837 (switch-symlinks profile name)
838 (display-search-paths packages
839 profile))))))))))
840
841 (define (process-query opts)
842 ;; Process any query specified by OPTS. Return #t when a query was
843 ;; actually processed, #f otherwise.
844 (let ((profile (assoc-ref opts 'profile)))
845 (match (assoc-ref opts 'query)
846 (('list-installed regexp)
847 (let* ((regexp (and regexp (make-regexp regexp)))
848 (manifest (profile-manifest profile))
849 (installed (manifest-packages manifest)))
850 (for-each (match-lambda
851 ((name version output path _)
852 (when (or (not regexp)
853 (regexp-exec regexp name))
854 (format #t "~a\t~a\t~a\t~a~%"
855 name (or version "?") output path))))
856 installed)
857 #t))
858
859 (('list-available regexp)
860 (let* ((regexp (and regexp (make-regexp regexp)))
861 (available (fold-packages
862 (lambda (p r)
863 (let ((n (package-name p)))
864 (if regexp
865 (if (regexp-exec regexp n)
866 (cons p r)
867 r)
868 (cons p r))))
869 '())))
870 (for-each (lambda (p)
871 (format #t "~a\t~a\t~a\t~a~%"
872 (package-name p)
873 (package-version p)
874 (string-join (package-outputs p) ",")
875 (location->string (package-location p))))
876 (sort available
877 (lambda (p1 p2)
878 (string<? (package-name p1)
879 (package-name p2)))))
880 #t))
881
882 (('search regexp)
883 (let ((regexp (make-regexp regexp regexp/icase)))
884 (for-each (cute package->recutils <> (current-output-port))
885 (find-packages-by-description regexp))
886 #t))
887
888 (('search-paths)
889 (let* ((manifest (profile-manifest profile))
890 (packages (manifest-packages manifest))
891 (settings (search-path-environment-variables packages
892 profile
893 (const #f))))
894 (format #t "~{~a~%~}" settings)
895 #t))
896
897 (_ #f))))
898
899 (let ((opts (parse-options)))
900 (or (process-query opts)
901 (with-error-handling
902 (parameterize ((%store (open-connection)))
903 (set-build-options (%store)
904 #:use-substitutes?
905 (assoc-ref opts 'substitutes?))
906
907 (parameterize ((%guile-for-build
908 (package-derivation (%store)
909 (if (assoc-ref opts 'bootstrap?)
910 %bootstrap-guile
911 guile-final))))
912 (process-actions opts)))))))