How to Run a Django Flake with Poetry2Nix

I’m trying to build and run a Django web application using Poetry2Nix and flakes. Here is my directory structure:

The parent directory is nix_django:

 .
├── flake.lock
├── flake.nix
├── nix_django
│   └── __init__.py
├── poetry.lock
├── pyproject.toml
├── README.md
├── runserver.py
├── tango_with_django_project
│   ├── db.sqlite3
│   ├── manage.py
│   └── tango_with_django_project
│       ├── __init__.py
│       ├── asgi.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
└── tests
    └── __init__.py

I’m able to start the app by executing these commands:

nix develop
cd tango_with_django_project
python manage.py runserver

I’m also able to build it by executing nix build. So far so good.

The issue is that I don’t know how to create a “nix compliant” binary. I think ideally I would like to run something like result/bin/my-app and have it give me the same results as the manage.py command above.

The poetry2nix app recommends doing this by adding something like the following to my pyproject.toml file:

[tool.poetry.scripts]
rs = "runserver:run_my_server"

However, when I try to run result/bin/rs I get the following error:

Traceback (most recent call last):
  File "/nix/store/314q4y5cmkrimhi4s5rwylv8zc3q2whz-python3.10-nix-django-0.1.0/bin/.rs-wrapped", line 6, in <module>
    from runserver import run_my_server
ModuleNotFoundError: No module named 'runserver'

Here is my flake.nix file:

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

  outputs = { self, nixpkgs }:
    let
      supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
      pkgs = forAllSystems (system: nixpkgs.legacyPackages.${system});
    in
    {
      packages = forAllSystems (system: {
        default = pkgs.${system}.poetry2nix.mkPoetryApplication { projectDir = self; };
      });

      devShells = forAllSystems (system: {
        default = pkgs.${system}.mkShellNoCC {
          packages = with pkgs.${system}; [
            (poetry2nix.mkPoetryEnv {
              projectDir = self;
            })
            poetry
          ];
        };
      });
    };
}

Also, here is my runserver.py script:

import subprocess

def run_my_server():
    cmd = ['python', 'tango_with_django_project/manage.py', 'runserver']
    subprocess.run(cmd)

Does anyone know which step I’m missing?

Did you ever get this figured out?

Maybe it’s just me not knowing how poetry works, but isn’t runserver.py supposed to be inside a project directory like tango_with_django_project? I thought poetry only uses this subdirectory-based approach. Or maybe you can write something like

[tool.poetry.scripts]
rs = "../runserver:run_my_server"
1 Like