unofficial mirror of help-guix@gnu.org 
 help / color / mirror / Atom feed
* set environment variables with guix shell [-m manifest.scm]
@ 2022-05-05  0:53 Kenny Ballou
  2022-05-05  2:29 ` Kenny Ballou
  2022-05-05  9:05 ` zimoun
  0 siblings, 2 replies; 4+ messages in thread
From: Kenny Ballou @ 2022-05-05  0:53 UTC (permalink / raw)
  To: help-guix


Dear list,

I have been using nix-shell for a while now to have on-demand/ad-hoc
development environments for different projects.  These are hooked in
with direnv and the whole experience is quite nice.  However, I would
like to translate these to Guix because I find the tooling around Guix
and Scheme far better and easier.

However, one necessary feature of `guix shell` I'm not seeing: ability
to set arbitrary environment variables when `guix shell` loads.

Certainly, I thought I could just add some `(setenv "FOO" "bar")` in the
`manifest.scm` file.  But if the environment is reloaded, these
statements do not seem to persist.

For example, I've been working on a manifest for my current main
project, it has the following, elided for brevity, contents:

```
(define z3-with-java
  (package
    (inherit z3)
    ...))

(let ((z3-dir (run-with-store (open-connection)
                (package-file z3-with-java))))
  (setenv "LD_LIBRARY_PATH" (string-append z3-dir "/lib"))
  (setenv "Z3_DIR" z3-dir))

(packages->manifest
  (list openjdk11
        ...
        z3-with-java))
```

Perhaps, this is not the right way to do this?  Is there a different
mechanism I am not aware of?  Would this be a good extension to `guix
shell` manifests?

This would not be an issue if I did not need the store path of the
package.  I could simply set the environment variables in the `.envrc`
file.  However, I still think it would be preferable to have a mechanism
for setting environment variables via `guix shell` similar to `nix
shell`.

Thoughts?

My gratitude in advance.

-Kenny

-- 
:SIG:!0xB0CAA28A02958308!:


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: set environment variables with guix shell [-m manifest.scm]
  2022-05-05  0:53 set environment variables with guix shell [-m manifest.scm] Kenny Ballou
@ 2022-05-05  2:29 ` Kenny Ballou
  2022-05-05  9:05 ` zimoun
  1 sibling, 0 replies; 4+ messages in thread
From: Kenny Ballou @ 2022-05-05  2:29 UTC (permalink / raw)
  To: help-guix



> ```
> (let ((z3-dir (run-with-store (open-connection)
>                 (package-file z3-with-java))))
>   (setenv "LD_LIBRARY_PATH" (string-append z3-dir "/lib"))
>   (setenv "Z3_DIR" z3-dir))
> ...
> ```

Because I cannot sit well enough alone.  I have learned some more
things: it appears these `setenv` calls work if manually invoking `guix
shell`.  However, they do not work when using direnv to invoke `guix
shell`.  This may come down to _how_ direnv invokes `guix shell`.
Worse, if corrected, direnv may not be able to guarantee the `setenv`
variables are hermetic to the project since it seems to be completely
unaware of them (i.e., leaving the directory may not unset the
added environment variables).

Perhaps, this helps stir some insight?

-Kenny


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: set environment variables with guix shell [-m manifest.scm]
  2022-05-05  0:53 set environment variables with guix shell [-m manifest.scm] Kenny Ballou
  2022-05-05  2:29 ` Kenny Ballou
@ 2022-05-05  9:05 ` zimoun
  2022-05-06 22:20   ` Kenny Ballou
  1 sibling, 1 reply; 4+ messages in thread
From: zimoun @ 2022-05-05  9:05 UTC (permalink / raw)
  To: Kenny Ballou, help-guix

Hi,

On Wed, 04 May 2022 at 18:53, Kenny Ballou <kb@devnulllabs.io> wrote:

> However, one necessary feature of `guix shell` I'm not seeing: ability
> to set arbitrary environment variables when `guix shell` loads.

Yes, it misses an equivalent to ’nix-shell’ «shellHook» [1], as also
reported by [2] Well, maybe you could achieve something via ’--file’
instead of ’--manifest’, I do not know.

Since you use Direnv [3], maybe you can set the environment variables
there instead of in the manifest file.


1: <https://nixos.org/manual/nix/stable/command-ref/nix-shell.html>
2: <https://yhetil.org/guix/E92751B5-F3ED-47A0-B7D2-40F9D3F5C5B9@univ-grenoble-alpes.fr>
3: <https://github.com/direnv/direnv/wiki/GNU-Guix>

Cheers,
simon


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: set environment variables with guix shell [-m manifest.scm]
  2022-05-05  9:05 ` zimoun
@ 2022-05-06 22:20   ` Kenny Ballou
  0 siblings, 0 replies; 4+ messages in thread
From: Kenny Ballou @ 2022-05-06 22:20 UTC (permalink / raw)
  To: zimoun, help-guix


On 2022-05-05 11:05 +02, zimoun wrote:
> Wed, 04 May 2022 at 18:53, Kenny Ballou <kb@devnulllabs.io> wrote:
>
>> However, one necessary feature of `guix shell` I'm not seeing: ability
>> to set arbitrary environment variables when `guix shell` loads.
>
> Yes, it misses an equivalent to ’nix-shell’ «shellHook» [1], as also
> reported by [2] Well, maybe you could achieve something via ’--file’
> instead of ’--manifest’, I do not know.

I was not aware of [2], thanks for pointing me to it.

I have not tried `--file`.  My understanding is that this does not quite
do the same thing.

Unrelated to search paths below, I had an idea that it would be possible
to construct a derivation which sets the environment variables and add
the resulting derivation to the "manifest".  However, I suspect I'm
misunderstanding the `#:env-vars` key to the `gexp->derivation`[5]
function.

Along this idea though, how difficult would it be to add an optional
parameter to `*->manifest` functions such that it constructs the profile
with additional environment variables?  Is that desirable?

>
> Since you use Direnv [3], maybe you can set the environment variables
> there instead of in the manifest file.

While this would work in general, I'm trying to add things that directly
reference computed store paths.  I'm not sure how I can easily achieve
this with `direnv`.

For example, I'm trying to set `LD_LIBRARY_PATH` to point to the `lib`
directory of the `z3` package.  Furthermore, I need to set `JAVA_HOME`
to the store path of `openjdk11-jdk`.


> 1: <https://nixos.org/manual/nix/stable/command-ref/nix-shell.html>
> 2: <https://yhetil.org/guix/E92751B5-F3ED-47A0-B7D2-40F9D3F5C5B9@univ-grenoble-alpes.fr>
> 3: <https://github.com/direnv/direnv/wiki/GNU-Guix>


I've had some success setting search paths[4], as here:

```
(define z3-with-java
  (package
   (inherit z3-4.8.10)
   (native-inputs
    `(("which" ,which)
      ("python" ,python-wrapper)
      ("jdk" ,java:openjdk11 "jdk")))
   (arguments
    `(#:tests? #f
      #:validate-runpath? #f
      #:phases
      (modify-phases %standard-phases
                     (add-after 'unpack 'set-JDK_HOME
                                (lambda* (#:key inputs #:allow-other-keys)
                                  (setenv "JDK_HOME" (assoc-ref inputs "jdk"))
                                  #t))
                     (replace 'configure
                              (lambda* (#:key inputs outputs #:allow-other-keys)
                                (invoke "python" "scripts/mk_make.py"
                                        "--java"
                                        (string-append "--prefix=" (assoc-ref outputs "out")))))
                     (add-after 'configure 'change-directory
                                (lambda _
                                  (chdir "build")
                                  #t)))))
   (native-search-paths
    (list (search-path-specification
           (variable "LD_LIBRARY_PATH")
           (separator #f)
           (files (list "lib/")))
          (search-path-specification
           (variable "Z3_DIR")
           (separator #f)
           (files (list "")))))))
```

Unrelated to this whole thing, and likely in need of a new thread, how
do I translate the above package definition which overrides the inputs
to use the new gexp style[6]?

-Kenny

[4]: https://guix.gnu.org/en/manual/devel/en/guix.html#Search-Paths
[5]: https://guix.gnu.org/en/manual/devel/en/guix.html#index-gexp_002d_003ederivation
[6]: https://guix.gnu.org/en/blog/2021/the-big-change/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-05-06 22:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-05  0:53 set environment variables with guix shell [-m manifest.scm] Kenny Ballou
2022-05-05  2:29 ` Kenny Ballou
2022-05-05  9:05 ` zimoun
2022-05-06 22:20   ` Kenny Ballou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).