Basic Django Project using EndoReg-DB
For local development, the application uses .env files for configuration:
-
Initialize the environment:
# Enter the devenv shell devenv shell # Test luxnix compatibility test-luxnix-compatibility # Initialize configuration devenv task run env:init-conf devenv task run env:build
-
Run the development server:
run-dev-server # or manually: python manage.py runserver localhost:8118
When deployed on luxnix-managed machines, configuration is automatic:
- No manual setup required - luxnix provides
local_settings.py - Database credentials are loaded from the luxnix vault system
- Security settings are managed by the deployment system
- Environment detection is automatic
This application is designed to work seamlessly with managed machines running on custom NixOS configurations via the luxnix project. The application automatically detects luxnix environments and adapts its configuration accordingly.
- Automatic Environment Detection: Detects when running in a luxnix managed environment by checking for
local_settings.py - Central Node Support: Automatically configures for central nodes when
CENTRAL_NODE=trueenvironment variable is set - Settings Override: Imports
local_settings.pywhen available, allowing luxnix to override default configurations - Flexible Configuration: All hardcoded values can be overridden via environment variables
When CENTRAL_NODE=true is set, the application operates as a central coordination node with additional capabilities:
- Database Access: Central nodes typically have access to the
endoregDbCentraldatabase instead of local databases - Network Topology: Central nodes are configured to communicate with local nodes across the network
- CORS Settings: Automatically configured to allow cross-origin requests from local nodes
- Additional Endpoints: May have access to coordination and management endpoints not available on local nodes
- Settings Module: Uses
endo_api.settings_centralinstead ofendo_api.settings_prod
- Data Aggregation: Collects and processes data from local nodes
- Coordination: Manages distributed operations across the network
- Authentication: May serve as authentication provider for local nodes
- Backup/Sync: Handles data synchronization and backup operations
CENTRAL_NODE=true
DJANGO_SETTINGS_MODULE=endo_api.settings_central
IS_CENTRAL_NODE=true
CENTRAL_NODES=["s-04"] # List of central node identifiers
LOCAL_NODES=["gs-01", "gs-02", "gc-05", "gc-06", "gc-10"] # Managed local nodesThe application respects the following environment variables for luxnix compatibility:
CENTRAL_NODE: Set to "true" for central node configurationDJANGO_HOST: Server host (default: localhost)DJANGO_PORT: Server port (default: 8118)DJANGO_DEBUG: Enable/disable debug modeDJANGO_SECRET_KEY: Django secret key (auto-generated if not provided)DJANGO_ALLOWED_HOSTS: Comma-separated list of allowed hosts
DATA_DIR/STORAGE_DIR: Data storage directoryCONF_DIR: Configuration directoryCONF_TEMPLATE_DIR: Template configuration directoryWORKING_DIR: Application working directory
DB_CONFIG_FILE: Database configuration file pathDB_PWD_FILE: Database password file path
DJANGO_SETTINGS_MODULE_DEVELOPMENT: Development settings moduleDJANGO_SETTINGS_MODULE_PRODUCTION: Production settings moduleDJANGO_SETTINGS_MODULE_CENTRAL: Central node settings module
BASE_URL: Full base URL for the applicationHTTP_PROTOCOL: Protocol (http/https)LX_MAINTENANCE_PASSWORD_FILE: Maintenance password file path
| Feature | Development | Production | Central Node |
|---|---|---|---|
| Settings Module | settings_dev |
settings_prod |
settings_central |
| Configuration Source | .env file |
local_settings.py |
local_settings.py |
| Database | SQLite (dev) | PostgreSQL | PostgreSQL (Central DB) |
| Debug Mode | Enabled | Disabled | Disabled |
| CORS | Permissive | Restricted | Cross-node enabled |
| Network Access | Local only | Node-specific | Multi-node |
| Secret Management | Local/Manual | Luxnix Vault | Luxnix Vault |
When deployed via luxnix, the following happens automatically:
- Configuration Generation:
local_settings.pyis created with environment-specific settings - Database Setup: PostgreSQL credentials are loaded from the luxnix vault system
- Security Configuration: SSL certificates, CORS settings, and security headers are configured
- Service Management: Systemd service
endo-api-boot.servicemanages the application lifecycle - Environment Detection: Application automatically detects luxnix environment and skips local config generation
# Development
run-dev-server # Start development server
set-dev-settings # Configure development settings
# Production
run-prod-server # Start production server (auto-detects central node)
set-prod-settings # Configure production settings
set-central-settings # Configure central node settings
# Environment Management
env-pipe # Full environment setup pipeline
env-init-conf # Initialize configuration files
env-build # Build .env file from templates
env-export # Export environment variables
# Database Management
ensure-psql # Ensure PostgreSQL is configured
deploy-migrate # Run database migrations
deploy-load-base-db-data # Load initial database data
deploy-collectstatic # Collect static files
# Testing and Diagnostics
test-luxnix-compatibility # Test luxnix integration
gpu-check # Check GPU availability-
Environment Variables Not Loading:
# Check .env file syntax cat .env # Reload environment direnv reload
-
Database Connection Issues:
# Verify database configuration python manage.py check # Run PostgreSQL setup ensure-psql
-
Luxnix Integration Problems:
# Test compatibility test-luxnix-compatibility # Check if local_settings.py exists ls -la local_settings.py
-
Service Issues (Luxnix Deployment):
# Check service logs sudo journalctl -u endo-api-boot.service -f # Restart service sudo systemctl restart endo-api-boot.service
- NixOS with devenv installed
- Git with submodule support
- Access to luxnix repository (for managed deployments)
-
Clone and Initialize:
git clone <repository-url> cd endo-api git submodule init git submodule update --remote --recursive
-
Enter Development Environment:
# This will automatically: # - Set up Python environment with uv # - Install dependencies # - Configure environment variables devenv shell
-
Initialize Configuration:
# Create configuration files and .env devenv task run env:init-conf devenv task run env:build -
Run Database Setup:
# Migrate database python manage.py migrate # Load initial data python manage.py load_base_db_data # Collect static files python manage.py collectstatic --noinput
-
Start Development Server:
run-dev-server # Server will be available at http://localhost:8118
The application includes git submodules for required libraries:
libs/endoreg-db: Core database models and utilitieslibs/lx-anonymizer: Video anonymization tools
To initialize submodules manually:
git submodule init
git submodule update --remote --recursiveNote: The devenv shell automatically handles submodule initialization.
# Nix Vars
varsNix = import ./devenv/vars.nix
varsNix {
dataDir = "data";
confDir = "./conf";
djangoModuleName = "endo_api";
host = "localhost";
port = "8118";
}
# Full Devenv Utils submodule
pkgs = import <nixpkgs> {}
defaultNix = import ./devenv/default.nix
defaultNix {
pkgs = pkgs;
djangoModuleName = "endo_api";
host = "localhost";
port = "8118";
dataDir = "data";
confDir = "./conf";
uvPackage = pkgs.uv;
}
Note: These manual steps are mostly automated in newer versions. This section is kept for reference and troubleshooting.
If you're using a system configured with luxnix (https://github.com/wg-lux/luxnix), you may need to run additional setup steps:
- First rebuild after activating the endo-api service takes time as dependencies are built
- Check logs:
sudo journalctl -xeu endo-api-boot.service(close and re-open for updates) - Common issues:
- May fail initially due to timing issues with .env file creation
- Ensure database password file exists:
/home/endoreg-service-user/endo-api/conf/db_pwd - Run PostgreSQL setup:
ensure-psql(requires sudo privileges)
- Restart service:
sudo systemctl restart endo-api-boot.service
# Ensure PostgreSQL is configured
sudo -u postgres ensure-psql
# Verify database connection
python manage.py check --database default
# Run migrations if needed
python manage.py migrate# Check service status
sudo systemctl status endo-api-boot.service
# View real-time logs
sudo journalctl -u endo-api-boot.service -f
# Restart service
sudo systemctl restart endo-api-boot.service- Secret Keys: Use generated secrets, never commit them to version control
- Debug Mode: Only enable in development environments
- Database: SQLite is used for development, never in production
- CORS: Permissive settings for local development only
- Secret Management: All secrets managed through luxnix vault system
- SSL/TLS: Automatically configured by luxnix deployment
- Database: PostgreSQL with proper authentication and encryption
- CORS: Restricted to known nodes and domains
- File Permissions: Managed by NixOS user/group permissions
- Network Isolation: Central nodes may have access to wider network ranges
- Authentication: Serves as authentication provider for local nodes
- Data Access: Has broader data access privileges across the network
- Monitoring: Enhanced logging and monitoring capabilities
- Follow Python/Django best practices
- Test luxnix compatibility with
test-luxnix-compatibility - Ensure environment variables are properly documented
- Test both development and production configurations
- Update both
devenv.nixand documentation when adding new environment variables - Test compatibility with both manual and luxnix deployments
- Update the compatibility test script when adding new features
- Test in development environment first
- Verify luxnix compatibility
- Test central node configurations if applicable
- Document any new setup requirements