Step 1: Understand Your Project's Dependencies
Before writing flake.nix, list exactly what your project needs:
# Example for a Python project:
# - Python 3.11
# - Poetry (package manager)
# - PostgreSQL (for dev database)
# - Redis (for cache)
# - Git (probably already have it)
Check your project's documentation, requirements files, etc.
Step 2: Write Your flake.nix
Start with a template and customize it:
{
description = "My Project - Dev Environment";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
in
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
# Add your dependencies here
];
shellHook = ''
echo "Development environment for My Project loaded!"
'';
};
}
);
}
Step 3: Test Your Environment
Initialize the flake:
nix flake update
Enter the environment:
nix develop
Verify your tools work:
# Run commands that your project needs
python --version
poetry --version
# etc.
Try building/running your project:
# Whatever your project needs:
npm install && npm start
# or
poetry install && poetry run python app.py
# or
cargo build
Step 4: Document Your Setup
Create a clear README section for developers:
## Development Setup
This project uses Nix for reproducible development environments.
### Quick Start
```bash
# Enter the dev environment
nix develop
# (Inside the environment)
poetry install
npm install
# etc.
```
### What's Included
- Python 3.11
- Poetry
- PostgreSQL
- Redis
### Troubleshooting
If something doesn't work:
1. Ensure you have Nix installed
2. Run `nix flake update`
3. Exit and re-enter: `exit` then `nix develop`
Step 5: Commit and Test
Add files to git:
git add flake.nix flake.lock README.md
git commit -m "Add Nix development environment"
Test reproducibility (simulate a new developer):
cd /tmp
git clone /path/to/your/project
cd project
nix develop
# Verify everything works
Push to GitHub/GitLab (if you have a repo):
git push origin main
Real-World Example: Full Stack Node App
{
description = "Full stack Node.js + PostgreSQL app";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
in
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
nodejs_20
postgresql
redis
git
];
shellHook = ''
echo "Full stack dev environment loaded!"
echo "Node: $(node --version)"
echo "PostgreSQL: ready (configure in .env)"
echo "Redis: ready on localhost:6379"
'';
};
}
);
}
Troubleshooting Your Flake
Package not found?
nix search nixpkgs python311 # Find the right package name
Environment variables not working?
shellHook = ''
export DATABASE_URL="postgresql://user:pass@localhost/db"
export REDIS_URL="redis://localhost:6379"
'';
Need system packages (C libraries, etc)?
buildInputs = with pkgs; [
libpq # For PostgreSQL
libsodium # For encryption
openssl # For SSL/TLS
pkg-config # For finding libraries
];
Next Steps After the Capstone
Congratulations! You've mastered the fundamentals. Here's where to go:
- Share with your team: Push your flake and show them how to use it
- Add Home Manager: Extend to user-level configuration
- Explore NixOS: If you want full system reproducibility
- Advanced overlays: Customize multiple packages together
- Join the community: Discourse.nixos.org for questions and ideas
What You've Accomplished
✓ You can now:
- Write and understand Nix expressions
- Create reproducible development environments with flakes
- Share setups with teammates that work identically everywhere
- Customize packages and compose complex configurations
- Troubleshoot and refine Nix setups independently
Key Takeaways
- Reproducibility is achievable—Nix makes it easy and elegant
- Flakes are the modern way to package and share Nix projects
- Declarative > Imperative—your whole setup is in code and version control
- Composition is powerful—simple pieces combine into robust systems
- The Nix community is helpful—you're never alone in debugging
Reflection
Think about the projects you work on. How would your team benefit from:
- Eliminating "works on my machine" issues?
- Onboarding new developers in minutes instead of hours?
- Having a single source of truth for your dev environment?
- Easy rollbacks and environment switching?
Nix makes all of this possible. You now have the skills to build it.
🎓 Course Complete!
You've mastered Nix fundamentals and practical applications.
Next: Share what you've built. Teach others. Join the community.