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

Question: How can I create a pure shell? #330

Closed
jason-riddle opened this issue Jan 27, 2023 · 17 comments
Closed

Question: How can I create a pure shell? #330

jason-riddle opened this issue Jan 27, 2023 · 17 comments
Labels
bug Something isn't working

Comments

@jason-riddle
Copy link

Describe the bug
Running devenv shell prepends the current $PATH with the devenv-profile bin directory. However, I would like a pristine $PATH with nothing inherited. This would be something similar to nix-shell --pure.

The reason is I don't want to accidentally use a binary outside of the nix store, which has happened a few times now.

To Reproduce

Run devenv init and then devenv shell. Run echo $PATH and notice the inherited path.

Version

$ devenv version
devenv: 0.5
@jason-riddle jason-riddle added the bug Something isn't working label Jan 27, 2023
@domenkozar
Copy link
Member

Hey! I'm still thinking about adding support for --pure.

One issue I have with it is that it removes all the tooling from your system, for example the editor, etc.

Could you provide an example of the binaries that got in the env?

@jason-riddle
Copy link
Author

jason-riddle commented Feb 1, 2023

One issue I have with it is that it removes all the tooling from your system, for example the editor, etc.

Ah, good point. Could a compromise be only known critical system paths are included for $PATH? So /usr/sbin, /usr/bin, /sbin, and /bin?

Could you provide an example of the binaries that got in the env?

Sure, it's only two binaries causing problems, python and virtualenv.

@domenkozar
Copy link
Member

Could you explain exactly what happened and what you expected to happen? That would allow me to design this to prevent such kind of mistakes :)

@jason-riddle
Copy link
Author

I expect the devenv.nix file, as defined below, to not let me execute virtualenv while in devenv shell.

{ pkgs, ... }:

{
  # https://devenv.sh/packages/
  packages = [];

  # https://devenv.sh/pre-commit-hooks/
  pre-commit.hooks.shellcheck.enable = false;
}

However, it was executed because it was found in my $PATH at /Users/jason/Library/Python/3.9/bin/virtualenv. I don't want /Users/jason/Library/Python/3.9/bin/virtualenv to be in my path while running devenv shell.

@arturkow2000
Copy link

We also would like to see --pure implemented. This feature comes handy when we want to determine which tools/libraries are needed to build some project. Otherwise libraries missing from devenv.nix would be taken from host, such a build environment is not reproducible and build could fail on another OS. Otherwise there may be other reasons to build an isolated environment.

@domenkozar
Copy link
Member

This one is easy to achieve once we tackle #240

@willjr
Copy link

willjr commented Mar 7, 2023

(Hope you don't mind my commenting)

Hey! I'm still thinking about adding support for --pure.

One issue I have with it is that it removes all the tooling from your system, for example the editor, etc.

I personally see this as a positive option, TBH.

Everywhere else I've worked that aspires to hermetic builds / build-env expects the editor to be able to run things (tests, etc) inside a kind-of "dev-env sandbox" anyway, so you get the nearest thing to your CI systems result(s) as well.

Those places can use things like a dev container, but sometimes that's difficult or inappropriate, so it's really helpful that devenv (and Nix!) can provide this without much extra work.

@risicle
Copy link

risicle commented Apr 19, 2023

The advantage of using nix-shell --pure is that you can normalize a lot of differences between platforms. No longer do you have to worry about the differences between macos sed and gnu sed, which variant of nc you're working with, shasum vs sha256sum, macos mktemp vs coreutils mktemp etc.

@herwig-hochleitner-gravie

I'm using this approach:

    enterShell = lib.mkMerge [
      ''
        export IMPURE_PATH="$PATH"
      ''
      ## if we're running in pure mode, reset PATH to be _just_ from devenv
      (lib.mkIf config.eval.pure ''
        export PATH="$DEVENV_PROFILE/bin"
      '')
    ];

and for commands that should call out into system tools

      # for example for firewall setup, restore original path
      firewall-setup.exec = ''
        export PATH=$IMPURE_PATH
        case $(uname) in
          Linux)
            sudo iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80  -j REDIRECT --to-ports 8000
            sudo iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 443 -j REDIRECT --to-ports 4430
          ;;
          Darwin)
            echo "
              rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080
              rdr pass inet proto tcp from any to any port 443 -> 127.0.0.1 port 4430" | \
            sudo pfctl -ef -
          ;;
         esac
      '';

this "pure-lite" mode is already very useful for catching missed packages early.

@minhuw
Copy link

minhuw commented Sep 23, 2023

Another reason I want a pure shell is that glibc linked by installed packages may be much newer than the system's default one. So I always encounter errors like the one below.

/lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_ABI_DT_RELR' not found (required by /nix/store/aw2fw9ag10wr9pf0qk4nk5sxi0q0bn56-glibc-2.37-8/lib/libpthread.so.0)

@domenkozar
Copy link
Member

That one should be solved for most cases in #745

@gonzaloetjo
Copy link

Is there any update on this issue? or does it remain a non-priority?

@domenkozar
Copy link
Member

This might be much easier using #745

@domenkozar
Copy link
Member

domenkozar commented Mar 3, 2024

I'm implementing this in #745 using devenv shell --clean.

I wonder if we should add a setting to devenv.yaml what variables to preserve:

clean-keep-vars:
- EDITOR
- ...

More control over $PATH:

clean-keep:
  vars:
    - EDITOR
  executables:
    - vim

@kalekseev
Copy link
Contributor

Another reason I want a pure shell is that glibc linked by installed packages may be much newer than the system's default one. So I always encounter errors like the one below.

/lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_ABI_DT_RELR' not found (required by /nix/store/aw2fw9ag10wr9pf0qk4nk5sxi0q0bn56-glibc-2.37-8/lib/libpthread.so.0)

I think devenv could use LIBRARY_PATH instead of LD_LIBRARY_PATH to mitigate this problem, I switch it in enter shell:

  export LIBRARY_PATH="${lib.getLib pkgs.glibc}/lib"
  unset LD_LIBRARY_PATH

@domenkozar
Copy link
Member

1.0.1 now implements:

clean:
  enable: true
  keep:
    - EDITOR

@domenkozar
Copy link
Member

I'm going to close this, please reopen if you'd like devenv to support keeping some executables around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants