`conda-script` proposal
This is a draft version of the pixi script proposal. The plan is to turn it into a CEP for the entire conda ecosystem, so it shouldn't be referred to as official work. As a first step, it will be implemented in pixi behind pixi run --experimental --script FILE to gather experience before finalizing the CEP.
Abstract
Standardize the conda-script comment block: how conda metadata is embedded in scripts and code files, and how tools or IDE extensions should treat it.
Motivation
To share a conda based script today, one has to send the code file (.py, .c, .rs, ...) together with a project file (pixi.toml / environment.yml). There is no way to store the requirements inside the script itself.
Embedding the metadata in the file enables:
- Fast prototyping for short-lived code, where a separate project file is unnecessary
- Sharing a single self-contained file via gist, URL or chat
This proposal standardizes how this metadata SHOULD be embedded and how tools like pixi or conda can use it. It does not define such a tool or how it should be used.
Specification
Relationship to PEP 723
Python already standardizes embedded script metadata via PEP 723, and pixi already runs such scripts with conda metadata under [tool.pixi]. Python scripts MAY keep using it, or use a conda-script block instead; both are fine. A file containing both a PEP 723 script block and a conda-script block is an error.
Block syntax
A conda-script block is recognized without knowing the language's comment syntax:
- The opening line is any line ending in
/// conda-script. Everything before the///is the prefix. The prefix MUST be non-empty and MUST NOT contain alphanumeric characters. - Every following line of the block MUST start with that exact prefix. A line equal to the prefix with trailing whitespace stripped is an empty metadata line.
- The block closes with the prefix followed by
/// end-conda-script. An unterminated block is an error. - Only line comments are supported, no block comments (
/* */). - A file MUST contain at most one
conda-scriptblock, at any position. More than one is an error.
The content of the block is TOML, extended with multiline inline tables from the upcoming TOML 1.1.
// /// conda-script
// channels = ["conda-forge"]
// entrypoint = "gcc -o ${CACHE}/main ${SCRIPT} -lz && ${CACHE}/main"
//
// [dependencies]
// gcc = "*"
// zlib = "*"
// /// end-conda-script
Since a shebang line lies outside the block, #!/usr/bin/env -S pixi run --experimental --script works where env supports -S (note that some systems pass only a single argument in shebang lines).
Keys
channels(required): the conda channels the dependencies are solved from.entrypoint(required): the command that runs the script.[dependencies](optional): maps package names to conda matchspecs.[tool.*](optional): tool-specific configuration, for example[tool.pixi.pypi-dependencies], or source dependencies under[tool.pixi.dependencies].
dependencies
Each entry maps a conda package name to either a string or a table. The string form is a version matchspec:
// [dependencies]
// python = "3.13.*"
// gcc = "*"
The table form supports exactly these keys, all optional; an unknown key is an error:
version: a version matchspec. Defaults to*.build: a build string matcher, for examplebuild = "*cuda*"to select a GPU variant.build-number: a build number matcher, for examplebuild-number = ">=2".channel: the channel this package must come from, overridingchannelsfor this package.subdir: the platform subdirectory the package must come from, for examplesubdir = "noarch".extras: optional dependency groups of the package to enable, for exampleextras = ["test"].flags: build flags the package must have been built with. A flag may use a*glob, soflags = ["blas:*"]matchesblas:mkl,blas:openblas, and so on.md5: the MD5 hash the package archive must have.sha256: the SHA256 hash the package archive must have.url: a direct URL to a package archive, resolved without any channel lookup. MUST NOT be combined with other keys exceptmd5,sha256andwhen, since the URL already determines the artifact.when: a condition under which the dependency applies, see below.
A hash or URL identifies one platform-specific artifact; a cross-platform script combines such entries with when.
A long spec reads better as a multiline inline table:
// [dependencies]
// pytorch = {
// version = ">=2.4",
// build = "*cuda*",
// extras = ["test"],
// }
Platform specific dependencies are expressed as conditional dependencies via when, using virtual packages such as __win, __unix or __linux:
// [dependencies]
// gcc = { version = "*", when = "__unix" }
// vs2022_win-64 = { version = "*", when = "__win" }
Source dependencies (built from a git repository, url or path) are deliberately not part of this specification, since they require a build system rather than just a solver. Tools MAY support them in their [tool.*] table.
[tool.*]
Tables under [tool.*] belong to the named tool and are not interpreted by this specification.
pixi reads [tool.pixi] with the same semantics as in a pyproject.toml, restricted the same way as for PEP 723 scripts: a script represents one implicit environment and has no task graph. [tool.pixi.dependencies] merges with [dependencies] the way pixi merges features: every spec applies, so a source dependency there composes with a version constraint in [dependencies], and tools that only implement this specification still see a solvable script. [tool.pixi.pypi-dependencies] adds PyPI packages to the environment.
entrypoint
Either a string, or a table keyed by platform when the command differs per platform:
entrypoint = {
unix = "cc -o ${CACHE}/main ${SCRIPT} && ${CACHE}/main",
win = "cl /Fe:${CACHE}/main.exe ${SCRIPT} && ${CACHE}/main.exe",
}
Keys are platform families (win, osx, linux, unix) or specific platforms (linux-64, ...). The most specific matching key is used. If no key matches the current platform, it is an error.
The entrypoint is NOT passed to a system shell. It uses a minimal, fully specified syntax:
- Arguments are split on whitespace. Single and double quotes protect spaces.
${VAR}is substituted bare and inside double quotes, single quotes suppress substitution. Outside single quotes,$MUST be followed by{NAME}or(, anything else is a parse error. An undefined variable is an error.$(command)is command substitution: the inner command sequence runs first and its stdout replaces the substitution, with the trailing newline stripped. Unquoted, the result is whitespace-split into arguments; inside double quotes it stays one argument. A failing inner command aborts the entrypoint. The canonical use is$(pkg-config --cflags --libs glib-2.0). The content of$(...)uses the same syntax as the entrypoint itself, recursively. The output of a substitution is never re-parsed: it only ever splits into arguments.&&sequences commands, a failing command aborts the sequence.- Nothing else: no pipes, redirects, globbing,
||,;, subshells or environment variable assignments.
Two variables are defined:
${SCRIPT}: the absolute path to the script file.${CACHE}: the absolute path to a persistent per-script directory owned by the tool, for build artifacts and other state that survives between runs. Its basename SHOULD be a lowercase identifier, since downstream tools derive names from it.
There is deliberately no variable for the environment prefix: conda-forge compilers search the prefix's include and lib directories by default and bake an rpath into linked binaries, and $(pkg-config ...) covers the remaining cases.
Arguments passed by the user are appended to the last && command:
pixi run --experimental --script main.c --verbose
# runs: gcc -o ${CACHE}/main /path/to/main.c -lz && ${CACHE}/main --verbose
Execution environment
The entrypoint runs inside the activated solved environment, with the working directory left at the directory the tool was invoked from, so relative-path arguments to the script work. Activation env vars such as CONDA_PREFIX are guaranteed to be set. Anything the entrypoint needs to persist belongs in ${CACHE}, never in the working directory.
Locking
Locking of dependencies is optional: running a script does not create a lock file. Tools MAY offer locking through additional options and SHOULD then follow the same conventions as for PEP 723 scripts. In pixi that means pixi lock --script FILE writes FILE.pixi.lock next to the script, and a run uses an adjacent lock file when one exists.
Prior art
- Python: PEP 723, on which this document is based
- Rust: cargo script (RFC 3424)
- Groovy:
@Grab, in-file dependency declarations since 2007 - C#: .NET 10 file-based apps with
#:packagedirectives - Scala: scala-cli
//> usingdirectives - Elixir:
Mix.install - Nix: nix-script
- Haskell: cabal single-file scripts
I like it! Have you thought about also integrating with shebang on Unix?
So that something like this would work:
#!/usr/bin/env pixi script
# /// conda-script
...
What kind of shell is being used in the entrypoints (on windows)? Should it be configurable? I really dislike cmd.exe
I like it! Have you thought about also integrating with shebang on Unix?
So that something like this would work:
#!/usr/bin/env pixi script # /// conda-script ...What kind of shell is being used in the entrypoints (on windows)? Should it be configurable? I really dislike cmd.exe
on some system, shebang line only accepts a single argument, so #!/usr/bin/env pixi script may not be executed correctly.
I like it too. My only thought is that conda-script might be not the right name because this seems quite pixi-specific (it's the pixi.toml format and we would probably execute the task in deno_task_shell).
So maybe calling it pixi-script would be better – or we should change the format to look a bit more like an environment.yml.
This looks amazing!
FWIW, this is what we ended up with in album when trying to do the single-file conda-scripts: https://docs.album.solutions/en/latest/solution-development.html. TL;DR: like @wolfv suggests, we eventually ended up with a data structure that started to resemble an environment.yml
I've found the uv scripts to be so useful that has become my primary single-file for most tasks these days, but if I could do it with pixi then I'd be ecstatic. I'm keen to watch/chime in!
Doesn't this proposal contradict PEP 723?
Tools MUST NOT read from metadata blocks with types that have not been standardized by this specification.
The first type of metadata block is named script, which contains script metadata (dependency data and tool configuration).
And then there are no other standard metadata blocks defined in the PEP.
I love the idea! One thought is to avoid defining a new type of script (not recognized by other tools) and instead try to align with the existing PEP 723 standard, where pypi dependencies are specified in the top-level dependencies field.
Then pixi-specific metadata could fall under tool metadata, like pixi's pyproject.toml support. E.g.,
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "lonboard==0.10.4",
# "polars==1.30.0",
# ]
#
# [tool.pixi.dependencies]
# gdal = "3.11.0"
# ///
This would allow pixi to run a subset of scripts created with a tool like uv, and vis versa. There might be further opportunity to align with PEP 725 if it is ever accepted for certain types of things that would also fall under pixi-specific dependencies for now. I've really appreciated how uv aligns with some PEPs, introduces special tool metadata where the standards lack, and migrate towards standards if/when they improve (e.g., dependency-groups).
Similar suggestion to:
- https://github.com/prefix-dev/pixi/issues/3267#issuecomment-2700420590
If any pixi folks are at SciPy I'd love to chat about this!
If any pixi folks are at SciPy I'd love to chat about this!
Please find mr @ruben-arts ! I'm sure he will be happy to chat about this!
I have made some progress on this in manzt/pep-723. Details.
As discussed above, we want to get a CEP out for that before we fully support this. However, with Rattler-Build it worked well to implement it first behind --experimental and then try it for a while before finalizing it as a CEP.
Let's therefore get an implementation out and support something like:
pixi run --experimental --script main.c
This would be really cool to see with Mojo as well! We may even consider adding first-class to support in our LSP for this in the future, so that it can recognize environments specified in files.