-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathflake.nix
More file actions
564 lines (476 loc) · 20.1 KB
/
flake.nix
File metadata and controls
564 lines (476 loc) · 20.1 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
{
description = "CARE - Care is a Digital Public Good enabling TeleICU & Decentralised Administration of Healthcare Capacity across States.";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{ nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
python = pkgs.python313;
# Create a Python environment with pip-installable packages
pythonEnv = python.withPackages (
ps: with ps; [
pip
setuptools
wheel
virtualenv
]
);
# Project-local data directory (still impure but isolated)
projectDataDir = ".nix-data";
postgresDir = "${projectDataDir}/postgres";
redisDir = "${projectDataDir}/redis";
minioDir = "${projectDataDir}/minio";
# Environment variables for development
envVars = {
# Database
POSTGRES_USER = "postgres";
POSTGRES_PASSWORD = "postgres";
POSTGRES_HOST = "localhost";
POSTGRES_DB = "care";
POSTGRES_PORT = "5432";
DATABASE_URL = "postgres://postgres:postgres@localhost:5432/care";
# Redis
REDIS_URL = "redis://localhost:6379";
CELERY_BROKER_URL = "redis://localhost:6379/0";
# Django
DJANGO_DEBUG = "true";
ATTACH_DEBUGGER = "false";
DJANGO_SETTINGS_MODULE = "config.settings.local";
# MinIO/S3
BUCKET_REGION = "ap-south-1";
BUCKET_KEY = "minioadmin";
BUCKET_SECRET = "minioadmin";
BUCKET_ENDPOINT = "http://localhost:9100";
BUCKET_EXTERNAL_ENDPOINT = "http://localhost:9100";
FILE_UPLOAD_BUCKET = "patient-bucket";
FACILITY_S3_BUCKET = "facility-bucket";
# PostgreSQL configuration for compilation (using Nix store paths)
PG_CONFIG = "${pkgs.postgresql_15}/bin/pg_config";
LDFLAGS = "-L${pkgs.postgresql_15}/lib";
CPPFLAGS = "-I${pkgs.postgresql_15}/include";
};
# Helper scripts
makeScript =
name: text:
pkgs.writeShellScriptBin name ''
set -euo pipefail
${text}
'';
# Service management scripts using Nix store binaries
startServices = makeScript "start-services" ''
echo "📂 Using project data directory: ${projectDataDir}"
mkdir -p ${postgresDir} ${redisDir} ${minioDir}
echo "🐘 Starting PostgreSQL..."
if ! ${pkgs.procps}/bin/pgrep -x "postgres" > /dev/null; then
# Check if database directory is corrupted or not properly initialized
if [ -d ${postgresDir} ] && [ ! -f ${postgresDir}/PG_VERSION ]; then
echo "Database directory exists but appears corrupted. Cleaning up..."
rm -rf ${postgresDir}
mkdir -p ${postgresDir}
fi
# Initialize database if it doesn't exist
if [ ! -f ${postgresDir}/PG_VERSION ]; then
echo "Initializing new PostgreSQL database..."
${pkgs.postgresql_15}/bin/initdb -D ${postgresDir} -U postgres --auth=trust
# Configure to use Unix socket in project directory
echo "unix_socket_directories = '$(pwd)/${postgresDir}'" >> ${postgresDir}/postgresql.conf
echo "port = 5432" >> ${postgresDir}/postgresql.conf
fi
${pkgs.postgresql_15}/bin/pg_ctl -D ${postgresDir} -l ${postgresDir}/logfile start
sleep 2
${pkgs.postgresql_15}/bin/createdb -h localhost -U postgres care || echo "Database 'care' already exists"
else
echo "PostgreSQL already running"
fi
echo "📮 Starting Redis..."
if ! ${pkgs.procps}/bin/pgrep -x "redis-server" > /dev/null; then
# Ensure Redis directory exists
mkdir -p ${redisDir}
${pkgs.redis}/bin/redis-server \
--daemonize yes \
--bind 127.0.0.1 \
--port 6379 \
--dir ${redisDir} \
--dbfilename dump.rdb
else
echo "Redis already running"
fi
echo "🗄️ Starting MinIO..."
if ! ${pkgs.procps}/bin/pgrep -x "minio" > /dev/null; then
MINIO_ROOT_USER="${envVars.BUCKET_KEY}" \
MINIO_ROOT_PASSWORD="${envVars.BUCKET_SECRET}" \
${pkgs.minio}/bin/minio server ${minioDir} \
--address ":9100" \
--console-address ":9001" &
sleep 3
else
echo "MinIO already running"
fi
echo "✅ All services started!"
echo " PostgreSQL data: ${postgresDir}"
echo " Redis data: ${redisDir}"
echo " MinIO data: ${minioDir}"
'';
stopServices = makeScript "stop-services" ''
PROJECT_DATA_DIR="${projectDataDir}"
POSTGRES_DIR="$PROJECT_DATA_DIR/postgres"
echo "🛑 Stopping services..."
echo "Stopping PostgreSQL..."
if [ -f "$POSTGRES_DIR/postmaster.pid" ]; then
${pkgs.postgresql_15}/bin/pg_ctl -D "$POSTGRES_DIR" stop
else
${pkgs.procps}/bin/pkill postgres || true
fi
echo "Stopping Redis..."
${pkgs.procps}/bin/pkill redis-server || true
echo "Stopping MinIO..."
${pkgs.procps}/bin/pkill minio || true
echo "✅ Services stopped"
'';
# Kill all development processes
killAll = makeScript "kill-care" ''
echo "🛑 Stopping all Care development processes..."
# Stop Django development server
echo "Stopping Django development server..."
${pkgs.procps}/bin/pkill -f "runserver_plus" || true
${pkgs.procps}/bin/pkill -f "manage.py runserver" || true
${pkgs.procps}/bin/pkill -f "python.*manage.py" || true
# Stop Celery workers and beat
echo "Stopping Celery workers..."
${pkgs.procps}/bin/pkill -f "celery.*worker" || true
${pkgs.procps}/bin/pkill -f "celery.*beat" || true
${pkgs.procps}/bin/pkill -f "watchmedo.*celery" || true
# Stop debugpy if running
echo "Stopping debugger..."
${pkgs.procps}/bin/pkill -f "debugpy" || true
# Stop background services
echo "Stopping background services..."
${stopServices}/bin/stop-services
# Clean up any remaining Python processes that might be related
echo "Cleaning up remaining processes..."
${pkgs.procps}/bin/pkill -f "python.*config.celery_app" || true
# Wait a moment for processes to terminate
sleep 2
# Force kill any stubborn processes
echo "Force killing stubborn processes..."
${pkgs.procps}/bin/pkill -9 -f "runserver_plus" 2>/dev/null || true
${pkgs.procps}/bin/pkill -9 -f "celery.*worker" 2>/dev/null || true
${pkgs.procps}/bin/pkill -9 -f "celery.*beat" 2>/dev/null || true
echo "✅ All development processes stopped"
echo ""
echo "To restart:"
echo " start-services # Start background services"
echo " rundev # Start unified development environment"
'';
# Clean project data (useful for fresh start)
cleanData = makeScript "clean-data" ''
PROJECT_DATA_DIR="${projectDataDir}"
echo "⚠️ This will delete all local service data (PostgreSQL, Redis, MinIO)"
read -p "Are you sure? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
${stopServices}/bin/stop-services
echo "🗑️ Removing $PROJECT_DATA_DIR..."
rm -rf "$PROJECT_DATA_DIR"
echo "✅ Project data cleaned"
else
echo "Cancelled"
fi
'';
# Development setup
setupDev = makeScript "setup-dev" ''
echo "🏗️ Setting up development environment..."
# Install Python dependencies
if [ ! -d ".venv" ]; then
echo "Creating virtual environment..."
${python}/bin/python -m venv .venv
fi
source .venv/bin/activate
echo "Installing Python dependencies..."
pip install --upgrade pip pipenv
pipenv install --dev --system
# Install plugins
python install_plugins.py
echo "✅ Development environment setup complete!"
echo ""
echo "Note: ruff ${pkgs.ruff.version} is available from Nix store"
'';
# Django management commands
djangoManage = makeScript "manage" ''
source .venv/bin/activate 2>/dev/null || true
python manage.py "$@"
'';
# Database operations
migrateDb = makeScript "migrate" ''
source .venv/bin/activate
python manage.py migrate
'';
makeMigrations = makeScript "makemigrations" ''
source .venv/bin/activate
python manage.py makemigrations "$@"
'';
loadFixtures = makeScript "load-fixtures" ''
source .venv/bin/activate
python manage.py load_fixtures
'';
# Unified development server (API + Celery)
runDev = makeScript "rundev" ''
source .venv/bin/activate
echo "🚀 Starting unified Care development environment..."
# Wait for services (using Nix bash)
${pkgs.bash}/bin/bash ./scripts/wait_for_db.sh
${pkgs.bash}/bin/bash ./scripts/wait_for_redis.sh
# Run migrations and setup
echo "📊 Running database migrations..."
python manage.py migrate --noinput
python manage.py compilemessages -v 0
python manage.py sync_permissions_roles
python manage.py sync_valueset
# Collect static files
echo "📦 Collecting static files..."
python manage.py collectstatic --noinput
echo "✅ Setup complete! Starting services..."
# Start Celery worker and beat in background
echo "🔄 Starting Celery worker with beat scheduler..."
watchmedo auto-restart --directory=./ --pattern=*.py --recursive -- \
celery --workdir="$(pwd)" -A config.celery_app worker -B --loglevel=INFO &
CELERY_PID=$!
# Cleanup function
cleanup() {
echo "🛑 Shutting down services..."
kill $CELERY_PID 2>/dev/null || true
exit 0
}
trap cleanup SIGINT SIGTERM
# Wait a moment for Celery to start
sleep 3
# Start Django development server
echo "🌐 Starting Django development server on http://localhost:9000..."
if [[ "''${ATTACH_DEBUGGER:-false}" == "true" ]]; then
echo "🐛 Debug mode enabled - waiting for debugger on port 9876..."
python -m debugpy --wait-for-client --listen 0.0.0.0:9876 manage.py runserver_plus 0.0.0.0:9000 --print-sql
else
python manage.py runserver_plus 0.0.0.0:9000 --print-sql
fi
'';
# Individual development server (API only)
runServer = makeScript "runserver" ''
source .venv/bin/activate
${pkgs.bash}/bin/bash ./scripts/wait_for_db.sh
${pkgs.bash}/bin/bash ./scripts/wait_for_redis.sh
echo "📊 Running migrations..."
python manage.py migrate --noinput
python manage.py compilemessages -v 0
python manage.py sync_permissions_roles
python manage.py sync_valueset
echo "📦 Collecting static files..."
python manage.py collectstatic --noinput
echo "🌐 Starting Django development server..."
if [[ "''${ATTACH_DEBUGGER:-false}" == "true" ]]; then
echo "🐛 Debug mode enabled - waiting for debugger on port 9876..."
python -m debugpy --wait-for-client --listen 0.0.0.0:9876 manage.py runserver_plus 0.0.0.0:9000 --print-sql
else
python manage.py runserver_plus 0.0.0.0:9000 --print-sql
fi
'';
# Individual Celery worker
runCelery = makeScript "celery" ''
source .venv/bin/activate
${pkgs.bash}/bin/bash ./scripts/wait_for_db.sh
${pkgs.bash}/bin/bash ./scripts/wait_for_redis.sh
echo "📊 Running migrations..."
python manage.py migrate --noinput
python manage.py compilemessages -v 0
python manage.py sync_permissions_roles
python manage.py sync_valueset
echo "🔄 Starting Celery worker with beat scheduler..."
watchmedo auto-restart --directory=./ --pattern=*.py --recursive -- \
celery --workdir="$(pwd)" -A config.celery_app worker -B --loglevel=INFO
'';
# Testing
runTests = makeScript "test" ''
source .venv/bin/activate
python manage.py test "''${@:-}" --settings=config.settings.test --keepdb --parallel --shuffle
'';
runTestsNoKeep = makeScript "test-no-keep" ''
source .venv/bin/activate
python manage.py test "''${@:-}" --settings=config.settings.test --parallel --shuffle
'';
testCoverage = makeScript "test-coverage" ''
source .venv/bin/activate
coverage run manage.py test --settings=config.settings.test --keepdb --parallel --shuffle
coverage combine || true
coverage xml
coverage report
'';
# Code quality (using Nix-provided ruff for NixOS compatibility)
ruffCheck = makeScript "ruff" ''
${pkgs.ruff}/bin/ruff check --fix $(git diff --name-only --staged | grep -E '\.py$|/pyproject\.toml$' || echo ".")
'';
ruffAll = makeScript "ruff-all" ''
${pkgs.ruff}/bin/ruff check .
'';
ruffFix = makeScript "ruff-fix-all" ''
${pkgs.ruff}/bin/ruff check --fix .
'';
# Database backup/restore using Nix store binaries
dumpDb = makeScript "dump-db" ''
${pkgs.postgresql_15}/bin/pg_dump -h localhost -U postgres -Fc care > care_db.dump
echo "✅ Database dumped to care_db.dump"
'';
loadDb = makeScript "load-db" ''
if [ -f "care_db.dump" ]; then
${pkgs.postgresql_15}/bin/pg_restore -h localhost -U postgres --clean --if-exists -d care care_db.dump
echo "✅ Database restored from care_db.dump"
else
echo "❌ care_db.dump not found"
exit 1
fi
'';
resetDb = makeScript "reset-db" ''
${pkgs.postgresql_15}/bin/dropdb -h localhost -U postgres care -f || true
${pkgs.postgresql_15}/bin/createdb -h localhost -U postgres care
echo "✅ Database reset"
'';
# Health check
healthCheck = makeScript "healthcheck" ''
source .venv/bin/activate
${pkgs.bash}/bin/bash ./scripts/healthcheck.sh
'';
in
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
# Python and package management
pythonEnv
# Databases and services (from Nix store)
postgresql_15
libpq
redis
minio
ruff # Ruff from Nix for NixOS compatibility
# System dependencies for building Python packages
pkg-config
zlib
libjpeg
gmp
gettext
curl
wget
git
# Process management
procps
# Development tools
pre-commit
# Build tools
gcc
gnumake
# Development scripts
setupDev
startServices
stopServices
killAll
cleanData
djangoManage
migrateDb
makeMigrations
loadFixtures
runDev
runServer
runCelery
runTests
runTestsNoKeep
testCoverage
ruffCheck
ruffAll
ruffFix
dumpDb
loadDb
resetDb
healthCheck
];
shellHook = ''
${builtins.concatStringsSep "\n" (
pkgs.lib.mapAttrsToList (name: value: "export ${name}='${value}'") envVars
)}
# Create project data directory
mkdir -p ${projectDataDir}
# Add .nix-data to .gitignore if not already there
if [ -f .gitignore ] && ! grep -q "^\.nix-data$" .gitignore; then
echo ".nix-data" >> .gitignore
fi
echo "🏥 Welcome to Care development environment!"
echo ""
echo "📦 Using reproducible Nix store binaries:"
echo " PostgreSQL: ${pkgs.postgresql_15.version}"
echo " Redis: ${pkgs.redis.version}"
echo " MinIO: ${pkgs.minio.version}"
echo " Ruff: ${pkgs.ruff.version}"
echo ""
echo "Available commands:"
echo " setup-dev - Set up the development environment"
echo " start-services - Start PostgreSQL, Redis, and MinIO"
echo " stop-services - Stop background services only"
echo " kill-care - 🛑 Stop ALL development processes and services"
echo " clean-data - 🗑️ Remove all local service data"
echo " rundev - 🚀 Start both API server and Celery worker (RECOMMENDED)"
echo " runserver - Start Django development server only"
echo " celery - Start Celery worker with beat only"
echo " manage <cmd> - Run Django management commands"
echo " migrate - Run database migrations"
echo " makemigrations - Create new migrations"
echo " load-fixtures - Load sample data"
echo " test - Run tests with coverage"
echo " test-no-keep - Run tests without keeping DB"
echo " test-coverage - Run tests with coverage report"
echo " ruff - Check and fix staged files"
echo " ruff-all - Check all files"
echo " ruff-fix-all - Fix all files"
echo " dump-db - Backup database"
echo " load-db - Restore database"
echo " reset-db - Reset database"
echo " healthcheck - Check application health"
echo ""
echo "🚀 Quick Start (Recommended):"
echo " 1. Run 'setup-dev' to install Python dependencies"
echo " 2. Run 'start-services' to start required services"
echo " 3. Run 'rundev' to start both API server and Celery worker"
echo ""
echo "The Django server will be available at http://localhost:9000"
echo "MinIO console will be available at http://localhost:9001"
echo ""
# Auto-activate virtual environment if it exists
if [ -d ".venv" ]; then
source .venv/bin/activate
echo "✅ Virtual environment activated"
else
echo "⚠️ Run 'setup-dev' to create virtual environment and install dependencies"
fi
# Verify tools are available
echo "✅ PostgreSQL development tools available (${pkgs.postgresql_15.version})"
echo "✅ Ruff available (${pkgs.ruff.version})"
'';
};
# Package for CI/CD or other use cases
packages.default = pkgs.writeShellApplication {
name = "care-dev";
runtimeInputs = [
pythonEnv
pkgs.postgresql_15
pkgs.redis
pkgs.minio
pkgs.ruff
];
text = ''
echo "Care development environment package"
echo "Use 'nix develop' to enter the development shell"
'';
};
}
);
}