guix-ui-system-generation.el 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ;;; guix-ui-system-generation.el --- Interface for displaying system generations -*- lexical-binding: t -*-
  2. ;; Copyright © 2016–2017 Alex Kost <alezost@gmail.com>
  3. ;; This file is part of Emacs-Guix.
  4. ;; Emacs-Guix is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; Emacs-Guix is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with Emacs-Guix. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file provides an interface for displaying system generations
  18. ;; in 'list' and 'info' buffers, and commands for working with them.
  19. ;;; Code:
  20. (require 'cl-lib)
  21. (require 'bui)
  22. (require 'guix nil t)
  23. (require 'guix-auto-mode) ; for `guix-store-directory'
  24. (require 'guix-ui)
  25. (require 'guix-ui-generation)
  26. (require 'guix-utils)
  27. (require 'guix-profiles)
  28. (guix-ui-define-entry-type system-generation)
  29. (defvar guix-system-generation-shepherd-config-regexp
  30. (rx-to-string `(and ,guix-store-directory "/"
  31. (+ alnum) "-shepherd.conf")
  32. t)
  33. "Regexp matching 'shepherd.conf' file placed in the store.")
  34. (defun guix-system-generation-add-kernel-config (entry)
  35. "Return ENTRY with 'kernel-config' parameter."
  36. (let* ((kernel (bui-entry-value entry 'kernel))
  37. (dir (file-name-directory kernel))
  38. ;; Nowadays kernel config has ".config" name, but before
  39. ;; <http://git.savannah.gnu.org/cgit/guix.git/commit/?id=e4e9c0a083962a770ff4f56c69082cf4b7046a6c>
  40. ;; it was "config" (without "."), so find the right name.
  41. (config (car (directory-files dir 'full-name
  42. "config\\'" 'no-sort))))
  43. `((kernel-config . ,config)
  44. ,@entry)))
  45. (defun guix-system-generation-add-shepherd-config (entry)
  46. "Return ENTRY with 'shepherd-config' parameter."
  47. (let* ((file-name (bui-entry-value entry 'file-name))
  48. (boot-file (expand-file-name "boot" file-name)))
  49. (with-temp-buffer
  50. (insert-file-contents-literally boot-file)
  51. (goto-char (point-min))
  52. (if (re-search-forward guix-system-generation-shepherd-config-regexp
  53. nil t)
  54. `((shepherd-config . ,(match-string 0))
  55. ,@entry)
  56. entry))))
  57. (defun guix-system-generation-get-entries (profile search-type
  58. search-values params)
  59. "Return 'system-generation' entries."
  60. (let* ((add-kernel-config? (or (null params)
  61. (memq 'kernel-config params)))
  62. (add-shepherd-config? (or (null params)
  63. (memq 'shepherd-config params)))
  64. (params (if (and add-kernel-config?
  65. (not (memq 'kernel params)))
  66. (cons 'kernel params)
  67. params))
  68. (params (if (and add-shepherd-config?
  69. (not (memq 'file-name params)))
  70. (cons 'file-name params)
  71. params)))
  72. (apply #'guix-modify-objects
  73. (guix-generation-get-entries
  74. 'system-generation-sexps
  75. profile search-type search-values params)
  76. (delq nil
  77. (list
  78. (and add-shepherd-config?
  79. #'guix-system-generation-add-shepherd-config)
  80. (and add-kernel-config?
  81. #'guix-system-generation-add-kernel-config))))))
  82. (defun guix-system-generation-get-display (search-type &rest search-values)
  83. "Search for system generations and show results.
  84. See `guix-ui-get-entries' for the meaning of SEARCH-TYPE and
  85. SEARCH-VALUES."
  86. (apply #'bui-list-get-display-entries
  87. 'guix-system-generation
  88. guix-system-profile
  89. search-type search-values))
  90. ;;; System generation 'info'
  91. (guix-ui-define-interface system-generation info
  92. :mode-name "System-Generation-Info"
  93. :buffer-name "*Guix Generation Info*"
  94. :get-entries-function 'guix-system-generation-info-get-entries
  95. :format '(guix-generation-info-insert-heading
  96. nil
  97. guix-generation-info-insert-buttons
  98. (label format (format))
  99. (prev-number format guix-generation-info-insert-previous)
  100. (current format guix-generation-info-insert-current)
  101. (number-of-packages format guix-generation-info-insert-packages)
  102. (file-name simple guix-system-generation-info-insert-file-name)
  103. (time format (time))
  104. (bootloader format (format))
  105. (root-device format (format))
  106. (store-device format (format))
  107. (store-mount-point format (format bui-file))
  108. (kernel-arguments format (format))
  109. (kernel-config simple (indent bui-file))
  110. (shepherd-config simple (indent bui-file)))
  111. :titles guix-generation-info-titles
  112. :required guix-generation-info-required-params)
  113. (defun guix-system-generation-info-get-entries (profile search-type
  114. &rest search-values)
  115. "Return 'system-generation' entries for displaying them in 'info' buffer."
  116. (guix-system-generation-get-entries
  117. profile search-type search-values
  118. (cl-union guix-system-generation-info-required-params
  119. (bui-info-displayed-params 'guix-system-generation))))
  120. (defun guix-system-generation-info-insert-file-name (file-name &optional _)
  121. "Insert generation FILE-NAME at point."
  122. (bui-info-insert-value-indent
  123. (list file-name
  124. (file-truename file-name))
  125. 'bui-file))
  126. ;;; System generation 'list'
  127. ;; FIXME It is better to make `guix-generation-list-shared-map' with
  128. ;; common keys for both usual and system generations.
  129. (defvar guix-system-generation-list-mode-map
  130. (copy-keymap guix-generation-list-mode-map)
  131. "Keymap for `guix-system-generation-list-mode' buffers.")
  132. (guix-ui-define-interface system-generation list
  133. :mode-name "System-Generation-List"
  134. :buffer-name "*Guix Generations*"
  135. :get-entries-function 'guix-system-generation-list-get-entries
  136. :describe-function 'guix-ui-list-describe
  137. :format '((number nil 5 bui-list-sort-numerically-0 :right-align t)
  138. (current guix-generation-list-get-current 10 t)
  139. (label nil 35 t)
  140. (number-of-packages nil 11 bui-list-sort-numerically-3
  141. :right-align t)
  142. (time bui-list-get-time 20 t))
  143. ;; Always get 'file-name' parameter to make it possible to call
  144. ;; `guix-generation-list-show-search-paths'.
  145. :required '(id file-name)
  146. :titles guix-generation-list-titles
  147. :hint guix-generation-list-hint
  148. :sort-key guix-generation-list-sort-key
  149. :marks guix-generation-list-additional-marks)
  150. (defun guix-system-generation-list-get-entries (profile search-type
  151. &rest search-values)
  152. "Return 'system-generation' entries for displaying them in 'list' buffer."
  153. (guix-system-generation-get-entries
  154. profile search-type search-values
  155. (cl-union guix-system-generation-list-required-params
  156. (bui-list-displayed-params 'guix-system-generation))))
  157. ;;; Interactive commands
  158. ;;;###autoload
  159. (defun guix-system-generations ()
  160. "Display information about system generations."
  161. (interactive)
  162. (guix-system-generation-get-display 'all))
  163. ;;;###autoload
  164. (defun guix-last-system-generations (number)
  165. "Display information about last NUMBER of system generations."
  166. (interactive "nThe number of last generations: ")
  167. (guix-system-generation-get-display 'last number))
  168. ;;;###autoload
  169. (defun guix-system-generations-by-time (from to)
  170. "Display information about system generations created between FROM and TO."
  171. (interactive
  172. (list (guix-read-date "Find generations (from): ")
  173. (guix-read-date "Find generations (to): ")))
  174. (guix-system-generation-get-display
  175. 'time (float-time from) (float-time to)))
  176. (provide 'guix-ui-system-generation)
  177. ;;; guix-ui-system-generation.el ends here