···11# Nix Development Environment for CARE
2233-This document describes how to set up and use the Nix-based development environment for the CARE
33+This document describes how to set up and use the Nix-based development environment for the CARE project.
4455## Prerequisites
66···2626 sudo dnf install direnv # Fedora
2727 ```
28282929-## Quick Setup
2929+## Automated Setup
30303131-For first-time setup, run the automated setup script:
3131+For first-time setup, use the automated setup script:
32323333```bash
3434./scripts/nix-dev-setup.sh
3535```
36363737This script will:
3838-- Verify Nix installation
3939-- Set up the development environment
4040-- Install all Python dependencies
3838+- Check if Nix is installed and flakes are enabled
3939+- Set up the Python virtual environment and install dependencies
4140- Start required services (PostgreSQL, Redis, MinIO)
4241- Run database migrations
4343-- Optionally load sample data
4242+- Optionally load sample fixtures
4343+- Provide helpful guidance for next steps
4444+4545+The script includes safety checks and won't run on NixOS systems (which should use NixOS-specific configurations).
4646+4747+## Quick Setup (Manual)
4848+4949+For first-time setup without the script:
5050+5151+```bash
5252+nix develop
5353+setup-dev
5454+start-services
5555+rundev
5656+```
5757+5858+This will:
5959+- Enter the development environment
6060+- Set up Python virtual environment and install dependencies
6161+- Start required services (PostgreSQL, Redis, MinIO)
6262+- Run database migrations and start the unified development server
44634564## Manual Setup
4665···5372```
54735574This will:
5656-- Install all required system packages
7575+- Install all required system packages from Nix store
5776- Set up environment variables
5877- Make development commands available
5978- Show a helpful welcome message
···72917392This starts:
7493- PostgreSQL on port 5432
7575-- Redis on port 6379
7676-- MinIO on port 9000 (console on 9001)
9494+- Redis on port 6379
9595+- MinIO on port 9100 (console on 9001)
77967897### 4. Set Up Database
7998···96115# Terminal 1: Django server
97116runserver
981179999-# Terminal 2: Celery worker
100100-nix develop --command celery
118118+# Terminal 2: Celery worker
119119+celery
101120```
102121103122The Django server will be available at http://localhost:9000
···110129- `start-services` - Start PostgreSQL, Redis, and MinIO
111130- `stop-services` - Stop background services only
112131- `kill-care` - **๐ Stop ALL development processes and services**
132132+- `clean-data` - **๐๏ธ Remove all local service data** (requires confirmation)
113133- `healthcheck` - Check application health
114134115135### Development Server
···152172- `POSTGRES_PASSWORD=postgres`
153173- `POSTGRES_HOST=localhost`
154174- `POSTGRES_DB=care`
175175+- `POSTGRES_PORT=5432`
155176- `DATABASE_URL=postgres://postgres:postgres@localhost:5432/care`
156177157178### Redis
···160181161182### Django
162183- `DJANGO_DEBUG=true`
184184+- `ATTACH_DEBUGGER=false`
163185- `DJANGO_SETTINGS_MODULE=config.settings.local`
164186165187### MinIO/S3
166166-- `BUCKET_ENDPOINT=http://localhost:9100`
188188+- `BUCKET_REGION=ap-south-1`
167189- `BUCKET_KEY=minioadmin`
168190- `BUCKET_SECRET=minioadmin`
191191+- `BUCKET_ENDPOINT=http://localhost:9100`
192192+- `BUCKET_EXTERNAL_ENDPOINT=http://localhost:9100`
193193+- `FILE_UPLOAD_BUCKET=patient-bucket`
194194+- `FACILITY_S3_BUCKET=facility-bucket`
195195+196196+### HCX Configuration (for local testing)
197197+- `HCX_AUTH_BASE_PATH=https://staging-hcx.swasth.app/auth/realms/swasth-health-claim-exchange/protocol/openid-connect/token`
198198+- `HCX_ENCRYPTION_PRIVATE_KEY_URL=https://raw.githubusercontent.com/Swasth-Digital-Health-Foundation/hcx-platform/main/demo-app/server/resources/keys/x509-private-key.pem`
199199+- `HCX_IG_URL=https://ig.hcxprotocol.io/v0.7.1`
200200+- `HCX_PARTICIPANT_CODE=qwertyreboot.gmail@swasth-hcx-staging`
201201+- `HCX_PASSWORD=Opensaber@123`
202202+- `HCX_PROTOCOL_BASE_PATH=http://staging-hcx.swasth.app/api/v0.7`
203203+- `HCX_USERNAME=qwertyreboot@gmail.com`
204204+- `HCX_CERT_URL=https://raw.githubusercontent.com/Swasth-Digital-Health-Foundation/hcx-platform/main/demo-app/server/resources/keys/x509-self-signed-certificate.pem`
169205170206## Service URLs
171207···205241 runserver
206242207243 # Terminal 2: Celery worker
208208- nix develop --command celery
244244+ celery
209245 ```
2102462112473. **Make changes and test**:
···245281# Check if services are running
246282ps aux | grep -E 'postgres|redis|minio'
247283248248-# View service logs
249249-journalctl --user -f # On systemd systems
284284+# View service logs (if using systemd)
285285+journalctl --user -f
250286```
251287252288#### Database Connection Issues
253289```bash
254290# Check PostgreSQL status
255255-pg_ctl status -D ~/.local/share/postgres
291291+pg_ctl status -D .nix-data/postgres
256292257293# Restart PostgreSQL
258294stop-services
···2663021. **"Permission denied" errors**:
267303 ```bash
268304 # Ensure directories are writable
269269- mkdir -p ~/.local/bin ~/.local/share/postgres ~/.local/share/minio
305305+ mkdir -p .nix-data/postgres .nix-data/redis .nix-data/minio
270306 ```
2713072723082. **Services won't start**:
···276312 # Check for processes using ports
277313 lsof -i :5432 # PostgreSQL
278314 lsof -i :6379 # Redis
279279- lsof -i :9000 # MinIO
315315+ lsof -i :9100 # MinIO
280316 ```
2813172823183. **Python dependencies issues**:
···293329 # Wait for PostgreSQL to fully start
294330 sleep 5
295331 # Or check if initialization is complete
296296- pg_ctl status -D ~/.local/share/postgres
332332+ pg_ctl status -D .nix-data/postgres
297333 ```
298334299335### Clean Reset
···304340# Stop all processes and services
305341kill-care
306342307307-# Clean up data directories
308308-rm -rf ~/.local/share/postgres ~/.local/share/minio
343343+# Clean up data directories (will prompt for confirmation)
344344+clean-data
309345310346# Exit and re-enter development shell
311347exit
···317353migrate
318354```
319355356356+## Data Storage
357357+358358+The Nix development environment stores all service data in the project-local `.nix-data` directory:
359359+360360+- **PostgreSQL data**: `.nix-data/postgres/`
361361+- **Redis data**: `.nix-data/redis/`
362362+- **MinIO data**: `.nix-data/minio/`
363363+364364+This directory is automatically added to `.gitignore` and provides isolation from system-wide services.
365365+320366## Differences from Docker Setup
321367322368### Advantages of Nix
···325371- **Reproducible**: Same environment across different machines
326372- **Integrated tooling**: All tools available in single shell
327373- **Easier debugging**: Direct access to processes and files
374374+- **Version pinning**: All tools use specific versions from Nix store
328375329376### Key Differences
330377- Services run directly on host (not in containers)
331331-- Data stored in `~/.local/share/` instead of Docker volumes
378378+- Data stored in `.nix-data/` instead of Docker volumes
332379- Environment variables set in shell instead of env files
333380- All commands available directly (no `docker compose exec`)
381381+- Uses Python 3.13 and PostgreSQL 15 from Nix store
334382335383## Integration with Existing Workflow
336384···340388- Same database schema and migrations
341389- Compatible with existing CI/CD pipelines
342390391391+## Available Tools
392392+393393+The development environment includes these tools from the Nix store:
394394+395395+- **Python 3.13**: Base Python interpreter
396396+- **PostgreSQL 15**: Database server and client tools
397397+- **Redis**: In-memory data structure store
398398+- **MinIO**: S3-compatible object storage
399399+- **Typst**: Modern typesetting system
400400+- **Pre-commit**: Git hook framework
401401+- **GCC & build tools**: For compiling Python packages
402402+343403## Contributing
344404345405When adding new dependencies:
···371431- Default credentials are for development only
372432- MinIO uses development keys (minioadmin/minioadmin)
373433- Database has no password (local development only)
374374-375375-For production deployment, use the Docker setup with proper security configurations.