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>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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))
53 (define %user-environment-directory
54 (and=> (getenv "HOME")
55 (cut string-append <> "/.guix-profile")))
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/" <>))
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"))
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 ())))))
75 (define (manifest-packages manifest)
76 "Return the packages listed in MANIFEST."
78 (('manifest ('version 0)
79 ('packages ((name version output path) ...)))
80 (zip name version output path
81 (make-list (length name) '())))
83 ;; Version 1 adds a list of propagated inputs to the
84 ;; name/version/output/path tuples.
85 (('manifest ('version 1)
86 ('packages (packages ...)))
90 (error "unsupported manifest format" manifest))))
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))
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)))
106 (define (visit basename result)
107 (if (select? basename)
108 (cons basename result)
111 (define (leaf name stat result)
113 (visit (basename name) result)))
115 (define (down name stat result)
118 (define (up name stat result)
121 (define (skip name stat result)
122 ;; All the sub-directories are skipped.
123 (visit (basename name) result))
125 (define (error name* stat errno result)
126 (if (string=? name name*) ; top-level NAME is unreadable
128 (visit (basename name*) result)))
130 (and=> (file-system-fold enter? leaf down up skip error #f name lstat)
132 (sort files entry<?))))
134 (match (scandir (dirname profile)
135 (cute regexp-exec (profile-regexp profile) <>))
136 (#f ; no profile directory
140 ((profiles ...) ; former profiles around
141 (map (compose string->number
142 (cut match:substring <> 1)
143 (cute regexp-exec (profile-regexp profile) <>))
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))
155 (profile-numbers profile)))
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."
162 (use-modules (ice-9 pretty-print)
165 (setvbuf (current-output-port) _IOLBF)
166 (setvbuf (current-error-port) _IOLBF)
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")
175 (pretty-print '(manifest (version 1)
176 (packages ,packages))
179 (build-expression->derivation store "user-environment"
182 (append-map (match-lambda
183 ((name version output path deps)
187 #:modules '((guix build union))))
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)))
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")))
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))
210 (cond ((not (file-exists? profile)) ; invalid profile
211 (format (current-error-port)
212 (_ "error: profile `~a' does not exist~%")
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) '()))
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~%")))
227 (switch-symlinks previous-profile prof)
229 (else (switch-link))))) ; anything else
231 (define (find-packages-by-description rx)
232 "Search in SYNOPSIS and DESCRIPTION using RX. Return a list of
234 (define (same-location? p1 p2)
235 ;; Compare locations of two packages.
236 (equal? (package-location p1) (package-location p2)))
240 (fold-packages (lambda (package result)
242 (cut regexp-exec rx <>))
244 (if (or (and=> (package-synopsis package)
245 (compose matches? gettext))
246 (and=> (package-description package)
247 (compose matches? gettext)))
248 (cons package result)
252 (string<? (package-name p1)
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))
261 (loop `(,name ,package "out")))
262 ((name package sub-drv)
263 (let*-values (((_ drv)
264 (package-derivation (%store) package))
266 (derivation-output-path
267 (assoc-ref (derivation-outputs drv) sub-drv))))
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))
278 (display #\cr (current-error-port))
279 (display blank (current-error-port))
280 (display #\cr (current-error-port))
281 (force-output (current-error-port))
284 (define (check-package-freshness package)
285 "Check whether PACKAGE has a newer version available upstream, and report
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)))
304 ;;; Command-line options.
307 (define %default-options
308 ;; Alist of default option values.
309 `((profile . ,%current-profile)))
312 (display (_ "Usage: guix package [OPTION]... PACKAGES...
313 Install, remove, or upgrade PACKAGES in a single transaction.\n"))
315 -i, --install=PACKAGE install PACKAGE"))
317 -e, --install-from-expression=EXP
318 install the package EXP evaluates to"))
320 -r, --remove=PACKAGE remove PACKAGE"))
322 -u, --upgrade=REGEXP upgrade all the installed packages matching REGEXP"))
324 --roll-back roll back to the previous generation"))
327 -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
329 -n, --dry-run show what would be done without actually doing it"))
331 --bootstrap use the bootstrap Guile to build the profile"))
333 --verbose produce verbose output"))
336 -s, --search=REGEXP search in synopsis and description using REGEXP"))
338 -I, --list-installed[=REGEXP]
339 list installed packages matching REGEXP"))
341 -A, --list-available[=REGEXP]
342 list available packages matching REGEXP"))
345 -h, --help display this help and exit"))
347 -V, --version display version information and exit"))
349 (show-bug-report-information))
352 ;; Specification of the command-line options.
353 (list (option '(#\h "help") #f #f
357 (option '(#\V "version") #f #f
359 (show-version-and-exit "guix package")))
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)
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 ""))
394 (option '(#\I "list-installed") #f #t
395 (lambda (opt name arg result)
396 (cons `(query list-installed ,(or arg ""))
398 (option '(#\A "list-available") #f #t
399 (lambda (opt name arg result)
400 (cons `(query list-available ,(or arg ""))
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))
415 (leave (_ "~A: extraneous argument~%") arg))
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))))
423 (define newest-available-packages
424 (memoize find-newest-available-packages))
426 (define (find-best-packages-by-name name version)
428 (find-packages-by-name name version)
429 (match (vhash-assoc name (newest-available-packages))
430 ((_ version pkgs ...) pkgs)
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)
439 (define (ensure-output p sub-drv)
440 (if (member sub-drv (package-outputs p))
442 (leave (_ "~a: error: package `~a' lacks output `~a'~%")
443 (location->string (package-location p))
444 (package-full-name p)
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))))))
453 (package-name->name+version name)))
454 (match (find-best-packages-by-name name version)
456 (list name (package-version p) sub-drv (ensure-output p sub-drv)
457 (package-transitive-propagated-inputs p)))
459 (format (current-error-port)
460 (_ "warning: ambiguous package specification `~a'~%")
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)))
469 (leave (_ "~a: package not found~%") request)))))
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
476 (match (vhash-assoc name (newest-available-packages))
477 ((_ candidate-version pkg . rest)
478 (case (version-compare candidate-version current-version)
481 ((=) (let ((candidate-path (derivation-path->output-path
482 (package-derivation (%store) pkg))))
483 (not (string=? current-path candidate-path))))))
486 (define (ensure-default-profile)
487 ;; Ensure the default profile symlink and directory exist.
489 ;; Create ~/.guix-profile if it doesn't exist yet.
490 (when (and %user-environment-directory
492 (not (false-if-exception
493 (lstat %user-environment-directory))))
494 (symlink %current-profile %user-environment-directory))
496 ;; Attempt to create /…/profiles/per-user/$USER if needed.
497 (unless (directory-exists? %profile-directory)
500 (mkdir-p %profile-directory))
502 ;; Often, we cannot create %PROFILE-DIRECTORY because its
503 ;; parent directory is root-owned and we're running
505 (format (current-error-port)
506 (_ "error: while creating directory `~a': ~a~%")
508 (strerror (system-error-errno args)))
509 (format (current-error-port)
510 (_ "Please create the `~a' directory, with you as the owner.~%")
514 (define (process-actions opts)
515 ;; Process any install/remove/upgrade action from OPTS.
517 (define dry-run? (assoc-ref opts 'dry-run?))
518 (define verbose? (assoc-ref opts 'verbose?))
519 (define profile (assoc-ref opts 'profile))
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)
529 (string=? path1 path2))))))
531 (delete-duplicates (map input->name+path deps) same?))
533 (define (package->tuple p)
534 (let ((path (package-derivation (%store) p))
535 (deps (package-transitive-propagated-inputs p)))
539 ;; When given a package via `-e', install the first of its
541 ,(car (package-outputs p))
544 ,(canonicalize-deps deps))))
546 ;; First roll back if asked to.
547 (if (and (assoc-ref opts 'roll-back?) (not dry-run?))
550 (process-actions (alist-delete 'roll-back? opts)))
551 (let* ((installed (manifest-packages (profile-manifest profile)))
552 (upgrade-regexps (filter-map (match-lambda
554 (make-regexp regexp))
557 (upgrade (if (null? upgrade-regexps)
559 (let ((newest (find-newest-available-packages)))
560 (filter-map (match-lambda
561 ((name version output path _)
562 (and (any (cut regexp-exec <> name)
564 (upgradeable? name version path)
565 (find-package name)))
570 (filter-map (match-lambda
571 (('install . (? package? p))
573 (('install . (? store-path?))
575 (('install . package)
576 (find-package package))
579 (drv (filter-map (match-lambda
580 ((name version sub-drv
583 (check-package-freshness package)
584 (package-derivation (%store) package))
588 (filter-map (match-lambda
589 (('install . (? package? p))
591 (('install . (? store-path? path))
592 (let-values (((name version)
593 (package-name->name+version
594 (store-path-package-name
596 `(,name ,version #f ,path ())))
599 (map (lambda (tuple drv)
601 ((name version sub-drv _ (deps ...))
603 (derivation-path->output-path
605 `(,name ,version ,sub-drv ,output-path
606 ,(canonicalize-deps deps))))))
608 (remove (filter-map (match-lambda
613 (packages (append install*
614 (fold (lambda (package result)
617 (alist-delete name result))))
618 (fold alist-delete installed remove)
621 (when (equal? profile %current-profile)
622 (ensure-default-profile))
624 (show-what-to-build (%store) drv 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))
636 ;; Always use NUMBER + 1 for the new profile,
637 ;; possibly overwriting a "previous future
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?))
650 (%make-void-port "w"))))
651 (build-derivations (%store) (list prof-drv)))
653 (switch-symlinks name prof)
654 (switch-symlinks profile name))))))))))
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))))
674 (('list-available regexp)
675 (let* ((regexp (and regexp (make-regexp regexp)))
676 (available (fold-packages
678 (let ((n (package-name p)))
680 (if (regexp-exec regexp n)
685 (for-each (lambda (p)
686 (format #t "~a\t~a\t~a\t~a~%"
689 (string-join (package-outputs p) ",")
690 (location->string (package-location p))))
693 (string<? (package-name p1)
694 (package-name p2)))))
698 (let ((regexp (make-regexp regexp regexp/icase)))
699 (for-each (cute package->recutils <> (current-output-port))
700 (find-packages-by-description regexp))
704 (let ((opts (parse-options)))
705 (or (process-query opts)
706 (parameterize ((%store (open-connection)))
708 (parameterize ((%guile-for-build
709 (package-derivation (%store)
710 (if (assoc-ref opts 'bootstrap?)
713 (process-actions opts)))))))