diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-04 10:25:53 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-04 10:25:53 +0000 |
| commit | 52bad9954cdddf55ab749fd0c6387edbc766632f (patch) | |
| tree | d9dd2078b70a627a71d1adb9555cee83faec5cd0 /docs/how-to/nix-flakes.md | |
| parent | db460efdd4cf34d3b6ac8c19b1b8f89f22bc279f (diff) | |
docs: use Diátaxis structure
Diffstat (limited to 'docs/how-to/nix-flakes.md')
| -rw-r--r-- | docs/how-to/nix-flakes.md | 412 |
1 files changed, 412 insertions, 0 deletions
diff --git a/docs/how-to/nix-flakes.md b/docs/how-to/nix-flakes.md new file mode 100644 index 0000000..4242368 --- /dev/null +++ b/docs/how-to/nix-flakes.md | |||
| @@ -0,0 +1,412 @@ | |||
| 1 | # How-To: Configure Nix Flakes for Development | ||
| 2 | |||
| 3 | **Purpose:** Set up and use Nix flakes for ngit-grasp development | ||
| 4 | **Difficulty:** Intermediate | ||
| 5 | **Time:** 10 minutes | ||
| 6 | |||
| 7 | --- | ||
| 8 | |||
| 9 | ## Problem | ||
| 10 | |||
| 11 | You want to: | ||
| 12 | - Set up a reproducible development environment | ||
| 13 | - Avoid "works on my machine" issues | ||
| 14 | - Use Nix flakes with ngit-grasp | ||
| 15 | |||
| 16 | --- | ||
| 17 | |||
| 18 | ## Prerequisites | ||
| 19 | |||
| 20 | - Nix installed (2.4 or later) | ||
| 21 | - Flakes enabled in your Nix configuration | ||
| 22 | |||
| 23 | --- | ||
| 24 | |||
| 25 | ## Solution | ||
| 26 | |||
| 27 | ### Step 1: Enable Flakes (if not already enabled) | ||
| 28 | |||
| 29 | Check if flakes are enabled: | ||
| 30 | |||
| 31 | ```bash | ||
| 32 | nix flake --version | ||
| 33 | ``` | ||
| 34 | |||
| 35 | If you get an error, enable flakes: | ||
| 36 | |||
| 37 | ```bash | ||
| 38 | # Add to ~/.config/nix/nix.conf (create if doesn't exist) | ||
| 39 | mkdir -p ~/.config/nix | ||
| 40 | echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf | ||
| 41 | |||
| 42 | # Or for system-wide (requires sudo): | ||
| 43 | echo "experimental-features = nix-command flakes" | sudo tee -a /etc/nix/nix.conf | ||
| 44 | ``` | ||
| 45 | |||
| 46 | Restart the Nix daemon: | ||
| 47 | |||
| 48 | ```bash | ||
| 49 | # On NixOS: | ||
| 50 | sudo systemctl restart nix-daemon | ||
| 51 | |||
| 52 | # On macOS: | ||
| 53 | sudo launchctl stop org.nixos.nix-daemon | ||
| 54 | sudo launchctl start org.nixos.nix-daemon | ||
| 55 | |||
| 56 | # On other Linux: | ||
| 57 | sudo pkill nix-daemon | ||
| 58 | ``` | ||
| 59 | |||
| 60 | --- | ||
| 61 | |||
| 62 | ### Step 2: Enter the Development Environment | ||
| 63 | |||
| 64 | ```bash | ||
| 65 | cd ngit-grasp | ||
| 66 | nix develop | ||
| 67 | ``` | ||
| 68 | |||
| 69 | **What this does:** | ||
| 70 | - Reads `flake.nix` in the current directory | ||
| 71 | - Downloads and builds all dependencies | ||
| 72 | - Creates a shell with Rust, Git, and other tools | ||
| 73 | - Sets environment variables | ||
| 74 | |||
| 75 | **First run:** Will take several minutes to download and build | ||
| 76 | **Subsequent runs:** Should be instant (cached) | ||
| 77 | |||
| 78 | --- | ||
| 79 | |||
| 80 | ### Step 3: Verify the Environment | ||
| 81 | |||
| 82 | ```bash | ||
| 83 | # Check Rust is available | ||
| 84 | rustc --version | ||
| 85 | cargo --version | ||
| 86 | |||
| 87 | # Check Git is available | ||
| 88 | git --version | ||
| 89 | |||
| 90 | # Check you're in the Nix shell | ||
| 91 | echo $IN_NIX_SHELL # Should output "impure" | ||
| 92 | ``` | ||
| 93 | |||
| 94 | --- | ||
| 95 | |||
| 96 | ### Step 4: Work with Subprojects | ||
| 97 | |||
| 98 | ngit-grasp has a subproject (`grasp-audit`) with its own flake: | ||
| 99 | |||
| 100 | ```bash | ||
| 101 | # Main project | ||
| 102 | cd ngit-grasp | ||
| 103 | nix develop # Uses ngit-grasp/flake.nix | ||
| 104 | |||
| 105 | # Subproject | ||
| 106 | cd grasp-audit | ||
| 107 | nix develop # Uses grasp-audit/flake.nix | ||
| 108 | ``` | ||
| 109 | |||
| 110 | **Important:** Each directory has its own flake and environment! | ||
| 111 | |||
| 112 | --- | ||
| 113 | |||
| 114 | ## Common Tasks | ||
| 115 | |||
| 116 | ### Build the Project | ||
| 117 | |||
| 118 | ```bash | ||
| 119 | cd grasp-audit | ||
| 120 | nix develop | ||
| 121 | cargo build | ||
| 122 | ``` | ||
| 123 | |||
| 124 | **Or in one command:** | ||
| 125 | |||
| 126 | ```bash | ||
| 127 | cd grasp-audit | ||
| 128 | nix develop -c cargo build | ||
| 129 | ``` | ||
| 130 | |||
| 131 | The `-c` flag runs a command in the Nix environment and exits. | ||
| 132 | |||
| 133 | --- | ||
| 134 | |||
| 135 | ### Run Tests | ||
| 136 | |||
| 137 | ```bash | ||
| 138 | cd grasp-audit | ||
| 139 | nix develop -c cargo test | ||
| 140 | ``` | ||
| 141 | |||
| 142 | --- | ||
| 143 | |||
| 144 | ### Build Without Entering Shell | ||
| 145 | |||
| 146 | ```bash | ||
| 147 | cd grasp-audit | ||
| 148 | nix build | ||
| 149 | ``` | ||
| 150 | |||
| 151 | This builds the package defined in `flake.nix` outputs. | ||
| 152 | |||
| 153 | --- | ||
| 154 | |||
| 155 | ### Update Dependencies | ||
| 156 | |||
| 157 | ```bash | ||
| 158 | # Update flake.lock (updates all inputs) | ||
| 159 | nix flake update | ||
| 160 | |||
| 161 | # Update specific input | ||
| 162 | nix flake lock --update-input nixpkgs | ||
| 163 | ``` | ||
| 164 | |||
| 165 | **When to update:** | ||
| 166 | - Security vulnerabilities in dependencies | ||
| 167 | - Need newer version of Rust or other tools | ||
| 168 | - Monthly maintenance | ||
| 169 | |||
| 170 | --- | ||
| 171 | |||
| 172 | ### Clean Nix Store | ||
| 173 | |||
| 174 | ```bash | ||
| 175 | # Remove unused packages | ||
| 176 | nix-collect-garbage | ||
| 177 | |||
| 178 | # Aggressive cleanup (removes all old generations) | ||
| 179 | nix-collect-garbage -d | ||
| 180 | ``` | ||
| 181 | |||
| 182 | **Warning:** This will remove all old versions. You'll need to re-download if you switch branches. | ||
| 183 | |||
| 184 | --- | ||
| 185 | |||
| 186 | ## Troubleshooting | ||
| 187 | |||
| 188 | ### "nix: command not found" | ||
| 189 | |||
| 190 | **Problem:** Nix is not installed or not in PATH | ||
| 191 | |||
| 192 | **Solution:** | ||
| 193 | ```bash | ||
| 194 | # Install Nix (official installer) | ||
| 195 | sh <(curl -L https://nixos.org/nix/install) --daemon | ||
| 196 | |||
| 197 | # Add to PATH (if needed) | ||
| 198 | source ~/.nix-profile/etc/profile.d/nix.sh | ||
| 199 | ``` | ||
| 200 | |||
| 201 | --- | ||
| 202 | |||
| 203 | ### "experimental features not enabled" | ||
| 204 | |||
| 205 | **Problem:** Flakes are not enabled | ||
| 206 | |||
| 207 | **Solution:** | ||
| 208 | ```bash | ||
| 209 | # Add to ~/.config/nix/nix.conf | ||
| 210 | echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf | ||
| 211 | |||
| 212 | # Restart Nix daemon (see Step 1) | ||
| 213 | ``` | ||
| 214 | |||
| 215 | --- | ||
| 216 | |||
| 217 | ### "nix-shell: command not found" or wrong behavior | ||
| 218 | |||
| 219 | **Problem:** Using old `nix-shell` command instead of `nix develop` | ||
| 220 | |||
| 221 | **Solution:** | ||
| 222 | ```bash | ||
| 223 | # ❌ Wrong (old Nix) | ||
| 224 | nix-shell | ||
| 225 | |||
| 226 | # ✅ Correct (Nix flakes) | ||
| 227 | nix develop | ||
| 228 | ``` | ||
| 229 | |||
| 230 | **Why:** Flakes use `nix develop`, not `nix-shell`. The old command looks for `shell.nix` which doesn't exist. | ||
| 231 | |||
| 232 | --- | ||
| 233 | |||
| 234 | ### "error: getting status of '/nix/store/...': No such file or directory" | ||
| 235 | |||
| 236 | **Problem:** Nix store is corrupted or incomplete | ||
| 237 | |||
| 238 | **Solution:** | ||
| 239 | ```bash | ||
| 240 | # Verify Nix store | ||
| 241 | nix-store --verify --check-contents | ||
| 242 | |||
| 243 | # Repair if needed | ||
| 244 | nix-store --repair --verify --check-contents | ||
| 245 | |||
| 246 | # If still broken, re-enter environment | ||
| 247 | nix develop --refresh | ||
| 248 | ``` | ||
| 249 | |||
| 250 | --- | ||
| 251 | |||
| 252 | ### Build fails with "cannot find crate" | ||
| 253 | |||
| 254 | **Problem:** Cargo cache is stale or corrupted | ||
| 255 | |||
| 256 | **Solution:** | ||
| 257 | ```bash | ||
| 258 | # Clean Cargo cache | ||
| 259 | cargo clean | ||
| 260 | |||
| 261 | # Rebuild | ||
| 262 | nix develop -c cargo build | ||
| 263 | ``` | ||
| 264 | |||
| 265 | --- | ||
| 266 | |||
| 267 | ### "error: unable to download" | ||
| 268 | |||
| 269 | **Problem:** Network issues or cache server down | ||
| 270 | |||
| 271 | **Solution:** | ||
| 272 | ```bash | ||
| 273 | # Use different substituter | ||
| 274 | nix develop --option substituters "https://cache.nixos.org" | ||
| 275 | |||
| 276 | # Or build from source (slow) | ||
| 277 | nix develop --no-substitutes | ||
| 278 | ``` | ||
| 279 | |||
| 280 | --- | ||
| 281 | |||
| 282 | ## Advanced Usage | ||
| 283 | |||
| 284 | ### Use direnv for Automatic Activation | ||
| 285 | |||
| 286 | Install [direnv](https://direnv.net/) to automatically enter Nix environment: | ||
| 287 | |||
| 288 | ```bash | ||
| 289 | # Install direnv | ||
| 290 | nix-env -iA nixpkgs.direnv | ||
| 291 | |||
| 292 | # Create .envrc | ||
| 293 | echo "use flake" > .envrc | ||
| 294 | |||
| 295 | # Allow direnv | ||
| 296 | direnv allow | ||
| 297 | |||
| 298 | # Now cd into directory automatically activates environment! | ||
| 299 | cd ngit-grasp # Automatically runs 'nix develop' | ||
| 300 | ``` | ||
| 301 | |||
| 302 | --- | ||
| 303 | |||
| 304 | ### Customize the Environment | ||
| 305 | |||
| 306 | Edit `flake.nix` to add packages: | ||
| 307 | |||
| 308 | ```nix | ||
| 309 | { | ||
| 310 | devShells.default = pkgs.mkShell { | ||
| 311 | buildInputs = with pkgs; [ | ||
| 312 | # Existing packages | ||
| 313 | cargo | ||
| 314 | rustc | ||
| 315 | |||
| 316 | # Add your packages here | ||
| 317 | jq # JSON processor | ||
| 318 | ripgrep # Fast grep | ||
| 319 | fd # Fast find | ||
| 320 | ]; | ||
| 321 | }; | ||
| 322 | } | ||
| 323 | ``` | ||
| 324 | |||
| 325 | Then reload: | ||
| 326 | |||
| 327 | ```bash | ||
| 328 | nix develop --refresh | ||
| 329 | ``` | ||
| 330 | |||
| 331 | --- | ||
| 332 | |||
| 333 | ### Pin to Specific Rust Version | ||
| 334 | |||
| 335 | Edit `flake.nix`: | ||
| 336 | |||
| 337 | ```nix | ||
| 338 | { | ||
| 339 | inputs.rust-overlay.url = "github:oxalica/rust-overlay"; | ||
| 340 | |||
| 341 | outputs = { self, nixpkgs, rust-overlay }: | ||
| 342 | let | ||
| 343 | pkgs = import nixpkgs { | ||
| 344 | overlays = [ rust-overlay.overlays.default ]; | ||
| 345 | }; | ||
| 346 | |||
| 347 | # Pin to specific version | ||
| 348 | rust = pkgs.rust-bin.stable."1.75.0".default; | ||
| 349 | in { | ||
| 350 | devShells.default = pkgs.mkShell { | ||
| 351 | buildInputs = [ rust ]; | ||
| 352 | }; | ||
| 353 | }; | ||
| 354 | } | ||
| 355 | ``` | ||
| 356 | |||
| 357 | --- | ||
| 358 | |||
| 359 | ## Best Practices | ||
| 360 | |||
| 361 | ### DO: | ||
| 362 | - ✅ Use `nix develop` for flakes (not `nix-shell`) | ||
| 363 | - ✅ Commit `flake.lock` to version control | ||
| 364 | - ✅ Update flakes monthly | ||
| 365 | - ✅ Use `-c` flag for one-off commands | ||
| 366 | - ✅ Use direnv for automatic activation | ||
| 367 | |||
| 368 | ### DON'T: | ||
| 369 | - ❌ Use `nix-shell` with flakes | ||
| 370 | - ❌ Manually edit `flake.lock` | ||
| 371 | - ❌ Ignore flake update warnings | ||
| 372 | - ❌ Mix Nix and non-Nix environments | ||
| 373 | - ❌ Commit `.direnv/` to git | ||
| 374 | |||
| 375 | --- | ||
| 376 | |||
| 377 | ## Quick Reference | ||
| 378 | |||
| 379 | ```bash | ||
| 380 | # Enter environment | ||
| 381 | nix develop | ||
| 382 | |||
| 383 | # Run command in environment | ||
| 384 | nix develop -c cargo build | ||
| 385 | |||
| 386 | # Build package | ||
| 387 | nix build | ||
| 388 | |||
| 389 | # Update dependencies | ||
| 390 | nix flake update | ||
| 391 | |||
| 392 | # Show flake info | ||
| 393 | nix flake show | ||
| 394 | |||
| 395 | # Check flake | ||
| 396 | nix flake check | ||
| 397 | |||
| 398 | # Clean up | ||
| 399 | nix-collect-garbage | ||
| 400 | ``` | ||
| 401 | |||
| 402 | --- | ||
| 403 | |||
| 404 | ## Related Documentation | ||
| 405 | |||
| 406 | - [Getting Started Tutorial](../tutorials/getting-started.md) - First-time setup | ||
| 407 | - [Nix Flakes Manual](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html) | ||
| 408 | - [grasp-audit README](../../grasp-audit/README.md) - Subproject docs | ||
| 409 | |||
| 410 | --- | ||
| 411 | |||
| 412 | *Part of the [ngit-grasp how-to guides](./)* | ||