π¦ Nix Flake Integration Guide
Reproducible development environment powered by Nix Flakes
π― Overviewβ
This project provides a fully declarative Nix flake that:
- β Manages all Python dependencies (Poetry, pytest, httpx, etc.)
- β Provides Docker and docker-compose in the environment
- β
Exposes executable apps via
nix run - β Integrates with other project components (Neoland, Phantom, etc.)
- β Works with direnv for automatic environment activation
π Quick Startβ
Prerequisitesβ
# Install Nix with flakes enabled
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
# Or enable flakes manually in ~/.config/nix/nix.conf
experimental-features = nix-command flakes
Instant Development Environmentβ
# Enter development shell
cd /home/kernelcore/arch/integration-tests
nix develop
# Your shell now has:
# β
Python 3.13 with all test dependencies
# β
Poetry and uv package managers
# β
Docker and docker-compose
# β
All testing tools (pytest, curl, jq)
π¦ Available Appsβ
Run tests without entering the dev shell:
# Run full test suite
nix run .#test
# Run quick tests (skip slow/performance tests)
nix run .#quick
# Run chaos engineering tests only
nix run .#chaos
# Check service health
nix run .#health
# Run mock AI agent simulation
nix run .#mock-agent
With Custom Argumentsβ
# Pass arguments to scripts
nix run .#test -- --verbose --no-cleanup
# Run mock agent with custom workload
nix run .#mock-agent -- --workload=nixos_rebuild
π§ Development Workflowβ
Option 1: Nix Develop Shell (Recommended)β
# Enter development environment
nix develop
# Now all tools are available:
pytest test_comprehensive_integration.py -v
poetry run pytest -v
./run_comprehensive_test.sh --quick
# Exit shell
exit
Option 2: Direnv Integration (Auto-activation)β
# Install direnv
nix-env -iA nixpkgs.direnv
# Hook into shell (~/.bashrc or ~/.zshrc)
eval "$(direnv hook bash)" # or zsh
# Allow this directory
cd /home/kernelcore/arch/integration-tests
direnv allow
# Now the environment activates automatically when you cd into this directory!
Option 3: One-off Commandsβ
# Run command in Nix environment without entering shell
nix develop -c pytest test_comprehensive_integration.py -v
nix develop -c poetry install
nix develop -c ./run_comprehensive_test.sh
ποΈ Flake Structureβ
Inputsβ
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
# Component Flakes (GitHub URIs)
neoland = {
url = "github:marcosfpina/neoland";
inputs.nixpkgs.follows = "nixpkgs";
};
phantom = {
url = "github:marcosfpina/phantom";
inputs.nixpkgs.follows = "nixpkgs";
};
neutron = {
url = "github:marcosfpina/neutron";
inputs.nixpkgs.follows = "nixpkgs";
};
cerebro = {
url = "github:marcosfpina/cerebro";
inputs.nixpkgs.follows = "nixpkgs";
};
spectre = {
url = "github:marcosfpina/spectre";
inputs.nixpkgs.follows = "nixpkgs";
};
adr-ledger = {
url = "github:marcosfpina/adr-ledger";
inputs.nixpkgs.follows = "nixpkgs";
};
};
Outputsβ
| Output | Description | Usage |
|---|---|---|
| packages.default | Test environment package | nix build |
| apps.test | Full test suite | nix run .#test |
| apps.quick | Quick tests | nix run .#quick |
| apps.chaos | Chaos tests | nix run .#chaos |
| apps.health | Health check | nix run .#health |
| apps.mock-agent | Mock AI agent | nix run .#mock-agent |
| devShells.default | Development environment | nix develop |
| checks | Automated checks | nix flake check |
π§ͺ Running Testsβ
Full Suiteβ
# With Nix app
nix run .#test
# In dev shell
nix develop
./run_comprehensive_test.sh
Quick Tests (CI/CD)β
nix run .#quick
# Equivalent to:
./run_comprehensive_test.sh --quick
Chaos Engineeringβ
nix run .#chaos
# Equivalent to:
./run_comprehensive_test.sh --chaos-only
Specific Scenariosβ
nix develop
pytest test_comprehensive_integration.py::test_scenario_01_thermal_spike_happy_path -v
pytest -m chaos # All chaos tests
pytest -m performance # All performance tests
π Service Health Monitoringβ
# Check all services
nix run .#health
# Output:
# π₯ Checking Service Health
# ββββββββββββββββββββββββββββββββββββββββββ
# β
Phantom is healthy
# β
NATS is healthy
# β Cerebro is not responding
#
# β οΈ Some services are not healthy
π³ Docker Managementβ
Via Nix Appsβ
# Start services
nix run .#docker-env
# Stop services
nix run .#docker-down
Manual (in dev shell)β
nix develop
# Start services
docker-compose -f docker-compose.test.yml up -d
# Check status
docker-compose -f docker-compose.test.yml ps
# View logs
docker-compose -f docker-compose.test.yml logs phantom
# Stop services
docker-compose -f docker-compose.test.yml down -v
π€ Mock AI Agentβ
Simulate workload patterns from Neoland AI agent:
# Run with default progression (idle β development β compilation β nixos_rebuild)
nix run .#mock-agent
# In dev shell with custom workload
nix develop
cd mocks
python mock_ai_agent.py
# Or directly
nix develop -c python mocks/mock_ai_agent.py
β Quality Checksβ
Flake Validationβ
# Check flake structure
nix flake check
# This runs:
# β
Flake structure validation
# β
Python syntax check
# β
YAML syntax check (docker-compose.test.yml)
Manual Checksβ
nix develop
# Python syntax
python -m py_compile test_comprehensive_integration.py
# Linting
ruff check .
# Formatting
black --check .
# Type checking (if mypy is added)
mypy test_comprehensive_integration.py
π Updating Dependenciesβ
Update Flake Inputsβ
# Update all inputs to latest
nix flake update
# Update specific input
nix flake lock --update-input nixpkgs
nix flake lock --update-input neoland
# Check what would be updated
nix flake update --dry-run
Update Python Dependenciesβ
nix develop
# With Poetry
poetry update
poetry show --outdated
# With pip (fallback)
pip list --outdated
π Integration with Other Componentsβ
Neoland (AI Agent)β
# Neoland is available as an input
nix develop
# Access Neoland package (if built)
ls -la ${neoland}
# Run Neoland from integration tests
neoland client --ml-api-url http://localhost:9000
Using Local Development Versionsβ
For local development, you can override to use local paths:
# Override with local path
nix develop --override-input neoland path:../neoland
# Or for all components
nix develop \
--override-input neoland path:../neoland \
--override-input phantom path:../phantom \
--override-input neutron path:../neutron
Or add to flake.nix temporarily:
inputs = {
# Use local development version
neoland.url = "path:../neoland";
# Or keep GitHub version
# neoland.url = "github:marcosfpina/neoland";
};
π― Tips & Tricksβ
Fast Iterationβ
# Use direnv to auto-activate environment
direnv allow
# Now when you cd into the directory:
cd /home/kernelcore/arch/integration-tests
# Environment activates automatically!
CI/CD Integrationβ
# GitHub Actions
- name: Run integration tests
run: |
nix run .#quick
# GitLab CI
script:
- nix --extra-experimental-features 'nix-command flakes' run .#quick
Offline Developmentβ
# Build all dependencies once
nix build --no-link
# Now you can work offline (dependencies cached)
nix develop --offline
Inspecting the Environmentβ
# See what's in PATH
nix develop -c bash -c 'echo $PATH'
# Check Python version
nix develop -c python --version
# List all available packages
nix develop -c env
π Troubleshootingβ
"experimental-features" Errorβ
# Add to ~/.config/nix/nix.conf
experimental-features = nix-command flakes
# Or use flag
nix --extra-experimental-features 'nix-command flakes' develop
Python Package Not Foundβ
# Rebuild flake
nix flake update
nix develop --rebuild
# Check if package is in nixpkgs
nix search nixpkgs python313Packages.yourpackage
Docker Not Availableβ
# Ensure Docker daemon is running
systemctl status docker # Linux
open -a Docker # macOS
# Test Docker access
nix develop -c docker ps
Direnv Not Workingβ
# Reload direnv
direnv reload
# Debug direnv
direnv status
# Re-allow
direnv allow .
π Benefits of Nix Flakesβ
| Benefit | Description |
|---|---|
| Reproducibility | Same environment on any machine |
| Isolation | No pollution of global environment |
| Declarative | All dependencies in one file |
| Fast | Cached builds, instant activation |
| Composable | Integrate with other flakes |
| Rollback | Switch between versions easily |
π Additional Resourcesβ
- Nix Flakes Manual
- Nixpkgs Python Guide
- Zero to Nix - Learn Nix from scratch
- Nix Pills - Deep dive into Nix