Why tests-flake.nix Was Necessary
The tests-flake.nix file was created to bridge the gap between Home Manager module context and flake checks context. Here's why:
- Context Mismatch: The validated scripts tests are defined within Home Manager modules (bash.nix), which have access to special arguments like mkValidatedScript, mkBashScript, etc. These are provided by the Home Manager module system.
- Flake Checks Requirements: Flake checks (flake-modules/tests.nix) operate in a different context - they need plain derivations that can be built by Nix, without Home Manager's special module system.
- Bridge Solution: tests-flake.nix acts as an adapter that: - Imports the bash scripts module with minimal dependencies - Provides the required arguments (mkValidatedScript, etc.) that would normally come from Home Manager - Exports the tests as plain derivations that flake checks can consume
Test Infrastructure in Your Repository
Your nixcfg repo has a comprehensive multi-layered testing architecture:
- Test Types
- Configuration Evaluation Tests (eval-*): Verify NixOS configurations can be evaluated without errors
- Module Integration Tests: Check that custom modules integrate correctly
- Service Configuration Tests: Ensure services like SSH are properly configured
- Build Dry-Run Tests: Verify configurations can be built (without actually building)
- Script Validation Tests: Test individual bash/python scripts for syntax and behavior
- VM-Based Integration Tests: Full system tests using NixOS test framework
- Test Locations
tests/ # Standalone test modules ├── ssh-auth.nix # SSH authentication tests ├── sops-nix.nix # SOPS encryption tests ├── sops-simple.nix # Simple SOPS roundtrip test └── integration/ # integration test fixtures └── test-secrets/ # SOPS fixtures (VM tests live in modules/flake-parts/vm-tests.nix)
flake-modules/ └── tests.nix # Main test orchestration
home/modules/validated-scripts/ ├── tests.nix # Test utilities for scripts ├── tests-flake.nix # Bridge for flake consumption └── bash.nix # Contains actual script tests
Nix Best Practices for Running Tests
Method 1: Flake Checks (Recommended)
nix flake check
nix flake check --keep-going
nix build .#checks.x86_64-linux.eval-thinky-nixos
nix build .#checks.x86_64-linux.test-name --show-trace
Pros:
- Native Nix integration
- Automatic caching
- Parallel execution
- CI/CD friendly
Method 2: Test Applications
nix run .#test-all
nix run .#test-integration
nix run .#snapshot
nix run .#regression-test
Pros:
- Better formatted output
- Interactive feedback
- Grouped test execution
Method 3: Direct Test Execution
nix-build -A checks.x86_64-linux.ssh-simple-test
nix-build -A checks.x86_64-linux.ssh-integration-test
Pros:
- Direct control
- Useful for debugging
- Can inspect build artifacts
Test Framework Features
- Caching: Test results are cached by Nix, only re-running when inputs change
- Parallelism: Tests automatically run in parallel based on available cores
- Reproducibility: Tests are deterministic and isolated
- VM Tests: Full system integration tests using NixOS test framework
- Validation: Both syntax checking and runtime behavior testing
Current Issue
The error shown indicates that the validated scripts tests are failing because the bash.nix module expects arguments (mkValidatedScript) that aren't being provided properly through tests-flake.nix. This is exactly the problem that tests-flake.nix was meant to solve - it needs to properly instantiate the module context.
Best Practice Summary
For your workflow:
- Development: Use nix flake check --keep-going to run all tests
- Debugging: Use specific test builds with --show-trace
- CI/CD: Use nix flake check in GitHub Actions
- Before commits: Run nix run .#regression-test for quick validation
Based on my comprehensive exploration of your repository, here's the breakdown of test locations and why flake-modules/tests.nix is so large. Your tests are spread across 3 main locations:
-
flake-modules/tests.nix(2,412 lines) - PRIMARY ACTIVE TESTS- All 30+ tests that run via
nix flake check - Contains configuration evaluation, module integration, and black-box functional tests
- This is where the tmux-session-picker runtime tests are located
- All 30+ tests that run via
-
home/modules/validated-scripts/(3,700+ lines across multiple files) - ORPHANED TESTSbash.nixcontains 72+ test definitions aspassthru.testson script derivations- Tests are defined but not collected into flake checks
- Test collection function exists in
default.nixbut is never called
-
tests/directory (6 files) - LEGACY/UNUSED- SOPS, SSH, integration tests
- Not imported into flake at all
The file is large because it's doing ALL the testing work for your entire repository:
- Configuration evaluation tests (checking nixos/home-manager configs build)
- Module integration tests (checking modules work together)
- Service configuration tests (SSH, users, etc.)
- Cross-module integration (WSL + base, SOPS + base, etc.)
- Script functionality tests (tmux-session-picker black-box tests with 9+ sub-tests each)
You have tests in two places that should be connected but aren't:
❌ CURRENT STATE:
┌─────────────────────────────────────┐
│ flake-modules/tests.nix (2,412 lines)│ ← All tests run here
│ - Config tests │
│ - Module tests │
│ - Script functional tests (manual) │ ← These are DUPLICATED!
└─────────────────────────────────────┘
┌─────────────────────────────────────┐
│ validated-scripts/bash.nix │ ← Tests defined here
│ - Scripts with passthru.tests │
│ - 72+ test derivations │ ← But NEVER RUN!
└─────────────────────────────────────┘
✅ CORRECT PATTERN:
┌─────────────────────────────────────┐
│ home/common/tmux.nix │
│ tmux-session-picker = writeShellApp {│
│ passthru.tests = { │ ← Tests WITH derivation
│ help = ...; │
│ args = ...; │
│ }; │
│ } │
└─────────────────────────────────────┘
↓ (auto-collected)
┌─────────────────────────────────────┐
│ flake-modules/tests.nix (300 lines) │ ← Much smaller!
│ checks = collectAllScriptTests │
│ // moduleIntegrationTests; │ ← Just collection
└─────────────────────────────────────┘
In flake-modules/tests.nix:
- Lines 1-500: Configuration and module tests (appropriate location)
- Lines 500-2412: Script functional tests that should be
passthru.testson the script derivations inhome/common/*.nix
In validated-scripts/bash.nix:
- 72+ test derivations defined as
passthru.tests - Collection function exists:
collectScriptTests - Problem: Never exported to flake checks, so tests never run
-
Move script tests to their derivations in
home/common/*.nix:# home/common/tmux.nix tmux-session-picker = pkgs.writeShellApplication { name = "tmux-session-picker"; text = builtins.readFile ../files/bin/tmux-session-picker; runtimeInputs = [ ... ]; passthru.tests = { help-availability = pkgs.runCommand ... { } ''...''; argument-validation = pkgs.runCommand ... { } ''...''; }; };
-
Create test collection in flake-modules/tests.nix:
checks = # Configuration/module tests { ... } // # Auto-collected script tests (lib.concatMapAttrs collectScriptTests [ config.home.common.tmux config.home.common.git # ... other modules ]);
-
Result:
flake-modules/tests.nixshrinks from 2,412 lines to ~300-500 lines