-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
nix-instantiate.el
94 lines (81 loc) · 3.14 KB
/
nix-instantiate.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
;;; nix-instantiate.el --- Run nix commands -*- lexical-binding: t -*-
;; Author: Matthew Bauer <[email protected]>
;; Homepage: https://github.com/NixOS/nix-mode
;; Keywords: nix
;; This file is NOT part of GNU Emacs.
;;; Commentary:
;;; Code:
(require 'nix)
(require 'json)
(defun nix-instantiate--parsed (drv)
"Get the parsed version of the .drv file.
DRV file to load from."
(cdar
(nix--process-json "show-derivation" drv)))
(defun nix-instantiate (nix-file &optional attribute parse)
"Run nix-instantiate on a Nix expression.
NIX-FILE the file to instantiate.
ATTRIBUTE an attribute of the Nix file to use.
PARSE whether to parse nix-instantiate output."
(interactive (list (read-file-name "Nix file: ") nil t))
(let ((stdout (generate-new-buffer "nix-instantiate"))
result)
(if attribute
(call-process nix-instantiate-executable nil (list stdout nil) nil
nix-file "-A" attribute)
(call-process nix-instantiate-executable nil (list stdout nil) nil
nix-file))
(with-current-buffer stdout
(when (eq (buffer-size) 0)
(error
"Error: nix-instantiate %s failed to produce any output"
nix-file))
(setq result (substring (buffer-string) 0 (- (buffer-size) 1)))
(when parse
(setq result (nix-instantiate--parsed result))))
(kill-buffer stdout)
result))
(defvar nix-instantiate--running-processes nil)
(defun nix-instantiate--sentinel (prop err proc event)
"Make a nix-instantiate process.
PROP the prop name of nix-instantiate--running-processes.
ERR the error buffer.
PROC the process that has been run.
EVENT the event that was fired."
(when (string= event "finished\n")
(with-current-buffer (process-buffer proc)
(unless (eq (buffer-size) 0)
(let ((drv (nix-instantiate--parsed
(substring (buffer-string) 0 (- (buffer-size) 1)))))
(dolist
(callback (lax-plist-get nix-instantiate--running-processes prop))
(funcall callback drv)))))
(setq nix-instantiate--running-processes
(lax-plist-put nix-instantiate--running-processes prop nil)))
(unless (process-live-p proc)
(kill-buffer (process-buffer proc))
(kill-buffer err)))
(defun nix-instantiate-async (callback nix-file &optional attribute)
"Run nix-instantiate on a Nix expression, asynchronously.
CALLBACK the function to call when instantiate completes.
NIX-FILE the file to instantiate
ATTRIBUTE an attribute of the Nix file to use."
(setq nix-file (expand-file-name nix-file))
(let* ((prop (if attribute
(expand-file-name attribute nix-file) nix-file))
(data (lax-plist-get nix-instantiate--running-processes prop))
(stdout (generate-new-buffer "nix-instantiate"))
(stderr (generate-new-buffer "nix-instantiate error")))
(setq nix-instantiate--running-processes
(lax-plist-put nix-instantiate--running-processes
prop (cons callback data)))
(make-process
:name "nix-instantiate"
:buffer stdout
:command (append (list nix-instantiate-executable nix-file)
(when attribute (list "-A" attribute)))
:noquery t
:sentinel (apply-partially 'nix-instantiate--sentinel prop stderr)
:stderr stderr)))
(provide 'nix-instantiate)
;;; nix-instantiate.el ends here