Klimi's new dotfiles with stow.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

105 líneas
3.5 KiB

hace 4 años
  1. ;;; nix-instantiate.el -- run nix commands in Emacs -*- lexical-binding: t -*-
  2. ;; Author: Matthew Bauer <mjbauer95@gmail.com>
  3. ;; Homepage: https://github.com/NixOS/nix-mode
  4. ;; Keywords: nix
  5. ;; Version: 1.4.0
  6. ;; This file is NOT part of GNU Emacs.
  7. ;;; Commentary:
  8. ;;; Code:
  9. (require 'nix)
  10. (require 'json)
  11. (defun nix-instantiate--parsed (drv)
  12. "Get the parsed version of the .drv file.
  13. DRV file to load from."
  14. (let ((stdout (generate-new-buffer "nix show-derivation"))
  15. result)
  16. (call-process nix-executable nil (list stdout nil) nil
  17. "show-derivation" drv)
  18. (setq result
  19. (cdar (with-current-buffer stdout
  20. (when (eq (buffer-size) 0)
  21. (error "Nix’s show-derivation %s failed to produce any output"
  22. drv))
  23. (goto-char (point-min))
  24. (json-read))))
  25. (kill-buffer stdout)
  26. result))
  27. (defun nix-instantiate (nix-file &optional attribute parse)
  28. "Run nix-instantiate on a Nix expression.
  29. NIX-FILE the file to instantiate.
  30. ATTRIBUTE an attribute of the Nix file to use.
  31. PARSE whether to parse nix-instantiate output."
  32. (interactive (list (read-file-name "Nix file: ") nil t))
  33. (let ((stdout (generate-new-buffer "nix-instantiate"))
  34. result)
  35. (if attribute
  36. (call-process nix-instantiate-executable nil (list stdout nil) nil
  37. nix-file "-A" attribute)
  38. (call-process nix-instantiate-executable nil (list stdout nil) nil
  39. nix-file))
  40. (with-current-buffer stdout
  41. (when (eq (buffer-size) 0)
  42. (error
  43. "Error: nix-instantiate %s failed to produce any output"
  44. nix-file))
  45. (setq result (substring (buffer-string) 0 (- (buffer-size) 1)))
  46. (when parse
  47. (setq result (nix-instantiate--parsed result))))
  48. (kill-buffer stdout)
  49. result))
  50. (defvar nix-instantiate--running-processes nil)
  51. (defun nix-instantiate--sentinel (prop err proc event)
  52. "Make a nix-instantiate process.
  53. PROP the prop name of nix-instantiate--running-processes.
  54. ERR the error buffer.
  55. PROC the process that has been run.
  56. EVENT the event that was fired."
  57. (when (string= event "finished\n")
  58. (with-current-buffer (process-buffer proc)
  59. (unless (eq (buffer-size) 0)
  60. (let ((drv (nix-instantiate--parsed
  61. (substring (buffer-string) 0 (- (buffer-size) 1)))))
  62. (dolist
  63. (callback (lax-plist-get nix-instantiate--running-processes prop))
  64. (funcall callback drv)))))
  65. (setq nix-instantiate--running-processes
  66. (lax-plist-put nix-instantiate--running-processes prop nil)))
  67. (unless (process-live-p proc)
  68. (kill-buffer (process-buffer proc))
  69. (kill-buffer err)))
  70. (defun nix-instantiate-async (callback nix-file &optional attribute)
  71. "Run nix-instantiate on a Nix expression, asynchronously.
  72. CALLBACK the function to call when instantiate completes.
  73. NIX-FILE the file to instantiate
  74. ATTRIBUTE an attribute of the Nix file to use."
  75. (setq nix-file (expand-file-name nix-file))
  76. (let* ((prop (if attribute
  77. (expand-file-name attribute nix-file) nix-file))
  78. (data (lax-plist-get nix-instantiate--running-processes prop))
  79. (stdout (generate-new-buffer "nix-instantiate"))
  80. (stderr (generate-new-buffer "nix-instantiate error")))
  81. (setq nix-instantiate--running-processes
  82. (lax-plist-put nix-instantiate--running-processes
  83. prop (cons callback data)))
  84. (make-process
  85. :name "nix-instantiate"
  86. :buffer stdout
  87. :command (append (list nix-instantiate-executable nix-file)
  88. (when attribute (list "-A" attribute)))
  89. :noquery t
  90. :sentinel (apply-partially 'nix-instantiate--sentinel prop stderr)
  91. :stderr stderr)))
  92. (provide 'nix-instantiate)
  93. ;;; nix-instantiate.el ends here