unofficial mirror of help-guix@gnu.org 
 help / color / mirror / Atom feed
From: raingloom <raingloom@riseup.net>
To: "help-guix@gnu.org" <help-guix@gnu.org>
Subject: Black magic to pass around environment variables?
Date: Sat, 13 Mar 2021 04:34:10 +0100	[thread overview]
Message-ID: <20210313043410.2366cddc@riseup.net> (raw)

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	other threads:[~2021-03-13  3:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-13  3:34 raingloom [this message]
2021-03-13  9:05 ` Black magic to pass around environment variables? Ricardo Wurmus
2021-03-13 23:49   ` raingloom

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210313043410.2366cddc@riseup.net \
    --to=raingloom@riseup.net \
    --cc=help-guix@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).