-
Notifications
You must be signed in to change notification settings - Fork 1
/
nix-env-install.el
355 lines (304 loc) · 13.5 KB
/
nix-env-install.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
;;; nix-env-install.el --- Install packages using nix-env -*- lexical-binding: t -*-
;; Copyright (C) 2019 Akira Komamura
;; Author: Akira Komamura <[email protected]>
;; Version: 0.1
;; Package-Requires: ((emacs "25.1"))
;; Keywords: processes tools
;; URL: https://github.com/akirak/nix-env-install
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This library lets you install packages using nix-env.
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(require 'json)
(declare-function xterm-color-filter "ext:xterm-color")
(declare-function ansi-color-filter-apply "ext:ansi-color")
(defgroup nix-env-install nil
"Install packages using nix-env."
:group 'nix)
(when (memq system-type '(ms-dos windows-nt cygwin))
(user-error "Nix doesn't run on non-UNIX systems"))
(defcustom nix-env-install-display-process-buffers t
"Whether to display process buffers."
:group 'nix-env-install
:type 'boolean)
(defcustom nix-env-install-window-height 15
"Window height of process buffers."
:group 'nix-env-install
:type 'number)
(defcustom nix-env-install-display-buffer
'nix-env-install--display-buffer-default
"Function used to display process buffers."
:group 'nix-env-install
:type 'function)
(defcustom nix-env-install-cachix-executable "cachix"
"Executable of cachix."
:group 'nix-env-install
:type 'file)
(defcustom nix-env-install-process-environment '("NIX_BUILD_SHELL")
"Additional environment variables for processes.
The entries in this variables are added to `process-environment'
and possibly overrides existing ones.
If you set NIX_BUILD_SHELL to something like zsh in your shell,
you need to unset it when you run commands in \"nix-shell\".
This variable allows that."
:group 'nix-env-install
:type '(repeat string))
(defcustom nix-env-install-process-filter #'nix-env-install-default-process-filter
"Filter function for processes."
:group 'nix-env-install
:type 'function)
(defcustom nix-env-install-process-output-filter
(cond
;; Function from xterm-color package.
;; Seems to properly propertizes colorized output from terminal
;; commands.
((fboundp #'xterm-color-filter)
#'xterm-color-filter)
;; Function from ansi-color package.
;; Doesn't colorize terminal commands when TERM is set to dumb,
;; but it gets rid of ugly escape sequences.
((fboundp #'ansi-color-filter-apply)
#'ansi-color-filter-apply))
"Function used to handle output from processes."
:group 'nix-env-install
:type '(choice function nil))
(defcustom nix-env-install-delete-process-window t
"When non-nil, delete the process window on success."
:group 'nix-env-install
:type 'boolean)
(defcustom nix-env-install-npm-node2nix-options "--nodejs-10"
"Additional command line arguments for node2nix."
:group 'nix-env-install
:type 'string)
(defvar nix-env-install-start-process-hook nil
"Hook to run immediately after creating a process in this package.
This hook is run in process buffers, so you can use it to
convert the output of each program, for example.")
(defvar nix-env-install-process-window nil)
;;;; Utility functions
(cl-defun nix-env-install--start-process (name buffer command
&key
clear-buffer
(display-buffer nix-env-install-display-process-buffers)
on-finished on-error
cleanup)
"Start an asynchronous process for running a system command.
NAME, BUFFER, and COMMAND are the same as in `make-process'.
COMMAND is a list of an executable name and arguments.
When CLEAR-BUFFER is non-nil, the buffer is erased before the
process starts.
When DISPLAY-BUFFER is non-nil, the buffer is displayed.
If it is a function, it is used to display the buffer.
Otherwise, `nix-env-install-display-buffer' is used.
ON-FINISHED is a function called after a successful exit of the
command. ON-ERROR is a function called when the process exits
with a non-zero exit code.
CLEANUP is a function whenever the process exits."
(declare (indent 1))
(when (and clear-buffer
(get-buffer buffer))
(with-current-buffer buffer
(erase-buffer)))
(let* ((sentinel (lambda (proc event)
(with-current-buffer (process-buffer proc)
(goto-char (point-max)))
(unless (process-live-p proc)
(when cleanup
(funcall cleanup)))
(cond
((and (equal event "finished\n")
on-finished)
(funcall on-finished))
((or (string-match (rx "exited abnormally with code " (group (+ digit)))
event)
(string-match (rx "failed with code " (group (+ digit)))
event))
(if on-error
(funcall on-error)
(message "Process %s has exited with %s" name
(match-string 1 event)))))))
(proc (let ((process-environment (nix-env-install--make-process-environment)))
(make-process :name name
:buffer buffer
:command command
:filter nix-env-install-process-filter
:sentinel sentinel))))
(with-current-buffer (process-buffer proc)
(setq-local header-line-format nil)
(run-hooks 'nix-env-install-start-process-hook)
(when display-buffer
(funcall (if (functionp display-buffer)
display-buffer
nix-env-install-display-buffer)
(current-buffer))))))
(defun nix-env-install-default-process-filter (proc str)
"Default process filter.
PROC is the process, and STR is the string."
(let* ((buf (process-buffer proc))
(window (get-buffer-window buf)))
(when window
(with-selected-window window
(goto-char (point-max))
(insert (if nix-env-install-process-output-filter
(funcall nix-env-install-process-output-filter str)
str))
(recenter -1)))))
(defun nix-env-install--make-process-environment ()
"Make a new value of `process-enviroment' with `nix-env-install-process-environment' merged."
(let ((alist (mapcar (lambda (raw-form)
(cons (car (nix-env-install--parse-environment raw-form)) raw-form))
process-environment)))
(dolist (raw-form nix-env-install-process-environment)
(let* ((our-env (nix-env-install--parse-environment raw-form))
(base-env (assoc (car our-env) alist)))
(if base-env
(setcdr base-env (cdr our-env))
(push (cons (car our-env) raw-form) alist))))
(mapcar #'cdr alist)))
(defun nix-env-install--parse-environment (raw-form)
"Return a cell of key and value from the RAW-FORM of a key=value pair.
If the equal sign is not contained in the form, it returns a cell
where the key is the form and the value is nil."
(if (string-match (rx bol (group (+ (not (any "="))))
"=" (group (+? anything)) eol)
raw-form)
(cons (match-string 1 raw-form)
(match-string 2 raw-form))
(cons raw-form nil)))
(defun nix-env-install--delete-process-window ()
"Delete the window for processes."
(when (and nix-env-install-delete-process-window
nix-env-install-process-window
(window-live-p nix-env-install-process-window))
(delete-window nix-env-install-process-window)))
(defun nix-env-install--display-buffer-default (buffer)
"Display BUFFER in a new dedicated window."
(if-let ((window (and nix-env-install-process-window
(window-live-p nix-env-install-process-window)
nix-env-install-process-window)))
(progn
(when (window-dedicated-p window)
(set-window-dedicated-p window nil))
(set-window-buffer window buffer)
(set-window-dedicated-p window t))
(setq nix-env-install-process-window
(display-buffer-in-side-window
buffer
`((side . bottom)
(height . ,nix-env-install-window-height))))))
;;;; Cachix support
(defconst nix-env-install-cachix-buffer "*nix-env-install cachix*")
(defun nix-env-install-cachix-exists-p ()
"Return non-nil if there is cachix executable."
(or (file-executable-p nix-env-install-cachix-executable)
(executable-find nix-env-install-cachix-executable)))
(cl-defun nix-env-install-cachix (&key (on-finished #'nix-env-install-cachix-setup))
"Install cachix, if you haven't already.
When the installation process is finished, ON-FINISHED is called."
(interactive)
(if (nix-env-install-cachix-exists-p)
(when (called-interactively-p 'interactive)
(message "Cachix is already installed"))
(nix-env-install--start-process
"nix-env" nix-env-install-cachix-buffer
'("nix-env" "-iA" "cachix"
"-f" "https://cachix.org/api/v1/install")
:on-finished on-finished)))
(defun nix-env-install-cachix-setup ()
"Start an interactive setup of cachix."
(browse-url "https://cachix.org/")
(message "After cachix is installed, follow the instructions to set up your account."))
;;;###autoload
(defun nix-env-install-cachix-use (name)
"Enable binary cache of NAME."
(interactive "sCachix: ")
(if (nix-env-install-cachix-exists-p)
(nix-env-install--start-process
"cachix" nix-env-install-cachix-buffer
(list nix-env-install-cachix-executable "use" name)
:display-buffer nil
:on-finished
(lambda ()
(nix-env-install--delete-process-window)
(message "Successfully enabled cachix from %s" name)))
(when (yes-or-no-p "Cachix is not installed yet. Install it? ")
(nix-env-install-cachix :on-finished `(lambda () (nix-env-install-cachix-use ,name))))))
;;;; Uninstallation command
;;;###autoload
(defun nix-env-install-uninstall (package)
"Uninstall PACKAGE installed by nix-env."
(interactive (list (completing-read "Package: "
(process-lines "nix-env" "-q"))))
(message (shell-command-to-string
(format "nix-env -e %s" (shell-quote-argument package)))))
;;;; NPM for JavaScript/TypeScript
(defconst nix-env-install-npm-buffer "*nix-env-install npm*")
(defcustom nix-env-install-npm-install-hook nil
"Hooks called after installation of npm packages."
:type 'hook
:group 'nix-env-install)
(defun nix-env-install--node2nix-temp-dir ()
"Generate a temporary directory for node2nix."
(string-trim-right
(shell-command-to-string "mktemp -d -t emacs-node2nix-XXX")))
;;;###autoload
(defun nix-env-install-npm (packages)
"Install PACKAGES from npm using Nix."
(interactive (list (split-string (read-string "npm packages: ") " ")))
(unless packages
(user-error "PACKAGES cannot be nil"))
(let* ((tmpdir (nix-env-install--node2nix-temp-dir))
(default-directory tmpdir)
(packages-json-file (expand-file-name "npm-packages.json" tmpdir)))
(with-temp-buffer
(insert (json-encode (cl-typecase packages
(list packages)
(string (list packages)))))
(write-region (point-min) (point-max) packages-json-file))
(message "Generating Nix expressions using node2nix for %s..." packages)
(nix-env-install--start-process
"nix-env-install-node2nix" nix-env-install-npm-buffer
`("nix-shell" "-p" "nodePackages.node2nix"
"--run" ,(format "node2nix -i %s %s" packages-json-file
nix-env-install-npm-node2nix-options))
:clear-buffer t
:on-finished
`(lambda ()
(message "Installing npm packages using nix-env...")
(nix-env-install--start-process
"nix-env"
nix-env-install-npm-buffer
',(apply #'append
(list "nix-env"
"-f" (expand-file-name "default.nix" tmpdir)
"-i")
(mapcar (lambda (it) (list "-A"
;; Quote explicitly to support packages including dots, e.g. mermaid.cli
(format "\"%s\"" it)))
(cl-etypecase packages
(list packages)
(string (list packages)))))
:on-finished
,`(lambda ()
(nix-env-install--delete-process-window)
(message "Finished installing npm packages: %s" (quote ,packages))
(run-hooks 'nix-env-install-npm-install-hook))
:cleanup
,`(lambda () (delete-directory ,tmpdir t)))))))
(provide 'nix-env-install)
;;; nix-env-install.el ends here