help-guix
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Black magic to pass around environment variables?


From: raingloom
Subject: Black magic to pass around environment variables?
Date: Sat, 13 Mar 2021 04:34:10 +0100

Motivation:
xfce4-terminal runs as a terminal and doesn't propagate environment
variables, which makes it a bit cumbersome to use it with complex `guix
environment` setups. While an upstream fix would be nice, this is a
general problem and one that shouldn't require special handling in
every application.

Partial solution:
Saving environment variables is relatively easy, just use `env -0`.
Since they are all C strings, they can't contain null bytes.
Serialization solved.

Loading is a bit more difficult, at least if you want to make the
solution Bourne Shell friendly. Yes, I too am planning to try
Ambrevar's Lisp shell setup, but until then, I need something that works
with regular old (bad) Unix tools.

This is what I have so far:
```
xargs -x0a <(env -0) -- sh -c 'env - $@ tcc -version'
```

Explanation:
xargs:
-a lets us load the environment variables from a file, which sidesteps
expansions
-0 uses null terminated string format, instead of splitting on
whitespace
-x makes sure that even in the unlikely case when we run into execve
limits, xargs still won't split the argument list and will just error
instead

sh:
we need it (or some other wrapper) because xargs can only append
argument lists or insert single arguments, but can't insert argument
lists, so we reorder them to come before the fixed arguments we want to
pass to the command.


This mostly works in isolation, but the most important use case -
starting a new Zsh session - doesn't.

```
xfce4-terminal -x xargs -x0a <(env -0) -- 'env - $@ zsh'
```

This results in a terminal popping up and closing immediately.

It also has the significant downside that trying to wrap it into
a script that passes arbitrary arguments to the command will require
escaping things for sh again.



Any ideas on how to proceed?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]