The thunk trap
Why your Nix loop crashes after it finishes
Mika Bohinen - NixCon 2026

Connecting presentation...
Choose where to resume.
Why your Nix loop crashes after it finishes
Mika Bohinen - NixCon 2026

diagnostics = makeReport entries;
initial = { total = 0; inherit diagnostics; };
sumStep = acc: entry: {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};total = 0diagnostics = makeReport entriestotal = 0 + 512diagnostics = the same reportresult carries the memory requested by 100000 service configurations and a deferred diagnostic report.
builtins.attrNames result?result.total?Having the record does not mean its total has been computed.
result = builtins.foldl'
(acc: entry: {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
})
initial entries;builtins.attrNames result?result = builtins.foldl'
(acc: entry: {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
})
initial entries;Demanding the final total enters n deferred additions; at n = 100000, the recorded run overflows.
result.total?acc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in builtins.seq next.total nextacc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in builtins.seq next.total nextacc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in builtins.seq next.total nextEach step computes its total before returning; the recorded run returns 76800000 MiB.
fixed.total?acc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in builtins.deepSeq next next| Demand | Evaluates | Still deferred |
|---|---|---|
seq next next | the record | total and diagnostics |
seq next.total next | total | diagnostics |
deepSeq next next | total and diagnostics | nothing |
fixed.total?diagnostics = throw "diagnostics evaluated";
sumStep = acc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in ...;targeted.total?deep.total?acc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in builtins.seq next.total nextRunnable examples - reading - PDFs
builtins.attrNames resultresult.totalbuiltins.attrNames resultresult.total(sumStep initial (builtins.head entries)).totalbuiltins.attrNames fixedfixed.totalfixed.totalbuiltins.attrNames initialtargeted.totaldeep.totalWhy your Nix loop crashes after it finishes
Mika Bohinen - NixCon 2026

diagnostics = makeReport entries;
initial = { total = 0; inherit diagnostics; };
sumStep = acc: entry: {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};total = 0diagnostics = makeReport entriestotal = 0 + 512diagnostics = the same reportresult carries the memory requested by 100000 service configurations and a deferred diagnostic report.
builtins.attrNames result?result.total?Having the record does not mean its total has been computed.
result = builtins.foldl'
(acc: entry: {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
})
initial entries;builtins.attrNames result?result = builtins.foldl'
(acc: entry: {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
})
initial entries;Demanding the final total enters n deferred additions; at n = 100000, the recorded run overflows.
result.total?acc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in builtins.seq next.total nextacc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in builtins.seq next.total nextacc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in builtins.seq next.total nextEach step computes its total before returning; the recorded run returns 76800000 MiB.
fixed.total?acc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in builtins.deepSeq next next| Demand | Evaluates | Still deferred |
|---|---|---|
seq next next | the record | total and diagnostics |
seq next.total next | total | diagnostics |
deepSeq next next | total and diagnostics | nothing |
fixed.total?diagnostics = throw "diagnostics evaluated";
sumStep = acc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in ...;targeted.total?deep.total?acc: entry:
let next = {
total = acc.total + entry.memoryMiB;
inherit diagnostics;
};
in builtins.seq next.total nextRunnable examples - reading - PDFs
End of presentation
Introduce the memory total and optional diagnostic report as two fields carried by a capacity-planning tool.
Explain total and diagnostics before the callback. The report lists entries missing a port and is a shared string. Each call adds entry.memoryMiB; diagnostics still names the same deferred report. initial starts at zero and entries supplies 100000 calls.
Ask for the field names, then the total. Expect diagnostics and total, then stack overflow. Neither query asks for the diagnostic report.
foldl' evaluates the outer record. The total and diagnostics fields can both remain deferred.
Follow the chain of total dependencies back to zero. Each callback returned before its addition was demanded.
Force next.total before returning next. Zero becomes 512 MiB while the report remains deferred.
The callback receives the computed 512, adds 1024, and returns 1536 MiB. diagnostics still refers to the same report.
Run fixed.total with the same entries and limits. Expect 76800000 MiB. Each callback computed its total before returning.
deepSeq also obtains the memory total, but generates the optional report. It is a shared string, so generation happens once. Explain that the next experiment detects this extra demand.
Replace makeReport entries with a throw so report evaluation is visible. Both choices complete the same callback and run through the full fold. Ask which returns the memory total without demanding diagnostics. Pause five seconds.
Run targeted.total and deep.total. Expect 76800000 MiB from A and diagnostics evaluated from B. Both queries request only total; the deep callback reaches diagnostics on its first call.
Return to the capacity-planning tool. Compute the running memory total as it advances and leave the optional report deferred. Point to the runnable examples.
Arrow keys or Page Up / Page Down navigate. B blanks or restores the audience. F in the audience window toggles its fullscreen. Typing controls keep their keys. Timer recovery resumes paused.