cashmere

cashmere

Nix Flake template for python projects

Description

This is a simple flake on how to configure a project based flake.

Literate Programming Nix Flake

Flake Structure

{

Description

Define the flake's purpose and functionality

description = "Development environment with Python and custom packages";

Inputs

Essential dependencies for the flake.

inputs = {
  nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  flake-utils.url = "github:numtide/flake-utils";
};

Outputs

Main flake output function.

outputs =
  {
    nixpkgs,
    flake-utils,
    ...
  }:

System Configuration

Multi-system support using flake-utils. Ensuring the flake works on all supported plattforms.

flake-utils.lib.eachDefaultSystem (
  system:
  let
    pkgs = nixpkgs.legacyPackages.${system};

Python Environment

Configure Python version for the project. In this case it is defined to Python version 3.13

python = pkgs.python313;

Output Configuration

Define the flake's outputs and environments

in
{

Flake Closure

}

pyproject.toml declaration

build-system

Here we add the hatchling build system, the same as we declared it before in the flake.nix

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

project specification

Here you may define some information about your project, which Python version is required and also its dependencies.

[project]
name = "Example project"
version = "0.1.0"
description = "Example description"
readme = "README.org"
requires-python = ">=3.10"
dependencies = [
    "fastapi",
    "uvicorn[standard]",
    "jinja2"
]

Scripts declaration

When I package my python projects i like to run them in the terminal as any other cli tool. So after packaging if I would run example-project-run then the application would start without the need to run it with the python3 prefix.

[project.scripts]
example-project-run = "src.main:main"

Folder project specification

Last but not least we need to specify the folder, were our package is specified. I like to work with src/ as any other common programming language.

[tool.hatch.build.targets.wheel]
packages = ["src"]