All this information is also included in the README.md
Initialize using
nix flake init --template "github:nulladmin1/nix-flake-templates#python-poetry"
OR Initialize using
nix flake init --template "github:nulladmin1/nix-flake-templates#python"
This is how the structure of the template looks like:
📦 python-poetry
├─ 📁 app
│ ├─ 🐍 __init__.py
│ └─ 🐍 main.py
├─ 🔒 flake.lock
├─ ⚙️ flake.nix
├─ 🔒 poetry.lock
├─ ⚙️ pyproject.toml
├─ 📃 README.md
└─ 📁 tests
└─ 🐍 test_main.py
It includes a basic Poetry 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
and Poetry
, and an app as an output that builds a Poetry
project using poetry2nix
. This section is inspired by the official docs for poetry2nix
Run app
nix run
Drop into a development shell
nix develop
Install project and its dependencies
poetry install
Run app
poetry run app
Drop into a development shell
nix develop
Install project and its dependencies
poetry install
Run Pytest
poetry run pytest
(Optional) Format flake.nix
using Alejandra
nix fmt
-
In
flake.nix
- Edit description
{ description = "Nix Flake Template for Python using Poetry"; }
- Change name of the Poetry Application (In this example, it's called app)
{ app = mkPoetryApplication {projectDir = ./.;}; }
- Change the name of the Poetry Application and the location of its binary (In this example, both are called app)
{ program = "${app}/bin/app"; }
- Edit description
-
- Change name, version, description, and authors of the project
[tool.poetry] name = "app" version = "0.1.0" description = "" authors = ["Your Name <[email protected]>"]
- Remove and add needed dependencies using
poetry add ...
[tool.poetry.dependencies] python = "^3.12" [tool.poetry.group.dev.dependencies] pytest = "^8.3.3"
- Change the name of the scripts and path
[tool.poetry.scripts] app = "app.main:main"
- Change name, version, description, and authors of the project
-
For the structure and code
- Rename the
app/
directory to the name of your project. Make sure its the same as the path in thepyproject.toml
📦 python-poetry ├─ 📁 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 libraryfrom app.main import main, get_sha256
- Add necessary test cases and modifications in the
tests/
subdirectory. Prepend all added files in that subdirectory withtest_
- Make sure that for the
test_main.py
, that it imports from the name of your project, and it imports the necessary functions for testingfrom app.main import main, get_sha256
- Rename the
-
Execution
- If running the project using
Poetry
, run using:where PROJECT_NAME is the name of the project. Make sure it matches the name of the script in thepoetry run PROJECT_NAME
pyproject.toml
where ${PROJECT_NAME} is the name of the project, and the ${PROJECT_DIR} is the location of the project (recommended to be the same as the PROJECT_NAME). By default it's:[tool.poetry.scripts] PROJECT_NAME = "PROJECT_DIR.main:main"
app/
- If running the project using