system: Add /etc/ssl symlink; set needed variables in /etc/profile.
[jackhill/guix/guix.git] / emacs / guix-base.el
index 8da7835..5129c87 100644 (file)
 ;;; Code:
 
 (require 'cl-lib)
+(require 'guix-profiles)
 (require 'guix-backend)
 (require 'guix-utils)
-
-\f
-;;; Profiles
-
-(defvar guix-user-profile
-  (expand-file-name "~/.guix-profile")
-  "User profile.")
-
-(defvar guix-default-profile
-  (concat (or (getenv "NIX_STATE_DIR") "/var/guix")
-          "/profiles/per-user/"
-          (getenv "USER")
-          "/guix-profile")
-  "Default Guix profile.")
-
-(defvar guix-current-profile guix-default-profile
-  "Current profile.")
-
-(defun guix-set-current-profile (path)
-  "Set `guix-current-profile' to PATH.
-Interactively, prompt for PATH.  With prefix, use
-`guix-default-profile'."
-  (interactive
-   (list (if current-prefix-arg
-             guix-default-profile
-           (read-file-name "Set profile: "
-                           (file-name-directory guix-current-profile)))))
-  (let ((path (directory-file-name (expand-file-name path))))
-    (setq guix-current-profile
-          (if (string= path guix-user-profile)
-              guix-default-profile
-            path))
-    (message "Current profile has been set to '%s'."
-             guix-current-profile)))
+(require 'guix-history)
+(require 'guix-messages)
 
 \f
 ;;; Parameters of the entries
@@ -73,6 +42,7 @@ Interactively, prompt for PATH.  With prefix, use
      (id                . "ID")
      (name              . "Name")
      (version           . "Version")
+     (source            . "Source")
      (license           . "License")
      (synopsis          . "Synopsis")
      (description       . "Description")
@@ -91,6 +61,7 @@ Interactively, prompt for PATH.  With prefix, use
      (id                . "ID")
      (name              . "Name")
      (version           . "Version")
+     (source            . "Source")
      (license           . "License")
      (synopsis          . "Synopsis")
      (description       . "Description")
@@ -107,6 +78,7 @@ Interactively, prompt for PATH.  With prefix, use
      (id                . "ID")
      (number            . "Number")
      (prev-number       . "Previous number")
+     (current           . "Current")
      (path              . "Path")
      (time              . "Time")))
   "List for defining titles of entry parameters.
@@ -134,6 +106,17 @@ Each element of the list has a form:
                       (guix-get-key-val entry 'version)
                       output))
 
+(defun guix-entry-to-specification (entry)
+  "Return name specification by the package or output ENTRY."
+  (guix-get-name-spec (guix-get-key-val entry 'name)
+                      (guix-get-key-val entry 'version)
+                      (guix-get-key-val entry 'output)))
+
+(defun guix-entries-to-specifications (entries)
+  "Return name specifications by the package or output ENTRIES."
+  (cl-remove-duplicates (mapcar #'guix-entry-to-specification entries)
+                        :test #'string=))
+
 (defun guix-get-installed-outputs (entry)
   "Return list of installed outputs for the package ENTRY."
   (mapcar (lambda (installed-entry)
@@ -190,8 +173,124 @@ If PATH is relative, it is considered to be relative to
       (recenter 1))))
 
 \f
+;;; Buffers and auto updating.
+
+(defcustom guix-update-after-operation 'current
+  "Define what information to update after executing an operation.
+
+After successful executing an operation in the Guix REPL (for
+example after installing a package), information in Guix buffers
+will or will not be automatically updated depending on a value of
+this variable.
+
+If nil, update nothing (do not revert any buffer).
+If `current', update the buffer from which an operation was performed.
+If `all', update all Guix buffers (not recommended)."
+  :type '(choice (const :tag "Do nothing" nil)
+                 (const :tag "Update operation buffer" current)
+                 (const :tag "Update all Guix buffers" all))
+  :group 'guix)
+
+(defcustom guix-buffer-name-function #'guix-buffer-name-default
+  "Function used to define name of a buffer for displaying information.
+The function is called with 4 arguments: PROFILE, BUFFER-TYPE,
+ENTRY-TYPE, SEARCH-TYPE.  See `guix-get-entries' for the meaning
+of the arguments."
+  :type '(choice (function-item guix-buffer-name-default)
+                 (function-item guix-buffer-name-simple)
+                 (function :tag "Other function"))
+  :group 'guix)
+
+(defun guix-buffer-name-simple (_profile buffer-type entry-type
+                                &optional _search-type)
+  "Return name of a buffer used for displaying information.
+The name is defined by `guix-ENTRY-TYPE-BUFFER-TYPE-buffer-name'
+variable."
+  (symbol-value
+   (guix-get-symbol "buffer-name" buffer-type entry-type)))
+
+(defun guix-buffer-name-default (profile buffer-type entry-type
+                                 &optional _search-type)
+  "Return name of a buffer used for displaying information.
+The name is almost the same as the one defined by
+`guix-buffer-name-simple' except the PROFILE name is added to it."
+  (let ((simple-name (guix-buffer-name-simple
+                      profile buffer-type entry-type))
+        (profile-name (file-name-base (directory-file-name profile)))
+        (re (rx string-start
+                (group (? "*"))
+                (group (*? any))
+                (group (? "*"))
+                string-end)))
+    (or (string-match re simple-name)
+        (error "Unexpected error in defining guix buffer name"))
+    (let ((first*    (match-string 1 simple-name))
+          (name-body (match-string 2 simple-name))
+          (last*     (match-string 3 simple-name)))
+      ;; Handle the case when buffer name is wrapped by '*'.
+      (if (and (string= "*" first*)
+               (string= "*" last*))
+          (concat "*" name-body ": " profile-name "*")
+        (concat simple-name ": " profile-name)))))
+
+(defun guix-buffer-name (profile buffer-type entry-type search-type)
+  "Return name of a buffer used for displaying information.
+See `guix-buffer-name-function' for details."
+  (let ((fun (if (functionp guix-buffer-name-function)
+                 guix-buffer-name-function
+               #'guix-buffer-name-default)))
+    (funcall fun profile buffer-type entry-type search-type)))
+
+(defun guix-switch-to-buffer (buffer)
+  "Switch to a 'list' or 'info' BUFFER."
+  (pop-to-buffer buffer
+                 '((display-buffer-reuse-window
+                    display-buffer-same-window))))
+
+(defun guix-buffer-p (&optional buffer modes)
+  "Return non-nil if BUFFER mode is derived from any of the MODES.
+If BUFFER is nil, check current buffer.
+If MODES is nil, use `guix-list-mode' and `guix-info-mode'."
+  (with-current-buffer (or buffer (current-buffer))
+    (apply #'derived-mode-p
+           (or modes
+               '(guix-list-mode guix-info-mode)))))
+
+(defun guix-buffers (&optional modes)
+  "Return list of all buffers with major modes derived from MODES.
+If MODES is nil, return list of all Guix 'list' and 'info' buffers."
+  (cl-remove-if-not (lambda (buf)
+                      (guix-buffer-p buf modes))
+                    (buffer-list)))
+
+(defun guix-update-buffer (buffer)
+  "Update information in a 'list' or 'info' BUFFER."
+  (with-current-buffer buffer
+    (guix-revert-buffer nil t)))
+
+(defun guix-update-buffers-maybe-after-operation ()
+  "Update buffers after Guix operation if needed.
+See `guix-update-after-operation' for details."
+  (let ((to-update
+         (and guix-operation-buffer
+              (cl-case guix-update-after-operation
+                (current (and (buffer-live-p guix-operation-buffer)
+                              (guix-buffer-p guix-operation-buffer)
+                              (list guix-operation-buffer)))
+                (all     (guix-buffers))))))
+    (setq guix-operation-buffer nil)
+    (mapc #'guix-update-buffer to-update)))
+
+(add-hook 'guix-after-repl-operation-hook
+          'guix-update-buffers-maybe-after-operation)
+
+\f
 ;;; Common definitions for buffer types
 
+(defvar-local guix-profile nil
+  "Profile used for the current buffer.")
+(put 'guix-profile 'permanent-local t)
+
 (defvar-local guix-entries nil
   "List of the currently displayed entries.
 Each element of the list is alist with entry info of the
@@ -219,13 +318,16 @@ VAL is a value of this parameter.")
   "Values of the current search.")
 (put 'guix-search-vals 'permanent-local t)
 
-(defsubst guix-set-vars (entries buffer-type entry-type
+(defsubst guix-set-vars (profile entries buffer-type entry-type
                          search-type search-vals)
-  (setq guix-entries     entries
-        guix-buffer-type buffer-type
-        guix-entry-type  entry-type
-        guix-search-type search-type
-        guix-search-vals search-vals))
+  "Set local variables for the current Guix buffer."
+  (setq default-directory profile
+        guix-profile      profile
+        guix-entries      entries
+        guix-buffer-type  buffer-type
+        guix-entry-type   entry-type
+        guix-search-type  search-type
+        guix-search-vals  search-vals))
 
 (defun guix-get-symbol (postfix buffer-type &optional entry-type)
   (intern (concat "guix-"
@@ -362,7 +464,7 @@ information)."
                  (const :tag "Display outputs" output))
   :group 'guix)
 
-(defun guix-get-entries (entry-type search-type search-vals
+(defun guix-get-entries (profile entry-type search-type search-vals
                          &optional params)
   "Search for entries of ENTRY-TYPE.
 
@@ -378,56 +480,57 @@ SEARCH-TYPE may be one of the following symbols:
   `all-available', `newest-available', `installed', `obsolete',
   `generation'.
 
-- If ENTRY-TYPE is `generation': `id', `last', `all'.
+- If ENTRY-TYPE is `generation': `id', `last', `all', `time'.
 
 PARAMS is a list of parameters for receiving.  If nil, get
 information with all available parameters."
   (guix-eval-read (guix-make-guile-expression
                    'entries
-                   guix-current-profile params
-                   entry-type search-type search-vals)))
+                   profile params entry-type search-type search-vals)))
 
-(defun guix-get-show-entries (buffer-type entry-type search-type
-                                          &rest search-vals)
+(defun guix-get-show-entries (profile buffer-type entry-type search-type
+                              &rest search-vals)
   "Search for ENTRY-TYPE entries and show results in BUFFER-TYPE buffer.
 See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS."
-  (let ((entries (guix-get-entries entry-type search-type search-vals
+  (let ((entries (guix-get-entries profile entry-type search-type search-vals
                                    (guix-get-params-for-receiving
                                     buffer-type entry-type))))
-    (guix-set-buffer entries buffer-type entry-type
+    (guix-set-buffer profile entries buffer-type entry-type
                      search-type search-vals)))
 
-(defun guix-set-buffer (entries buffer-type entry-type search-type
-                        search-vals &optional history-replace)
+(defun guix-set-buffer (profile entries buffer-type entry-type search-type
+                        search-vals &optional history-replace no-display)
   "Set up BUFFER-TYPE buffer for displaying ENTRY-TYPE ENTRIES.
 
-Display ENTRIES, set variables and make history item.
+Insert ENTRIES in buffer, set variables and make history item.
 ENTRIES should have a form of `guix-entries'.
 
 See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS.
 
 If HISTORY-REPLACE is non-nil, replace current history item,
-otherwise add the new one."
+otherwise add the new one.
+
+If NO-DISPLAY is non-nil, do not switch to the buffer."
   (when entries
-    (let ((buf (if (eq major-mode (guix-get-symbol
-                                   "mode" buffer-type entry-type))
+    (let ((buf (if (and (eq major-mode
+                            (guix-get-symbol "mode" buffer-type entry-type))
+                        (equal guix-profile profile))
                    (current-buffer)
                  (get-buffer-create
-                  (symbol-value
-                   (guix-get-symbol "buffer-name"
-                                    buffer-type entry-type))))))
+                  (guix-buffer-name profile buffer-type
+                                    entry-type search-type)))))
       (with-current-buffer buf
         (guix-show-entries entries buffer-type entry-type)
-        (guix-set-vars entries buffer-type entry-type
+        (guix-set-vars profile entries buffer-type entry-type
                        search-type search-vals)
         (funcall (if history-replace
                      #'guix-history-replace
                    #'guix-history-add)
                  (guix-make-history-item)))
-      (pop-to-buffer buf
-                     '((display-buffer-reuse-window
-                        display-buffer-same-window)))))
-  (guix-result-message entries entry-type search-type search-vals))
+      (or no-display
+          (guix-switch-to-buffer buf))))
+  (guix-result-message profile entries entry-type
+                       search-type search-vals))
 
 (defun guix-show-entries (entries buffer-type entry-type)
   "Display ENTRY-TYPE ENTRIES in the current BUFFER-TYPE buffer."
@@ -439,18 +542,19 @@ otherwise add the new one."
              entries entry-type)
     (goto-char (point-min))))
 
-(defun guix-history-call (entries buffer-type entry-type
+(defun guix-history-call (profile entries buffer-type entry-type
                           search-type search-vals)
   "Function called for moving by history."
   (guix-show-entries entries buffer-type entry-type)
-  (guix-set-vars entries buffer-type entry-type
+  (guix-set-vars profile entries buffer-type entry-type
                  search-type search-vals)
-  (guix-result-message entries entry-type search-type search-vals))
+  (guix-result-message profile entries entry-type
+                       search-type search-vals))
 
 (defun guix-make-history-item ()
   "Make and return a history item for the current buffer."
   (list #'guix-history-call
-        guix-entries guix-buffer-type guix-entry-type
+        guix-profile guix-entries guix-buffer-type guix-entry-type
         guix-search-type guix-search-vals))
 
 (defun guix-get-params-for-receiving (buffer-type entry-type)
@@ -473,120 +577,177 @@ See `revert-buffer' for the meaning of NOCONFIRM."
              (guix-get-symbol "revert-no-confirm"
                               guix-buffer-type guix-entry-type))
             (y-or-n-p "Update current information? "))
-    (let ((entries (guix-get-entries
-                    guix-entry-type guix-search-type guix-search-vals
-                    (guix-get-params-for-receiving guix-buffer-type
-                                                   guix-entry-type))))
-      (guix-set-buffer entries guix-buffer-type guix-entry-type
-                       guix-search-type guix-search-vals t))))
-
-(defun guix-redisplay-buffer ()
-  "Redisplay current information.
+    (let* ((search-type guix-search-type)
+           (search-vals guix-search-vals)
+           (params (guix-get-params-for-receiving guix-buffer-type
+                                                  guix-entry-type))
+           (entries (guix-get-entries
+                     guix-profile guix-entry-type
+                     guix-search-type guix-search-vals params))
+           ;; If a REPL was restarted, package/output IDs are not actual
+           ;; anymore, because 'object-address'-es died with the REPL, so if a
+           ;; search by ID didn't give results, search again by name.
+           (entries (if (and (null entries)
+                             (eq guix-search-type 'id)
+                             (or (eq guix-entry-type 'package)
+                                 (eq guix-entry-type 'output)))
+                        (progn
+                          (setq search-type 'name
+                                search-vals (guix-entries-to-specifications
+                                             guix-entries))
+                          (guix-get-entries
+                           guix-profile guix-entry-type
+                           search-type search-vals params))
+                      entries)))
+      (guix-set-buffer guix-profile entries guix-buffer-type guix-entry-type
+                       search-type search-vals t t))))
+
+(cl-defun guix-redisplay-buffer (&key buffer profile entries buffer-type
+                                      entry-type search-type search-vals)
+  "Redisplay a Guix BUFFER.
+Restore the point and window positions after redisplaying if possible.
+
 This function will not update the information, use
-\"\\[revert-buffer]\" if you want the full update."
+\"\\[revert-buffer]\" if you want the full update.
+
+If BUFFER is nil, use the current buffer.  For the meaning of the
+rest arguments, see `guix-set-buffer'."
   (interactive)
-  (guix-show-entries guix-entries guix-buffer-type guix-entry-type)
-  (guix-result-message guix-entries guix-entry-type
-                       guix-search-type guix-search-vals))
+  (or buffer (setq buffer (current-buffer)))
+  (with-current-buffer buffer
+    (or (derived-mode-p 'guix-info-mode 'guix-list-mode)
+        (error "%S is not a Guix buffer" buffer))
+    (let* ((point (point))
+           (was-at-button (button-at point))
+           ;; For simplicity, ignore an unlikely case when multiple
+           ;; windows display the same BUFFER.
+           (window (car (get-buffer-window-list buffer nil t)))
+           (window-start (and window (window-start window))))
+      (guix-set-buffer (or profile     guix-profile)
+                       (or entries     guix-entries)
+                       (or buffer-type guix-buffer-type)
+                       (or entry-type  guix-entry-type)
+                       (or search-type guix-search-type)
+                       (or search-vals guix-search-vals)
+                       t t)
+      (goto-char point)
+      (and was-at-button
+           (not (button-at (point)))
+           (forward-button 1))
+      (when window
+        (set-window-point window (point))
+        (set-window-start window window-start)))))
 
 \f
-;;; Messages
+;;; Generations
+
+(defcustom guix-generation-packages-buffer-name-function
+  #'guix-generation-packages-buffer-name-default
+  "Function used to define name of a buffer with generation packages.
+This function is called with 2 arguments: PROFILE (string) and
+GENERATION (number)."
+  :type '(choice (function-item guix-generation-packages-buffer-name-default)
+                 (function-item guix-generation-packages-buffer-name-long)
+                 (function :tag "Other function"))
+  :group 'guix)
 
-(defvar guix-messages
-  '((package
-     (id
-      (0 "Packages not found.")
-      (1 "")
-      (many "%d packages." count))
-     (name
-      (0 "The package '%s' not found." val)
-      (1 "A single package with name '%s'." val)
-      (many "%d packages with '%s' name." count val))
-     (regexp
-      (0 "No packages matching '%s'." val)
-      (1 "A single package matching '%s'." val)
-      (many "%d packages matching '%s'." count val))
-     (all-available
-      (0 "No packages are available for some reason.")
-      (1 "A single available package (that's strange).")
-      (many "%d available packages." count))
-     (newest-available
-      (0 "No packages are available for some reason.")
-      (1 "A single newest available package (that's strange).")
-      (many "%d newest available packages." count))
-     (installed
-      (0 "No installed packages.")
-      (1 "A single package installed.")
-      (many "%d packages installed." count))
-     (obsolete
-      (0 "No obsolete packages.")
-      (1 "A single obsolete package.")
-      (many "%d obsolete packages." count))
-     (generation
-      (0 "No packages installed in generation %d." val)
-      (1 "A single package installed in generation %d." val)
-      (many "%d packages installed in generation %d." count val)))
-    (output
-     (id
-      (0 "Package outputs not found.")
-      (1 "")
-      (many "%d package outputs." count))
-     (name
-      (0 "The package output '%s' not found." val)
-      (1 "A single package output with name '%s'." val)
-      (many "%d package outputs with '%s' name." count val))
-     (regexp
-      (0 "No package outputs matching '%s'." val)
-      (1 "A single package output matching '%s'." val)
-      (many "%d package outputs matching '%s'." count val))
-     (all-available
-      (0 "No package outputs are available for some reason.")
-      (1 "A single available package output (that's strange).")
-      (many "%d available package outputs." count))
-     (newest-available
-      (0 "No package outputs are available for some reason.")
-      (1 "A single newest available package output (that's strange).")
-      (many "%d newest available package outputs." count))
-     (installed
-      (0 "No installed package outputs.")
-      (1 "A single package output installed.")
-      (many "%d package outputs installed." count))
-     (obsolete
-      (0 "No obsolete package outputs.")
-      (1 "A single obsolete package output.")
-      (many "%d obsolete package outputs." count))
-     (generation
-      (0 "No package outputs installed in generation %d." val)
-      (1 "A single package output installed in generation %d." val)
-      (many "%d package outputs installed in generation %d." count val)))
-    (generation
-     (id
-      (0 "Generations not found.")
-      (1 "")
-      (many "%d generations." count))
-     (last
-      (0 "No available generations.")
-      (1 "The last generation.")
-      (many "%d last generations." count))
-     (all
-      (0 "No available generations.")
-      (1 "A single available generation.")
-      (many "%d available generations." count)))))
-
-(defun guix-result-message (entries entry-type search-type search-vals)
-  "Display an appropriate message after displaying ENTRIES."
-  (let* ((val (car search-vals))
-         (count (length entries))
-         (count-key (if (> count 1) 'many count))
-         (msg-spec (guix-get-key-val guix-messages
-                                     entry-type search-type count-key))
-         (format (car msg-spec))
-         (args (cdr msg-spec)))
-    (mapc (lambda (subst)
-            (setq args (cl-substitute (car subst) (cdr subst) args)))
-          (list (cons count 'count)
-                (cons val 'val)))
-    (apply #'message format args)))
+(defcustom guix-generation-packages-update-buffer t
+  "If non-nil, always update list of packages during comparing generations.
+If nil, generation packages are received only once.  So when you
+compare generation 1 and generation 2, the packages for both
+generations will be received.  Then if you compare generation 1
+and generation 3, only the packages for generation 3 will be
+received.  Thus if you use comparing of different generations a
+lot, you may set this variable to nil to improve the
+performance."
+  :type 'boolean
+  :group 'guix)
+
+(defvar guix-output-name-width 30
+  "Width of an output name \"column\".
+This variable is used in auxiliary buffers for comparing generations.")
+
+(defun guix-generation-file (profile generation)
+  "Return the file name of a PROFILE's GENERATION."
+  (format "%s-%s-link" profile generation))
+
+(defun guix-manifest-file (profile &optional generation)
+  "Return the file name of a PROFILE's manifest.
+If GENERATION number is specified, return manifest file name for
+this generation."
+  (expand-file-name "manifest"
+                    (if generation
+                        (guix-generation-file profile generation)
+                      profile)))
+
+(defun guix-generation-packages (profile generation)
+  "Return a list of sorted packages installed in PROFILE's GENERATION.
+Each element of the list is a list of the package specification and its path."
+  (let ((names+paths (guix-eval-read
+                      (guix-make-guile-expression
+                       'generation-package-specifications+paths
+                       profile generation))))
+    (sort names+paths
+          (lambda (a b)
+            (string< (car a) (car b))))))
+
+(defun guix-generation-packages-buffer-name-default (profile generation)
+  "Return name of a buffer for displaying GENERATION's package outputs.
+Use base name of PROFILE path."
+  (let ((profile-name (file-name-base (directory-file-name profile))))
+    (format "*Guix %s: generation %s*"
+            profile-name generation)))
+
+(defun guix-generation-packages-buffer-name-long (profile generation)
+  "Return name of a buffer for displaying GENERATION's package outputs.
+Use the full PROFILE path."
+  (format "*Guix generation %s (%s)*"
+          generation profile))
+
+(defun guix-generation-packages-buffer-name (profile generation)
+  "Return name of a buffer for displaying GENERATION's package outputs."
+  (let ((fun (if (functionp guix-generation-packages-buffer-name-function)
+                 guix-generation-packages-buffer-name-function
+               #'guix-generation-packages-buffer-name-default)))
+    (funcall fun profile generation)))
+
+(defun guix-generation-insert-package (name path)
+  "Insert package output NAME and PATH at point."
+  (insert name)
+  (indent-to guix-output-name-width 2)
+  (insert path "\n"))
+
+(defun guix-generation-insert-packages (buffer profile generation)
+  "Insert package outputs installed in PROFILE's GENERATION in BUFFER."
+  (with-current-buffer buffer
+    (setq buffer-read-only nil
+          indent-tabs-mode nil)
+    (erase-buffer)
+    (mapc (lambda (name+path)
+            (guix-generation-insert-package
+             (car name+path) (cadr name+path)))
+          (guix-generation-packages profile generation))))
+
+(defun guix-generation-packages-buffer (profile generation)
+  "Return buffer with package outputs installed in PROFILE's GENERATION.
+Create the buffer if needed."
+  (let ((buf-name (guix-generation-packages-buffer-name
+                   profile generation)))
+    (or (and (null guix-generation-packages-update-buffer)
+             (get-buffer buf-name))
+        (let ((buf (get-buffer-create buf-name)))
+          (guix-generation-insert-packages buf profile generation)
+          buf))))
+
+(defun guix-profile-generation-manifest-file (generation)
+  "Return the file name of a GENERATION's manifest.
+GENERATION is a generation number of `guix-profile' profile."
+  (guix-manifest-file guix-profile generation))
+
+(defun guix-profile-generation-packages-buffer (generation)
+  "Insert GENERATION's package outputs in a buffer and return it.
+GENERATION is a generation number of `guix-profile' profile."
+  (guix-generation-packages-buffer guix-profile generation))
 
 \f
 ;;; Actions on packages and generations
@@ -660,8 +821,9 @@ VARIABLE is a name of an option variable.")
       guix-operation-option-true-string
     guix-operation-option-false-string))
 
-(defun guix-process-package-actions (&rest actions)
-  "Process package ACTIONS.
+(defun guix-process-package-actions (profile actions
+                                     &optional operation-buffer)
+  "Process package ACTIONS on PROFILE.
 Each action is a list of the form:
 
   (ACTION-TYPE PACKAGE-SPEC ...)
@@ -679,22 +841,25 @@ PACKAGE-SPEC should have the following form: (ID [OUTPUT] ...)."
                 ((remove delete) (setq remove (append remove specs))))))
           actions)
     (when (guix-continue-package-operation-p
+           profile
            :install install :upgrade upgrade :remove remove)
       (guix-eval-in-repl
        (guix-make-guile-expression
-        'process-package-actions guix-current-profile
+        'process-package-actions profile
         :install install :upgrade upgrade :remove remove
         :use-substitutes? (or guix-use-substitutes 'f)
-        :dry-run? (or guix-dry-run 'f))))))
+        :dry-run? (or guix-dry-run 'f))
+       (and (not guix-dry-run) operation-buffer)))))
 
-(cl-defun guix-continue-package-operation-p (&key install upgrade remove)
+(cl-defun guix-continue-package-operation-p (profile
+                                             &key install upgrade remove)
   "Return non-nil if a package operation should be continued.
 Ask a user if needed (see `guix-operation-confirm').
 INSTALL, UPGRADE, REMOVE are 'package action specifications'.
 See `guix-process-package-actions' for details."
   (or (null guix-operation-confirm)
       (let* ((entries (guix-get-entries
-                       'package 'id
+                       profile 'package 'id
                        (append (mapcar #'car install)
                                (mapcar #'car upgrade)
                                (mapcar #'car remove))
@@ -708,6 +873,7 @@ See `guix-process-package-actions' for details."
                 (setq-local cursor-type nil)
                 (setq buffer-read-only nil)
                 (erase-buffer)
+                (insert "Profile: " profile "\n\n")
                 (guix-insert-package-strings install-strings "install")
                 (guix-insert-package-strings upgrade-strings "upgrade")
                 (guix-insert-package-strings remove-strings "remove")
@@ -750,13 +916,14 @@ ENTRIES is a list of package entries to get info about packages."
           strings)
     (insert "\n")))
 
-(defun guix-operation-prompt ()
-  "Prompt a user for continuing the current package operation.
-Return non-nil, if the operation should be continued; nil otherwise."
+(defun guix-operation-prompt (&optional prompt)
+  "Prompt a user for continuing the current operation.
+Return non-nil, if the operation should be continued; nil otherwise.
+Ask a user with PROMPT for continuing an operation."
   (let* ((option-keys (mapcar #'guix-operation-option-key
                               guix-operation-options))
          (keys (append '(?y ?n) option-keys))
-         (prompt (concat (propertize "Continue operation?"
+         (prompt (concat (propertize (or prompt "Continue operation?")
                                      'face 'minibuffer-prompt)
                          " ("
                          (mapconcat
@@ -766,9 +933,11 @@ Return non-nil, if the operation should be continued; nil otherwise."
                           keys
                           ", ")
                          ") ")))
-    (prog1 (guix-operation-prompt-1 prompt keys)
-      ;; Clear the minibuffer after prompting.
-      (message ""))))
+    (let ((mode-line mode-line-format))
+      (prog1 (guix-operation-prompt-1 prompt keys)
+        (setq mode-line-format mode-line)
+        ;; Clear the minibuffer after prompting.
+        (message "")))))
 
 (defun guix-operation-prompt-1 (prompt keys)
   "This function is internal for `guix-operation-prompt'."
@@ -801,6 +970,97 @@ Return non-nil, if the operation should be continued; nil otherwise."
                  guix-operation-option-separator)))
   (force-mode-line-update))
 
+(defun guix-delete-generations (profile generations
+                                &optional operation-buffer)
+  "Delete GENERATIONS from PROFILE.
+Each element from GENERATIONS is a generation number."
+  (when (or (not guix-operation-confirm)
+            (y-or-n-p
+             (let ((count (length generations)))
+               (if (> count 1)
+                   (format "Delete %d generations from profile '%s'? "
+                           count profile)
+                 (format "Delete generation %d from profile '%s'? "
+                         (car generations) profile)))))
+    (guix-eval-in-repl
+     (guix-make-guile-expression
+      'delete-generations* profile generations)
+     operation-buffer)))
+
+(defun guix-switch-to-generation (profile generation
+                                  &optional operation-buffer)
+  "Switch PROFILE to GENERATION."
+  (when (or (not guix-operation-confirm)
+            (y-or-n-p (format "Switch profile '%s' to generation %d? "
+                              profile generation)))
+    (guix-eval-in-repl
+     (guix-make-guile-expression
+      'switch-to-generation profile generation)
+     operation-buffer)))
+
+(defun guix-package-source-path (package-id)
+  "Return a store file path to a source of a package PACKAGE-ID."
+  (message "Calculating the source derivation ...")
+  (guix-eval-read
+   (guix-make-guile-expression
+    'package-source-path package-id)))
+
+(defvar guix-after-source-download-hook nil
+  "Hook run after successful performing a 'source-download' operation.")
+
+(defun guix-package-source-build-derivation (package-id &optional prompt)
+  "Build source derivation of a package PACKAGE-ID.
+Ask a user with PROMPT for continuing an operation."
+  (when (or (not guix-operation-confirm)
+            (guix-operation-prompt (or prompt
+                                       "Build the source derivation?")))
+    (guix-eval-in-repl
+     (guix-make-guile-expression
+      'package-source-build-derivation
+      package-id
+      :use-substitutes? (or guix-use-substitutes 'f)
+      :dry-run? (or guix-dry-run 'f))
+     nil 'source-download)))
+
+\f
+;;; Pull
+
+(defcustom guix-update-after-pull t
+  "If non-nil, update Guix buffers after performing \\[guix-pull]."
+  :type 'boolean
+  :group 'guix)
+
+(defvar guix-after-pull-hook
+  '(guix-restart-repl-after-pull guix-update-buffers-maybe-after-pull)
+  "Hook run after successful performing `guix-pull' operation.")
+
+(defun guix-restart-repl-after-pull ()
+  "Restart Guix REPL after `guix-pull' operation."
+  (guix-repl-exit)
+  (guix-start-process-maybe
+   "Restarting Guix REPL after pull operation ..."))
+
+(defun guix-update-buffers-maybe-after-pull ()
+  "Update buffers depending on `guix-update-after-pull'."
+  (when guix-update-after-pull
+    (mapc #'guix-update-buffer
+          ;; No need to update "generation" buffers.
+          (guix-buffers '(guix-package-list-mode
+                          guix-package-info-mode
+                          guix-output-list-mode
+                          guix-output-info-mode)))
+    (message "Guix buffers have been updated.")))
+
+;;;###autoload
+(defun guix-pull (&optional verbose)
+  "Run Guix pull operation.
+If VERBOSE is non-nil (with prefix argument), produce verbose output."
+  (interactive)
+  (let ((args (and verbose '("--verbose"))))
+    (guix-eval-in-repl
+     (apply #'guix-make-guile-expression 'guix-pull args)
+     nil 'pull)))
+
 (provide 'guix-base)
 
 ;;; guix-base.el ends here