Skip to content

Instantly share code, notes, and snippets.

@haselwarter
Created January 11, 2026 16:07
Show Gist options
  • Select an option

  • Save haselwarter/ba30eb7c63d1d9e2635ce5801b2ac5b5 to your computer and use it in GitHub Desktop.

Select an option

Save haselwarter/ba30eb7c63d1d9e2635ce5801b2ac5b5 to your computer and use it in GitHub Desktop.
opam from env
(defun opam-from-env ()
"Set environment and Emacs `exec-path' and `load-path' variables according to the opam configuration of the current working directory."
(interactive nil)
(let* ((opam-config (car (read-from-string (shell-command-to-string "opam config env --sexp"))))
(_ignore (dolist (var opam-config) (setenv (car var) (cadr var))))
(opam-switch-prefix (file-name-as-directory (getenv "OPAM_SWITCH_PREFIX")))
(opam-path-p (lambda (p) (string-match-p "/[._]opam/" p)))
(opam-path
(car (seq-filter opam-path-p (split-string (cadr (assoc "PATH" opam-config)) ":"))))
(f (lambda (d) (if (funcall opam-path-p d) opam-path d)))
(g (lambda (d) (replace-regexp-in-string
".*/[._]opam/.*/share/emacs/site-lisp"
(concat opam-switch-prefix "/share/emacs/site-lisp") d))))
(setq exec-path (mapcar f exec-path))
(setq load-path (mapcar g load-path))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment