Subject: [PATCH 0/3] Add 'guix shell --check'
From: Ludovic Courtès <[email-masked]>
To: [email-masked]
Cc: Ludovic Courtès <[email-masked]>
Date: Tue, 19 Oct 2021 12:03:51 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello Guix!

You may be familiar with the crucial footnote about shell startup files:

  https://guix.gnu.org/manual/en/html_node/Invoking-guix-environment.html#FOOT11

In short, if your shell startup file mess up with environment variables,
‘--pure’ won’t deliver its promise.  That’s a common source of annoyance.

This patch set adds a ‘--check’ option to ‘guix shell’ (and ‘guix environment’
as well, mostly out of simplicity).  Here’s an example:

--8<---------------cut here---------------start------------->8---
$ tail -2 ~/.bashrc
echo "hi from bashrc!"
export PATH=muahahaaa:$PATH
$ ./pre-inst-env guix shell --check coreutils 
guix shell: checking the environment variables visible from shell '/gnu/store/87kif0bpf0anwbsaw0jvg8fyciw4sz67-bash-5.0.16/bin/bash'...
guix shell: warning: variable 'PATH' is clobbered: 'muahahaaa:/gnu/store/cq8sfmm6in0r4nfx0zxjvx1fg5ky6a7r-profile/bin'
hint: One or more environment variables have a different value in the shell than the one we set.
This means that you may find yourself running code in an environment different from the one
you asked Guix to prepare.

This usually indicates that your shell startup files are unexpectedly modifying those
environment variables.  For example, if you are using Bash, make sure that environment
variables are set or modified in `~/.bash_profile' and _not_ in `~/.bashrc'.  For more
information on Bash startup files, run:

     info "(bash) Bash Startup Files"

Alternatively, you can avoid the problem by passing the `--container' or `-C' option.  That
will give you a fully isolated environment running in a "container", immune to the issue
described above.
--8<---------------cut here---------------end--------------->8---

Once I’ve cleaned up ~/.bashrc, I can check again and proceed:

--8<---------------cut here---------------start------------->8---
$ ./pre-inst-env guix shell --check coreutils 
guix shell: checking the environment variables visible from shell '/gnu/store/87kif0bpf0anwbsaw0jvg8fyciw4sz67-bash-5.0.16/bin/bash'...
guix shell: All is good!  The shell gets correct environment variables.
[env]$ uname -o
GNU/Linux
--8<---------------cut here---------------end--------------->8---

This works by launching an interactive shell in a pseudo-terminal, and
running “env” or “set” in that shell to retrieve the list of environment
variables once it has sourced its startup files.

It works well with Bash, Zsh, and presumably any POSIX shell.  It kinda
works with fish, but that’s not entirely clear.

To allow users to learn about this option, the last patch has ‘guix shell’
print a suggest to run ‘--check’ if (1) the hint has never been printed,
(2) the user is doing an interactive session, and (3) the user did not
pass ‘--container’ or ‘--search-paths’.

Thoughts?

Ludo’.

Ludovic Courtès (3):
  syscalls: Add 'openpty' and 'login-tty'.
  environment: Add '--check'.
  shell: Suggest running '--check' once for interactive use.

 doc/guix.texi                |  39 ++++++---
 guix/build/syscalls.scm      |  39 +++++++++
 guix/scripts/environment.scm | 162 ++++++++++++++++++++++++++++++++++-
 guix/scripts/shell.scm       |  41 ++++++++-
 tests/syscalls.scm           |  35 ++++++++
 5 files changed, 303 insertions(+), 13 deletions(-)

base-commit: 467d599bd972f16a958a394827bc925bc497c19a
-- 
2.33.0