upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/docs/how-to/nix-flakes.md
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-11-04 10:25:53 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2025-11-04 10:25:53 +0000
commit52bad9954cdddf55ab749fd0c6387edbc766632f (patch)
treed9dd2078b70a627a71d1adb9555cee83faec5cd0 /docs/how-to/nix-flakes.md
parentdb460efdd4cf34d3b6ac8c19b1b8f89f22bc279f (diff)
docs: use Diátaxis structure
Diffstat (limited to 'docs/how-to/nix-flakes.md')
-rw-r--r--docs/how-to/nix-flakes.md412
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
11You 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
29Check if flakes are enabled:
30
31```bash
32nix flake --version
33```
34
35If you get an error, enable flakes:
36
37```bash
38# Add to ~/.config/nix/nix.conf (create if doesn't exist)
39mkdir -p ~/.config/nix
40echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf
41
42# Or for system-wide (requires sudo):
43echo "experimental-features = nix-command flakes" | sudo tee -a /etc/nix/nix.conf
44```
45
46Restart the Nix daemon:
47
48```bash
49# On NixOS:
50sudo systemctl restart nix-daemon
51
52# On macOS:
53sudo launchctl stop org.nixos.nix-daemon
54sudo launchctl start org.nixos.nix-daemon
55
56# On other Linux:
57sudo pkill nix-daemon
58```
59
60---
61
62### Step 2: Enter the Development Environment
63
64```bash
65cd ngit-grasp
66nix 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
84rustc --version
85cargo --version
86
87# Check Git is available
88git --version
89
90# Check you're in the Nix shell
91echo $IN_NIX_SHELL # Should output "impure"
92```
93
94---
95
96### Step 4: Work with Subprojects
97
98ngit-grasp has a subproject (`grasp-audit`) with its own flake:
99
100```bash
101# Main project
102cd ngit-grasp
103nix develop # Uses ngit-grasp/flake.nix
104
105# Subproject
106cd grasp-audit
107nix 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
119cd grasp-audit
120nix develop
121cargo build
122```
123
124**Or in one command:**
125
126```bash
127cd grasp-audit
128nix develop -c cargo build
129```
130
131The `-c` flag runs a command in the Nix environment and exits.
132
133---
134
135### Run Tests
136
137```bash
138cd grasp-audit
139nix develop -c cargo test
140```
141
142---
143
144### Build Without Entering Shell
145
146```bash
147cd grasp-audit
148nix build
149```
150
151This builds the package defined in `flake.nix` outputs.
152
153---
154
155### Update Dependencies
156
157```bash
158# Update flake.lock (updates all inputs)
159nix flake update
160
161# Update specific input
162nix 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
176nix-collect-garbage
177
178# Aggressive cleanup (removes all old generations)
179nix-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)
195sh <(curl -L https://nixos.org/nix/install) --daemon
196
197# Add to PATH (if needed)
198source ~/.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
210echo "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)
224nix-shell
225
226# ✅ Correct (Nix flakes)
227nix 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
241nix-store --verify --check-contents
242
243# Repair if needed
244nix-store --repair --verify --check-contents
245
246# If still broken, re-enter environment
247nix 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
259cargo clean
260
261# Rebuild
262nix 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
274nix develop --option substituters "https://cache.nixos.org"
275
276# Or build from source (slow)
277nix develop --no-substitutes
278```
279
280---
281
282## Advanced Usage
283
284### Use direnv for Automatic Activation
285
286Install [direnv](https://direnv.net/) to automatically enter Nix environment:
287
288```bash
289# Install direnv
290nix-env -iA nixpkgs.direnv
291
292# Create .envrc
293echo "use flake" > .envrc
294
295# Allow direnv
296direnv allow
297
298# Now cd into directory automatically activates environment!
299cd ngit-grasp # Automatically runs 'nix develop'
300```
301
302---
303
304### Customize the Environment
305
306Edit `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
325Then reload:
326
327```bash
328nix develop --refresh
329```
330
331---
332
333### Pin to Specific Rust Version
334
335Edit `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
381nix develop
382
383# Run command in environment
384nix develop -c cargo build
385
386# Build package
387nix build
388
389# Update dependencies
390nix flake update
391
392# Show flake info
393nix flake show
394
395# Check flake
396nix flake check
397
398# Clean up
399nix-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](./)*