scripts: Remove hyphen in the command name shown by `--version'.
[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 (switch-symlinks link target)
196 "Atomically switch LINK, a symbolic link, to point to TARGET. Works
197 both when LINK already exists and when it does not."
198 (let ((pivot (string-append link ".new")))
199 (symlink target pivot)
200 (rename-file pivot link)))
201
202 (define (roll-back profile)
203 "Roll back to the previous generation of PROFILE."
204 (let* ((number (profile-number profile))
205 (previous-number (previous-profile-number profile number))
206 (previous-profile (format #f "~a-~a-link"
207 profile previous-number))
208 (manifest (string-append previous-profile "/manifest")))
209
210 (define (switch-link)
211 ;; Atomically switch PROFILE to the previous profile.
212 (format #t (_ "switching from generation ~a to ~a~%")
213 number previous-number)
214 (switch-symlinks profile previous-profile))
215
216 (cond ((not (file-exists? profile)) ; invalid profile
217 (format (current-error-port)
218 (_ "error: profile `~a' does not exist~%")
219 profile))
220 ((zero? number) ; empty profile
221 (format (current-error-port)
222 (_ "nothing to do: already at the empty profile~%")))
223 ((or (zero? previous-number) ; going to emptiness
224 (not (file-exists? previous-profile)))
225 (let*-values (((drv-path drv)
226 (profile-derivation (%store) '()))
227 ((prof)
228 (derivation-output-path
229 (assoc-ref (derivation-outputs drv) "out"))))
230 (when (not (build-derivations (%store) (list drv-path)))
231 (leave (_ "failed to build the empty profile~%")))
232
233 (switch-symlinks previous-profile prof)
234 (switch-link)))
235 (else (switch-link))))) ; anything else
236
237 (define (find-packages-by-description rx)
238 "Search in SYNOPSIS and DESCRIPTION using RX. Return a list of
239 matching packages."
240 (define (same-location? p1 p2)
241 ;; Compare locations of two packages.
242 (equal? (package-location p1) (package-location p2)))
243
244 (delete-duplicates
245 (sort
246 (fold-packages (lambda (package result)
247 (define matches?
248 (cut regexp-exec rx <>))
249
250 (if (or (and=> (package-synopsis package)
251 (compose matches? gettext))
252 (and=> (package-description package)
253 (compose matches? gettext)))
254 (cons package result)
255 result))
256 '())
257 (lambda (p1 p2)
258 (string<? (package-name p1)
259 (package-name p2))))
260 same-location?))
261
262 (define (input->name+path input)
263 "Convert the name/package/sub-drv tuple INPUT to a name/store-path tuple."
264 (let loop ((input input))
265 (match input
266 ((name package)
267 (loop `(,name ,package "out")))
268 ((name package sub-drv)
269 (let*-values (((_ drv)
270 (package-derivation (%store) package))
271 ((out)
272 (derivation-output-path
273 (assoc-ref (derivation-outputs drv) sub-drv))))
274 `(,name ,out))))))
275
276 \f
277 ;;;
278 ;;; Command-line options.
279 ;;;
280
281 (define %default-options
282 ;; Alist of default option values.
283 `((profile . ,%current-profile)))
284
285 (define (show-help)
286 (display (_ "Usage: guix package [OPTION]... PACKAGES...
287 Install, remove, or upgrade PACKAGES in a single transaction.\n"))
288 (display (_ "
289 -i, --install=PACKAGE install PACKAGE"))
290 (display (_ "
291 -r, --remove=PACKAGE remove PACKAGE"))
292 (display (_ "
293 -u, --upgrade=REGEXP upgrade all the installed packages matching REGEXP"))
294 (display (_ "
295 --roll-back roll back to the previous generation"))
296 (newline)
297 (display (_ "
298 -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
299 (display (_ "
300 -n, --dry-run show what would be done without actually doing it"))
301 (display (_ "
302 --bootstrap use the bootstrap Guile to build the profile"))
303 (display (_ "
304 --verbose produce verbose output"))
305 (newline)
306 (display (_ "
307 -s, --search=REGEXP search in synopsis and description using REGEXP"))
308 (display (_ "
309 -I, --list-installed[=REGEXP]
310 list installed packages matching REGEXP"))
311 (display (_ "
312 -A, --list-available[=REGEXP]
313 list available packages matching REGEXP"))
314 (newline)
315 (display (_ "
316 -h, --help display this help and exit"))
317 (display (_ "
318 -V, --version display version information and exit"))
319 (newline)
320 (show-bug-report-information))
321
322 (define %options
323 ;; Specification of the command-line options.
324 (list (option '(#\h "help") #f #f
325 (lambda args
326 (show-help)
327 (exit 0)))
328 (option '(#\V "version") #f #f
329 (lambda args
330 (show-version-and-exit "guix package")))
331
332 (option '(#\i "install") #t #f
333 (lambda (opt name arg result)
334 (alist-cons 'install arg result)))
335 (option '(#\r "remove") #t #f
336 (lambda (opt name arg result)
337 (alist-cons 'remove arg result)))
338 (option '(#\u "upgrade") #t #f
339 (lambda (opt name arg result)
340 (alist-cons 'upgrade arg result)))
341 (option '("roll-back") #f #f
342 (lambda (opt name arg result)
343 (alist-cons 'roll-back? #t result)))
344 (option '(#\p "profile") #t #f
345 (lambda (opt name arg result)
346 (alist-cons 'profile arg
347 (alist-delete 'profile result))))
348 (option '(#\n "dry-run") #f #f
349 (lambda (opt name arg result)
350 (alist-cons 'dry-run? #t result)))
351 (option '("bootstrap") #f #f
352 (lambda (opt name arg result)
353 (alist-cons 'bootstrap? #t result)))
354 (option '("verbose") #f #f
355 (lambda (opt name arg result)
356 (alist-cons 'verbose? #t result)))
357 (option '(#\s "search") #t #f
358 (lambda (opt name arg result)
359 (cons `(query search ,(or arg ""))
360 result)))
361 (option '(#\I "list-installed") #f #t
362 (lambda (opt name arg result)
363 (cons `(query list-installed ,(or arg ""))
364 result)))
365 (option '(#\A "list-available") #f #t
366 (lambda (opt name arg result)
367 (cons `(query list-available ,(or arg ""))
368 result)))))
369
370 \f
371 ;;;
372 ;;; Entry point.
373 ;;;
374
375 (define (guix-package . args)
376 (define (parse-options)
377 ;; Return the alist of option values.
378 (args-fold args %options
379 (lambda (opt name arg result)
380 (leave (_ "~A: unrecognized option~%") name))
381 (lambda (arg result)
382 (leave (_ "~A: extraneous argument~%") arg))
383 %default-options))
384
385 (define (guile-missing?)
386 ;; Return #t if %GUILE-FOR-BUILD is not available yet.
387 (let ((out (derivation-path->output-path (%guile-for-build))))
388 (not (valid-path? (%store) out))))
389
390 (define (show-what-to-build drv dry-run?)
391 ;; Show what will/would be built in realizing the derivations listed
392 ;; in DRV.
393 (let* ((req (append-map (lambda (drv-path)
394 (let ((d (call-with-input-file drv-path
395 read-derivation)))
396 (derivation-prerequisites-to-build
397 (%store) d)))
398 drv))
399 (req* (delete-duplicates
400 (append (remove (compose (cute valid-path? (%store) <>)
401 derivation-path->output-path)
402 drv)
403 (map derivation-input-path req)))))
404 (if dry-run?
405 (format (current-error-port)
406 (N_ "~:[the following derivation would be built:~%~{ ~a~%~}~;~]"
407 "~:[the following derivations would be built:~%~{ ~a~%~}~;~]"
408 (length req*))
409 (null? req*) req*)
410 (format (current-error-port)
411 (N_ "~:[the following derivation will be built:~%~{ ~a~%~}~;~]"
412 "~:[the following derivations will be built:~%~{ ~a~%~}~;~]"
413 (length req*))
414 (null? req*) req*))))
415
416 (define newest-available-packages
417 (memoize find-newest-available-packages))
418
419 (define (find-best-packages-by-name name version)
420 (if version
421 (find-packages-by-name name version)
422 (match (vhash-assoc name (newest-available-packages))
423 ((_ version pkgs ...) pkgs)
424 (#f '()))))
425
426 (define (find-package name)
427 ;; Find the package NAME; NAME may contain a version number and a
428 ;; sub-derivation name. If the version number is not present,
429 ;; return the preferred newest version.
430 (define request name)
431
432 (define (ensure-output p sub-drv)
433 (if (member sub-drv (package-outputs p))
434 p
435 (leave (_ "~a: error: package `~a' lacks output `~a'~%")
436 (location->string (package-location p))
437 (package-full-name p)
438 sub-drv)))
439
440 (let*-values (((name sub-drv)
441 (match (string-rindex name #\:)
442 (#f (values name "out"))
443 (colon (values (substring name 0 colon)
444 (substring name (+ 1 colon))))))
445 ((name version)
446 (package-name->name+version name)))
447 (match (find-best-packages-by-name name version)
448 ((p)
449 (list name (package-version p) sub-drv (ensure-output p sub-drv)
450 (package-transitive-propagated-inputs p)))
451 ((p p* ...)
452 (format (current-error-port)
453 (_ "warning: ambiguous package specification `~a'~%")
454 request)
455 (format (current-error-port)
456 (_ "warning: choosing ~a from ~a~%")
457 (package-full-name p)
458 (location->string (package-location p)))
459 (list name (package-version p) sub-drv (ensure-output p sub-drv)
460 (package-transitive-propagated-inputs p)))
461 (()
462 (leave (_ "~a: package not found~%") request)))))
463
464 (define (upgradeable? name current-version current-path)
465 ;; Return #t if there's a version of package NAME newer than
466 ;; CURRENT-VERSION, or if the newest available version is equal to
467 ;; CURRENT-VERSION but would have an output path different than
468 ;; CURRENT-PATH.
469 (match (vhash-assoc name (newest-available-packages))
470 ((_ candidate-version pkg . rest)
471 (case (version-compare candidate-version current-version)
472 ((>) #t)
473 ((<) #f)
474 ((=) (let ((candidate-path (derivation-path->output-path
475 (package-derivation (%store) pkg))))
476 (not (string=? current-path candidate-path))))))
477 (#f #f)))
478
479 (define (ensure-default-profile)
480 ;; Ensure the default profile symlink and directory exist.
481
482 ;; Create ~/.guix-profile if it doesn't exist yet.
483 (when (and %user-environment-directory
484 %current-profile
485 (not (false-if-exception
486 (lstat %user-environment-directory))))
487 (symlink %current-profile %user-environment-directory))
488
489 ;; Attempt to create /…/profiles/per-user/$USER if needed.
490 (unless (directory-exists? %profile-directory)
491 (catch 'system-error
492 (lambda ()
493 (mkdir-p %profile-directory))
494 (lambda args
495 ;; Often, we cannot create %PROFILE-DIRECTORY because its
496 ;; parent directory is root-owned and we're running
497 ;; unprivileged.
498 (format (current-error-port)
499 (_ "error: while creating directory `~a': ~a~%")
500 %profile-directory
501 (strerror (system-error-errno args)))
502 (format (current-error-port)
503 (_ "Please create the `~a' directory, with you as the owner.~%")
504 %profile-directory)
505 (exit 1)))))
506
507 (define (process-actions opts)
508 ;; Process any install/remove/upgrade action from OPTS.
509
510 (define dry-run? (assoc-ref opts 'dry-run?))
511 (define verbose? (assoc-ref opts 'verbose?))
512 (define profile (assoc-ref opts 'profile))
513
514 (define (canonicalize-deps deps)
515 ;; Remove duplicate entries from DEPS, a list of propagated inputs,
516 ;; where each input is a name/path tuple.
517 (define (same? d1 d2)
518 (match d1
519 ((_ path1)
520 (match d2
521 ((_ path2)
522 (string=? path1 path2))))))
523
524 (delete-duplicates (map input->name+path deps) same?))
525
526 ;; First roll back if asked to.
527 (if (and (assoc-ref opts 'roll-back?) (not dry-run?))
528 (begin
529 (roll-back profile)
530 (process-actions (alist-delete 'roll-back? opts)))
531 (let* ((installed (manifest-packages (profile-manifest profile)))
532 (upgrade-regexps (filter-map (match-lambda
533 (('upgrade . regexp)
534 (make-regexp regexp))
535 (_ #f))
536 opts))
537 (upgrade (if (null? upgrade-regexps)
538 '()
539 (let ((newest (find-newest-available-packages)))
540 (filter-map (match-lambda
541 ((name version output path _)
542 (and (any (cut regexp-exec <> name)
543 upgrade-regexps)
544 (upgradeable? name version path)
545 (find-package name)))
546 (_ #f))
547 installed))))
548 (install (append
549 upgrade
550 (filter-map (match-lambda
551 (('install . (? store-path?))
552 #f)
553 (('install . package)
554 (find-package package))
555 (_ #f))
556 opts)))
557 (drv (filter-map (match-lambda
558 ((name version sub-drv
559 (? package? package)
560 (deps ...))
561 (package-derivation (%store) package))
562 (_ #f))
563 install))
564 (install* (append
565 (filter-map (match-lambda
566 (('install . (? store-path? path))
567 (let-values (((name version)
568 (package-name->name+version
569 (store-path-package-name
570 path))))
571 `(,name ,version #f ,path ())))
572 (_ #f))
573 opts)
574 (map (lambda (tuple drv)
575 (match tuple
576 ((name version sub-drv _ (deps ...))
577 (let ((output-path
578 (derivation-path->output-path
579 drv sub-drv)))
580 `(,name ,version ,sub-drv ,output-path
581 ,(canonicalize-deps deps))))))
582 install drv)))
583 (remove (filter-map (match-lambda
584 (('remove . package)
585 package)
586 (_ #f))
587 opts))
588 (packages (append install*
589 (fold (lambda (package result)
590 (match package
591 ((name _ ...)
592 (alist-delete name result))))
593 (fold alist-delete installed remove)
594 install*))))
595
596 (when (equal? profile %current-profile)
597 (ensure-default-profile))
598
599 (show-what-to-build drv dry-run?)
600
601 (or dry-run?
602 (and (build-derivations (%store) drv)
603 (let* ((prof-drv (profile-derivation (%store) packages))
604 (prof (derivation-path->output-path prof-drv))
605 (old-drv (profile-derivation
606 (%store) (manifest-packages
607 (profile-manifest profile))))
608 (old-prof (derivation-path->output-path old-drv))
609 (number (profile-number profile))
610
611 ;; Always use NUMBER + 1 for the new profile,
612 ;; possibly overwriting a "previous future
613 ;; generation".
614 (name (format #f "~a-~a-link"
615 profile (+ 1 number))))
616 (if (string=? old-prof prof)
617 (when (or (pair? install) (pair? remove))
618 (format (current-error-port)
619 (_ "nothing to be done~%")))
620 (and (parameterize ((current-build-output-port
621 ;; Output something when Guile
622 ;; needs to be built.
623 (if (or verbose? (guile-missing?))
624 (current-error-port)
625 (%make-void-port "w"))))
626 (build-derivations (%store) (list prof-drv)))
627 (begin
628 (switch-symlinks name prof)
629 (switch-symlinks profile name))))))))))
630
631 (define (process-query opts)
632 ;; Process any query specified by OPTS. Return #t when a query was
633 ;; actually processed, #f otherwise.
634 (let ((profile (assoc-ref opts 'profile)))
635 (match (assoc-ref opts 'query)
636 (('list-installed regexp)
637 (let* ((regexp (and regexp (make-regexp regexp)))
638 (manifest (profile-manifest profile))
639 (installed (manifest-packages manifest)))
640 (for-each (match-lambda
641 ((name version output path _)
642 (when (or (not regexp)
643 (regexp-exec regexp name))
644 (format #t "~a\t~a\t~a\t~a~%"
645 name (or version "?") output path))))
646 installed)
647 #t))
648
649 (('list-available regexp)
650 (let* ((regexp (and regexp (make-regexp regexp)))
651 (available (fold-packages
652 (lambda (p r)
653 (let ((n (package-name p)))
654 (if regexp
655 (if (regexp-exec regexp n)
656 (cons p r)
657 r)
658 (cons p r))))
659 '())))
660 (for-each (lambda (p)
661 (format #t "~a\t~a\t~a\t~a~%"
662 (package-name p)
663 (package-version p)
664 (string-join (package-outputs p) ",")
665 (location->string (package-location p))))
666 (sort available
667 (lambda (p1 p2)
668 (string<? (package-name p1)
669 (package-name p2)))))
670 #t))
671
672 (('search regexp)
673 (let ((regexp (make-regexp regexp regexp/icase)))
674 (for-each (cute package->recutils <> (current-output-port))
675 (find-packages-by-description regexp))
676 #t))
677 (_ #f))))
678
679 (let ((opts (parse-options)))
680 (or (process-query opts)
681 (parameterize ((%store (open-connection)))
682 (with-error-handling
683 (parameterize ((%guile-for-build
684 (package-derivation (%store)
685 (if (assoc-ref opts 'bootstrap?)
686 %bootstrap-guile
687 guile-final))))
688 (process-actions opts)))))))