Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cached-nix-shell blocks certain environment variables that nix-shell doesn't. #26

Closed
uri-canva opened this issue Aug 8, 2022 · 2 comments

Comments

@uri-canva
Copy link
Contributor

The list of environment variables that cached-nix-shell keeps (https://sourcegraph.com/github.com/xzfc/cached-nix-shell@1955b9d0aaaf90337541ab799d5071eeaac2360c/-/blob/src/main.rs?L172-184) and that nix-shell keeps (https://sourcegraph.com/github.com/NixOS/[email protected]/-/blob/src/nix-build/nix-build.cc?L107-110) are not in sync. The environment variables that cached-nix-shell keeps in addition are not an issue since the --pure will have the nix-shell invocation strip them anyway if needed, but the ones that are missing lead to a difference in behaviour when using cached-nix-shell as a drop in replacement of nix-shell. For example not keeping TERM results in bash defaulting to dumb and setting it in the environment (https://sourcegraph.com/github.com/bminor/[email protected]/-/blob/variables.c?L496), leading to programs disabling ncurses and similar terminal UIs.

@zopsicle
Copy link

zopsicle commented Mar 28, 2023

Rather than trying to replicate all the behavior of nix-shell, wouldn’t it be easier to record the path to the shell’s .drv file in the cache (instead of the environment variables), and then simply exec nix-shell <drv path> "$@"? nix-shell will then handle the details of setting up the environment, and the behavior would be identical but without the slow expression evaluation.

xzfc added a commit that referenced this issue May 23, 2023
@xzfc xzfc closed this as completed in 212e531 May 24, 2023
@xzfc
Copy link
Owner

xzfc commented May 24, 2023

The environment variables that cached-nix-shell keeps in addition are not an issue since the --pure will have the nix-shell invocation strip them anyway if needed

cached-nix-shell strips as much of environment variables as possible before passing it to nix-shell --pure to avoid cache invalidation on every possible variable change. E.g. $GNOME_TERMINAL_SCREEN is different in every terminal tab, so it would be inconvenient not to reuse the cache every time you open a tab. Some of the variables may affect the evaluation result, so cache invalidation is required if any of the passed variables are changed.

I've added the logic to fetch TERM and others from the environment before running the user shell. These are still not passed to nix-shell --pure during the first invocation (slow path). I think it should cover the listed use cases.

Rather than trying to replicate all the behavior of nix-shell, wouldn’t it be easier to record the path to the shell’s .drv file in the cache (instead of the environment variables), and then simply exec nix-shell <drv path> "$@"? nix-shell will then handle the details of setting up the environment, and the behavior would be identical but without the slow expression evaluation.

No, it would still be slow. This way you'll get rid of the nix expression evaluation stage (which is slow), but you still have to run setup.sh (which is slow too). cached-nix-shell caches both stages at once.

$ drv=$(nix show-derivation $(nix-shell --pure -p i3.buildInputs --run 'echo $out') | jq -r 'keys[0]')
$ time NIX_BUILD_SHELL=bash nix-shell $drv\!out --run ':'

real	0m0.744s
user	0m0.528s
sys	0m0.092s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants