guix-ui-system-generation.el 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-ui)
  24. (require 'guix-ui-generation)
  25. (require 'guix-utils)
  26. (require 'guix-profiles)
  27. (guix-ui-define-entry-type system-generation)
  28. (defun guix-system-generation-add-kernel-config (entry)
  29. "Return ENTRY with 'kernel-config' parameter."
  30. (let* ((kernel (bui-entry-value entry 'kernel))
  31. (dir (file-name-directory kernel))
  32. ;; Nowadays kernel config has ".config" name, but before
  33. ;; <http://git.savannah.gnu.org/cgit/guix.git/commit/?id=e4e9c0a083962a770ff4f56c69082cf4b7046a6c>
  34. ;; it was "config" (without "."), so find the right name.
  35. (config (car (directory-files dir 'full-name
  36. "config\\'" 'no-sort))))
  37. `((kernel-config . ,config)
  38. ,@entry)))
  39. (defun guix-system-generation-add-shepherd-config (entry)
  40. "Return ENTRY with 'shepherd-config' parameter."
  41. (let* ((file-name (bui-entry-value entry 'file-name))
  42. (boot-file (expand-file-name "boot" file-name)))
  43. (with-temp-buffer
  44. (insert-file-contents-literally boot-file)
  45. (goto-char (point-min))
  46. (if (re-search-forward (rx "/gnu/store/" (+ alnum)
  47. "-shepherd.conf")
  48. nil t)
  49. `((shepherd-config . ,(match-string 0))
  50. ,@entry)
  51. entry))))
  52. (defun guix-system-generation-get-entries (profile search-type
  53. search-values params)
  54. "Return 'system-generation' entries."
  55. (let* ((add-kernel-config? (or (null params)
  56. (memq 'kernel-config params)))
  57. (add-shepherd-config? (or (null params)
  58. (memq 'shepherd-config params)))
  59. (params (if (and add-kernel-config?
  60. (not (memq 'kernel params)))
  61. (cons 'kernel params)
  62. params))
  63. (params (if (and add-shepherd-config?
  64. (not (memq 'file-name params)))
  65. (cons 'file-name params)
  66. params)))
  67. (apply #'guix-modify-objects
  68. (guix-generation-get-entries
  69. 'system-generation-sexps
  70. profile search-type search-values params)
  71. (delq nil
  72. (list
  73. (and add-shepherd-config?
  74. #'guix-system-generation-add-shepherd-config)
  75. (and add-kernel-config?
  76. #'guix-system-generation-add-kernel-config))))))
  77. (defun guix-system-generation-get-display (search-type &rest search-values)
  78. "Search for system generations and show results.
  79. See `guix-ui-get-entries' for the meaning of SEARCH-TYPE and
  80. SEARCH-VALUES."
  81. (apply #'bui-list-get-display-entries
  82. 'guix-system-generation
  83. guix-system-profile
  84. search-type search-values))
  85. ;;; System generation 'info'
  86. (guix-ui-define-interface system-generation info
  87. :mode-name "System-Generation-Info"
  88. :buffer-name "*Guix Generation Info*"
  89. :get-entries-function 'guix-system-generation-info-get-entries
  90. :format '(guix-generation-info-insert-heading
  91. nil
  92. (label format (format))
  93. (prev-number format guix-generation-info-insert-previous)
  94. (current format guix-generation-info-insert-current)
  95. (number-of-packages format guix-generation-info-insert-packages)
  96. (file-name format (format bui-file))
  97. (time format (time))
  98. (root-device format (format))
  99. (store-device format (format))
  100. (store-mount-point format (format))
  101. (kernel-arguments format (format))
  102. (kernel-config simple (indent bui-file))
  103. (shepherd-config simple
  104. guix-system-generation-info-insert-shepherd))
  105. :titles guix-generation-info-titles
  106. :required guix-generation-info-required-params)
  107. (defun guix-system-generation-info-get-entries (profile search-type
  108. &rest search-values)
  109. "Return 'system-generation' entries for displaying them in 'info' buffer."
  110. (guix-system-generation-get-entries
  111. profile search-type search-values
  112. (cl-union guix-system-generation-info-required-params
  113. (bui-info-displayed-params 'guix-system-generation))))
  114. (defun guix-system-generation-pretty-print (config)
  115. "Pretty print Sheprhed CONFIG file name."
  116. (let* ((base-name (file-name-nondirectory config))
  117. (buffer (generate-new-buffer base-name)))
  118. (with-current-buffer buffer
  119. (insert-file-contents config)
  120. (goto-char (point-min))
  121. ;; Put "/gnu/store/..." strings and service names on the new
  122. ;; lines.
  123. (save-excursion
  124. (while (re-search-forward "(quote" nil t)
  125. (down-list)
  126. (forward-sexp)
  127. (while (not (looking-at ")"))
  128. (insert "\n")
  129. (forward-sexp))))
  130. (scheme-mode))
  131. (guix-pretty-print-buffer buffer)
  132. (switch-to-buffer buffer)))
  133. (defun guix-system-generation-info-insert-shepherd (config &optional _)
  134. "Insert Shepherd CONFIG file name and 'Pretty print' button at point."
  135. (bui-insert-action-button
  136. "Pretty print"
  137. (lambda (btn)
  138. (guix-system-generation-pretty-print (button-get btn 'config)))
  139. "Show Shepherd config in a human-readable form"
  140. 'config config)
  141. (bui-info-insert-value-indent config 'bui-file))
  142. ;;; System generation 'list'
  143. ;; FIXME It is better to make `guix-generation-list-shared-map' with
  144. ;; common keys for both usual and system generations.
  145. (defvar guix-system-generation-list-mode-map
  146. (copy-keymap guix-generation-list-mode-map)
  147. "Keymap for `guix-system-generation-list-mode' buffers.")
  148. (guix-ui-define-interface system-generation list
  149. :mode-name "System-Generation-List"
  150. :buffer-name "*Guix Generations*"
  151. :get-entries-function 'guix-system-generation-list-get-entries
  152. :describe-function 'guix-ui-list-describe
  153. :format '((number nil 5 bui-list-sort-numerically-0 :right-align t)
  154. (current guix-generation-list-get-current 10 t)
  155. (label nil 35 t)
  156. (number-of-packages nil 11 bui-list-sort-numerically-3
  157. :right-align t)
  158. (time bui-list-get-time 20 t))
  159. :titles guix-generation-list-titles
  160. :hint guix-generation-list-hint
  161. :sort-key guix-generation-list-sort-key
  162. :marks guix-generation-list-additional-marks)
  163. (defun guix-system-generation-list-get-entries (profile search-type
  164. &rest search-values)
  165. "Return 'system-generation' entries for displaying them in 'list' buffer."
  166. (guix-system-generation-get-entries
  167. profile search-type search-values
  168. (cl-union guix-system-generation-list-required-params
  169. (bui-list-displayed-params 'guix-system-generation))))
  170. ;;; Interactive commands
  171. ;;;###autoload
  172. (defun guix-system-generations ()
  173. "Display information about system generations."
  174. (interactive)
  175. (guix-system-generation-get-display 'all))
  176. ;;;###autoload
  177. (defun guix-last-system-generations (number)
  178. "Display information about last NUMBER of system generations."
  179. (interactive "nThe number of last generations: ")
  180. (guix-system-generation-get-display 'last number))
  181. ;;;###autoload
  182. (defun guix-system-generations-by-time (from to)
  183. "Display information about system generations created between FROM and TO."
  184. (interactive
  185. (list (guix-read-date "Find generations (from): ")
  186. (guix-read-date "Find generations (to): ")))
  187. (guix-system-generation-get-display
  188. 'time (float-time from) (float-time to)))
  189. (provide 'guix-ui-system-generation)
  190. ;;; guix-ui-system-generation.el ends here