Environment Variables and Eshell Part 2 (publ. 2024-09-25)
Didn't mean to make this a two part post, but I ran out of lunch-break time yesterday.
Anyways, I see that the emacs-guix interface has a "shell" menu, which presumably mirrors "guix shell" functionality in some way, but it is broken at present. I have submitted a bug report. It also has a "set Emacs environment" command, but it does not seem to be quite what I am looking for. You pass it the path to a profile, and it supposedly makes the emacs environment match that profile. So, conceivably I could build the profile with guix shell, and then pass the $GUIX_ENVIRONMENT to the "set Emacs environment" command.
Presumably I could also use the emacs-guix REPL to get a list of environment variables and then set those in Eshell, but I would need to do a lot more research to get there, for the correct syntax and such.
An approach which seemed in principle to be very simple was to just use the run "*guix shell <arguments> -- env" within eshell, get the environment variables outputted, and then set them within eshell. In practice, there a lot of nuances to this which make it a difficult affair, such as capturing the correct output and sourcing the variables correctly. Eventually, I was able to get there, however, with this eshell script:
eshell-debug error
set #'temppath (make-temp-file "guix-env")
*guix shell $* -- bash -c (concat "env > " temppath)
prefix-exports (concat temppath)
. (concat temppath)
The prefix-exports function is as follows:
(defun prefix-exports (file-path)
(with-temp-buffer
(insert-file-contents file-path)
(goto-char (point-min))
(let ((lines-left 0))
(while (= lines-left 0)
(insert "export ")
(setq lines-left (forward-line 1))))
(write-region nil nil file-path)))
So I can, e.g., call
~ $ . ~/local/bin/guix-env.esh hello
/tmp/guix-envCixnA8
Variable ‘PWD’ is not settable
Variable ‘_’ is not settable
Variable ‘LINES’ is not settable
Variable ‘COLUMNS’ is not settable
Variable ‘OLDPWD’ is not settable
~ $ hello
Hello, world!
~ $
I get the error about the variables because those are built-in variables in Eshell, and are therefore cannot be set (unlike in bash). But the other variables are set, so it still works. Notice that I used a call to "bash -c" in the second line of the script, as a means of getting the env output redirected into a file without getting it mixed up with the status output from "guix shell".
Not saying this is the best approach, but it works in a hackish way. Perhaps I will have time in the future to explore some other approach.
Copyright
This work © 2024 by Christopher Howard is licensed under Attribution-ShareAlike 4.0 International.
CC BY-SA 4.0 Deed