Skip to content

Latest commit

 

History

History
134 lines (116 loc) · 3.94 KB

README.md

File metadata and controls

134 lines (116 loc) · 3.94 KB

Nix Flake Template for Python using UV

All of this information is also included in the README.md

Initialize using

nix flake init --template "github:nulladmin1/nix-flake-templates#python-uv"

Heavily inspired by the official uv2nix docs

This is how the structure of the template looks like:

📦 python-uv
├─ ⚙️ .python-version
├─ 📁 app
│  ├─ 🐍 __init__.py
│  └─ 🐍 main.py
├─ 🔒 flake.lock
├─ ⚙️ flake.nix
├─ ⚙️ pyproject.toml
├─ 📃 README.md
└─ 📁 tests
│  └─ 🐍 main.py
└─ 🔒 uv.lock

It includes a basic Python project that returns an SHA256 encoded string of the user's input. It has a testcase that can be run using Pytest or unittest.

The flake is able to run in the specified systems listed in the flake. It contains a devShells as an output with Python,Setuptools and Pip, and an app as an output that builds a Python project using buildPythonPackage.

Run using uv2nix

nix run

Run

Go into Development Shell

nix develop

(Optional) Format flake.nix using Alejandra

nix fmt

To customize it to your own needs:

  • In 'flake.nix'
    • Edit description
      {
          description = "Nix Flake Template for Python using UV";
      }
    • Change virtualenv name for devShell
      virtualenv = editablePythonSet.mkVirtualEnv "app" workspace.deps.all;
    • Change virtualenv name for the package
      default = pythonSet.${system}.mkVirtualEnv "app" workspace.deps.default;
    • Change executable name for app
      program = "${self.packages.${system}.default}/bin/app";
  • In pyproject.toml
    • Change project details accordingly
      [project]
      name = "app"
      version = "0.1.0"
      description = "Add your description here"
      readme = "README.md"
      dependencies = []
    • Change Python version accordingly
      requires-python = ">=3.12"
    • Add necessary and optional build dependencies
      [tool.uv]
      dev-dependencies = [
      "pytest>=8.3.3",
      ]
      The best way to add dependencies would be using
      uv add DEPENDENCY
      Likewise, the best way to remove dependencies would be using
      uv remove DEPENDENCY
      Adding or removing dev dependencies would require adding the --dev option
    • Change the name and path of scripts if needed. These can be run by uv run script_name
      [project.scripts]
      app = "app:main"
    • Change the build-system if needed (Hatchling is good enough, however uv will definitely come out with it's own build system in the future)
      [build-system]
      requires = ["hatchling"]
      build-backend = "hatchling.build"
  • For the structure and code
    • Rename the app/ directory to the name of your project. Make sure it's the same as the path in the pyproject.toml
      📦 python-uv
      ├─ 📁 app
      │  ├─ 🐍 __init__.py
      │  └─ 🐍 main.py
      
    • Add necessary code for the program in the previous subdirectory.
    • Make sure that for the __init__.py, that it imports from the name of your project, and it imports the necessary functions to be used as a library
      from app.main import main, get_sha256
    • Add necessary test cases and modifications in the tests/ subdirectory. Prepend all added files in that subdirectory with test_
    • Make sure that for the test_main.py, that it imports from the name of your project, and it imports the necessary functions for testing
      from app.main import main, get_sha256