Skip to content

Multi Environment Support#

Motivating Example#

There are multiple scenarios where multiple environments are useful.

  • Testing of multiple package versions, e.g. py39 and py310 or polars 0.12 and 0.13.
  • Smaller single tool environments, e.g. lint or docs.
  • Large developer environments, that combine all the smaller environments, e.g. dev.
  • Strict supersets of environments, e.g. prod and test-prod where test-prod is a strict superset of prod.
  • Multiple machines from one project, e.g. a cuda environment and a cpu environment.
  • And many more. (Feel free to edit this document in our GitHub and add your use case.)

This prepares pixi for use in large projects with multiple use-cases, multiple developers and different CI needs.

Design Considerations#

There are a few things we wanted to keep in mind in the design:

  1. User-friendliness: Pixi is a user focussed tool that goes beyond developers. The feature should have good error reporting and helpful documentation from the start.
  2. Keep it simple: Not understanding the multiple environments feature shouldn't limit a user to use pixi. The feature should be "invisible" to the non-multi env use-cases.
  3. No Automatic Combinatorial: To ensure the dependency resolution process remains manageable, the solution should avoid a combinatorial explosion of dependency sets. By making the environments user defined and not automatically inferred by testing a matrix of the features.
  4. Single environment Activation: The design should allow only one environment to be active at any given time, simplifying the resolution process and preventing conflicts.
  5. Fixed lock files: It's crucial to preserve fixed lock files for consistency and predictability. Solutions must ensure reliability not just for authors but also for end-users, particularly at the time of lock file creation.

Feature & Environment Set Definitions#

Introduce environment sets into the pixi.toml this describes environments based on feature's. Introduce features into the pixi.toml that can describe parts of environments. As an environment goes beyond just dependencies the features should be described including the following fields:

  • dependencies: The conda package dependencies
  • pypi-dependencies: The pypi package dependencies
  • system-requirements: The system requirements of the environment
  • activation: The activation information for the environment
  • platforms: The platforms the environment can be run on.
  • channels: The channels used to create the environment. Adding the priority field to the channels to allow concatenation of channels instead of overwriting.
  • target: All the above features but also separated by targets.
  • tasks: Feature specific tasks, tasks in one environment are selected as default tasks for the environment.
Default features
[dependencies] # short for [feature.default.dependencies]
python = "*"
numpy = "==2.3"

[pypi-dependencies] # short for [feature.default.pypi-dependencies]
pandas = "*"

[system-requirements] # short for [feature.default.system-requirements]
libc = "2.33"

[activation] # short for [feature.default.activation]
scripts = [""]
Different dependencies per feature
python = "~=3.9.0"
python = "~=3.10.0"
pytest = "*"
Full set of environment modification in one feature
dependencies = {cuda = "x.y.z", cudnn = "12.0"}
pypi-dependencies = {torch = "1.9.0"}
platforms = ["linux-64", "osx-arm64"]
activation = {scripts = [""]}
system-requirements = {cuda = "12"}
# Channels concatenate using a priority instead of overwrite, so the default channels are still used.
# Using the priority the concatenation is controlled, default is 0, the default channels are used last.
# Highest priority comes first.
channels = ["nvidia", {channel = "pytorch", priority = -1}] # Results in:  ["nvidia", "conda-forge", "pytorch"] when the default is `conda-forge`
tasks = { warmup = "python" }
target.osx-arm64 = {dependencies = {mlx = "x.y.z"}}
Define tasks as defaults of an environment
test = "pytest"

test = ["test"]

# `pixi run test` == `pixi run --environment test test`

The environment definition should contain the following fields:

  • features: Vec<Feature>: The features that are included in the environment set, which is also the default field in the environments.
  • solve-group: String: The solve group is used to group environments together at the solve stage. This is useful for environments that need to have the same dependencies but might extend them with additional dependencies. For instance when testing a production environment with additional test dependencies.
Creating environments from features
# implicit: default = ["default"]
default = ["py39"] # implicit: default = ["py39", "default"]
py310 = ["py310"] # implicit: py310 = ["py310", "default"]
test = ["test"] # implicit: test = ["test", "default"]
test39 = ["test", "py39"] # implicit: test39 = ["test", "py39", "default"]
Testing a production environment with additional dependencies
# Creating a `prod` environment which is the minimal set of dependencies used for production.
prod = {features = ["py39"], solve-group = "prod"}
# Creating a `test_prod` environment which is the `prod` environment plus the `test` feature.
test_prod = {features = ["py39", "test"], solve-group = "prod"}
# Using the `solve-group` to solve the `prod` and `test_prod` environments together
# Which makes sure the tested environment has the same version of the dependencies as the production environment.
Creating environments without including the default feature
python = "*"
numpy = "*"

pre-commit = "*"

# Create a custom environment which only has the `lint` feature (numpy isn't part of that env).
lint = {features = ["lint"], no-default-feature = true}

lock file Structure#

Within the pixi.lock file, a package may now include an additional environments field, specifying the environment to which it belongs. To avoid duplication the packages environments field may contain multiple environments so the lock file is of minimal size.

- platform: linux-64
  name: pre-commit
  version: 3.3.3
  category: main
    - dev
    - test
    - lint
- platform: linux-64
  name: python
  version: 3.9.3
  category: main
    - dev
    - test
    - lint
    - py39
    - default

User Interface Environment Activation#

Users can manually activate the desired environment via command line or configuration. This approach guarantees a conflict-free environment by allowing only one feature set to be active at a time. For the user the cli would look like this:

Default behavior
 pixi run python
# Runs python in the `default` environment
Activating an specific environment
 pixi run -e test pytest
 pixi run --environment test pytest
# Runs `pytest` in the `test` environment
Activating a shell in an environment
 pixi shell -e cuda
pixi shell --environment cuda
# Starts a shell in the `cuda` environment
Running any command in an environment
 pixi run -e test any_command
# Runs any_command in the `test` environment which doesn't require to be predefined as a task.

Abmigious Environment Selection#

It's possible to define tasks in multiple environments, in this case the user should be prompted to select the environment.

Here is a simple example of a task only manifest:

name = "test_ambiguous_env"
channels = []
platforms = ["linux-64", "win-64", "osx-64", "osx-arm64"]

default = "echo Default"
ambi = "echo Ambi::Default"
test = "echo Test"
ambi = "echo Ambi::Test"

dev = "echo Dev"
ambi = "echo Ambi::Dev"

default = ["test", "dev"]
test = ["test"]
dev = ["dev"]
Trying to run the abmi task will prompt the user to select the environment. As it is available in all environments.

Interactive selection of environments if task is in multiple environments
 pixi run ambi
? The task 'ambi' can be run in multiple environments.

Please select an environment to run the task in:  default # selecting default

 Pixi task (ambi in default): echo Ambi::Test

As you can see it runs the task defined in the feature.task but it is run in the default environment. This happens because the ambi task is defined in the test feature, and it is overwritten in the default environment. So the tasks.default is now non-reachable from any environment.

Some other results running in this example:

 pixi run --environment test ambi
 Pixi task (ambi in test): echo Ambi::Test

 pixi run --environment dev ambi
 Pixi task (ambi in dev): echo Ambi::Dev

# dev is run in the default environment
 pixi run dev
 Pixi task (dev in default): echo Dev

# dev is run in the dev environment
 pixi run -e dev dev
 Pixi task (dev in dev): echo Dev

Real world example use cases#

Polarify test setup

In polarify they want to test multiple versions combined with multiple versions of polars. This is currently done by using a matrix in GitHub actions. This can be replaced by using multiple environments.

name = "polarify"
# ...
channels = ["conda-forge"]
platforms = ["linux-64", "osx-arm64", "osx-64", "win-64"]

postinstall = "pip install --no-build-isolation --no-deps --disable-pip-version-check -e ."

python = ">=3.9"
pip = "*"
polars = ">=0.14.24,<0.21"

python = "3.9.*"
python = "3.10.*"
python = "3.11.*"
python = "3.12.*"
polars = "0.17.*"
polars = "0.18.*"
polars = "0.19.*"
polars = "0.20.*"

pytest = "*"
pytest-md = "*"
pytest-emoji = "*"
hypothesis = "*"
test = "pytest"

pre-commit = "*"
lint = "pre-commit run --all"

pl017 = ["pl017", "py39", "test"]
pl018 = ["pl018", "py39", "test"]
pl019 = ["pl019", "py39", "test"]
pl020 = ["pl020", "py39", "test"]
py39 = ["py39", "test"]
py310 = ["py310", "test"]
py311 = ["py311", "test"]
py312 = ["py312", "test"]
    runs-on: ubuntu-latest
        environment: [py311, py312]
    - uses: actions/checkout@v4
      - uses: prefix-dev/setup-pixi@v0.5.1
          environments: ${{ matrix.environment }}
      - name: Run tasks
        run: |
          pixi run --environment ${{ matrix.environment }} test
    runs-on: ubuntu-latest
    - uses: actions/checkout@v4
    - uses: prefix-dev/setup-pixi@v0.5.1
       environments: pl017 pl018
    - run: |
        pixi run -e pl017 test
        pixi run -e pl018 test
Test vs Production example

This is an example of a project that has a test feature and prod environment. The prod environment is a production environment that contains the run dependencies. The test feature is a set of dependencies and tasks that we want to put on top of the previously solved prod environment. This is a common use case where we want to test the production environment with additional dependencies.

name = "my-app"
# ...
channels = ["conda-forge"]
platforms = ["osx-arm64", "linux-64"]

postinstall-e = "pip install --no-build-isolation --no-deps --disable-pip-version-check -e ."
postinstall = "pip install --no-build-isolation --no-deps --disable-pip-version-check ."
dev = "uvicorn --reload"
serve = "uvicorn"

python = ">=3.12"
pip = "*"
pydantic = ">=2"
fastapi = ">=0.105.0"
sqlalchemy = ">=2,<3"
uvicorn = "*"
aiofiles = "*"

pytest = "*"
pytest-md = "*"
pytest-asyncio = "*"
test = "pytest"

# both default and prod will have exactly the same dependency versions when they share a dependency
default = {features = ["test"], solve-group = "prod-group"}
prod = {features = [], solve-group = "prod-group"}
In ci you would run the following commands:
pixi run postinstall-e && pixi run test
Locally you would run the following command:
pixi run postinstall-e && pixi run dev

Then in a Dockerfile you would run the following command:

FROM # this doesn't exist yet
COPY . .
RUN pixi run --environment prod postinstall
CMD ["/usr/local/bin/pixi", "run", "--environment", "prod", "serve"]

Multiple machines from one project

This is an example for an ML project that should be executable on a machine that supports cuda and mlx. It should also be executable on machines that don't support cuda or mlx, we use the cpu feature for this.

name = "my-ml-project"
description = "A project that does ML stuff"
authors = ["Your Name <>"]
channels = ["conda-forge", "pytorch"]
# All platforms that are supported by the project as the features will take the intersection of the platforms defined there.
platforms = ["win-64", "linux-64", "osx-64", "osx-arm64"]

train-model = "python"
evaluate-model = "python"

python = "3.11.*"
pytorch = {version = ">=2.0.1", channel = "pytorch"}
torchvision = {version = ">=0.15", channel = "pytorch"}
polars = ">=0.20,<0.21"
matplotlib-base = ">=3.8.2,<3.9"
ipykernel = ">=6.28.0,<6.29"

platforms = ["win-64", "linux-64"]
channels = ["nvidia", {channel = "pytorch", priority = -1}]
system-requirements = {cuda = "12.1"}

train-model = "python --cuda"
evaluate-model = "python --cuda"

pytorch-cuda = {version = "12.1.*", channel = "pytorch"}

platforms = ["osx-arm64"]

train-model = "python --mlx"
evaluate-model = "python --mlx"

mlx = ">=0.5.0,<0.6.0"

platforms = ["win-64", "linux-64", "osx-64", "osx-arm64"]

cuda = ["cuda"]
mlx = ["mlx"]
default = ["cpu"]
Running the project on a cuda machine
pixi run train-model --environment cuda
# will execute `python --cuda`
# fails if not on linux-64 or win-64 with cuda 12.1
Running the project with mlx
pixi run train-model --environment mlx
# will execute `python --mlx`
# fails if not on osx-arm64
Running the project on a machine without cuda or mlx
pixi run train-model