Code Quality and Validation Guide
This NixOS template includes comprehensive code quality tools and validation processes to ensure clean, secure, and maintainable configurations.
Quick Start
# Enter development environment
nix develop
# Set up complete development environment (recommended for new users)
just dev-setup
# Run basic validation
just validate
# Run comprehensive quality checks
just quality
Available Tools
Core Validation Tools
| Tool | Purpose | Command |
|---|---|---|
| nixfmt (via treefmt) | Format Nix, shell, Markdown, YAML, JSON | just fmt |
| statix | Lint and analyze Nix code | just lint |
| deadnix | Detect unused code | just dead-code-check |
| vulnix | Security vulnerability scanning | just security-audit |
| nix flake check | Validate flake syntax and build | just check |
Development Utilities
| Tool | Purpose | Usage |
|---|---|---|
| pre-commit | Git hooks automation | just install-hooks |
| fd | Better file finding | fd pattern |
| ripgrep | Better text searching | rg pattern |
| bat | Better file viewing | bat file.nix |
| eza | Better directory listing | eza -la |
| fzf | Fuzzy finding | history \| fzf |
Validation Commands
Basic Validation
# Format all Nix files
just fmt
# Check flake validity
just check
# Comprehensive validation (format-check, lint, dead-code)
just validate
Advanced Quality Checks
# Full code quality suite
just quality
# Individual checks
just lint # Nix code analysis
just dead-code-check # Unused code detection
just security-audit # Vulnerability scanning
just outdated-check # Dependency freshness
# Format validation (non-destructive)
just format-check # Check if files need formatting
Targeted Validation
# Check specific file or directory
just check-path modules/desktop/
# Fix dead code automatically
just dead-code-fix
Git Hooks Integration
Setup
# Install pre-commit hooks (one-time setup)
just install-hooks
# Complete development environment setup
just dev-setup
Pre-commit Hook Features
The pre-commit configuration automatically runs these checks:
- File Formatting:
nixfmt(the official Nix formatter) viatreefmt - Code Linting:
statixanalyzes code quality - Dead Code Detection:
deadnixfinds unused code - Flake Validation: Ensures flake builds correctly
- General Checks: Trailing whitespace, file endings, JSON/YAML syntax
- Documentation: Markdown linting
- Security: Basic vulnerability checks
Managing Hooks
# Run hooks on all files manually
just run-hooks
# Test hooks without committing
just test-hooks
# Update hook versions
just update-hooks
# Skip hooks for a commit (not recommended)
git commit --no-verify -m "commit message"
Configuration Files
Pre-commit Configuration
.pre-commit-config.yaml- Hook definitions and settings.markdownlint.yaml- Markdown linting rules
Tool Behavior
Statix (Linter):
- Checks for common Nix anti-patterns
- Suggests improvements and best practices
- Reports unused variables and imports
Deadnix (Dead Code Detection):
- Finds unused function parameters
- Detects unreferenced variables
- Identifies unused imports
Vulnix (Security):
- Scans for known vulnerabilities in dependencies
- Cross-references with NixOS security database
- Provides remediation suggestions
Development Workflow
Recommended Workflow
-
Initial Setup:
nix develop just dev-setup -
During Development:
# Make changes to Nix files just validate # Quick validation just test host # Test configuration -
Before Committing:
just quality # Comprehensive checks git add . git commit # Hooks run automatically -
Regular Maintenance:
just update # Update dependencies just outdated-check # Check for newer versions just security-audit # Security review
Common Issues and Solutions
Formatting Issues
# Problem: Files not properly formatted
ERROR: Some files need formatting. Run 'just fmt' to fix.
# Solution: Format files
just fmt
Linting Warnings
# Problem: Statix reports code issues
WARNING: Unused binding 'pkgs'
# Solutions:
1. Remove unused variables
2. Prefix with underscore: _pkgs
3. Add to function signature if needed
Dead Code Detection
# Problem: deadnix finds unused code
ERROR: Unused binding at line 42
# Solutions:
just dead-code-fix # Automatic removal
# Or manually remove unused code
Hook Failures
# Problem: Pre-commit hooks fail
ERROR: pre-commit hook failed
# Solutions:
1. Run individual checks: just lint
2. Fix issues manually
3. Re-run: just run-hooks
4. Skip hooks if necessary: git commit --no-verify
Best Practices
Code Quality
- Format Early and Often: Run
just fmtfrequently - Validate Before Committing: Use
just validate - Regular Security Audits: Run
just security-auditweekly - Keep Dependencies Updated: Use
just updateregularly
Development Environment
- Use Development Shell: Always work in
nix develop - Install Hooks: Set up pre-commit hooks early
- Test Configurations: Use
just testbefore switching - Document Changes: Update relevant documentation
Git Workflow
- Small, Focused Commits: Easier to validate and review
- Meaningful Messages: Describe what and why
- Clean History: Use interactive rebase to clean up
- Pre-push Validation: Run
just qualitybefore pushing
Continuous Integration
GitHub Actions Integration
# .github/workflows/validation.yml
name: Validation
on: [push, pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v24
- run: nix develop --command just validate
- run: nix develop --command just security-audit
Local CI Simulation
# Simulate CI environment locally
just quality # Run all quality checks
just check # Validate flake
just build example-desktop # Test build
Customization
Adding Custom Checks
Edit .pre-commit-config.yaml to add new hooks:
- repo: local
hooks:
- id: custom-check
name: Custom validation
entry: your-command
language: system
files: '\.nix$'
Tool Configuration
- Statix: Create
statix.tomlfor custom rules - Deadnix: Use command-line flags in justfile
- Pre-commit: Modify
.pre-commit-config.yaml
Resources
Documentation
Learning Resources
- Nix Pills - Deep dive into Nix
- NixOS Wiki - Community knowledge base
- Nix Reference Manual - Complete reference
Contributing
When contributing to this template:
- Follow the Quality Standards: All code must pass
just quality - Update Documentation: Keep docs in sync with changes
- Test Thoroughly: Validate on multiple configurations
- Use Conventional Commits: Follow commit message conventions
Getting Help
- Check Documentation: Start with this guide and tool docs
- Run Diagnostics: Use
just --listto see all commands - Validate Environment: Ensure
nix developworks correctly - Community Resources: NixOS Discord, Reddit, and Discourse