-
Notifications
You must be signed in to change notification settings - Fork 1
/
devenv.nix
93 lines (81 loc) · 2.24 KB
/
devenv.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{ pkgs, ... }:
let
db_user = "postgres";
db_host = "localhost";
db_port = "5432";
db_name = "db";
python_version = "3.10";
in
{
packages = [ pkgs.git pkgs.postgresql_14 pkgs.python310 pkgs.poetry ];
env = {
PYTHON_VERSION = python_version;
DATABASE_URL = "postgresql://${db_user}@${db_host}:${db_port}/${db_name}";
DEBUG = true;
STATIC_ROOT = "/tmp/static";
};
enterShell = ''
create-poetry-environment
# Activate poetry environment on shell entry
# https://pythonspeed.com/articles/activate-virtualenv-dockerfile/
VIRTUAL_ENV=`poetry env info --path` && export VIRTUAL_ENV
export PATH="$VIRTUAL_ENV/bin:$PATH";
'';
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
'';
};
scripts = {
create-poetry-environment.exec = ''
echo "Building poetry virtual environment..."
poetry env use ${python_version}
echo "Using versions ..."
python --version
poetry --version
poetry install
'';
start-db.exec = ''
psql --version
# Start Postgres if not running ...
if ! nc -z ${db_host} ${db_port};
then
echo "Starting Postgres in background on ${db_host}:${db_port} ..."
# NOTE: This is a hack to get Postgres to run in the background
# ... that relies on `devenv` naming the process `postgres`
# ... startup shell as `start-postgres`
nohup start-postgres > /tmp/postgres.log 2>&1 &
fi
'';
wait-for-db.exec = ''
echo "Waiting for Postgres to start on ${db_host}:${db_port} ..."
timer=0;
n_seconds=20;
while true;
do
if nc -z ${db_host} ${db_port}; then
echo "Database is running!"
break
elif [ $timer -gt $n_seconds ]; then
echo "Database failed to launch!"
break
else
sleep 0.1
let timer++
fi
done
'';
run-tests.exec = ''
start-db
wait-for-db
python manage.py collectstatic --noinput
python manage.py test
'';
};
}