A Nix flake for a Python project managed with uv. purr builds the flake outputs from the files in develop/.
- Python from nixpkgs, chosen by
requires-python - A package built from
uv.lockwith uv2nix, pyproject.nix and build-system-pkgs - A dev shell with a virtualenv, your project installed in editable mode,
uvand the lint tools - Pre-commit hooks from git-hooks.nix:
nixfmt,deadnix,statix,ruff,ruff-format nixfmt-treeas the flake formatter
nix flake init -t "github:nixcafe/develop-templates#python" --refresh
git init && git add -ATo use a shorter registry name:
nix registry add beans "github:nixcafe/develop-templates"
nix flake init -t beans#python
git init && git add -AIf you use cattery-modules, the beans registry entry already exists.
Run git init && git add -A before nix develop. Nix only sees files that git tracks, and both the REPO_ROOT editable install and pre-commit hooks require a git repository. The first nix command writes flake.lock. Commit it together with uv.lock.
You can also create a GitHub repository from this one:
gh repo create my-python-project --template nixcafe-develop/python --clonenix develop # enter the dev shell and install the git hooks
nix fmt # format the Nix files
nix flake check # run the hooks on all files
nix build # build the application
nix run # run the example scriptWith direnv, run direnv allow once and the shell loads when you enter the directory.
Inside the shell:
example # the script from project.scripts
python -m example
ruff check
ruff formatEdits under src/ take effect right away.
flake.nix inputs and the purr call
pyproject.toml
uv.lock
statix.toml
src/example/
develop/
lib/default.nix uv workspace and Python package set
packages/default/default.nix application build
shells/default/default.nix dev shell
checks/git-hooks/default.nix hooks
Use uv inside the shell:
uv add requests
uv add --dev pytestThis updates pyproject.toml and uv.lock but installs nothing, because Nix builds the virtualenv. Reload the shell with direnv reload, or exit and run nix develop again.
The shell includes all optional dependencies and dependency groups. The package includes the runtime dependencies and the groups in tool.uv.default-groups.
uv2nix installs wheels where it can. A package that has to build from source may need extra build inputs. Add an override to mkPythonSet in develop/lib/default.nix, following the uv2nix docs.
Set requires-python in pyproject.toml, then relock. The first version number picks the nixpkgs interpreter, so >=3.12 means python312. Without a version, you get python3.
Change name and project.scripts in pyproject.toml, rename src/example, update the imports, then relock. The Nix files read the name from pyproject.toml.
The dev shell does not build while uv.lock disagrees with the project name or Python version. Regenerate the lock with uv from nixpkgs:
nix shell nixpkgs#uv nixpkgs#python3 -c uv lock
git add -AEdit develop/checks/git-hooks/default.nix. The hook list shows what is available. statix.toml holds the statix settings.
Add develop/packages/<name>/default.nix, develop/shells/<name>/default.nix or develop/checks/<name>/default.nix. purr picks them up without changes to flake.nix.