Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Misc code for using emacsclient with nix-shell
(defun nix-shell-c-system-header-paths ()
"Extract header search paths from the NIX_CFLAGS_COMPILE env variable"
(cl-labels ((every-other (li &optional pick)
(when li
(if pick
(cons (car li) (every-other (cdr li)))
(every-other (cdr li) (not pick))))
))
(let* ((cflags (getenv "NIX_CFLAGS_COMPILE")))
(every-other (split-string cflags)))))
(defun nix-shell-add-company-c-headers (&optional paths)
(setq company-c-headers-path-system
(append company-c-headers-path-system
(or paths (nix-shell-c-system-header-paths)))))
(defun nix-shell-add-c-headers-paths ()
(let ((paths (nix-shell-c-system-header-paths)))
(nix-shell-set-company-c-headers paths)
;; Why the fuck doesn't ffap-c know about semantic-dependency-system-include-path?
(setq ffap-c-path
(append ffap-c-path paths))
(dolist (path paths)
(semantic-add-system-include path major-mode))))
(defun nix-shell--split-env-var-str (varstr)
"Split VARSTR - a 'VAR=VALUE' string"
(let ((i (find ?= varstr)))
(cons (substring varstr 0 i)
(substring varstr (+ i 1))))
)
;; See https://github.com/travisbhartwell/nix-emacs/blob/master/nix-sandbox.el
;; for useful helper functions and a candidate for a future home
(defun nix-shell-load-env (nix-file)
"Loads the environment defined by NIX-FILE. Runs nix-shell to get the
environment and makes process-environment buffer local"
(interactive "fnix-file")
(let ((environment
(with-temp-buffer
(let* ((nix-file-shell-safe (shell-quote-argument nix-file))
(command (format "nix-shell %s --command 'printenv -0' 2>/dev/null"
nix-file-shell-safe))
(status (call-process-shell-command command nil t)))
(when (> status 0)
(message "'nix-shell %s' failed with %d" nix-file-shell-safe status)
(return nil))
(split-string (buffer-string) "\0")))))
(when environment
(set (make-local-variable 'process-environment)
environment))
))
;; (split-string (shell-command-to-string "printenv -0") "\0" t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment