guix package: Inform about new upstream versions of GNU packages.
[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 (when (gnu-package? package)
289 (let ((name (package-name package))
290 (full-name (package-full-name package)))
291 (match (waiting (latest-release name)
292 (_ "looking for the latest release of GNU ~a...") name)
293 ((latest-version . _)
294 (when (version>? latest-version full-name)
295 (format (current-error-port)
296 (_ "~a: note: using ~a \
297 but ~a is available upstream~%")
298 (location->string (package-location package))
299 full-name latest-version)))
300 (_ #t)))))
301
302 \f
303 ;;;
304 ;;; Command-line options.
305 ;;;
306
307 (define %default-options
308 ;; Alist of default option values.
309 `((profile . ,%current-profile)))
310
311 (define (show-help)
312 (display (_ "Usage: guix package [OPTION]... PACKAGES...
313 Install, remove, or upgrade PACKAGES in a single transaction.\n"))
314 (display (_ "
315 -i, --install=PACKAGE install PACKAGE"))
316 (display (_ "
317 -e, --install-from-expression=EXP
318 install the package EXP evaluates to"))
319 (display (_ "
320 -r, --remove=PACKAGE remove PACKAGE"))
321 (display (_ "
322 -u, --upgrade=REGEXP upgrade all the installed packages matching REGEXP"))
323 (display (_ "
324 --roll-back roll back to the previous generation"))
325 (newline)
326 (display (_ "
327 -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
328 (display (_ "
329 -n, --dry-run show what would be done without actually doing it"))
330 (display (_ "
331 --bootstrap use the bootstrap Guile to build the profile"))
332 (display (_ "
333 --verbose produce verbose output"))
334 (newline)
335 (display (_ "
336 -s, --search=REGEXP search in synopsis and description using REGEXP"))
337 (display (_ "
338 -I, --list-installed[=REGEXP]
339 list installed packages matching REGEXP"))
340 (display (_ "
341 -A, --list-available[=REGEXP]
342 list available packages matching REGEXP"))
343 (newline)
344 (display (_ "
345 -h, --help display this help and exit"))
346 (display (_ "
347 -V, --version display version information and exit"))
348 (newline)
349 (show-bug-report-information))
350
351 (define %options
352 ;; Specification of the command-line options.
353 (list (option '(#\h "help") #f #f
354 (lambda args
355 (show-help)
356 (exit 0)))
357 (option '(#\V "version") #f #f
358 (lambda args
359 (show-version-and-exit "guix package")))
360
361 (option '(#\i "install") #t #f
362 (lambda (opt name arg result)
363 (alist-cons 'install arg result)))
364 (option '(#\e "install-from-expression") #t #f
365 (lambda (opt name arg result)
366 (alist-cons 'install (read/eval-package-expression arg)
367 result)))
368 (option '(#\r "remove") #t #f
369 (lambda (opt name arg result)
370 (alist-cons 'remove arg result)))
371 (option '(#\u "upgrade") #t #f
372 (lambda (opt name arg result)
373 (alist-cons 'upgrade arg result)))
374 (option '("roll-back") #f #f
375 (lambda (opt name arg result)
376 (alist-cons 'roll-back? #t result)))
377 (option '(#\p "profile") #t #f
378 (lambda (opt name arg result)
379 (alist-cons 'profile arg
380 (alist-delete 'profile result))))
381 (option '(#\n "dry-run") #f #f
382 (lambda (opt name arg result)
383 (alist-cons 'dry-run? #t result)))
384 (option '("bootstrap") #f #f
385 (lambda (opt name arg result)
386 (alist-cons 'bootstrap? #t result)))
387 (option '("verbose") #f #f
388 (lambda (opt name arg result)
389 (alist-cons 'verbose? #t result)))
390 (option '(#\s "search") #t #f
391 (lambda (opt name arg result)
392 (cons `(query search ,(or arg ""))
393 result)))
394 (option '(#\I "list-installed") #f #t
395 (lambda (opt name arg result)
396 (cons `(query list-installed ,(or arg ""))
397 result)))
398 (option '(#\A "list-available") #f #t
399 (lambda (opt name arg result)
400 (cons `(query list-available ,(or arg ""))
401 result)))))
402
403 \f
404 ;;;
405 ;;; Entry point.
406 ;;;
407
408 (define (guix-package . args)
409 (define (parse-options)
410 ;; Return the alist of option values.
411 (args-fold args %options
412 (lambda (opt name arg result)
413 (leave (_ "~A: unrecognized option~%") name))
414 (lambda (arg result)
415 (leave (_ "~A: extraneous argument~%") arg))
416 %default-options))
417
418 (define (guile-missing?)
419 ;; Return #t if %GUILE-FOR-BUILD is not available yet.
420 (let ((out (derivation-path->output-path (%guile-for-build))))
421 (not (valid-path? (%store) out))))
422
423 (define newest-available-packages
424 (memoize find-newest-available-packages))
425
426 (define (find-best-packages-by-name name version)
427 (if version
428 (find-packages-by-name name version)
429 (match (vhash-assoc name (newest-available-packages))
430 ((_ version pkgs ...) pkgs)
431 (#f '()))))
432
433 (define (find-package name)
434 ;; Find the package NAME; NAME may contain a version number and a
435 ;; sub-derivation name. If the version number is not present,
436 ;; return the preferred newest version.
437 (define request name)
438
439 (define (ensure-output p sub-drv)
440 (if (member sub-drv (package-outputs p))
441 p
442 (leave (_ "~a: error: package `~a' lacks output `~a'~%")
443 (location->string (package-location p))
444 (package-full-name p)
445 sub-drv)))
446
447 (let*-values (((name sub-drv)
448 (match (string-rindex name #\:)
449 (#f (values name "out"))
450 (colon (values (substring name 0 colon)
451 (substring name (+ 1 colon))))))
452 ((name version)
453 (package-name->name+version name)))
454 (match (find-best-packages-by-name name version)
455 ((p)
456 (list name (package-version p) sub-drv (ensure-output p sub-drv)
457 (package-transitive-propagated-inputs p)))
458 ((p p* ...)
459 (format (current-error-port)
460 (_ "warning: ambiguous package specification `~a'~%")
461 request)
462 (format (current-error-port)
463 (_ "warning: choosing ~a from ~a~%")
464 (package-full-name p)
465 (location->string (package-location p)))
466 (list name (package-version p) sub-drv (ensure-output p sub-drv)
467 (package-transitive-propagated-inputs p)))
468 (()
469 (leave (_ "~a: package not found~%") request)))))
470
471 (define (upgradeable? name current-version current-path)
472 ;; Return #t if there's a version of package NAME newer than
473 ;; CURRENT-VERSION, or if the newest available version is equal to
474 ;; CURRENT-VERSION but would have an output path different than
475 ;; CURRENT-PATH.
476 (match (vhash-assoc name (newest-available-packages))
477 ((_ candidate-version pkg . rest)
478 (case (version-compare candidate-version current-version)
479 ((>) #t)
480 ((<) #f)
481 ((=) (let ((candidate-path (derivation-path->output-path
482 (package-derivation (%store) pkg))))
483 (not (string=? current-path candidate-path))))))
484 (#f #f)))
485
486 (define (ensure-default-profile)
487 ;; Ensure the default profile symlink and directory exist.
488
489 ;; Create ~/.guix-profile if it doesn't exist yet.
490 (when (and %user-environment-directory
491 %current-profile
492 (not (false-if-exception
493 (lstat %user-environment-directory))))
494 (symlink %current-profile %user-environment-directory))
495
496 ;; Attempt to create /…/profiles/per-user/$USER if needed.
497 (unless (directory-exists? %profile-directory)
498 (catch 'system-error
499 (lambda ()
500 (mkdir-p %profile-directory))
501 (lambda args
502 ;; Often, we cannot create %PROFILE-DIRECTORY because its
503 ;; parent directory is root-owned and we're running
504 ;; unprivileged.
505 (format (current-error-port)
506 (_ "error: while creating directory `~a': ~a~%")
507 %profile-directory
508 (strerror (system-error-errno args)))
509 (format (current-error-port)
510 (_ "Please create the `~a' directory, with you as the owner.~%")
511 %profile-directory)
512 (exit 1)))))
513
514 (define (process-actions opts)
515 ;; Process any install/remove/upgrade action from OPTS.
516
517 (define dry-run? (assoc-ref opts 'dry-run?))
518 (define verbose? (assoc-ref opts 'verbose?))
519 (define profile (assoc-ref opts 'profile))
520
521 (define (canonicalize-deps deps)
522 ;; Remove duplicate entries from DEPS, a list of propagated inputs,
523 ;; where each input is a name/path tuple.
524 (define (same? d1 d2)
525 (match d1
526 ((_ path1)
527 (match d2
528 ((_ path2)
529 (string=? path1 path2))))))
530
531 (delete-duplicates (map input->name+path deps) same?))
532
533 (define (package->tuple p)
534 (let ((path (package-derivation (%store) p))
535 (deps (package-transitive-propagated-inputs p)))
536 `(,(package-name p)
537 ,(package-version p)
538
539 ;; When given a package via `-e', install the first of its
540 ;; outputs (XXX).
541 ,(car (package-outputs p))
542
543 ,path
544 ,(canonicalize-deps deps))))
545
546 ;; First roll back if asked to.
547 (if (and (assoc-ref opts 'roll-back?) (not dry-run?))
548 (begin
549 (roll-back profile)
550 (process-actions (alist-delete 'roll-back? opts)))
551 (let* ((installed (manifest-packages (profile-manifest profile)))
552 (upgrade-regexps (filter-map (match-lambda
553 (('upgrade . regexp)
554 (make-regexp regexp))
555 (_ #f))
556 opts))
557 (upgrade (if (null? upgrade-regexps)
558 '()
559 (let ((newest (find-newest-available-packages)))
560 (filter-map (match-lambda
561 ((name version output path _)
562 (and (any (cut regexp-exec <> name)
563 upgrade-regexps)
564 (upgradeable? name version path)
565 (find-package name)))
566 (_ #f))
567 installed))))
568 (install (append
569 upgrade
570 (filter-map (match-lambda
571 (('install . (? package? p))
572 #f)
573 (('install . (? store-path?))
574 #f)
575 (('install . package)
576 (find-package package))
577 (_ #f))
578 opts)))
579 (drv (filter-map (match-lambda
580 ((name version sub-drv
581 (? package? package)
582 (deps ...))
583 (check-package-freshness package)
584 (package-derivation (%store) package))
585 (_ #f))
586 install))
587 (install* (append
588 (filter-map (match-lambda
589 (('install . (? package? p))
590 (package->tuple p))
591 (('install . (? store-path? path))
592 (let-values (((name version)
593 (package-name->name+version
594 (store-path-package-name
595 path))))
596 `(,name ,version #f ,path ())))
597 (_ #f))
598 opts)
599 (map (lambda (tuple drv)
600 (match tuple
601 ((name version sub-drv _ (deps ...))
602 (let ((output-path
603 (derivation-path->output-path
604 drv sub-drv)))
605 `(,name ,version ,sub-drv ,output-path
606 ,(canonicalize-deps deps))))))
607 install drv)))
608 (remove (filter-map (match-lambda
609 (('remove . package)
610 package)
611 (_ #f))
612 opts))
613 (packages (append install*
614 (fold (lambda (package result)
615 (match package
616 ((name _ ...)
617 (alist-delete name result))))
618 (fold alist-delete installed remove)
619 install*))))
620
621 (when (equal? profile %current-profile)
622 (ensure-default-profile))
623
624 (show-what-to-build (%store) drv dry-run?)
625
626 (or dry-run?
627 (and (build-derivations (%store) drv)
628 (let* ((prof-drv (profile-derivation (%store) packages))
629 (prof (derivation-path->output-path prof-drv))
630 (old-drv (profile-derivation
631 (%store) (manifest-packages
632 (profile-manifest profile))))
633 (old-prof (derivation-path->output-path old-drv))
634 (number (profile-number profile))
635
636 ;; Always use NUMBER + 1 for the new profile,
637 ;; possibly overwriting a "previous future
638 ;; generation".
639 (name (format #f "~a-~a-link"
640 profile (+ 1 number))))
641 (if (string=? old-prof prof)
642 (when (or (pair? install) (pair? remove))
643 (format (current-error-port)
644 (_ "nothing to be done~%")))
645 (and (parameterize ((current-build-output-port
646 ;; Output something when Guile
647 ;; needs to be built.
648 (if (or verbose? (guile-missing?))
649 (current-error-port)
650 (%make-void-port "w"))))
651 (build-derivations (%store) (list prof-drv)))
652 (begin
653 (switch-symlinks name prof)
654 (switch-symlinks profile name))))))))))
655
656 (define (process-query opts)
657 ;; Process any query specified by OPTS. Return #t when a query was
658 ;; actually processed, #f otherwise.
659 (let ((profile (assoc-ref opts 'profile)))
660 (match (assoc-ref opts 'query)
661 (('list-installed regexp)
662 (let* ((regexp (and regexp (make-regexp regexp)))
663 (manifest (profile-manifest profile))
664 (installed (manifest-packages manifest)))
665 (for-each (match-lambda
666 ((name version output path _)
667 (when (or (not regexp)
668 (regexp-exec regexp name))
669 (format #t "~a\t~a\t~a\t~a~%"
670 name (or version "?") output path))))
671 installed)
672 #t))
673
674 (('list-available regexp)
675 (let* ((regexp (and regexp (make-regexp regexp)))
676 (available (fold-packages
677 (lambda (p r)
678 (let ((n (package-name p)))
679 (if regexp
680 (if (regexp-exec regexp n)
681 (cons p r)
682 r)
683 (cons p r))))
684 '())))
685 (for-each (lambda (p)
686 (format #t "~a\t~a\t~a\t~a~%"
687 (package-name p)
688 (package-version p)
689 (string-join (package-outputs p) ",")
690 (location->string (package-location p))))
691 (sort available
692 (lambda (p1 p2)
693 (string<? (package-name p1)
694 (package-name p2)))))
695 #t))
696
697 (('search regexp)
698 (let ((regexp (make-regexp regexp regexp/icase)))
699 (for-each (cute package->recutils <> (current-output-port))
700 (find-packages-by-description regexp))
701 #t))
702 (_ #f))))
703
704 (let ((opts (parse-options)))
705 (or (process-query opts)
706 (parameterize ((%store (open-connection)))
707 (with-error-handling
708 (parameterize ((%guile-for-build
709 (package-derivation (%store)
710 (if (assoc-ref opts 'bootstrap?)
711 %bootstrap-guile
712 guile-final))))
713 (process-actions opts)))))))