tvl-depot/tools/emacs-pkgs/nix-util/nix-util.el
sterni 88eff2080c refactor(tools/emacs-pkgs): move sly-from-depot to tvl.el
The function is depot specific and thus uses tvl-depot-path, so it
belongs in `tvl.el`. Since non-sly-users won't need it, we tie its
definition to loading the sly package.

Change-Id: I8b104deab455d218d3df6a800e35cc104220a841
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4960
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
2022-01-17 15:02:53 +00:00

69 lines
2.4 KiB
EmacsLisp

;;; nix-util.el --- Utilities for dealing with Nix code. -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2019 Google Inc.
;; Copyright (C) 2022 The TVL Authors
;;
;; Author: Vincent Ambo <tazjin@google.com>
;; Version: 1.0
;; Package-Requires: (json map s)
;;
;;; Commentary:
;;
;; This package adds some functionality that I find useful when
;; working in Nix buffers or programs installed from Nix.
(require 'json)
(require 'map)
(require 's)
(defun nix/prefetch-github (owner repo) ; TODO(tazjin): support different branches
"Fetch the master branch of a GitHub repository and insert the
call to `fetchFromGitHub' at point."
(interactive "sOwner: \nsRepository: ")
(let* (;; Keep these vars around for output insertion
(point (point))
(buffer (current-buffer))
(name (concat "github-fetcher/" owner "/" repo))
(outbuf (format "*%s*" name))
(errbuf (get-buffer-create "*github-fetcher/errors*"))
(cleanup (lambda ()
(kill-buffer outbuf)
(kill-buffer errbuf)
(with-current-buffer buffer
(read-only-mode -1))))
(prefetch-handler
(lambda (_process event)
(unwind-protect
(pcase event
("finished\n"
(let* ((json-string (with-current-buffer outbuf
(buffer-string)))
(result (json-read-from-string json-string)))
(with-current-buffer buffer
(goto-char point)
(map-let (("rev" rev) ("sha256" sha256)) result
(read-only-mode -1)
(insert (format "fetchFromGitHub {
owner = \"%s\";
repo = \"%s\";
rev = \"%s\";
sha256 = \"%s\";
};" owner repo rev sha256))
(indent-region point (point))))))
(_ (with-current-buffer errbuf
(error "Failed to prefetch %s/%s: %s"
owner repo (buffer-string)))))
(funcall cleanup)))))
;; Fetching happens asynchronously, but we'd like to make sure the
;; point stays in place while that happens.
(read-only-mode)
(make-process :name name
:buffer outbuf
:command `("nix-prefetch-github" ,owner ,repo)
:stderr errbuf
:sentinel prefetch-handler)))
(provide 'nix-util)