this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Updated flake.nix based on nix standards. (#3311)

* fix db init

* update nix flakes

* more nix ways

* updated docs and scripts

fixx

* fixed whitespace

authored by

Vysakh Premkumar and committed by
GitHub
f6afbc43 b9002370

+298 -154
+1
.gitignore
··· 361 361 care_db.dump 362 362 363 363 typst* 364 + .nix-data
+83 -25
docs/development/nix-development.md
··· 1 1 # Nix Development Environment for CARE 2 2 3 - This document describes how to set up and use the Nix-based development environment for the CARE 3 + This document describes how to set up and use the Nix-based development environment for the CARE project. 4 4 5 5 ## Prerequisites 6 6 ··· 26 26 sudo dnf install direnv # Fedora 27 27 ``` 28 28 29 - ## Quick Setup 29 + ## Automated Setup 30 30 31 - For first-time setup, run the automated setup script: 31 + For first-time setup, use the automated setup script: 32 32 33 33 ```bash 34 34 ./scripts/nix-dev-setup.sh 35 35 ``` 36 36 37 37 This script will: 38 - - Verify Nix installation 39 - - Set up the development environment 40 - - Install all Python dependencies 38 + - Check if Nix is installed and flakes are enabled 39 + - Set up the Python virtual environment and install dependencies 41 40 - Start required services (PostgreSQL, Redis, MinIO) 42 41 - Run database migrations 43 - - Optionally load sample data 42 + - Optionally load sample fixtures 43 + - Provide helpful guidance for next steps 44 + 45 + The script includes safety checks and won't run on NixOS systems (which should use NixOS-specific configurations). 46 + 47 + ## Quick Setup (Manual) 48 + 49 + For first-time setup without the script: 50 + 51 + ```bash 52 + nix develop 53 + setup-dev 54 + start-services 55 + rundev 56 + ``` 57 + 58 + This will: 59 + - Enter the development environment 60 + - Set up Python virtual environment and install dependencies 61 + - Start required services (PostgreSQL, Redis, MinIO) 62 + - Run database migrations and start the unified development server 44 63 45 64 ## Manual Setup 46 65 ··· 53 72 ``` 54 73 55 74 This will: 56 - - Install all required system packages 75 + - Install all required system packages from Nix store 57 76 - Set up environment variables 58 77 - Make development commands available 59 78 - Show a helpful welcome message ··· 72 91 73 92 This starts: 74 93 - PostgreSQL on port 5432 75 - - Redis on port 6379 76 - - MinIO on port 9000 (console on 9001) 94 + - Redis on port 6379 95 + - MinIO on port 9100 (console on 9001) 77 96 78 97 ### 4. Set Up Database 79 98 ··· 96 115 # Terminal 1: Django server 97 116 runserver 98 117 99 - # Terminal 2: Celery worker 100 - nix develop --command celery 118 + # Terminal 2: Celery worker 119 + celery 101 120 ``` 102 121 103 122 The Django server will be available at http://localhost:9000 ··· 110 129 - `start-services` - Start PostgreSQL, Redis, and MinIO 111 130 - `stop-services` - Stop background services only 112 131 - `kill-care` - **๐Ÿ›‘ Stop ALL development processes and services** 132 + - `clean-data` - **๐Ÿ—‘๏ธ Remove all local service data** (requires confirmation) 113 133 - `healthcheck` - Check application health 114 134 115 135 ### Development Server ··· 152 172 - `POSTGRES_PASSWORD=postgres` 153 173 - `POSTGRES_HOST=localhost` 154 174 - `POSTGRES_DB=care` 175 + - `POSTGRES_PORT=5432` 155 176 - `DATABASE_URL=postgres://postgres:postgres@localhost:5432/care` 156 177 157 178 ### Redis ··· 160 181 161 182 ### Django 162 183 - `DJANGO_DEBUG=true` 184 + - `ATTACH_DEBUGGER=false` 163 185 - `DJANGO_SETTINGS_MODULE=config.settings.local` 164 186 165 187 ### MinIO/S3 166 - - `BUCKET_ENDPOINT=http://localhost:9100` 188 + - `BUCKET_REGION=ap-south-1` 167 189 - `BUCKET_KEY=minioadmin` 168 190 - `BUCKET_SECRET=minioadmin` 191 + - `BUCKET_ENDPOINT=http://localhost:9100` 192 + - `BUCKET_EXTERNAL_ENDPOINT=http://localhost:9100` 193 + - `FILE_UPLOAD_BUCKET=patient-bucket` 194 + - `FACILITY_S3_BUCKET=facility-bucket` 195 + 196 + ### HCX Configuration (for local testing) 197 + - `HCX_AUTH_BASE_PATH=https://staging-hcx.swasth.app/auth/realms/swasth-health-claim-exchange/protocol/openid-connect/token` 198 + - `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` 199 + - `HCX_IG_URL=https://ig.hcxprotocol.io/v0.7.1` 200 + - `HCX_PARTICIPANT_CODE=qwertyreboot.gmail@swasth-hcx-staging` 201 + - `HCX_PASSWORD=Opensaber@123` 202 + - `HCX_PROTOCOL_BASE_PATH=http://staging-hcx.swasth.app/api/v0.7` 203 + - `HCX_USERNAME=qwertyreboot@gmail.com` 204 + - `HCX_CERT_URL=https://raw.githubusercontent.com/Swasth-Digital-Health-Foundation/hcx-platform/main/demo-app/server/resources/keys/x509-self-signed-certificate.pem` 169 205 170 206 ## Service URLs 171 207 ··· 205 241 runserver 206 242 207 243 # Terminal 2: Celery worker 208 - nix develop --command celery 244 + celery 209 245 ``` 210 246 211 247 3. **Make changes and test**: ··· 245 281 # Check if services are running 246 282 ps aux | grep -E 'postgres|redis|minio' 247 283 248 - # View service logs 249 - journalctl --user -f # On systemd systems 284 + # View service logs (if using systemd) 285 + journalctl --user -f 250 286 ``` 251 287 252 288 #### Database Connection Issues 253 289 ```bash 254 290 # Check PostgreSQL status 255 - pg_ctl status -D ~/.local/share/postgres 291 + pg_ctl status -D .nix-data/postgres 256 292 257 293 # Restart PostgreSQL 258 294 stop-services ··· 266 302 1. **"Permission denied" errors**: 267 303 ```bash 268 304 # Ensure directories are writable 269 - mkdir -p ~/.local/bin ~/.local/share/postgres ~/.local/share/minio 305 + mkdir -p .nix-data/postgres .nix-data/redis .nix-data/minio 270 306 ``` 271 307 272 308 2. **Services won't start**: ··· 276 312 # Check for processes using ports 277 313 lsof -i :5432 # PostgreSQL 278 314 lsof -i :6379 # Redis 279 - lsof -i :9000 # MinIO 315 + lsof -i :9100 # MinIO 280 316 ``` 281 317 282 318 3. **Python dependencies issues**: ··· 293 329 # Wait for PostgreSQL to fully start 294 330 sleep 5 295 331 # Or check if initialization is complete 296 - pg_ctl status -D ~/.local/share/postgres 332 + pg_ctl status -D .nix-data/postgres 297 333 ``` 298 334 299 335 ### Clean Reset ··· 304 340 # Stop all processes and services 305 341 kill-care 306 342 307 - # Clean up data directories 308 - rm -rf ~/.local/share/postgres ~/.local/share/minio 343 + # Clean up data directories (will prompt for confirmation) 344 + clean-data 309 345 310 346 # Exit and re-enter development shell 311 347 exit ··· 317 353 migrate 318 354 ``` 319 355 356 + ## Data Storage 357 + 358 + The Nix development environment stores all service data in the project-local `.nix-data` directory: 359 + 360 + - **PostgreSQL data**: `.nix-data/postgres/` 361 + - **Redis data**: `.nix-data/redis/` 362 + - **MinIO data**: `.nix-data/minio/` 363 + 364 + This directory is automatically added to `.gitignore` and provides isolation from system-wide services. 365 + 320 366 ## Differences from Docker Setup 321 367 322 368 ### Advantages of Nix ··· 325 371 - **Reproducible**: Same environment across different machines 326 372 - **Integrated tooling**: All tools available in single shell 327 373 - **Easier debugging**: Direct access to processes and files 374 + - **Version pinning**: All tools use specific versions from Nix store 328 375 329 376 ### Key Differences 330 377 - Services run directly on host (not in containers) 331 - - Data stored in `~/.local/share/` instead of Docker volumes 378 + - Data stored in `.nix-data/` instead of Docker volumes 332 379 - Environment variables set in shell instead of env files 333 380 - All commands available directly (no `docker compose exec`) 381 + - Uses Python 3.13 and PostgreSQL 15 from Nix store 334 382 335 383 ## Integration with Existing Workflow 336 384 ··· 340 388 - Same database schema and migrations 341 389 - Compatible with existing CI/CD pipelines 342 390 391 + ## Available Tools 392 + 393 + The development environment includes these tools from the Nix store: 394 + 395 + - **Python 3.13**: Base Python interpreter 396 + - **PostgreSQL 15**: Database server and client tools 397 + - **Redis**: In-memory data structure store 398 + - **MinIO**: S3-compatible object storage 399 + - **Typst**: Modern typesetting system 400 + - **Pre-commit**: Git hook framework 401 + - **GCC & build tools**: For compiling Python packages 402 + 343 403 ## Contributing 344 404 345 405 When adding new dependencies: ··· 371 431 - Default credentials are for development only 372 432 - MinIO uses development keys (minioadmin/minioadmin) 373 433 - Database has no password (local development only) 374 - 375 - For production deployment, use the Docker setup with proper security configurations.
+3 -3
flake.lock
··· 20 20 }, 21 21 "nixpkgs": { 22 22 "locked": { 23 - "lastModified": 1755027561, 24 - "narHash": "sha256-IVft239Bc8p8Dtvf7UAACMG5P3ZV+3/aO28gXpGtMXI=", 23 + "lastModified": 1760878510, 24 + "narHash": "sha256-K5Osef2qexezUfs0alLvZ7nQFTGS9DL2oTVsIXsqLgs=", 25 25 "owner": "NixOS", 26 26 "repo": "nixpkgs", 27 - "rev": "005433b926e16227259a1843015b5b2b7f7d1fc3", 27 + "rev": "5e2a59a5b1a82f89f2c7e598302a9cacebb72a67", 28 28 "type": "github" 29 29 }, 30 30 "original": {
+188 -123
flake.nix
··· 1 1 { 2 - description = "CARE - Care is a Digital Public Good enabling TeleICU & Decentralised Administration of Healthcare Capacity across States."; 2 + description = "CARE - Care is a Digital Public Good enabling TeleICU & Decentralised Administration of Healthcare Capacity across States."; 3 3 4 4 inputs = { 5 5 nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; 6 6 flake-utils.url = "github:numtide/flake-utils"; 7 7 }; 8 8 9 - outputs = { self, nixpkgs, flake-utils }: 10 - flake-utils.lib.eachDefaultSystem (system: 9 + outputs = 10 + { nixpkgs, flake-utils, ... }: 11 + flake-utils.lib.eachDefaultSystem ( 12 + system: 11 13 let 12 14 pkgs = nixpkgs.legacyPackages.${system}; 13 15 python = pkgs.python313; 14 16 15 17 # Create a Python environment with pip-installable packages 16 - pythonEnv = python.withPackages (ps: with ps; [ 17 - pip 18 - setuptools 19 - wheel 20 - virtualenv 21 - ]); 18 + pythonEnv = python.withPackages ( 19 + ps: with ps; [ 20 + pip 21 + setuptools 22 + wheel 23 + virtualenv 24 + ] 25 + ); 26 + 27 + # Project-local data directory (still impure but isolated) 28 + projectDataDir = ".nix-data"; 29 + postgresDir = "${projectDataDir}/postgres"; 30 + redisDir = "${projectDataDir}/redis"; 31 + minioDir = "${projectDataDir}/minio"; 22 32 23 33 # Environment variables for development 24 34 envVars = { ··· 58 68 HCX_USERNAME = "qwertyreboot@gmail.com"; 59 69 HCX_CERT_URL = "https://raw.githubusercontent.com/Swasth-Digital-Health-Foundation/hcx-platform/main/demo-app/server/resources/keys/x509-self-signed-certificate.pem"; 60 70 61 - # Typst 62 - TYPST_VERSION = "0.12.0"; 63 - 64 - # PostgreSQL configuration for compilation 71 + # PostgreSQL configuration for compilation (using Nix store paths) 65 72 PG_CONFIG = "${pkgs.postgresql_15}/bin/pg_config"; 66 73 LDFLAGS = "-L${pkgs.postgresql_15}/lib"; 67 74 CPPFLAGS = "-I${pkgs.postgresql_15}/include"; 68 75 }; 69 76 70 77 # Helper scripts 71 - makeScript = name: text: pkgs.writeShellScriptBin name '' 72 - set -euo pipefail 73 - ${text} 74 - ''; 75 - 76 - # Install Typst 77 - typstInstaller = makeScript "install-typst" '' 78 - if ! command -v typst >/dev/null 2>&1; then 79 - echo "Installing Typst v${envVars.TYPST_VERSION}..." 80 - TYPST_INSTALL_DIR="$HOME/.local/bin" TYPST_VERSION="${envVars.TYPST_VERSION}" ./scripts/install_typst.sh 81 - export PATH="$HOME/.local/bin:$PATH" 82 - else 83 - echo "Typst already installed" 84 - fi 85 - ''; 78 + makeScript = 79 + name: text: 80 + pkgs.writeShellScriptBin name '' 81 + set -euo pipefail 82 + ${text} 83 + ''; 86 84 87 - # Service management scripts 85 + # Service management scripts using Nix store binaries 88 86 startServices = makeScript "start-services" '' 89 - echo "Starting PostgreSQL..." 90 - if ! pgrep -x "postgres" > /dev/null; then 91 - mkdir -p ~/.local/share/postgres/sockets 92 - initdb -D ~/.local/share/postgres -U postgres --auth=trust || true 87 + echo "๐Ÿ“‚ Using project data directory: ${projectDataDir}" 88 + mkdir -p ${postgresDir} ${redisDir} ${minioDir} 93 89 94 - # Configure socket directory 95 - echo "unix_socket_directories = '$HOME/.local/share/postgres/sockets'" >> ~/.local/share/postgres/postgresql.conf 90 + echo "๐Ÿ˜ Starting PostgreSQL..." 91 + if ! ${pkgs.procps}/bin/pgrep -x "postgres" > /dev/null; then 92 + # Check if database directory is corrupted or not properly initialized 93 + if [ -d ${postgresDir} ] && [ ! -f ${postgresDir}/PG_VERSION ]; then 94 + echo "Database directory exists but appears corrupted. Cleaning up..." 95 + rm -rf ${postgresDir} 96 + mkdir -p ${postgresDir} 97 + fi 96 98 97 - pg_ctl -D ~/.local/share/postgres -l ~/.local/share/postgres/logfile start 99 + # Initialize database if it doesn't exist 100 + if [ ! -f ${postgresDir}/PG_VERSION ]; then 101 + echo "Initializing new PostgreSQL database..." 102 + ${pkgs.postgresql_15}/bin/initdb -D ${postgresDir} -U postgres --auth=trust 103 + # Configure to use Unix socket in project directory 104 + echo "unix_socket_directories = '$(pwd)/${postgresDir}'" >> ${postgresDir}/postgresql.conf 105 + echo "port = 5432" >> ${postgresDir}/postgresql.conf 106 + fi 107 + 108 + ${pkgs.postgresql_15}/bin/pg_ctl -D ${postgresDir} -l ${postgresDir}/logfile start 98 109 sleep 2 99 - createdb -U postgres care || echo "Database 'care' already exists" 110 + ${pkgs.postgresql_15}/bin/createdb -h localhost -U postgres care || echo "Database 'care' already exists" 100 111 else 101 112 echo "PostgreSQL already running" 102 113 fi 103 114 104 - echo "Starting Redis..." 105 - if ! pgrep -x "redis-server" > /dev/null; then 106 - redis-server --daemonize yes --bind 127.0.0.1 --port 6379 115 + echo "๐Ÿ“ฎ Starting Redis..." 116 + if ! ${pkgs.procps}/bin/pgrep -x "redis-server" > /dev/null; then 117 + # Ensure Redis directory exists 118 + mkdir -p ${redisDir} 119 + 120 + ${pkgs.redis}/bin/redis-server \ 121 + --daemonize yes \ 122 + --bind 127.0.0.1 \ 123 + --port 6379 \ 124 + --dir ${redisDir} \ 125 + --dbfilename dump.rdb 107 126 else 108 127 echo "Redis already running" 109 128 fi 110 129 111 - echo "Starting MinIO..." 112 - if ! pgrep -x "minio" > /dev/null; then 113 - mkdir -p ~/.local/share/minio 114 - MINIO_ROOT_USER="${envVars.BUCKET_KEY}" MINIO_ROOT_PASSWORD="${envVars.BUCKET_SECRET}" \ 115 - minio server ~/.local/share/minio --address ":9100" --console-address ":9001" & 130 + echo "๐Ÿ—„๏ธ Starting MinIO..." 131 + if ! ${pkgs.procps}/bin/pgrep -x "minio" > /dev/null; then 132 + MINIO_ROOT_USER="${envVars.BUCKET_KEY}" \ 133 + MINIO_ROOT_PASSWORD="${envVars.BUCKET_SECRET}" \ 134 + ${pkgs.minio}/bin/minio server ${minioDir} \ 135 + --address ":9100" \ 136 + --console-address ":9001" & 116 137 sleep 3 117 138 else 118 139 echo "MinIO already running" 119 140 fi 120 141 121 - echo "All services started!" 142 + echo "โœ… All services started!" 143 + echo " PostgreSQL data: ${postgresDir}" 144 + echo " Redis data: ${redisDir}" 145 + echo " MinIO data: ${minioDir}" 122 146 ''; 123 147 124 148 stopServices = makeScript "stop-services" '' 125 - echo "Stopping services..." 126 - pkill postgres || true 127 - pkill redis-server || true 128 - pkill minio || true 129 - echo "Services stopped" 149 + PROJECT_DATA_DIR="${projectDataDir}" 150 + POSTGRES_DIR="$PROJECT_DATA_DIR/postgres" 151 + 152 + echo "๐Ÿ›‘ Stopping services..." 153 + 154 + echo "Stopping PostgreSQL..." 155 + if [ -f "$POSTGRES_DIR/postmaster.pid" ]; then 156 + ${pkgs.postgresql_15}/bin/pg_ctl -D "$POSTGRES_DIR" stop 157 + else 158 + ${pkgs.procps}/bin/pkill postgres || true 159 + fi 160 + 161 + echo "Stopping Redis..." 162 + ${pkgs.procps}/bin/pkill redis-server || true 163 + 164 + echo "Stopping MinIO..." 165 + ${pkgs.procps}/bin/pkill minio || true 166 + 167 + echo "โœ… Services stopped" 130 168 ''; 131 169 132 170 # Kill all development processes ··· 135 173 136 174 # Stop Django development server 137 175 echo "Stopping Django development server..." 138 - pkill -f "runserver_plus" || true 139 - pkill -f "manage.py runserver" || true 140 - pkill -f "python.*manage.py" || true 176 + ${pkgs.procps}/bin/pkill -f "runserver_plus" || true 177 + ${pkgs.procps}/bin/pkill -f "manage.py runserver" || true 178 + ${pkgs.procps}/bin/pkill -f "python.*manage.py" || true 141 179 142 180 # Stop Celery workers and beat 143 181 echo "Stopping Celery workers..." 144 - pkill -f "celery.*worker" || true 145 - pkill -f "celery.*beat" || true 146 - pkill -f "watchmedo.*celery" || true 182 + ${pkgs.procps}/bin/pkill -f "celery.*worker" || true 183 + ${pkgs.procps}/bin/pkill -f "celery.*beat" || true 184 + ${pkgs.procps}/bin/pkill -f "watchmedo.*celery" || true 147 185 148 186 # Stop debugpy if running 149 187 echo "Stopping debugger..." 150 - pkill -f "debugpy" || true 188 + ${pkgs.procps}/bin/pkill -f "debugpy" || true 151 189 152 190 # Stop background services 153 191 echo "Stopping background services..." 154 - pkill postgres || true 155 - pkill redis-server || true 156 - pkill minio || true 192 + ${stopServices}/bin/stop-services 157 193 158 194 # Clean up any remaining Python processes that might be related 159 195 echo "Cleaning up remaining processes..." 160 - pkill -f "python.*config.celery_app" || true 196 + ${pkgs.procps}/bin/pkill -f "python.*config.celery_app" || true 161 197 162 198 # Wait a moment for processes to terminate 163 199 sleep 2 164 200 165 201 # Force kill any stubborn processes 166 202 echo "Force killing stubborn processes..." 167 - pkill -9 -f "runserver_plus" 2>/dev/null || true 168 - pkill -9 -f "celery.*worker" 2>/dev/null || true 169 - pkill -9 -f "celery.*beat" 2>/dev/null || true 203 + ${pkgs.procps}/bin/pkill -9 -f "runserver_plus" 2>/dev/null || true 204 + ${pkgs.procps}/bin/pkill -9 -f "celery.*worker" 2>/dev/null || true 205 + ${pkgs.procps}/bin/pkill -9 -f "celery.*beat" 2>/dev/null || true 170 206 171 207 echo "โœ… All development processes stopped" 172 208 echo "" ··· 175 211 echo " rundev # Start unified development environment" 176 212 ''; 177 213 214 + # Clean project data (useful for fresh start) 215 + cleanData = makeScript "clean-data" '' 216 + PROJECT_DATA_DIR="${projectDataDir}" 217 + 218 + echo "โš ๏ธ This will delete all local service data (PostgreSQL, Redis, MinIO)" 219 + read -p "Are you sure? (y/N) " -n 1 -r 220 + echo 221 + if [[ $REPLY =~ ^[Yy]$ ]]; then 222 + ${stopServices}/bin/stop-services 223 + echo "๐Ÿ—‘๏ธ Removing $PROJECT_DATA_DIR..." 224 + rm -rf "$PROJECT_DATA_DIR" 225 + echo "โœ… Project data cleaned" 226 + else 227 + echo "Cancelled" 228 + fi 229 + ''; 230 + 178 231 # Development setup 179 232 setupDev = makeScript "setup-dev" '' 180 - echo "Setting up development environment..." 233 + echo "๐Ÿ—๏ธ Setting up development environment..." 181 234 182 235 # Install Python dependencies 183 236 if [ ! -d ".venv" ]; then 184 237 echo "Creating virtual environment..." 185 - python -m venv .venv 238 + ${python}/bin/python -m venv .venv 186 239 fi 187 240 188 241 source .venv/bin/activate ··· 194 247 # Install plugins 195 248 python install_plugins.py 196 249 197 - # Install Typst 198 - ${typstInstaller}/bin/install-typst 199 - 200 - echo "Development environment setup complete!" 250 + echo "โœ… Development environment setup complete!" 251 + echo "" 252 + echo "Note: Typst ${pkgs.typst.version} and ruff ${pkgs.ruff.version} are available from Nix store" 201 253 ''; 202 254 203 255 # Django management commands ··· 228 280 229 281 echo "๐Ÿš€ Starting unified Care development environment..." 230 282 231 - # Wait for services 232 - ./scripts/wait_for_db.sh 233 - ./scripts/wait_for_redis.sh 283 + # Wait for services (using Nix bash) 284 + ${pkgs.bash}/bin/bash ./scripts/wait_for_db.sh 285 + ${pkgs.bash}/bin/bash ./scripts/wait_for_redis.sh 234 286 235 - # Run migrations and setup (from celery scripts) 287 + # Run migrations and setup 236 288 echo "๐Ÿ“Š Running database migrations..." 237 289 python manage.py migrate --noinput 238 290 python manage.py compilemessages -v 0 239 291 python manage.py sync_permissions_roles 240 292 python manage.py sync_valueset 241 293 242 - # Collect static files (from start script) 294 + # Collect static files 243 295 echo "๐Ÿ“ฆ Collecting static files..." 244 296 python manage.py collectstatic --noinput 245 297 ··· 277 329 # Individual development server (API only) 278 330 runServer = makeScript "runserver" '' 279 331 source .venv/bin/activate 280 - ./scripts/wait_for_db.sh 281 - ./scripts/wait_for_redis.sh 332 + ${pkgs.bash}/bin/bash ./scripts/wait_for_db.sh 333 + ${pkgs.bash}/bin/bash ./scripts/wait_for_redis.sh 282 334 283 335 echo "๐Ÿ“Š Running migrations..." 284 336 python manage.py migrate --noinput ··· 301 353 # Individual Celery worker 302 354 runCelery = makeScript "celery" '' 303 355 source .venv/bin/activate 304 - ./scripts/wait_for_db.sh 305 - ./scripts/wait_for_redis.sh 356 + ${pkgs.bash}/bin/bash ./scripts/wait_for_db.sh 357 + ${pkgs.bash}/bin/bash ./scripts/wait_for_redis.sh 306 358 307 359 echo "๐Ÿ“Š Running migrations..." 308 360 python manage.py migrate --noinput ··· 334 386 coverage report 335 387 ''; 336 388 337 - # Code quality 389 + # Code quality (using Nix-provided ruff for NixOS compatibility) 338 390 ruffCheck = makeScript "ruff" '' 339 - source .venv/bin/activate 340 - ruff check --fix $(git diff --name-only --staged | grep -E '\.py$|/pyproject\.toml$' || echo ".") 391 + ${pkgs.ruff}/bin/ruff check --fix $(git diff --name-only --staged | grep -E '\.py$|/pyproject\.toml$' || echo ".") 341 392 ''; 342 393 343 394 ruffAll = makeScript "ruff-all" '' 344 - source .venv/bin/activate 345 - ruff check . 395 + ${pkgs.ruff}/bin/ruff check . 346 396 ''; 347 397 348 398 ruffFix = makeScript "ruff-fix-all" '' 349 - source .venv/bin/activate 350 - ruff check --fix . 399 + ${pkgs.ruff}/bin/ruff check --fix . 351 400 ''; 352 401 353 - # Database backup/restore 402 + # Database backup/restore using Nix store binaries 354 403 dumpDb = makeScript "dump-db" '' 355 - pg_dump -U postgres -Fc care > care_db.dump 356 - echo "Database dumped to care_db.dump" 404 + ${pkgs.postgresql_15}/bin/pg_dump -h localhost -U postgres -Fc care > care_db.dump 405 + echo "โœ… Database dumped to care_db.dump" 357 406 ''; 358 407 359 408 loadDb = makeScript "load-db" '' 360 409 if [ -f "care_db.dump" ]; then 361 - pg_restore -U postgres --clean --if-exists -d care care_db.dump 362 - echo "Database restored from care_db.dump" 410 + ${pkgs.postgresql_15}/bin/pg_restore -h localhost -U postgres --clean --if-exists -d care care_db.dump 411 + echo "โœ… Database restored from care_db.dump" 363 412 else 364 - echo "care_db.dump not found" 413 + echo "โŒ care_db.dump not found" 365 414 exit 1 366 415 fi 367 416 ''; 368 417 369 418 resetDb = makeScript "reset-db" '' 370 - dropdb -U postgres care -f || true 371 - createdb -U postgres care 372 - echo "Database reset" 419 + ${pkgs.postgresql_15}/bin/dropdb -h localhost -U postgres care -f || true 420 + ${pkgs.postgresql_15}/bin/createdb -h localhost -U postgres care 421 + echo "โœ… Database reset" 373 422 ''; 374 423 375 424 # Health check 376 425 healthCheck = makeScript "healthcheck" '' 377 426 source .venv/bin/activate 378 - ./scripts/healthcheck.sh 427 + ${pkgs.bash}/bin/bash ./scripts/healthcheck.sh 379 428 ''; 380 429 381 430 in ··· 385 434 # Python and package management 386 435 pythonEnv 387 436 388 - # Databases and services 389 - postgresql_15 # PostgreSQL server and client 390 - libpq # PostgreSQL client library 391 - libpq.pg_config # pg_config tool for psycopg compilation 437 + # Databases and services (from Nix store) 438 + postgresql_15 439 + libpq 392 440 redis 393 441 minio 442 + typst # Typst directly from nixpkgs 443 + ruff # Ruff from Nix for NixOS compatibility 394 444 395 445 # System dependencies for building Python packages 396 446 pkg-config ··· 402 452 wget 403 453 git 404 454 455 + # Process management 456 + procps 457 + 405 458 # Development tools 406 459 pre-commit 407 460 ··· 414 467 startServices 415 468 stopServices 416 469 killAll 470 + cleanData 417 471 djangoManage 418 472 migrateDb 419 473 makeMigrations ··· 431 485 loadDb 432 486 resetDb 433 487 healthCheck 434 - typstInstaller 435 488 ]; 436 489 437 490 shellHook = '' 438 - # Add local binaries to PATH (Nix automatically adds buildInputs to PATH) 439 - export PATH="$HOME/.local/bin:$PATH" 491 + ${builtins.concatStringsSep "\n" ( 492 + pkgs.lib.mapAttrsToList (name: value: "export ${name}='${value}'") envVars 493 + )} 440 494 441 - ${builtins.concatStringsSep "\n" (pkgs.lib.mapAttrsToList (name: value: "export ${name}='${value}'") envVars)} 495 + # Create project data directory 496 + mkdir -p ${projectDataDir} 442 497 443 - # Create necessary directories 444 - mkdir -p ~/.local/bin ~/.local/share/postgres ~/.local/share/postgres/sockets ~/.local/share/minio 498 + # Add .nix-data to .gitignore if not already there 499 + if [ -f .gitignore ] && ! grep -q "^\.nix-data$" .gitignore; then 500 + echo ".nix-data" >> .gitignore 501 + fi 445 502 446 503 echo "๐Ÿฅ Welcome to Care development environment!" 447 504 echo "" 505 + echo "๐Ÿ“ฆ Using reproducible Nix store binaries:" 506 + echo " PostgreSQL: ${pkgs.postgresql_15.version}" 507 + echo " Redis: ${pkgs.redis.version}" 508 + echo " MinIO: ${pkgs.minio.version}" 509 + echo " Typst: ${pkgs.typst.version}" 510 + echo " Ruff: ${pkgs.ruff.version}" 511 + echo "" 448 512 echo "Available commands:" 449 513 echo " setup-dev - Set up the development environment" 450 514 echo " start-services - Start PostgreSQL, Redis, and MinIO" 451 515 echo " stop-services - Stop background services only" 452 516 echo " kill-care - ๐Ÿ›‘ Stop ALL development processes and services" 517 + echo " clean-data - ๐Ÿ—‘๏ธ Remove all local service data" 453 518 echo " rundev - ๐Ÿš€ Start both API server and Celery worker (RECOMMENDED)" 454 519 echo " runserver - Start Django development server only" 455 520 echo " celery - Start Celery worker with beat only" ··· 469 534 echo " healthcheck - Check application health" 470 535 echo "" 471 536 echo "๐Ÿš€ Quick Start (Recommended):" 472 - echo " 1. Run 'setup-dev' to install dependencies" 537 + echo " 1. Run 'setup-dev' to install Python dependencies" 473 538 echo " 2. Run 'start-services' to start required services" 474 539 echo " 3. Run 'rundev' to start both API server and Celery worker" 475 540 echo "" 476 - echo "๐Ÿ“‹ Manual Setup:" 477 - echo " 1. Run 'setup-dev' to install dependencies" 478 - echo " 2. Run 'start-services' to start required services" 479 - echo " 3. Run 'migrate' to set up the database" 480 - echo " 4. Run 'runserver' and 'celery' in separate terminals" 481 - echo "" 482 541 echo "The Django server will be available at http://localhost:9000" 483 542 echo "MinIO console will be available at http://localhost:9001" 484 543 echo "" ··· 491 550 echo "โš ๏ธ Run 'setup-dev' to create virtual environment and install dependencies" 492 551 fi 493 552 494 - # Verify pg_config is available 495 - if command -v pg_config >/dev/null 2>&1; then 496 - echo "โœ… PostgreSQL development tools available" 497 - else 498 - echo "โŒ PostgreSQL development tools not found in PATH" 499 - fi 553 + # Verify tools are available 554 + echo "โœ… PostgreSQL development tools available (${pkgs.postgresql_15.version})" 555 + echo "โœ… Typst available (${pkgs.typst.version})" 556 + echo "โœ… Ruff available (${pkgs.ruff.version})" 500 557 ''; 501 558 }; 502 559 503 - # Additional outputs for flexibility 560 + # Package for CI/CD or other use cases 504 561 packages.default = pkgs.writeShellApplication { 505 562 name = "care-dev"; 506 - runtimeInputs = [ pythonEnv ]; 563 + runtimeInputs = [ 564 + pythonEnv 565 + pkgs.postgresql_15 566 + pkgs.redis 567 + pkgs.minio 568 + pkgs.typst 569 + pkgs.ruff 570 + ]; 507 571 text = '' 508 572 echo "Care development environment package" 509 573 echo "Use 'nix develop' to enter the development shell" 510 574 ''; 511 575 }; 512 - }); 576 + } 577 + ); 513 578 }
+23 -3
scripts/nix-dev-setup.sh
··· 10 10 echo "๐Ÿฅ Care Development Environment Setup" 11 11 echo "======================================" 12 12 13 + # Check if running on NixOS 14 + if [[ -f /etc/NIXOS || -f /etc/nixos/configuration.nix ]]; then 15 + echo "โŒ This script is not intended to run on NixOS." 16 + echo " Please use the NixOS-specific configuration instead." 17 + exit 1 18 + fi 19 + 13 20 # Check if Nix is installed 14 21 if ! command -v nix >/dev/null 2>&1; then 15 22 echo "โŒ Nix is not installed. Please install Nix first:" ··· 74 81 echo '' 75 82 echo 'Available services:' 76 83 echo ' - Django server: http://localhost:9000' 77 - echo ' - MinIO console: http://localhost:9001 (admin/minioadmin)' 84 + echo ' - MinIO console: http://localhost:9001 (minioadmin/minioadmin)' 85 + echo ' - MinIO API: http://localhost:9100' 78 86 echo ' - PostgreSQL: localhost:5432 (postgres/postgres)' 79 87 echo ' - Redis: localhost:6379' 80 88 echo '' ··· 83 91 echo ' - runserver Start Django development server only' 84 92 echo ' - celery Start Celery worker and beat only' 85 93 echo ' - manage <cmd> Run Django management commands' 86 - echo ' - test Run tests' 87 - echo ' - ruff-all Check code style' 94 + echo ' - test Run tests with keepdb' 95 + echo ' - test-no-keep Run tests without keepdb' 96 + echo ' - test-coverage Run tests with coverage report' 97 + echo ' - ruff Check and fix staged files' 98 + echo ' - ruff-all Check all files' 99 + echo ' - ruff-fix-all Fix all files' 88 100 echo ' - kill-care ๐Ÿ›‘ Stop ALL development processes and services' 89 101 echo ' - stop-services Stop background services only' 102 + echo ' - clean-data ๐Ÿ—‘๏ธ Remove all local service data' 103 + echo ' - dump-db Backup database' 104 + echo ' - load-db Restore database' 105 + echo ' - reset-db Reset database' 106 + echo ' - healthcheck Check application health' 90 107 echo '' 91 108 " 92 109 ··· 96 113 echo "To start developing:" 97 114 echo " nix develop" 98 115 echo "" 116 + echo "๐Ÿ“‚ Project data is stored in .nix-data/ (git-ignored)" 117 + echo "" 99 118 echo "If you encounter any issues:" 100 119 echo " - Check that all services are running: ps aux | grep -E 'postgres|redis|minio'" 101 120 echo " - Restart services: stop-services && start-services" 121 + echo " - Clean all data for fresh start: clean-data" 102 122 echo "" 103 123 echo "Happy Developing! ๐Ÿš€"