Skip to content

Conversation

@fecet
Copy link
Contributor

@fecet fecet commented Aug 28, 2025

Add --to-prefix option for custom environment installation

Summary

This PR introduces the --to-prefix option to the pixi install command, enabling installation of environments to custom directories instead of the default .pixi/envs/<env-name> location. This addresses a long-standing community need with an officially-supported solution that supersedes existing third-party tools.

Motivation

The community has requested flexible environment installation locations for container deployments, CI/CD pipelines, system integration, and production scenarios. Previously, users relied on third-party tools like pixi-install-to-prefix, which had critical limitations:

  • No PyPI package support: Only conda packages could be installed
  • Maintenance overhead: Required independent updates and version coordination
  • Limited integration: Could not leverage pixi's internal optimizations
  • Behavioral inconsistencies: Different behavior from native pixi commands

Implementation

Core Features

Custom Prefix Installation

pixi install --to-prefix ./my-environment

Platform Override Support

pixi install --to-prefix ./env --platform linux-64

Platform Validation

  • Pre-installation compatibility checks
  • Clear error messages for unsupported platform combinations

Technical Architecture

PrefixOverrideGuard: Thread-local RAII pattern for safe prefix management

pub struct PrefixOverrideGuard {
    env_names: Vec<String>,
}

Environment Integration: Modified Environment::dir() and Environment::best_platform() to check thread-local overrides before falling back to default behavior.

- Add PrefixOverrideGuard for thread-local prefix management
- Support platform overrides with --platform flag
- Enable installing environments to custom directories
- Add validation for platform compatibility
- Update CLI documentation and integration tests
Copy link
Contributor

@baszalmstra baszalmstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach is very hacky. The PrefixOverrideGuard approach uses global variables to cause side effects.

I think instead we could split the code path slightly where we have a path for --to-prefix and the original code.

The --to-prefix path would have to get an up-to-date lock-file, extract the specs from the lock-file for the desired environment and call CommandDispatcher::install_pixi_environment.

Alternatively I can also imagine that this could go into a separate sub-command?

@ruben-arts From a UX point of view, what are your thoughts?

Comment on lines +83 to +93
if let Some(prefix_path) = &args.to_prefix {
tokio::fs::create_dir_all(prefix_path)
.await
.into_diagnostic()
.with_context(|| {
format!(
"Failed to create prefix directory: {}",
prefix_path.display()
)
})?;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? I would assume this happens during installation.

}

pub async fn execute(args: Args) -> miette::Result<()> {
use miette::{Context, IntoDiagnostic};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets just move this up to the top of the file.

Comment on lines +99 to +105
let envs = if args.to_prefix.is_some() {
vec![
args.environment
.and_then(|envs| envs.into_iter().next())
.unwrap_or_else(|| "default".to_string()),
]
} else if let Some(envs) = args.environment {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this code? It no longer matches the comment above.

@lucascolley lucascolley added the enhancement New features label Sep 5, 2025
@lucascolley lucascolley changed the title feat: add --to-prefix option for custom environment installation feat(install): add --to-prefix option Sep 8, 2025
@lucascolley lucascolley added the area:install Related to pixi install label Sep 8, 2025
@lucascolley
Copy link
Collaborator

see also #3953

@ruben-arts
Copy link
Contributor

I'm all for Pixi supporting the ability to install to a specific environment. But it needs more than just the installation to work. E.g. activation is needed to make this usable and we should figure out what tools would interact with the same thing.

conda has the --prefix we might be able to implement the same thing in some way instead of --env, but I would like to do more of a design before implementing it. As I don't think the actual code is the hard part here.

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

Labels

area:install Related to pixi install enhancement New features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants