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

Support pure/isolated environments #289

Closed
matrss opened this issue Aug 22, 2023 · 5 comments · Fixed by #1395
Closed

Support pure/isolated environments #289

matrss opened this issue Aug 22, 2023 · 5 comments · Fixed by #1395
Labels
needs-design Needs a design so it can be implemented ✨ enhancement Feature request

Comments

@matrss
Copy link

matrss commented Aug 22, 2023

Problem description

pixi should allow shell/run to use a pure and isolated environment, similar to how guix supports it. What I mean by that is that pixi should make it (optionally) impossible to use tools/dependencies/packages, which are not installed with pixi.

To cite the guix docs:

By default, the shell session or command runs in an augmented environment, where the new packages are added to search path environment variables such as PATH. You can, instead, choose to create an isolated environment containing nothing but the packages you asked for. Passing the --pure option clears environment variable definitions found in the parent environment14; passing --container goes one step further by spawning a container isolated from the rest of the system:

(From: https://guix.gnu.org/en/manual/devel/en/guix.html#Invoking-guix-shell)

What we currently have is what they call an augmented environment. A pure environment would reduce "works on my machine" errors due to mistakenly using packages not installed into the pixi project. This would also side-step issues like #246 and in general be an improvement to reproducibility.

@matrss matrss added the ✨ enhancement Feature request label Aug 22, 2023
@baszalmstra
Copy link
Contributor

I like this a lot.

@ctb
Copy link

ctb commented Aug 23, 2023

yes please!

@ruben-arts
Copy link
Contributor

I'm taking a look at this issue.

Sharing a few of my learnings.

I'm now (as a prototype) excluding std::env::vars() from the environments, and completely replacing the $PATH with only the pixi environment.

On linux, I can run pixi run install in pixi (e.g. compiling pixi including some C stuff) when I add the following packages:

pixi add make coreutils grep findutils

These packages install tools like cp rm mv fin etc. Which I'm not sure is a nice flow. But it at least is fully isolated 🤷‍♂️

Some questions that come to mind.

Should we embed the "isolated" toggle in the CLI or in the task definition, possibly both? Some ideas:

pixi run --isolated python main.py
[tasks]
build-iso = {cmd = "cargo build --release", isolated = true}

@ruben-arts
Copy link
Contributor

I could also imagine a user wanting to specify it per environment 🤔

baszalmstra pushed a commit that referenced this issue Jun 11, 2024
Fixes #289 slightly

Adds:
```
pixi run --clean-env xxx
```

```toml
[tasks]
isolated-task = {cmd = "/usr/bin/env", clean-env = true}
```

This doesn't work on Windows, as it is just to shitty to get working on
windows.
You need to many variables and paths for a working environment for the
label "clean" as you need the `Program Files` directories for the
compilers. And other stuff.

And I'm honestly a 120% Done with this PR. And it's breaking into
actually being productive.
jjjermiah pushed a commit to jjjermiah/pixi that referenced this issue Jun 11, 2024
Fixes prefix-dev#289 slightly

Adds:
```
pixi run --clean-env xxx
```

```toml
[tasks]
isolated-task = {cmd = "/usr/bin/env", clean-env = true}
```

This doesn't work on Windows, as it is just to shitty to get working on
windows.
You need to many variables and paths for a working environment for the
label "clean" as you need the `Program Files` directories for the
compilers. And other stuff.

And I'm honestly a 120% Done with this PR. And it's breaking into
actually being productive.
@ruben-arts
Copy link
Contributor

Leaving this here for future reference for the clean env feature on windows. When we find a way to improve the cleaning of a windows shell environment.

// Enable this when we found a better way to support windows.
// On Windows the path is not completely replace, but we need to strip some paths to keep it as clean as possible.
if cfg!(target_os = "windows") {
    let path = env
        .get("Path")
        .map(|path| {
            // Keep some of the paths
            let win_path = std::env::split_paths(&path).filter(|p| {
                // Required for base functionalities
                p.to_string_lossy().contains(":\\Windows")
                    // Required for compilers
                    || p.to_string_lossy().contains("\\Program Files")
                    // Required for pixi environments
                    || p.starts_with(environment.dir())
            });
            // Join back up the paths
            std::env::join_paths(win_path).expect("Could not join paths")
        })
        .expect("Could not find PATH in environment variables");
    // Insert the path back into the env.
    env.insert(
        "Path".to_string(),
        path.to_str()
            .expect("Path contains non-utf8 paths")
            .to_string(),
    );
}

ruben-arts added a commit that referenced this issue Jun 14, 2024
Fixes #1503

The issue was that the work on the clean-env introduced a situation
where all env vars of the current environment are added to the shell
activation. This is not what we want, so this pr reverts that behavior.

This pr adds
- Cleanup of the activation code
- Storage of both the clean and normal environment variables when used
in the tasks.
- Introduces Environment variable behavior to tell the activator what to
do with the current environment
- Removes windows non-sense, I added the possible code to the related
issue : #289 for future reference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-design Needs a design so it can be implemented ✨ enhancement Feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants