Skip to content
Public template

About

Python flake template with uv2nix, uv, ruff, and pre-commit hooks.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

Repository files navigation

Python development template

A Nix flake for a Python project managed with uv. purr builds the flake outputs from the files in develop/.

What you get

  • Python from nixpkgs, chosen by requires-python
  • A package built from uv.lock with uv2nix, pyproject.nix and build-system-pkgs
  • A dev shell with a virtualenv, your project installed in editable mode, uv and the lint tools
  • Pre-commit hooks from git-hooks.nix: nixfmt, deadnix, statix, ruff, ruff-format
  • nixfmt-tree as the flake formatter

Start a project

nix flake init -t "github:nixcafe/develop-templates#python" --refresh
git init && git add -A

To use a shorter registry name:

nix registry add beans "github:nixcafe/develop-templates"
nix flake init -t beans#python
git init && git add -A

If 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 --clone

Commands

nix 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 script

With 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 format

Edits under src/ take effect right away.

Layout

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

Customize

Dependencies

Use uv inside the shell:

uv add requests
uv add --dev pytest

This 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.

Python version

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.

Rename the project

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.

Relock outside the shell

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 -A

Hooks

Edit develop/checks/git-hooks/default.nix. The hook list shows what is available. statix.toml holds the statix settings.

More outputs

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.

About

Python flake template with uv2nix, uv, ruff, and pre-commit hooks.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages

Generated from nixcafe-develop/nix