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 (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")))
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))
209 (cond ((not (file-exists? profile)) ; invalid profile
210 (format (current-error-port)
211 (_ "error: profile `~a' does not exist~%")
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) '()))
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~%")))
226 (switch-symlinks previous-profile prof)
228 (else (switch-link))))) ; anything else
230 (define (find-packages-by-description rx)
231 "Search in SYNOPSIS and DESCRIPTION using RX. Return a list of
233 (define (same-location? p1 p2)
234 ;; Compare locations of two packages.
235 (equal? (package-location p1) (package-location p2)))
239 (fold-packages (lambda (package result)
241 (cut regexp-exec rx <>))
243 (if (or (and=> (package-synopsis package)
244 (compose matches? gettext))
245 (and=> (package-description package)
246 (compose matches? gettext)))
247 (cons package result)
251 (string<? (package-name p1)
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))
260 (loop `(,name ,package "out")))
261 ((name package sub-drv)
262 (let*-values (((_ drv)
263 (package-derivation (%store) package))
265 (derivation-output-path
266 (assoc-ref (derivation-outputs drv) sub-drv))))
271 ;;; Command-line options.
274 (define %default-options
275 ;; Alist of default option values.
276 `((profile . ,%current-profile)))
279 (display (_ "Usage: guix package [OPTION]... PACKAGES...
280 Install, remove, or upgrade PACKAGES in a single transaction.\n"))
282 -i, --install=PACKAGE install PACKAGE"))
284 -r, --remove=PACKAGE remove PACKAGE"))
286 -u, --upgrade=REGEXP upgrade all the installed packages matching REGEXP"))
288 --roll-back roll back to the previous generation"))
291 -p, --profile=PROFILE use PROFILE instead of the user's default profile"))
293 -n, --dry-run show what would be done without actually doing it"))
295 --bootstrap use the bootstrap Guile to build the profile"))
297 --verbose produce verbose output"))
300 -s, --search=REGEXP search in synopsis and description using REGEXP"))
302 -I, --list-installed[=REGEXP]
303 list installed packages matching REGEXP"))
305 -A, --list-available[=REGEXP]
306 list available packages matching REGEXP"))
309 -h, --help display this help and exit"))
311 -V, --version display version information and exit"))
313 (show-bug-report-information))
316 ;; Specification of the command-line options.
317 (list (option '(#\h "help") #f #f
321 (option '(#\V "version") #f #f
323 (show-version-and-exit "guix package")))
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 ""))
354 (option '(#\I "list-installed") #f #t
355 (lambda (opt name arg result)
356 (cons `(query list-installed ,(or arg ""))
358 (option '(#\A "list-available") #f #t
359 (lambda (opt name arg result)
360 (cons `(query list-available ,(or arg ""))
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))
375 (leave (_ "~A: extraneous argument~%") arg))
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))))
383 (define newest-available-packages
384 (memoize find-newest-available-packages))
386 (define (find-best-packages-by-name name version)
388 (find-packages-by-name name version)
389 (match (vhash-assoc name (newest-available-packages))
390 ((_ version pkgs ...) pkgs)
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)
399 (define (ensure-output p sub-drv)
400 (if (member sub-drv (package-outputs p))
402 (leave (_ "~a: error: package `~a' lacks output `~a'~%")
403 (location->string (package-location p))
404 (package-full-name p)
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))))))
413 (package-name->name+version name)))
414 (match (find-best-packages-by-name name version)
416 (list name (package-version p) sub-drv (ensure-output p sub-drv)
417 (package-transitive-propagated-inputs p)))
419 (format (current-error-port)
420 (_ "warning: ambiguous package specification `~a'~%")
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)))
429 (leave (_ "~a: package not found~%") request)))))
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
436 (match (vhash-assoc name (newest-available-packages))
437 ((_ candidate-version pkg . rest)
438 (case (version-compare candidate-version current-version)
441 ((=) (let ((candidate-path (derivation-path->output-path
442 (package-derivation (%store) pkg))))
443 (not (string=? current-path candidate-path))))))
446 (define (ensure-default-profile)
447 ;; Ensure the default profile symlink and directory exist.
449 ;; Create ~/.guix-profile if it doesn't exist yet.
450 (when (and %user-environment-directory
452 (not (false-if-exception
453 (lstat %user-environment-directory))))
454 (symlink %current-profile %user-environment-directory))
456 ;; Attempt to create /…/profiles/per-user/$USER if needed.
457 (unless (directory-exists? %profile-directory)
460 (mkdir-p %profile-directory))
462 ;; Often, we cannot create %PROFILE-DIRECTORY because its
463 ;; parent directory is root-owned and we're running
465 (format (current-error-port)
466 (_ "error: while creating directory `~a': ~a~%")
468 (strerror (system-error-errno args)))
469 (format (current-error-port)
470 (_ "Please create the `~a' directory, with you as the owner.~%")
474 (define (process-actions opts)
475 ;; Process any install/remove/upgrade action from OPTS.
477 (define dry-run? (assoc-ref opts 'dry-run?))
478 (define verbose? (assoc-ref opts 'verbose?))
479 (define profile (assoc-ref opts 'profile))
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)
489 (string=? path1 path2))))))
491 (delete-duplicates (map input->name+path deps) same?))
493 ;; First roll back if asked to.
494 (if (and (assoc-ref opts 'roll-back?) (not dry-run?))
497 (process-actions (alist-delete 'roll-back? opts)))
498 (let* ((installed (manifest-packages (profile-manifest profile)))
499 (upgrade-regexps (filter-map (match-lambda
501 (make-regexp regexp))
504 (upgrade (if (null? upgrade-regexps)
506 (let ((newest (find-newest-available-packages)))
507 (filter-map (match-lambda
508 ((name version output path _)
509 (and (any (cut regexp-exec <> name)
511 (upgradeable? name version path)
512 (find-package name)))
517 (filter-map (match-lambda
518 (('install . (? store-path?))
520 (('install . package)
521 (find-package package))
524 (drv (filter-map (match-lambda
525 ((name version sub-drv
528 (package-derivation (%store) package))
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
538 `(,name ,version #f ,path ())))
541 (map (lambda (tuple drv)
543 ((name version sub-drv _ (deps ...))
545 (derivation-path->output-path
547 `(,name ,version ,sub-drv ,output-path
548 ,(canonicalize-deps deps))))))
550 (remove (filter-map (match-lambda
555 (packages (append install*
556 (fold (lambda (package result)
559 (alist-delete name result))))
560 (fold alist-delete installed remove)
563 (when (equal? profile %current-profile)
564 (ensure-default-profile))
566 (show-what-to-build (%store) drv 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))
578 ;; Always use NUMBER + 1 for the new profile,
579 ;; possibly overwriting a "previous future
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?))
592 (%make-void-port "w"))))
593 (build-derivations (%store) (list prof-drv)))
595 (switch-symlinks name prof)
596 (switch-symlinks profile name))))))))))
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))))
616 (('list-available regexp)
617 (let* ((regexp (and regexp (make-regexp regexp)))
618 (available (fold-packages
620 (let ((n (package-name p)))
622 (if (regexp-exec regexp n)
627 (for-each (lambda (p)
628 (format #t "~a\t~a\t~a\t~a~%"
631 (string-join (package-outputs p) ",")
632 (location->string (package-location p))))
635 (string<? (package-name p1)
636 (package-name p2)))))
640 (let ((regexp (make-regexp regexp regexp/icase)))
641 (for-each (cute package->recutils <> (current-output-port))
642 (find-packages-by-description regexp))
646 (let ((opts (parse-options)))
647 (or (process-query opts)
648 (parameterize ((%store (open-connection)))
650 (parameterize ((%guile-for-build
651 (package-derivation (%store)
652 (if (assoc-ref opts 'bootstrap?)
655 (process-actions opts)))))))