Skip to content

Instantly share code, notes, and snippets.

@olejorgenb
Last active January 30, 2017 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olejorgenb/b3493841f93dca353c766e62b3ff852d to your computer and use it in GitHub Desktop.
Save olejorgenb/b3493841f93dca353c766e62b3ff852d to your computer and use it in GitHub Desktop.
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