Skip to content

Activate environment *after* the shell has started #297

Closed

Description

@baszalmstra

Problem description

Currently, when running the shell cli command we first activate the environment and then start a new shell process. This has some issues:

When the shell is started (after activation) it sources the shell profile

This causes some issues:

The PS1 variable needs to be set/modified from the shell process

The PS1 environment variable is a special one. It only seems to work if you modify the variable from the shell process itself. This means you cannot set it before starting the shell. Pixi currently works around this by setting a special environment variable (PIXI_PROMPT) and requires some bash profile modifications (as part of the completion script).

This is a less than ideal situation because it requires modifications to your bash profile. Although our default installation script can do this for you, people might not use that to install pixi (they might use homebrew, cargo, simply copy the binary, etc.).

This is also explained in the docs but I'd prefer that this just works out of the box.

We cannot do isolated shells

As requested by #289, it would be nice if we could spawn a shell without any outside environment variables set. Essentially running a completely pure environment. Currently, because the profile is sourced we cannot guarantee this.

Proposed solution

The solution is to run the pixi installation script after and inside the spawned shell process. We already found several workarounds to achieve this.

  • Some shells (powershell and cmdexe) have native support to run a file before becoming interactive
  • @pavelzw found a hacky way for bash
  • @pavelzw found a hacky way for zsh
  • We can inject keystrokes in the spawned process to basically type "source <our_activation_script" (this is the approach taken by poetry)
  • More?

Activity

  1. jonashaag commented on Aug 24, 2023

    @jonashaag

    FWIW I would expect @pavelzw's approach to be more robust than sending keystrokes. It should be feasible for all (most?) shells.

    Another approach would be to have a special magic script sourced in your .bashrc (etc) that deals with all of this logic. We can pass params to the script via environment variables.

  2. pavelzw commented on Aug 24, 2023

    @pavelzw
    Collaborator

    special magic script sourced in your .bashrc

    I would prefer to not need to change my bashrc except for shell completion if possible.

  3. baszalmstra commented on Aug 24, 2023

    @baszalmstra
    ContributorAuthor

    I would prefer to not need to change my bashrc except for shell completion if possible.

    This is also my philosophy. This is exactly what I dislike about conda/mamba/micromamba. Although our installation script can add these things for you there are a number of different ways to install pixi on your system (homebrew, cargo, just put binary on your PATH). Preferably all features just work out of the box.

  4. added
    bug
    on Aug 24, 2023
  5. pelson commented on Aug 25, 2023

    @pelson

    If a user uses the default installation of conda they have a base environment which is sourced in the profile. This basically overwrites all conda specific environment variables which activates the base environment instead of the conda environment. 🙈

    This is a big one, IMO (and one I just tripped over). Essentially, it gives the impression that things are working (the PS1 is being updated), but you don't actually get the right shell environment. I have a fairly vanilla miniforge installation...

    Reproducer:

    $ docker run -it condaforge/miniforge3 bash
    $ apt-get update; apt-get install curl
    $ curl -fsSL https://pixi.sh/install.sh | bash
    $ source /root/.bashrc 
    $ pixi init new-project
    $ cd new-project/
    $ pixi add 'python<3.10'
    
    # No surprise, we have a newer Python (from the base image)
    $ python --version
    Python 3.10.12
    
    # Enable a shell for the project
    $ pixi shell
    # Uh-oh! Not the Python we should be getting...
    $ python --version
    Python 3.10.12
    
  6. wolfv commented on Aug 25, 2023

    @wolfv
    Member

    Yes, we are actively working on this - using rexpect to mimic what poetry is doing with pexpect - starting a shell and sending some keystrokes!

  7. twrightsman commented on Sep 27, 2023

    @twrightsman
    Contributor

    Is this solved by #316? From my reading it seems the PS1 issue persists?

  8. baszalmstra commented on Sep 28, 2023

    @baszalmstra
    ContributorAuthor

    @twrightsman Yes this should be solved by #316. That PR changes the way the shell is started. The environment activation now happens after the shell has started which should solve issues with environment variables being overwritten by the shell profile.

    There are still some issues with the PS1 for different shells which we will solve in another PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions