guix build: '--with-commit' makes recursive checkouts.
[jackhill/guix/guix.git] / guix / scripts / package.scm
index 73cbccb..b0c6a7c 100644 (file)
@@ -1,11 +1,12 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
 ;;; Copyright © 2013, 2015 Mark H Weaver <mhw@netris.org>
 ;;; Copyright © 2014, 2016 Alex Kost <alezost@gmail.com>
 ;;; Copyright © 2016 Roel Janssen <roel@gnu.org>
 ;;; Copyright © 2016 Benz Schenk <benz.schenk@uzh.ch>
 ;;; Copyright © 2016 Chris Marusich <cmmarusich@gmail.com>
+;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -24,6 +25,7 @@
 
 (define-module (guix scripts package)
   #:use-module (guix ui)
+  #:use-module ((guix status) #:select (with-status-verbosity))
   #:use-module (guix store)
   #:use-module (guix grafts)
   #:use-module (guix derivations)
@@ -35,7 +37,7 @@
   #:use-module (guix config)
   #:use-module (guix scripts)
   #:use-module (guix scripts build)
-  #:autoload   (guix describe) (current-profile-entries)
+  #:autoload   (guix describe) (package-provenance)
   #:use-module ((guix build utils)
                 #:select (directory-exists? mkdir-p))
   #:use-module (ice-9 format)
@@ -54,6 +56,7 @@
   #:autoload   (gnu packages bootstrap) (%bootstrap-guile)
   #:export (build-and-use-profile
             delete-generations
+            delete-matching-generations
             display-search-paths
             guix-package))
 
 
 (define (ensure-default-profile)
   "Ensure the default profile symlink and directory exist and are writable."
-
-  (define (rtfm)
-    (format (current-error-port)
-            (G_ "Try \"info '(guix) Invoking guix package'\" for \
-more information.~%"))
-    (exit 1))
+  (ensure-profile-directory)
 
   ;; Create ~/.guix-profile if it doesn't exist yet.
   (when (and %user-profile-directory
              %current-profile
              (not (false-if-exception
                    (lstat %user-profile-directory))))
-    (symlink %current-profile %user-profile-directory))
-
-  (let ((s (stat %profile-directory #f)))
-    ;; Attempt to create /…/profiles/per-user/$USER if needed.
-    (unless (and s (eq? 'directory (stat:type s)))
-      (catch 'system-error
-        (lambda ()
-          (mkdir-p %profile-directory))
-        (lambda args
-          ;; Often, we cannot create %PROFILE-DIRECTORY because its
-          ;; parent directory is root-owned and we're running
-          ;; unprivileged.
-          (format (current-error-port)
-                  (G_ "error: while creating directory `~a': ~a~%")
-                  %profile-directory
-                  (strerror (system-error-errno args)))
-          (format (current-error-port)
-                  (G_ "Please create the `~a' directory, with you as the owner.~%")
-                  %profile-directory)
-          (rtfm))))
-
-    ;; Bail out if it's not owned by the user.
-    (unless (or (not s) (= (stat:uid s) (getuid)))
-      (format (current-error-port)
-              (G_ "error: directory `~a' is not owned by you~%")
-              %profile-directory)
-      (format (current-error-port)
-              (G_ "Please change the owner of `~a' to user ~s.~%")
-              %profile-directory (or (getenv "USER")
-                                     (getenv "LOGNAME")
-                                     (getuid)))
-      (rtfm))))
+    (symlink %current-profile %user-profile-directory)))
 
 (define (delete-generations store profile generations)
   "Delete GENERATIONS from PROFILE.
@@ -155,21 +122,21 @@ denote ranges as interpreted by 'matching-generations'."
 
 (define* (build-and-use-profile store profile manifest
                                 #:key
+                                (hooks %default-profile-hooks)
                                 allow-collisions?
                                 bootstrap? use-substitutes?
                                 dry-run?)
   "Build a new generation of PROFILE, a file name, using the packages
 specified in MANIFEST, a manifest object.  When ALLOW-COLLISIONS? is true,
-do not treat collisions in MANIFEST as an error."
+do not treat collisions in MANIFEST as an error.  HOOKS is a list of \"profile
+hooks\" run when building the profile."
   (when (equal? profile %current-profile)
     (ensure-default-profile))
 
   (let* ((prof-drv (run-with-store store
                      (profile-derivation manifest
                                          #:allow-collisions? allow-collisions?
-                                         #:hooks (if bootstrap?
-                                                     '()
-                                                     %default-profile-hooks)
+                                         #:hooks (if bootstrap? '() hooks)
                                          #:locales? (not bootstrap?))))
          (prof     (derivation->output-path prof-drv)))
     (show-what-to-build store (list prof-drv)
@@ -199,7 +166,9 @@ do not treat collisions in MANIFEST as an error."
                               count)
                        count)
                (display-search-paths entries (list profile)
-                                     #:kind 'prefix))))))))
+                                     #:kind 'prefix)))
+
+        (warn-about-disk-space profile))))))
 
 \f
 ;;;
@@ -253,31 +222,32 @@ of relevance scores."
     ('dismiss
      transaction)
     (($ <manifest-entry> name version output (? string? path))
-     (match (vhash-assoc name (find-newest-available-packages))
-       ((_ candidate-version pkg . rest)
-        (match (package-superseded pkg)
-          ((? package? new)
-           (supersede entry new))
-          (#f
-           (case (version-compare candidate-version version)
-             ((>)
-              (manifest-transaction-install-entry
-               (package->manifest-entry* pkg output)
-               transaction))
-             ((<)
-              transaction)
-             ((=)
-              (let ((candidate-path (derivation->output-path
-                                     (package-derivation (%store) pkg))))
-                ;; XXX: When there are propagated inputs, assume we need to
-                ;; upgrade the whole entry.
-                (if (and (string=? path candidate-path)
-                         (null? (package-propagated-inputs pkg)))
-                    transaction
-                    (manifest-transaction-install-entry
-                     (package->manifest-entry* pkg output)
-                     transaction))))))))
-       (#f
+     (match (find-best-packages-by-name name #f)
+       ((pkg . rest)
+        (let ((candidate-version (package-version pkg)))
+          (match (package-superseded pkg)
+            ((? package? new)
+             (supersede entry new))
+            (#f
+             (case (version-compare candidate-version version)
+               ((>)
+                (manifest-transaction-install-entry
+                 (package->manifest-entry* pkg output)
+                 transaction))
+               ((<)
+                transaction)
+               ((=)
+                (let ((candidate-path (derivation->output-path
+                                       (package-derivation (%store) pkg))))
+                  ;; XXX: When there are propagated inputs, assume we need to
+                  ;; upgrade the whole entry.
+                  (if (and (string=? path candidate-path)
+                           (null? (package-propagated-inputs pkg)))
+                      transaction
+                      (manifest-transaction-install-entry
+                       (package->manifest-entry* pkg output)
+                       transaction)))))))))
+       (()
         (warning (G_ "package '~a' no longer exists~%") name)
         transaction)))))
 
@@ -326,11 +296,14 @@ ENTRIES, a list of manifest entries, in the context of PROFILE."
 
 (define %default-options
   ;; Alist of default option values.
-  `((verbosity . 0)
+  `((verbosity . 1)
+    (debug . 0)
     (graft? . #t)
     (substitutes? . #t)
     (build-hook? . #t)
-    (print-build-trace? . #t)))
+    (print-build-trace? . #t)
+    (print-extended-build-trace? . #t)
+    (multiplexed-build-output? . #t)))
 
 (define (show-help)
   (display (G_ "Usage: guix package [OPTION]...
@@ -377,7 +350,7 @@ Install, remove, or upgrade packages in a single transaction.\n"))
   (display (G_ "
       --bootstrap        use the bootstrap Guile to build the profile"))
   (display (G_ "
-      --verbose          produce verbose output"))
+  -v, --verbosity=LEVEL  use the given verbosity LEVEL"))
   (newline)
   (display (G_ "
   -s, --search=REGEXP    search in synopsis and description using REGEXP"))
@@ -503,13 +476,21 @@ kind of search path~%")
                    (values (alist-cons 'dry-run? #t
                                        (alist-cons 'graft? #f result))
                            #f)))
+         (option '(#\v "verbosity") #t #f
+                 (lambda (opt name arg result arg-handler)
+                   (let ((level (string->number* arg)))
+                     (values (alist-cons 'verbosity level
+                                         (alist-delete 'verbosity result))
+                             #f))))
          (option '("bootstrap") #f #f
                  (lambda (opt name arg result arg-handler)
                    (values (alist-cons 'bootstrap? #t result)
                            #f)))
-         (option '("verbose") #f #f
+         (option '("verbose") #f #f               ;deprecated
                  (lambda (opt name arg result arg-handler)
-                   (values (alist-cons 'verbose? #t result)
+                   (values (alist-cons 'verbosity 2
+                                       (alist-delete 'verbosity
+                                                     result))
                            #f)))
          (option '("allow-collisions") #f #f
                  (lambda (opt name arg result arg-handler)
@@ -546,14 +527,14 @@ upgrading, #f otherwise."
   (define upgrade-regexps
     (filter-map (match-lambda
                   (('upgrade . regexp)
-                   (make-regexp* (or regexp "")))
+                   (make-regexp* (or regexp "") regexp/icase))
                   (_ #f))
                 opts))
 
   (define do-not-upgrade-regexps
     (filter-map (match-lambda
                   (('do-not-upgrade . regexp)
-                   (make-regexp* regexp))
+                   (make-regexp* regexp regexp/icase))
                   (_ #f))
                 opts))
 
@@ -572,40 +553,6 @@ upgrading, #f otherwise."
       (output "out")                              ;XXX: wild guess
       (item item))))
 
-(define (package-provenance package)
-  "Return the provenance of PACKAGE as an sexp for use as the 'provenance'
-property of manifest entries, or #f if it could not be determined."
-  (define (entry-source entry)
-    (match (assq 'source
-                 (manifest-entry-properties entry))
-      (('source value) value)
-      (_ #f)))
-
-  (match (and=> (package-location package) location-file)
-    (#f #f)
-    (file
-     (let ((file (if (string-prefix? "/" file)
-                     file
-                     (search-path %load-path file))))
-       (and file
-            (string-prefix? (%store-prefix) file)
-
-            ;; Always store information about the 'guix' channel and
-            ;; optionally about the specific channel FILE comes from.
-            (or (let ((main  (and=> (find (lambda (entry)
-                                            (string=? "guix"
-                                                      (manifest-entry-name entry)))
-                                          (current-profile-entries))
-                                    entry-source))
-                      (extra (any (lambda (entry)
-                                    (let ((item (manifest-entry-item entry)))
-                                      (and (string-prefix? item file)
-                                           (entry-source entry))))
-                                  (current-profile-entries))))
-                  (and main
-                       `(,main
-                         ,@(if extra (list extra) '()))))))))))
-
 (define (package->manifest-entry* package output)
   "Like 'package->manifest-entry', but attach PACKAGE provenance meta-data to
 the resulting manifest entry."
@@ -626,12 +573,12 @@ and upgrades."
     (options->upgrade-predicate opts))
 
   (define upgraded
-    (fold-right (lambda (entry transaction)
-                  (if (upgrade? (manifest-entry-name entry))
-                      (transaction-upgrade-entry entry transaction)
-                      transaction))
-                transaction
-                (manifest-entries manifest)))
+    (fold (lambda (entry transaction)
+            (if (upgrade? (manifest-entry-name entry))
+                (transaction-upgrade-entry entry transaction)
+                transaction))
+          transaction
+          (manifest-entries manifest)))
 
   (define to-install
     (filter-map (match-lambda
@@ -740,7 +687,7 @@ processed, #f otherwise."
        #t)
 
       (('list-installed regexp)
-       (let* ((regexp    (and regexp (make-regexp* regexp)))
+       (let* ((regexp    (and regexp (make-regexp* regexp regexp/icase)))
               (manifest  (profile-manifest profile))
               (installed (manifest-entries manifest)))
          (leave-on-EPIPE
@@ -756,30 +703,35 @@ processed, #f otherwise."
          #t))
 
       (('list-available regexp)
-       (let* ((regexp    (and regexp (make-regexp* regexp)))
-              (available (fold-packages
-                          (lambda (p r)
-                            (let ((n (package-name p)))
-                              (if (and (supported-package? p)
-                                       (not (package-superseded p)))
-                                  (if regexp
-                                      (if (regexp-exec regexp n)
-                                          (cons p r)
-                                          r)
-                                      (cons p r))
-                                  r)))
+       (let* ((regexp    (and regexp (make-regexp* regexp regexp/icase)))
+              (available (fold-available-packages
+                          (lambda* (name version result
+                                         #:key outputs location
+                                         supported? deprecated?
+                                         #:allow-other-keys)
+                            (if (and supported? (not deprecated?))
+                                (if regexp
+                                    (if (regexp-exec regexp name)
+                                        (cons `(,name ,version
+                                                      ,outputs ,location)
+                                              result)
+                                        result)
+                                    (cons `(,name ,version
+                                                  ,outputs ,location)
+                                          result))
+                                result))
                           '())))
          (leave-on-EPIPE
-          (for-each (lambda (p)
-                      (format #t "~a\t~a\t~a\t~a~%"
-                              (package-name p)
-                              (package-version p)
-                              (string-join (package-outputs p) ",")
-                              (location->string (package-location p))))
+          (for-each (match-lambda
+                      ((name version outputs location)
+                       (format #t "~a\t~a\t~a\t~a~%"
+                               name version
+                               (string-join outputs ",")
+                               (location->string location))))
                     (sort available
-                          (lambda (p1 p2)
-                            (string<? (package-name p1)
-                                      (package-name p2))))))
+                          (match-lambda*
+                            (((name1 . _) (name2 . _))
+                             (string<? name1 name2))))))
          #t))
 
       (('search _)
@@ -802,9 +754,13 @@ processed, #f otherwise."
       (('show requested-name)
        (let-values (((name version)
                      (package-name->name+version requested-name)))
-         (leave-on-EPIPE
-          (for-each (cute package->recutils <> (current-output-port))
-                    (find-packages-by-name name version)))
+         (match (find-packages-by-name name version)
+           (()
+            (leave (G_ "~a~@[@~a~]: package not found~%") name version))
+           (packages
+            (leave-on-EPIPE
+             (for-each (cute package->recutils <> (current-output-port))
+                       packages))))
          #t))
 
       (('search-paths kind)
@@ -934,21 +890,17 @@ processed, #f otherwise."
   (define opts
     (parse-command-line args %options (list %default-options #f)
                         #:argument-handler handle-argument))
-  (define verbose?
-    (assoc-ref opts 'verbose?))
 
   (with-error-handling
     (or (process-query opts)
         (parameterize ((%store  (open-connection))
                        (%graft? (assoc-ref opts 'graft?)))
-          (set-build-options-from-command-line (%store) opts)
-
-          (parameterize ((%guile-for-build
-                          (package-derivation
-                           (%store)
-                           (if (assoc-ref opts 'bootstrap?)
-                               %bootstrap-guile
-                               (canonical-package guile-2.2))))
-                         (current-build-output-port
-                          (build-output-port #:verbose? verbose?)))
-            (process-actions (%store) opts))))))
+          (with-status-verbosity (assoc-ref opts 'verbosity)
+            (set-build-options-from-command-line (%store) opts)
+            (parameterize ((%guile-for-build
+                            (package-derivation
+                             (%store)
+                             (if (assoc-ref opts 'bootstrap?)
+                                 %bootstrap-guile
+                                 (canonical-package guile-2.2)))))
+              (process-actions (%store) opts)))))))