Skip to content

Latest commit

 

History

History

python-pyproject-nix

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Nix Flake Template for Python using pyproject-nix

Initialize using

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

OR

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

This is how the structure of the template looks like:

📦 python-poetry
├── ⚙️ .envrc
├── 🙈 .gitignore
├── 📃 README.md
├── ❄️ flake.nix
├── 📁 project_name
│   ├── 🐍 __init__.py
│   └── 🐍 main.py
└── ⚙️ pyproject.toml

It includes a basic Python project that returns an SHA256 encoded string of the user's input.

The flake is able to run in the specified systems listed in the flake. It contains a devShells as an output with a pythonEnv that contains all project dependencies, and an app as an output that builds a Python project using pyproject-nix's 'buildPythonPackage renderer.

Run using Nix

nix 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 = "project_name";
      }
    • Change Python version if necessary
      python = forEachSystem (system: pkgsFor.${system}.python312);
    • Change devShell dependencies
      {
      pkgsFor.${system}.mkShell {
          packages = [
            pythonEnv
          ];
        };
      }
    • Change executable name for app
      program = "${self.packages.${system}.default}/bin/project_name";
  • In pyproject.toml

    • Change project details accordingly

      [project]
      name = "project_name"
      version = "0.1.0"
      description = ""
      authors = [
          {name = "Your Name", email = "[email protected]"},
      ]
    • Change Python version accordingly

      requires-python = ">= 3.11"
    • Add necessary and optional build dependencies

      dependencies = [
      ]
      
      [project.optional-dependencies]
      test = [
      "pytest"
      ]
    • Change the name and path of scripts if needed

      [project.scripts]
      project_name = "project_name:main"
  • For the structure and code

    • Rename the project_name/ directory to the name of your project. Make sure its the same as the path in the pyproject.toml
      📦 python-pyproject-nix
      ├─ 📁 project_name
      │  ├─ 🐍 __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 project_name.main import main, get_sha256