unofficial mirror of help-guix@gnu.org 
 help / color / mirror / Atom feed
* Black magic to pass around environment variables?
@ 2021-03-13  3:34 raingloom
  2021-03-13  9:05 ` Ricardo Wurmus
  0 siblings, 1 reply; 3+ messages in thread
From: raingloom @ 2021-03-13  3:34 UTC (permalink / raw)
  To: help-guix@gnu.org

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?


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

end of thread, other threads:[~2021-03-14  1:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-13  3:34 Black magic to pass around environment variables? raingloom
2021-03-13  9:05 ` Ricardo Wurmus
2021-03-13 23:49   ` raingloom

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).