opamnushell

How do I use `eval $(opam env)` with nushell?


opam env doesn't natively support nushell. What command can I use to make it work?

The nushell docs have some info about this problem and there's a hacky way to do it via a Bash shell (not great on Windows).

The powershell output is the easiest to parse, and I almost get all the way there with this command:

opam env --shell=powershell | parse "$env:{key} = '{val}'" | select key val | load-env

It works up until load-env but then says load-env expects a single record and I'm actually feeding it a table. However it seems very non trivial to convert a table to a record for some reason.

I tried this:

opam env --shell=powershell | parse "$env:{key} = '{val}'" | each {|e| load-env {$e.key: $e.val}}

which outputs empty list and no error, but it also doesn't update the environment.


Solution

  • For your first command, you can use transpose -rd to convert the table to a record that works with load-env.

    opam env --shell=powershell | parse "$env:{key} = '{val}'" | transpose -rd | load-env
    

    Also make sure that if you are using this line in a custom command you use the def --env flag so that it doesn't scope the environment. The second command is not working because of this, the load-env is scoped to the closure of the each.