pip-purifier full guide

Detailed setup, dependency management, build commands, and troubleshooting. Start with the overview for the initial commands; use the sections below for the complete setup and behavior. pip-purifier is classified at maturity tier 2 (see tier assessment for details).

Contents

Setup and activation

Use the overview’s Quickstart for the initial command sequence. The setup details below apply to that workflow.

prefetch creates and installs the project’s .venv from the locked dependencies. The activation command makes it available to your shell.

Commit the lockfiles that the lock command writes, including the build tool’s own lockfile when it has one. Keep using the project’s usual build, test and editor commands with this setup. Nix provides the locked dependencies; the development commands above run outside the Nix sandbox. No project Nix files are needed for this workflow.

Sandboxed builds are an optional next step.

Install these packages through your usual Nix configuration.

Version compatibility

Tool or language Packaged defaults Tested versions
pip 26.1.2 26.1.2
Python (runs the tool) — 3.14.8
Python (runs the project) 3.14.8 3.12.15, 3.14.8

Operating systems and architectures

OS CPU architecture Packaged toolchain Recorded workflow
macOS ARM64 Yes Tested
Linux ARM64 Yes Tested
Linux x86-64 Yes Tested

Development loop

pip has no plugin mechanism, so pip-purifier is a command of its own that runs pip. The development loop is the project’s .venv, and pip-purifier pip runs pip against the locked files in place of a pip inside .venv.

pip-purifier prefetch installs the project into .venv offline, with editable installs left editable. Then work in .venv as usual: an edit to the project’s source shows in the next run without a reinstall.

pip-purifier prefetch
.venv/bin/pytest

pip-purifier pip runs any pip command offline against the locked files:

pip-purifier pip --python .venv/bin/python list

After a change to the requirements, pip-purifier lock relocks. A relock keeps the files of the existing lockfile: for each package that the lockfile has, including the build requirements of sdists, the lock run offers pip only those files. An unchanged project relocks to the same file byte for byte, and adding a requirement leaves the other versions alone. When a changed requirement needs a version that the lockfile lacks, the lock run says so and resolves afresh.

pip-purifier update resolves every package afresh, and pip-purifier update <package>... resolves only the named ones, with the pip install arguments that the lockfile records. A pylock.toml pins every version itself, so to update such a project, run pip lock, then pip-purifier lock.

Using locked dependencies

Where a project has a pylock.toml or a requirements file with --hash pins, pip-purifier locks those versions. They also constrain the environments in which pip builds sdists, through pip’s --build-constraint. The Nix build reads the files that a pylock.toml names, with their URLs and hashes, from the pylock.toml itself.

Working offline

prefetch keeps everything the development loop needs alive with garbage-collector roots in .pip-purifier/, beside the lockfile: the locked downloads for this system, and the pip-purifier command with its Python, pip, nixpkgs and purifier library. After a prefetch, .venv, the editor and pip-purifier pip work with the network cut off. Keep the command itself installed, for example in a dev shell, so that running it needs no download either.

A project can instead keep its build outside its tree, such as in a packaging flake for a project whose author does not use pip-purifier. Give the path of pip-purifier.lock.json to each command with --lockfile, or set PIP_PURIFIER_LOCKFILE, and pass the same path to buildPipProject as lockFile:

PIP_PURIFIER_LOCKFILE=../packaging/pip-purifier.lock.json \
  pip-purifier lock

The same goes for pylock.toml, through --tool-lockfile or PIP_PURIFIER_TOOL_LOCKFILE, and the pylock argument of buildPipProject. With either one, lock and update read the pylock.toml there, and when it does not exist yet, lock starts from a copy of the pylock.toml in the tree.

Language tooling integration

Point the editor at .venv, as for any virtual environment. For pyright and basedpyright, prefetch makes .venv in the project root, where they look for it by default. The packages in .venv hold their sources, so go-to-definition and type information work offline. The language server itself comes from your editor setup, such as nixpkgs’ pyright.

Nix builds

Once the native development workflow is working, you can also build and test the whole project in the Nix sandbox. For this step, keep a flake or another Nix build definition in the project’s repository, or maintain an external packaging repository that points at its sources and lockfiles. The builder consumes the same lockfiles as the development workflow.

A flake is optional. It can live in the project or in a separate packaging repository and call the builder directly:

{
  inputs.pip-purifier.url =
    "github:nix-purifiers/pip-purifier";
  inputs.nixpkgs.follows = "pip-purifier/nixpkgs";

  outputs =
    { nixpkgs, pip-purifier, ... }:
    let
      pkgs = import nixpkgs {
        system = "x86_64-linux";
        overlays = [ pip-purifier.overlays.default ];
      };
      project = pkgs.pip-purifier.buildPipProject {
        src = ./.;
      };
    in
    {
      packages.x86_64-linux = {
        default = project.environment;
        greet = project.programs.greet;
        release = project.release;
      };
      checks.x86_64-linux.tests = project.tests;
    };
}

buildPipProject gives:

The lockfile names every output, so Nix knows them at evaluation time.

buildPipProject takes src and these optional arguments:

buildPipProject.override replaces the packages it takes from the package set, and pip-purifier.overrideScope replaces what everything in the scope shares. lib.pip-purifier.readLock { src; } reads both lockfiles into the lock shape that every purifier gives.

We recommend committing pip-purifier.lock.json and pylock.toml to the repository of the project. The builder and the commands look for them there.

One-off builds without a project Nix file

For an ad hoc build, the builder can also be evaluated directly from the project root. Replace the example package name with one from your project:

nix build --impure --expr 'let
  purifier = builtins.getFlake "github:nix-purifiers/pip-purifier";
  pkgs = import purifier.inputs.nixpkgs {
    system = builtins.currentSystem;
    overlays = [ purifier.overlays.default ];
  };
in
(pkgs.pip-purifier.buildPipProject { src = ./.; }).environment'

This uses the purifier’s pinned nixpkgs, matching the lock command above. To build with ambient <nixpkgs> from your Nix search path instead, replace import purifier.inputs.nixpkgs with import <nixpkgs>. Both forms need no Nix files in your project. The build tool and language versions must match the lockfile; with ambient nixpkgs, lock with the purifier from that package set or pass a compatible toolchain to the builder.

Project projections

nix shell 'github:nix-purifiers/pip-purifier'
pip-purifier lock \
  --input 'github:owner/library/<rev>#library'
pip-purifier prefetch
source .venv/bin/activate

The producer exposes projections.python; uv and PDM producers use the same format. Repeat --input for more producers. Pip resolves those packages from their Nix-built wheels, without changing project requirements. The lock records the Nix source pin and omits supplied wheels from downloads. Relocking keeps that pin; update refreshes it.

Use pip-purifier pip for pip operations, including check, so they apply the same bindings. Without a lockfile, it warns and runs ordinary pip.

Each local package and the complete build expose projections.python. Wheelhouses include the native runtime dependency graph, with Nix-derived versions and rewritten dependency metadata for ordinary installers. Composed publishing uploads these wheels. Use an index accepting local versions, or consume the repository with --find-links.

Supported scope

Supported language repositories

pip-purifier locks what pip installs from:

  • package indexes, from PIP_INDEX_URL and PIP_EXTRA_INDEX_URL, from pip’s configuration files, or from the index options of the requirements, and find-links directories;
  • requirements files, with nested -r and -c files, --hash pins (as pip-compile writes them) and environment markers;
  • pylock.toml files (PEP 751), as pip lock writes them;
  • the dependencies of a script (PEP 723), with --requirements-from-script;
  • local projects, editable or not, inside the project directory or beside it;
  • git, Mercurial, Subversion and Bazaar repositories, and URLs of wheels and sdists;
  • a repository or URL that another package names as a source in its own metadata. pip clones or downloads those itself, and the offline install answers it: git through url.<local clone>.insteadOf, Mercurial, Subversion and Bazaar through stand-in commands that copy the locked checkout, and URLs through mitm-cache replay, an HTTPS proxy that serves the locked files;
  • sdists, which pip builds with the build requirements that the lock run recorded.

A lock covers x86_64-linux and aarch64-linux, with the wheels of the Python version that the lock run used. The lockfile records URLs without their credentials, so a private index needs a fetchers.fetchurl that supplies them, such as nixpkgs’ fetchurl with netrcPhase.

Target systems

  • x86_64-linux
  • aarch64-linux
  • aarch64-darwin

Troubleshooting

Examples

For day-to-day work on these projects, use the native-tool setup and commands in Quickstart and Development loop. The Nix expressions below show optional sandboxed packaging of those projects.

pallets/click installs its test dependencies with pip install -r requirements/tests.txt -e .. In a clone of it, lock those arguments, and work offline from .venv:

nix shell 'github:nix-purifiers/pip-purifier'
pip-purifier lock -- -r \
  requirements/tests.txt -e .
pip-purifier prefetch
.venv/bin/pytest tests

Then add a flake.nix that builds its sdist and wheel and runs its tests in the sandbox:

{
  inputs.pip-purifier.url =
    "github:nix-purifiers/pip-purifier";
  inputs.nixpkgs.follows = "pip-purifier/nixpkgs";

  outputs =
    { nixpkgs, pip-purifier, ... }:
    let
      pkgs = import nixpkgs {
        system = "x86_64-linux";
        overlays = [ pip-purifier.overlays.default ];
      };
      project = pkgs.pip-purifier.buildPipProject {
        src = ./.;
        checkPhase = "pytest tests";
      };
    in
    {
      packages.x86_64-linux.default =
        project.packages.click;
      checks.x86_64-linux.tests = project.tests;
    };
}

nix build makes the sdist and wheel, and nix flake check runs the tests. Commit pip-purifier.lock.json and flake.nix with the project.