Skip to content

Commit

Permalink
docs: add django example
Browse files Browse the repository at this point in the history
Co-authored-by: bobvanderlinden
Co-authored-by: domenkozar
  • Loading branch information
rdmolony committed Apr 8, 2024
1 parent a71323c commit 03ae5d6
Show file tree
Hide file tree
Showing 17 changed files with 649 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/python-django/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
35 changes: 35 additions & 0 deletions examples/python-django/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# `devenv-django`

This example demonstrates using [devenv.sh](https://devenv.sh/) alongside [`poetry`](https://python-poetry.org/docs/) for building a `Django` development environment.

Specifically, `devenv` uses `nix` to install system level packages like `postgresql_14` from `nixpkgs` & `poetry` uses `pip` to install `Python` packages from `pypi`

> **Note**: Also see https://github.com/nix-community/poetry2nix/ which converts `poetry` projects to `nix`, this example uses both tools separately

---


## Installation

Install [`devenv.sh`](https://devenv.sh/getting-started)


---


## Usage

`devenv` enables defining scripts in `devenv.nix` that are automatically added to the shell path ...

- Launch a development server via `devenv up`

- Launch a development shell via `devenv shell`

> [!WARNING]
> This example depends on `Postgres`, so please ensure that it's running via `devenv up` (or automatically via `direnv`) before running `python manage.py ...`
- Run tests via `devenv test`

> [!TIP]
> See [`devenv.sh`](https://devenv.sh/tests/) for more information)
156 changes: 156 additions & 0 deletions examples/python-django/devenv.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"nodes": {
"devenv": {
"locked": {
"dir": "src/modules",
"lastModified": 1712143604,
"owner": "cachix",
"repo": "devenv",
"rev": "6b274c61bc67e44885920c4c99389ee777bbce71",
"treeHash": "9f62793de434e026d0bc48cd93350cdfc13f2716",
"type": "github"
},
"original": {
"dir": "src/modules",
"owner": "cachix",
"repo": "devenv",
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1696426674,
"owner": "edolstra",
"repo": "flake-compat",
"rev": "0f9255e01c2351cc7d116c072cb317785dd33b33",
"treeHash": "2addb7b71a20a25ea74feeaf5c2f6a6b30898ecb",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"treeHash": "bd263f021e345cb4a39d80c126ab650bebc3c10c",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"pre-commit-hooks",
"nixpkgs"
]
},
"locked": {
"lastModified": 1709087332,
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
"treeHash": "ca14199cabdfe1a06a7b1654c76ed49100a689f9",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1710796454,
"owner": "cachix",
"repo": "devenv-nixpkgs",
"rev": "06fb0f1c643aee3ae6838dda3b37ef0abc3c763b",
"treeHash": "9bb13f7f39e825a5d91bbe4139fbc129243b907d",
"type": "github"
},
"original": {
"owner": "cachix",
"ref": "rolling",
"repo": "devenv-nixpkgs",
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1711668574,
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "219951b495fc2eac67b1456824cc1ec1fd2ee659",
"treeHash": "51ec3259d600ba0dbef93d23999da1141e943713",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.11",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
],
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1712055707,
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "e35aed5fda3cc79f88ed7f1795021e559582093a",
"treeHash": "41bee5f5bd9314a987ef3d7cba730fb6aa264a4e",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"devenv": "devenv",
"nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"treeHash": "cce81f2a0f0743b2eb61bc2eb6c7adbe2f2c6beb",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
43 changes: 43 additions & 0 deletions examples/python-django/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{ pkgs, config, ... }:

let
db_user = "postgres";
db_host = "localhost";
db_port = "5432";
db_name = "db";
in
{
packages = [ pkgs.git pkgs.postgresql_14 ];

languages.python = {
enable = true;
package = pkgs.python310;
poetry.enable = true;
};

env = {
DATABASE_URL = "postgres:https://${db_user}@/${db_name}?host=${config.env.PGHOST}";
DEBUG = true;
SECRET_KEY = "123";
STATIC_ROOT = "/tmp";
};

services.postgres = {
enable = true;
initialScript = "CREATE USER ${db_user} SUPERUSER;";
initialDatabases = [{ name = db_name; }];
listen_addresses = db_host;
};

processes = {
runserver.exec = ''
devenv shell python manage.py runserver
'';
};

enterTest = ''
wait_for_port ${db_port}
python manage.py test
'';

}
3 changes: 3 additions & 0 deletions examples/python-django/devenv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inputs:
nixpkgs:
url: github:cachix/devenv-nixpkgs/rolling
22 changes: 22 additions & 0 deletions examples/python-django/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/nix/store/iw1vmh509hcbby8dbpsaanbri4zsq7dj-python3-3.10.10/bin/python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file.
6 changes: 6 additions & 0 deletions examples/python-django/myapp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class MyAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'myapp'
Empty file.
5 changes: 5 additions & 0 deletions examples/python-django/myapp/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.test import TestCase

class Test(TestCase):
def test(self):
self.assertEqual(1, 1)
Empty file.
16 changes: 16 additions & 0 deletions examples/python-django/myproject/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for myproject project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

application = get_asgi_application()
Loading

0 comments on commit 03ae5d6

Please sign in to comment.