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 #:export (guix-package))
52 (define %user-environment-directory
53 (and=> (getenv "HOME")
54 (cut string-append <> "/.guix-profile")))
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/" <>))
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"))
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 ())))))
74 (define (manifest-packages manifest)
75 "Return the packages listed in MANIFEST."
77 (('manifest ('version 0)
78 ('packages ((name version output path) ...)))
79 (zip name version output path
80 (make-list (length name) '())))
82 ;; Version 1 adds a list of propagated inputs to the
83 ;; name/version/output/path tuples.
84 (('manifest ('version 1)
85 ('packages (packages ...)))
89 (error "unsupported manifest format" manifest))))
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))
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)))
105 (define (visit basename result)
106 (if (select? basename)
107 (cons basename result)
110 (define (leaf name stat result)
112 (visit (basename name) result)))
114 (define (down name stat result)
117 (define (up name stat result)
120 (define (skip name stat result)
121 ;; All the sub-directories are skipped.
122 (visit (basename name) result))
124 (define (error name* stat errno result)
125 (if (string=? name name*) ; top-level NAME is unreadable
127 (visit (basename name*) result)))
129 (and=> (file-system-fold enter? leaf down up skip error #f name lstat)
131 (sort files entry<?))))
133 (match (scandir (dirname profile)
134 (cute regexp-exec (profile-regexp profile) <>))
135 (#f ; no profile directory
139 ((profiles ...) ; former profiles around
140 (map (compose string->number
141 (cut match:substring <> 1)
142 (cute regexp-exec (profile-regexp profile) <>))
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))
154 (profile-numbers profile)))
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."
161 (use-modules (ice-9 pretty-print)
164 (setvbuf (current-output-port) _IOLBF)
165 (setvbuf (current-error-port) _IOLBF)
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")
174 (pretty-print '(manifest (version 1)
175 (packages ,packages))
178 (build-expression->derivation store "user-environment"
181 (append-map (match-lambda
182 ((name version output path deps)
186 #:modules '((guix build union))))
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)))
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)))
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")))
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))
216 (cond ((not (file-exists? profile)) ; invalid profile
217 (format (current-error-port)
218 (_ "error: profile `~a' does not exist~%")
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) '()))
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~%")))
233 (switch-symlinks previous-profile prof)
235 (else (switch-link))))) ; anything else
237 (define (find-packages-by-description rx)
238 "Search in SYNOPSIS and DESCRIPTION using RX. Return a list of
240 (define (same-location? p1 p2)
241 ;; Compare locations of two packages.
242 (equal? (package-location p1) (package-location p2)))
246 (fold-packages (lambda (package result)
248 (cut regexp-exec rx <>))
250 (if (or (and=> (package-synopsis package)
251 (compose matches? gettext))
252 (and=> (package-description package)
253 (compose matches? gettext)))
254 (cons package result)
258 (string<? (package-name p1)
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))
267 (loop `(,name ,package "out")))
268 ((name package sub-drv)
269 (let*-values (((_ drv)
270 (package-derivation (%store) package))
272 (derivation-output-path
273 (assoc-ref (derivation-outputs drv) sub-drv))))
278 ;;; Command-line options.
281 (define %default-options
282 ;; Alist of default option values.
283 `((profile . ,%current-profile)))
286 (display (_ "Usage: guix package [OPTION]... PACKAGES...
287 Install, remove, or upgrade PACKAGES in a single transaction.\n"))
289 -i, --install=PACKAGE install PACKAGE"))
291 -r, --remove=PACKAGE remove PACKAGE"))
293 -u, --upgrade=REGEXP upgrade all the installed packages matching REGEXP"))
295 --roll-back roll back to the previous generation"))
298 -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
300 -n, --dry-run show what would be done without actually doing it"))
302 --bootstrap use the bootstrap Guile to build the profile"))
304 --verbose produce verbose output"))
307 -s, --search=REGEXP search in synopsis and description using REGEXP"))
309 -I, --list-installed[=REGEXP]
310 list installed packages matching REGEXP"))
312 -A, --list-available[=REGEXP]
313 list available packages matching REGEXP"))
316 -h, --help display this help and exit"))
318 -V, --version display version information and exit"))
320 (show-bug-report-information))
323 ;; Specification of the command-line options.
324 (list (option '(#\h "help") #f #f
328 (option '(#\V "version") #f #f
330 (show-version-and-exit "guix package")))
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 ""))
361 (option '(#\I "list-installed") #f #t
362 (lambda (opt name arg result)
363 (cons `(query list-installed ,(or arg ""))
365 (option '(#\A "list-available") #f #t
366 (lambda (opt name arg result)
367 (cons `(query list-available ,(or arg ""))
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))
382 (leave (_ "~A: extraneous argument~%") arg))
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))))
390 (define (show-what-to-build drv dry-run?)
391 ;; Show what will/would be built in realizing the derivations listed
393 (let* ((req (append-map (lambda (drv-path)
394 (let ((d (call-with-input-file drv-path
396 (derivation-prerequisites-to-build
399 (req* (delete-duplicates
400 (append (remove (compose (cute valid-path? (%store) <>)
401 derivation-path->output-path)
403 (map derivation-input-path req)))))
405 (format (current-error-port)
406 (N_ "~:[the following derivation would be built:~%~{ ~a~%~}~;~]"
407 "~:[the following derivations would be built:~%~{ ~a~%~}~;~]"
410 (format (current-error-port)
411 (N_ "~:[the following derivation will be built:~%~{ ~a~%~}~;~]"
412 "~:[the following derivations will be built:~%~{ ~a~%~}~;~]"
414 (null? req*) req*))))
416 (define newest-available-packages
417 (memoize find-newest-available-packages))
419 (define (find-best-packages-by-name name version)
421 (find-packages-by-name name version)
422 (match (vhash-assoc name (newest-available-packages))
423 ((_ version pkgs ...) pkgs)
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)
432 (define (ensure-output p sub-drv)
433 (if (member sub-drv (package-outputs p))
435 (leave (_ "~a: error: package `~a' lacks output `~a'~%")
436 (location->string (package-location p))
437 (package-full-name p)
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))))))
446 (package-name->name+version name)))
447 (match (find-best-packages-by-name name version)
449 (list name (package-version p) sub-drv (ensure-output p sub-drv)
450 (package-transitive-propagated-inputs p)))
452 (format (current-error-port)
453 (_ "warning: ambiguous package specification `~a'~%")
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)))
462 (leave (_ "~a: package not found~%") request)))))
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
469 (match (vhash-assoc name (newest-available-packages))
470 ((_ candidate-version pkg . rest)
471 (case (version-compare candidate-version current-version)
474 ((=) (let ((candidate-path (derivation-path->output-path
475 (package-derivation (%store) pkg))))
476 (not (string=? current-path candidate-path))))))
479 (define (ensure-default-profile)
480 ;; Ensure the default profile symlink and directory exist.
482 ;; Create ~/.guix-profile if it doesn't exist yet.
483 (when (and %user-environment-directory
485 (not (false-if-exception
486 (lstat %user-environment-directory))))
487 (symlink %current-profile %user-environment-directory))
489 ;; Attempt to create /…/profiles/per-user/$USER if needed.
490 (unless (directory-exists? %profile-directory)
493 (mkdir-p %profile-directory))
495 ;; Often, we cannot create %PROFILE-DIRECTORY because its
496 ;; parent directory is root-owned and we're running
498 (format (current-error-port)
499 (_ "error: while creating directory `~a': ~a~%")
501 (strerror (system-error-errno args)))
502 (format (current-error-port)
503 (_ "Please create the `~a' directory, with you as the owner.~%")
507 (define (process-actions opts)
508 ;; Process any install/remove/upgrade action from OPTS.
510 (define dry-run? (assoc-ref opts 'dry-run?))
511 (define verbose? (assoc-ref opts 'verbose?))
512 (define profile (assoc-ref opts 'profile))
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)
522 (string=? path1 path2))))))
524 (delete-duplicates (map input->name+path deps) same?))
526 ;; First roll back if asked to.
527 (if (and (assoc-ref opts 'roll-back?) (not dry-run?))
530 (process-actions (alist-delete 'roll-back? opts)))
531 (let* ((installed (manifest-packages (profile-manifest profile)))
532 (upgrade-regexps (filter-map (match-lambda
534 (make-regexp regexp))
537 (upgrade (if (null? upgrade-regexps)
539 (let ((newest (find-newest-available-packages)))
540 (filter-map (match-lambda
541 ((name version output path _)
542 (and (any (cut regexp-exec <> name)
544 (upgradeable? name version path)
545 (find-package name)))
550 (filter-map (match-lambda
551 (('install . (? store-path?))
553 (('install . package)
554 (find-package package))
557 (drv (filter-map (match-lambda
558 ((name version sub-drv
561 (package-derivation (%store) package))
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
571 `(,name ,version #f ,path ())))
574 (map (lambda (tuple drv)
576 ((name version sub-drv _ (deps ...))
578 (derivation-path->output-path
580 `(,name ,version ,sub-drv ,output-path
581 ,(canonicalize-deps deps))))))
583 (remove (filter-map (match-lambda
588 (packages (append install*
589 (fold (lambda (package result)
592 (alist-delete name result))))
593 (fold alist-delete installed remove)
596 (when (equal? profile %current-profile)
597 (ensure-default-profile))
599 (show-what-to-build drv 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))
611 ;; Always use NUMBER + 1 for the new profile,
612 ;; possibly overwriting a "previous future
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?))
625 (%make-void-port "w"))))
626 (build-derivations (%store) (list prof-drv)))
628 (switch-symlinks name prof)
629 (switch-symlinks profile name))))))))))
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))))
649 (('list-available regexp)
650 (let* ((regexp (and regexp (make-regexp regexp)))
651 (available (fold-packages
653 (let ((n (package-name p)))
655 (if (regexp-exec regexp n)
660 (for-each (lambda (p)
661 (format #t "~a\t~a\t~a\t~a~%"
664 (string-join (package-outputs p) ",")
665 (location->string (package-location p))))
668 (string<? (package-name p1)
669 (package-name p2)))))
673 (let ((regexp (make-regexp regexp regexp/icase)))
674 (for-each (cute package->recutils <> (current-output-port))
675 (find-packages-by-description regexp))
679 (let ((opts (parse-options)))
680 (or (process-query opts)
681 (parameterize ((%store (open-connection)))
683 (parameterize ((%guile-for-build
684 (package-derivation (%store)
685 (if (assoc-ref opts 'bootstrap?)
688 (process-actions opts)))))))