ui: Factorize `show-what-to-build'.
[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 #:export (guix-package))
43
44 (define %store
45 (make-parameter #f))
46
47 \f
48 ;;;
49 ;;; User environment.
50 ;;;
51
52 (define %user-environment-directory
53 (and=> (getenv "HOME")
54 (cut string-append <> "/.guix-profile")))
55
56 (define %profile-directory
57 (string-append (or (getenv "NIX_STATE_DIR") %state-directory) "/profiles/"
58 (or (and=> (getenv "USER")
59 (cut string-append "per-user/" <>))
60 "default")))
61
62 (define %current-profile
63 ;; Call it `guix-profile', not `profile', to allow Guix profiles to
64 ;; coexist with Nix profiles.
65 (string-append %profile-directory "/guix-profile"))
66
67 (define (profile-manifest profile)
68 "Return the PROFILE's manifest."
69 (let ((manifest (string-append profile "/manifest")))
70 (if (file-exists? manifest)
71 (call-with-input-file manifest read)
72 '(manifest (version 1) (packages ())))))
73
74 (define (manifest-packages manifest)
75 "Return the packages listed in MANIFEST."
76 (match manifest
77 (('manifest ('version 0)
78 ('packages ((name version output path) ...)))
79 (zip name version output path
80 (make-list (length name) '())))
81
82 ;; Version 1 adds a list of propagated inputs to the
83 ;; name/version/output/path tuples.
84 (('manifest ('version 1)
85 ('packages (packages ...)))
86 packages)
87
88 (_
89 (error "unsupported manifest format" manifest))))
90
91 (define (profile-regexp profile)
92 "Return a regular expression that matches PROFILE's name and number."
93 (make-regexp (string-append "^" (regexp-quote (basename profile))
94 "-([0-9]+)")))
95
96 (define (profile-numbers profile)
97 "Return the list of generation numbers of PROFILE, or '(0) if no
98 former profiles were found."
99 (define* (scandir name #:optional (select? (const #t))
100 (entry<? (@ (ice-9 i18n) string-locale<?)))
101 ;; XXX: Bug-fix version introduced in Guile v2.0.6-62-g139ce19.
102 (define (enter? dir stat result)
103 (and stat (string=? dir name)))
104
105 (define (visit basename result)
106 (if (select? basename)
107 (cons basename result)
108 result))
109
110 (define (leaf name stat result)
111 (and result
112 (visit (basename name) result)))
113
114 (define (down name stat result)
115 (visit "." '()))
116
117 (define (up name stat result)
118 (visit ".." result))
119
120 (define (skip name stat result)
121 ;; All the sub-directories are skipped.
122 (visit (basename name) result))
123
124 (define (error name* stat errno result)
125 (if (string=? name name*) ; top-level NAME is unreadable
126 result
127 (visit (basename name*) result)))
128
129 (and=> (file-system-fold enter? leaf down up skip error #f name lstat)
130 (lambda (files)
131 (sort files entry<?))))
132
133 (match (scandir (dirname profile)
134 (cute regexp-exec (profile-regexp profile) <>))
135 (#f ; no profile directory
136 '(0))
137 (() ; no profiles
138 '(0))
139 ((profiles ...) ; former profiles around
140 (map (compose string->number
141 (cut match:substring <> 1)
142 (cute regexp-exec (profile-regexp profile) <>))
143 profiles))))
144
145 (define (previous-profile-number profile number)
146 "Return the number of the generation before generation NUMBER of
147 PROFILE, or 0 if none exists. It could be NUMBER - 1, but it's not the
148 case when generations have been deleted (there are \"holes\")."
149 (fold (lambda (candidate highest)
150 (if (and (< candidate number) (> candidate highest))
151 candidate
152 highest))
153 0
154 (profile-numbers profile)))
155
156 (define (profile-derivation store packages)
157 "Return a derivation that builds a profile (a user environment) with
158 all of PACKAGES, a list of name/version/output/path/deps tuples."
159 (define builder
160 `(begin
161 (use-modules (ice-9 pretty-print)
162 (guix build union))
163
164 (setvbuf (current-output-port) _IOLBF)
165 (setvbuf (current-error-port) _IOLBF)
166
167 (let ((output (assoc-ref %outputs "out"))
168 (inputs (map cdr %build-inputs)))
169 (format #t "building user environment `~a' with ~a packages...~%"
170 output (length inputs))
171 (union-build output inputs)
172 (call-with-output-file (string-append output "/manifest")
173 (lambda (p)
174 (pretty-print '(manifest (version 1)
175 (packages ,packages))
176 p))))))
177
178 (build-expression->derivation store "user-environment"
179 (%current-system)
180 builder
181 (append-map (match-lambda
182 ((name version output path deps)
183 `((,name ,path)
184 ,@deps)))
185 packages)
186 #:modules '((guix build union))))
187
188 (define (profile-number profile)
189 "Return PROFILE's number or 0. An absolute file name must be used."
190 (or (and=> (false-if-exception (regexp-exec (profile-regexp profile)
191 (basename (readlink profile))))
192 (compose string->number (cut match:substring <> 1)))
193 0))
194
195 (define (roll-back profile)
196 "Roll back to the previous generation of PROFILE."
197 (let* ((number (profile-number profile))
198 (previous-number (previous-profile-number profile number))
199 (previous-profile (format #f "~a-~a-link"
200 profile previous-number))
201 (manifest (string-append previous-profile "/manifest")))
202
203 (define (switch-link)
204 ;; Atomically switch PROFILE to the previous profile.
205 (format #t (_ "switching from generation ~a to ~a~%")
206 number previous-number)
207 (switch-symlinks profile previous-profile))
208
209 (cond ((not (file-exists? profile)) ; invalid profile
210 (format (current-error-port)
211 (_ "error: profile `~a' does not exist~%")
212 profile))
213 ((zero? number) ; empty profile
214 (format (current-error-port)
215 (_ "nothing to do: already at the empty profile~%")))
216 ((or (zero? previous-number) ; going to emptiness
217 (not (file-exists? previous-profile)))
218 (let*-values (((drv-path drv)
219 (profile-derivation (%store) '()))
220 ((prof)
221 (derivation-output-path
222 (assoc-ref (derivation-outputs drv) "out"))))
223 (when (not (build-derivations (%store) (list drv-path)))
224 (leave (_ "failed to build the empty profile~%")))
225
226 (switch-symlinks previous-profile prof)
227 (switch-link)))
228 (else (switch-link))))) ; anything else
229
230 (define (find-packages-by-description rx)
231 "Search in SYNOPSIS and DESCRIPTION using RX. Return a list of
232 matching packages."
233 (define (same-location? p1 p2)
234 ;; Compare locations of two packages.
235 (equal? (package-location p1) (package-location p2)))
236
237 (delete-duplicates
238 (sort
239 (fold-packages (lambda (package result)
240 (define matches?
241 (cut regexp-exec rx <>))
242
243 (if (or (and=> (package-synopsis package)
244 (compose matches? gettext))
245 (and=> (package-description package)
246 (compose matches? gettext)))
247 (cons package result)
248 result))
249 '())
250 (lambda (p1 p2)
251 (string<? (package-name p1)
252 (package-name p2))))
253 same-location?))
254
255 (define (input->name+path input)
256 "Convert the name/package/sub-drv tuple INPUT to a name/store-path tuple."
257 (let loop ((input input))
258 (match input
259 ((name package)
260 (loop `(,name ,package "out")))
261 ((name package sub-drv)
262 (let*-values (((_ drv)
263 (package-derivation (%store) package))
264 ((out)
265 (derivation-output-path
266 (assoc-ref (derivation-outputs drv) sub-drv))))
267 `(,name ,out))))))
268
269 \f
270 ;;;
271 ;;; Command-line options.
272 ;;;
273
274 (define %default-options
275 ;; Alist of default option values.
276 `((profile . ,%current-profile)))
277
278 (define (show-help)
279 (display (_ "Usage: guix package [OPTION]... PACKAGES...
280 Install, remove, or upgrade PACKAGES in a single transaction.\n"))
281 (display (_ "
282 -i, --install=PACKAGE install PACKAGE"))
283 (display (_ "
284 -r, --remove=PACKAGE remove PACKAGE"))
285 (display (_ "
286 -u, --upgrade=REGEXP upgrade all the installed packages matching REGEXP"))
287 (display (_ "
288 --roll-back roll back to the previous generation"))
289 (newline)
290 (display (_ "
291 -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
292 (display (_ "
293 -n, --dry-run show what would be done without actually doing it"))
294 (display (_ "
295 --bootstrap use the bootstrap Guile to build the profile"))
296 (display (_ "
297 --verbose produce verbose output"))
298 (newline)
299 (display (_ "
300 -s, --search=REGEXP search in synopsis and description using REGEXP"))
301 (display (_ "
302 -I, --list-installed[=REGEXP]
303 list installed packages matching REGEXP"))
304 (display (_ "
305 -A, --list-available[=REGEXP]
306 list available packages matching REGEXP"))
307 (newline)
308 (display (_ "
309 -h, --help display this help and exit"))
310 (display (_ "
311 -V, --version display version information and exit"))
312 (newline)
313 (show-bug-report-information))
314
315 (define %options
316 ;; Specification of the command-line options.
317 (list (option '(#\h "help") #f #f
318 (lambda args
319 (show-help)
320 (exit 0)))
321 (option '(#\V "version") #f #f
322 (lambda args
323 (show-version-and-exit "guix package")))
324
325 (option '(#\i "install") #t #f
326 (lambda (opt name arg result)
327 (alist-cons 'install arg result)))
328 (option '(#\r "remove") #t #f
329 (lambda (opt name arg result)
330 (alist-cons 'remove arg result)))
331 (option '(#\u "upgrade") #t #f
332 (lambda (opt name arg result)
333 (alist-cons 'upgrade arg result)))
334 (option '("roll-back") #f #f
335 (lambda (opt name arg result)
336 (alist-cons 'roll-back? #t result)))
337 (option '(#\p "profile") #t #f
338 (lambda (opt name arg result)
339 (alist-cons 'profile arg
340 (alist-delete 'profile result))))
341 (option '(#\n "dry-run") #f #f
342 (lambda (opt name arg result)
343 (alist-cons 'dry-run? #t result)))
344 (option '("bootstrap") #f #f
345 (lambda (opt name arg result)
346 (alist-cons 'bootstrap? #t result)))
347 (option '("verbose") #f #f
348 (lambda (opt name arg result)
349 (alist-cons 'verbose? #t result)))
350 (option '(#\s "search") #t #f
351 (lambda (opt name arg result)
352 (cons `(query search ,(or arg ""))
353 result)))
354 (option '(#\I "list-installed") #f #t
355 (lambda (opt name arg result)
356 (cons `(query list-installed ,(or arg ""))
357 result)))
358 (option '(#\A "list-available") #f #t
359 (lambda (opt name arg result)
360 (cons `(query list-available ,(or arg ""))
361 result)))))
362
363 \f
364 ;;;
365 ;;; Entry point.
366 ;;;
367
368 (define (guix-package . args)
369 (define (parse-options)
370 ;; Return the alist of option values.
371 (args-fold args %options
372 (lambda (opt name arg result)
373 (leave (_ "~A: unrecognized option~%") name))
374 (lambda (arg result)
375 (leave (_ "~A: extraneous argument~%") arg))
376 %default-options))
377
378 (define (guile-missing?)
379 ;; Return #t if %GUILE-FOR-BUILD is not available yet.
380 (let ((out (derivation-path->output-path (%guile-for-build))))
381 (not (valid-path? (%store) out))))
382
383 (define newest-available-packages
384 (memoize find-newest-available-packages))
385
386 (define (find-best-packages-by-name name version)
387 (if version
388 (find-packages-by-name name version)
389 (match (vhash-assoc name (newest-available-packages))
390 ((_ version pkgs ...) pkgs)
391 (#f '()))))
392
393 (define (find-package name)
394 ;; Find the package NAME; NAME may contain a version number and a
395 ;; sub-derivation name. If the version number is not present,
396 ;; return the preferred newest version.
397 (define request name)
398
399 (define (ensure-output p sub-drv)
400 (if (member sub-drv (package-outputs p))
401 p
402 (leave (_ "~a: error: package `~a' lacks output `~a'~%")
403 (location->string (package-location p))
404 (package-full-name p)
405 sub-drv)))
406
407 (let*-values (((name sub-drv)
408 (match (string-rindex name #\:)
409 (#f (values name "out"))
410 (colon (values (substring name 0 colon)
411 (substring name (+ 1 colon))))))
412 ((name version)
413 (package-name->name+version name)))
414 (match (find-best-packages-by-name name version)
415 ((p)
416 (list name (package-version p) sub-drv (ensure-output p sub-drv)
417 (package-transitive-propagated-inputs p)))
418 ((p p* ...)
419 (format (current-error-port)
420 (_ "warning: ambiguous package specification `~a'~%")
421 request)
422 (format (current-error-port)
423 (_ "warning: choosing ~a from ~a~%")
424 (package-full-name p)
425 (location->string (package-location p)))
426 (list name (package-version p) sub-drv (ensure-output p sub-drv)
427 (package-transitive-propagated-inputs p)))
428 (()
429 (leave (_ "~a: package not found~%") request)))))
430
431 (define (upgradeable? name current-version current-path)
432 ;; Return #t if there's a version of package NAME newer than
433 ;; CURRENT-VERSION, or if the newest available version is equal to
434 ;; CURRENT-VERSION but would have an output path different than
435 ;; CURRENT-PATH.
436 (match (vhash-assoc name (newest-available-packages))
437 ((_ candidate-version pkg . rest)
438 (case (version-compare candidate-version current-version)
439 ((>) #t)
440 ((<) #f)
441 ((=) (let ((candidate-path (derivation-path->output-path
442 (package-derivation (%store) pkg))))
443 (not (string=? current-path candidate-path))))))
444 (#f #f)))
445
446 (define (ensure-default-profile)
447 ;; Ensure the default profile symlink and directory exist.
448
449 ;; Create ~/.guix-profile if it doesn't exist yet.
450 (when (and %user-environment-directory
451 %current-profile
452 (not (false-if-exception
453 (lstat %user-environment-directory))))
454 (symlink %current-profile %user-environment-directory))
455
456 ;; Attempt to create /…/profiles/per-user/$USER if needed.
457 (unless (directory-exists? %profile-directory)
458 (catch 'system-error
459 (lambda ()
460 (mkdir-p %profile-directory))
461 (lambda args
462 ;; Often, we cannot create %PROFILE-DIRECTORY because its
463 ;; parent directory is root-owned and we're running
464 ;; unprivileged.
465 (format (current-error-port)
466 (_ "error: while creating directory `~a': ~a~%")
467 %profile-directory
468 (strerror (system-error-errno args)))
469 (format (current-error-port)
470 (_ "Please create the `~a' directory, with you as the owner.~%")
471 %profile-directory)
472 (exit 1)))))
473
474 (define (process-actions opts)
475 ;; Process any install/remove/upgrade action from OPTS.
476
477 (define dry-run? (assoc-ref opts 'dry-run?))
478 (define verbose? (assoc-ref opts 'verbose?))
479 (define profile (assoc-ref opts 'profile))
480
481 (define (canonicalize-deps deps)
482 ;; Remove duplicate entries from DEPS, a list of propagated inputs,
483 ;; where each input is a name/path tuple.
484 (define (same? d1 d2)
485 (match d1
486 ((_ path1)
487 (match d2
488 ((_ path2)
489 (string=? path1 path2))))))
490
491 (delete-duplicates (map input->name+path deps) same?))
492
493 ;; First roll back if asked to.
494 (if (and (assoc-ref opts 'roll-back?) (not dry-run?))
495 (begin
496 (roll-back profile)
497 (process-actions (alist-delete 'roll-back? opts)))
498 (let* ((installed (manifest-packages (profile-manifest profile)))
499 (upgrade-regexps (filter-map (match-lambda
500 (('upgrade . regexp)
501 (make-regexp regexp))
502 (_ #f))
503 opts))
504 (upgrade (if (null? upgrade-regexps)
505 '()
506 (let ((newest (find-newest-available-packages)))
507 (filter-map (match-lambda
508 ((name version output path _)
509 (and (any (cut regexp-exec <> name)
510 upgrade-regexps)
511 (upgradeable? name version path)
512 (find-package name)))
513 (_ #f))
514 installed))))
515 (install (append
516 upgrade
517 (filter-map (match-lambda
518 (('install . (? store-path?))
519 #f)
520 (('install . package)
521 (find-package package))
522 (_ #f))
523 opts)))
524 (drv (filter-map (match-lambda
525 ((name version sub-drv
526 (? package? package)
527 (deps ...))
528 (package-derivation (%store) package))
529 (_ #f))
530 install))
531 (install* (append
532 (filter-map (match-lambda
533 (('install . (? store-path? path))
534 (let-values (((name version)
535 (package-name->name+version
536 (store-path-package-name
537 path))))
538 `(,name ,version #f ,path ())))
539 (_ #f))
540 opts)
541 (map (lambda (tuple drv)
542 (match tuple
543 ((name version sub-drv _ (deps ...))
544 (let ((output-path
545 (derivation-path->output-path
546 drv sub-drv)))
547 `(,name ,version ,sub-drv ,output-path
548 ,(canonicalize-deps deps))))))
549 install drv)))
550 (remove (filter-map (match-lambda
551 (('remove . package)
552 package)
553 (_ #f))
554 opts))
555 (packages (append install*
556 (fold (lambda (package result)
557 (match package
558 ((name _ ...)
559 (alist-delete name result))))
560 (fold alist-delete installed remove)
561 install*))))
562
563 (when (equal? profile %current-profile)
564 (ensure-default-profile))
565
566 (show-what-to-build (%store) drv dry-run?)
567
568 (or dry-run?
569 (and (build-derivations (%store) drv)
570 (let* ((prof-drv (profile-derivation (%store) packages))
571 (prof (derivation-path->output-path prof-drv))
572 (old-drv (profile-derivation
573 (%store) (manifest-packages
574 (profile-manifest profile))))
575 (old-prof (derivation-path->output-path old-drv))
576 (number (profile-number profile))
577
578 ;; Always use NUMBER + 1 for the new profile,
579 ;; possibly overwriting a "previous future
580 ;; generation".
581 (name (format #f "~a-~a-link"
582 profile (+ 1 number))))
583 (if (string=? old-prof prof)
584 (when (or (pair? install) (pair? remove))
585 (format (current-error-port)
586 (_ "nothing to be done~%")))
587 (and (parameterize ((current-build-output-port
588 ;; Output something when Guile
589 ;; needs to be built.
590 (if (or verbose? (guile-missing?))
591 (current-error-port)
592 (%make-void-port "w"))))
593 (build-derivations (%store) (list prof-drv)))
594 (begin
595 (switch-symlinks name prof)
596 (switch-symlinks profile name))))))))))
597
598 (define (process-query opts)
599 ;; Process any query specified by OPTS. Return #t when a query was
600 ;; actually processed, #f otherwise.
601 (let ((profile (assoc-ref opts 'profile)))
602 (match (assoc-ref opts 'query)
603 (('list-installed regexp)
604 (let* ((regexp (and regexp (make-regexp regexp)))
605 (manifest (profile-manifest profile))
606 (installed (manifest-packages manifest)))
607 (for-each (match-lambda
608 ((name version output path _)
609 (when (or (not regexp)
610 (regexp-exec regexp name))
611 (format #t "~a\t~a\t~a\t~a~%"
612 name (or version "?") output path))))
613 installed)
614 #t))
615
616 (('list-available regexp)
617 (let* ((regexp (and regexp (make-regexp regexp)))
618 (available (fold-packages
619 (lambda (p r)
620 (let ((n (package-name p)))
621 (if regexp
622 (if (regexp-exec regexp n)
623 (cons p r)
624 r)
625 (cons p r))))
626 '())))
627 (for-each (lambda (p)
628 (format #t "~a\t~a\t~a\t~a~%"
629 (package-name p)
630 (package-version p)
631 (string-join (package-outputs p) ",")
632 (location->string (package-location p))))
633 (sort available
634 (lambda (p1 p2)
635 (string<? (package-name p1)
636 (package-name p2)))))
637 #t))
638
639 (('search regexp)
640 (let ((regexp (make-regexp regexp regexp/icase)))
641 (for-each (cute package->recutils <> (current-output-port))
642 (find-packages-by-description regexp))
643 #t))
644 (_ #f))))
645
646 (let ((opts (parse-options)))
647 (or (process-query opts)
648 (parameterize ((%store (open-connection)))
649 (with-error-handling
650 (parameterize ((%guile-for-build
651 (package-derivation (%store)
652 (if (assoc-ref opts 'bootstrap?)
653 %bootstrap-guile
654 guile-final))))
655 (process-actions opts)))))))