- Switch nixpkgs input to official zstd tarball from channels.nixos.org - Update flake.lock with verified tarball release - Pin nix.registry.nixpkgs.flake to inputs.nixpkgs in modules/system/base.nix - Update development environment and antigravity documentation examples
3.3 KiB
💻 Declarative Development Environments
This guide shows how to run isolated project environments using nix-shell or devShells alongside direnv and uv to keep the global system environment clean.
⚡ DevShells and nix-shell
Instead of installing packages globally in apps.nix for one-off projects, define them per-project.
1. Classic shell.nix (Nix Channels)
For standard Nix projects, place a shell.nix in your project root:
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
nativeBuildInputs = with pkgs; [
nodejs_20
yarn
];
shellHook = ''
echo "Welcome to the Node.js environment!"
'';
}
Run nix-shell to drop into this isolated shell with node and yarn pre-loaded.
2. Flake-based devShell
For modern Flake projects, declare your devShell inside your flake.nix:
{
inputs = {
# Official project-hosted zstd tarball (faster downloads, forge-independent)
# nixpkgs-unstable is ideal for standalone dev shells
nixpkgs.url = "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.zst";
};
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
devShells.${system}.default = pkgs.mkShell {
buildInputs = with pkgs; [
go
golangci-lint
];
};
};
}
Run nix develop to enter this environment.
🔄 Automated Shell Loading with direnv
direnv automatically activates your project-specific environment when you cd into its directory.
Configuration
- In your project root, create a file named
.envrc. - Add one of the following lines based on your setup:
- For classic projects (
shell.nix):use nix - For Flake-based projects (
flake.nix):use flake
- For classic projects (
- Allow the environment to load:
direnv allow
As soon as you step into the directory, direnv populates $PATH and other variables with your Nix-defined tools. Leaving the folder unloads them automatically.
🐍 Python Projects: Nix + uv Workflow
To prevent library conflicts and keep the host system clean, combine Nix (for system-level packages/binaries like C libraries, python compilers) with uv (for project-level Python packages).
Recommended Workflow:
- Use Nix (
devShellsornix-shell) to make Python and system dependencies (like OpenSSL or PostgreSQL drivers) available:pkgs.mkShell { buildInputs = with pkgs; [ python311 uv openssl ]; } - Use
uvinside that environment to manage the virtual environment and install Python-specific libraries cleanly:# Create the virtualenv uv venv # Activate the environment source .venv/bin/activate # Install dependencies lightning-fast without polluting system packages uv pip install fastapi uvicorn - Ensure
.envrcloads both the Nix shell and the virtual environment automatically:This triggers standard Nix tools and sourcesuse flake layout python.venv/bin/activatesimultaneously upon changing directories.