This repository has been archived by the owner on May 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
nix-generation.el
134 lines (108 loc) · 5.19 KB
/
nix-generation.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
;;; nix-generation.el --- Nix generations integration -*- lexical-binding: t -*-
;; Copyright (C) 2017 Mario Rodas <[email protected]>
;; Author: Mario Rodas <[email protected]>
;; Keywords: convenience
;; Version: 0.1
;; Package-Requires: ((emacs "25.1") (let-alist "1.0.1") (tablist "0.70"))
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Nix generations integration
;;; Code:
(eval-when-compile
(require 'cl-lib)
(require 'subr-x))
(require 'tablist)
(require 'nix)
(require 'nix-package)
(defgroup nix-generation nil
"Interface for NixOS genarations."
:prefix "nix-generation-"
:group 'languages)
(cl-defstruct (nix-generation
(:constructor nix-generation-new)
(:type vector))
"A struct holding the information of a Nix generation"
number date current)
(defun nix-generation-entries ()
"Return a list current available nix-env generations as `nix-generation' structs."
(cl-loop for line in (nix-exec-lines nix-package-nix-env-executable "--list-generations")
collect (cl-multiple-value-bind (number date current)
(split-string (string-trim line) "[[:space:]][[:space:]]+" 'omit-nulls)
(nix-generation-new :number number :date date :current (string-equal current "(current)")))))
(defun nix-generation-list-entries ()
"Return a entry `tabulated-list-entries' for nix generations."
(mapcar (lambda (gen)
(list (nix-generation-number gen)
(vector (nix-generation-number gen)
(nix-generation-date gen)
(if (nix-generation-current gen) "●" ""))))
(nix-generation-entries)))
(defun nix-generation-do-remove (&optional arg)
"Install ARG entries."
(interactive "P")
(apply #'nix-exec nix-package-nix-env-executable
"--delete-generations"
(cl-loop for (attr . _entry) in (tablist-get-marked-items arg)
collect attr)))
(declare-function ztree-diff "ztree-diff")
(defun nix-generation-diff (gen1 gen2)
"Show a difference between generations GEN1 and GEN2."
(interactive (list (completing-read "Generation 1: " (funcall tabulated-list-entries))
(completing-read "Generation 2: " (funcall tabulated-list-entries))))
(cl-assert (fboundp 'ztree-diff) nil "`%s' needs `ztree' package installed" this-command)
(ztree-diff (nix-file-relative (nix-generation-find gen1)) (nix-file-relative (nix-generation-find gen2))))
(defun nix-generation-find (id)
"Find nix generation by ID."
(let ((system-generation (format "/nix/var/nix/profiles/default-%s-link" id)))
(if (file-exists-p (nix-file-relative system-generation))
system-generation
(format "/nix/var/nix/profiles/per-user/%s/profile-%s-link" (nix-login-name) id))))
(defun nix-generation-tablist-operations (operation &rest arguments)
"Function for tablist OPERATION and is called with ARGUMENTS.
See `tablist-operations-function' for more information."
(cl-ecase operation
(delete (cl-multiple-value-bind (ids) arguments
(apply #'nix-exec nix-package-nix-env-executable "--delete-generations" ids)))
(find-entry (cl-multiple-value-bind (id) arguments
(nix-find-file-relative (nix-generation-find id))))
(supported-operations '(delete find-entry))))
(defvar nix-generation-list-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map tabulated-list-mode-map)
(define-key map (kbd "RET") 'tablist-find-entry)
(define-key map "f" 'tablist-find-entry)
(define-key map "D" 'tablist-do-delete)
map)
"Local keymap for `nix-generation-list-mode' buffers.")
(define-derived-mode nix-generation-list-mode tabulated-list-mode "nix-generations"
"List available nix packages.
\\{nix-generation-list-mode-map}"
(setq tabulated-list-format [("number" 10 t :read-only t)
("date" 30 t :read-only t)
("current" 30 t :read-only t)]
tabulated-list-padding 2
tabulated-list-entries 'nix-generation-list-entries
tablist-operations-function 'nix-generation-tablist-operations)
(tabulated-list-init-header))
(add-hook 'nix-generation-list-mode-hook #'tablist-minor-mode)
;;;###autoload
(defun nix-generation-list ()
"Show a list of available nix-packages."
(interactive)
(with-current-buffer (get-buffer-create (nix-tramp-buffer-name "nix-generations"))
(nix-generation-list-mode)
(tabulated-list-print)
(pop-to-buffer (current-buffer))))
(provide 'nix-generation)
;;; nix-generation.el ends here