Mika Bohinen / NixCon 2026

The thunk trap

The loop finishes before its total is computed

The thunk trap

Why your Nix loop crashes after it finishes

Mika Bohinen - NixCon 2026

A dog insists this is fine while the room burns around him.Kleisli

A capacity-planning tool sums requested memory across service configurations and can produce a diagnostic report. Asking for the memory total should not require generating the report. A strict fold can finish while leaving the total itself unevaluated, so reading it overflows the stack. Compute each new total before returning the record, while leaving diagnostics deferred. You only need to know Nix functions and attribute sets.

Sum requested memory. Generate the report when requested.

total is in MiB; makeReport lists service configurations missing a port
Code
diagnostics = makeReport entries;
initial = { total = 0; inherit diagnostics; };

sumStep = acc: entry: {
  total = acc.total + entry.memoryMiB;
  inherit diagnostics;
};
One call: the callback builds the next record
Accumulatortotal = 0diagnostics = makeReport entries
callback
Next accumulatortotal = 0 + 512diagnostics = the same report
becomes acc in the next call

A capacity-planning tool sums the memory requested by service configurations and can produce a diagnostic report. We keep total and diagnostics together, but only need the memory total now. diagnostics names a shared report string generated by makeReport. The fold starts at initial and calls sumStep once per entry; each returned record becomes the next acc. entry is the current service configuration; its memoryMiB field supplies the amount to add.

The fold produces a record, but reading its total overflows.

result carries the memory requested by 100000 service configurations and a deferred diagnostic report.

Which fields are in the result?
builtins.attrNames result["diagnostics","total"]
Explore code
What is the final total?
result.totalerror: stack overflow (possible infinite recursion)
Explore code

Having the record does not mean its total has been computed.

result carries the requested memory total and a deferred diagnostic report. Listing its fields returns diagnostics and total. Reading its total forces a chain of 100000 additions and overflows the stack.

foldl' evaluates each record, but its total can remain unevaluated.

initial = { total = 0; inherit diagnostics; }
Code
result = builtins.foldl'
  (acc: entry: {
    total = acc.total + entry.memoryMiB;
    inherit diagnostics;
  })
  initial entries;
foldl' evaluates each record; its total stays deferred
recordtotal
00
1total[0] + 512
2total[1] + 1024
  • computed number
  • deferred expression
  • evaluated record
Three records shown; total[k] is the total of record k.
builtins.attrNames result["diagnostics","total"]
Explore code

entries contains 100000 service configurations with alternating 512 and 1024 MiB memory requests. foldl' evaluates each new accumulator to weak head normal form. That exposes the record without computing total or generating diagnostics.

Reading the final total follows a chain of deferred additions.

initial = { total = 0; inherit diagnostics; }
Code
result = builtins.foldl'
  (acc: entry: {
    total = acc.total + entry.memoryMiB;
    inherit diagnostics;
  })
  initial entries;
Reading the final total climbs through every deferred addition
Reading the final total climbs through every deferred additionBuilding the states returns after each step without evaluating any total. Reading the final total forces the previous total, which forces the one before it. Each forced addition waits inside the previous one until the evaluator reaches its stack limit.1234stack limittotal[n−2]total[n−1]total[n]⋯stack overflowevaluatorfold entriesread result.totalforcing depthReading the final total climbs through every deferred additionBuilding the states returns after each step without evaluating any total. Reading the final total forces the previous total, which forces the one before it. Each forced addition waits inside the previous one until the evaluator reaches its stack limit.1234stack limittotal[n−2]total[n−1]total[n]⋯stack overflowevaluatorfold entriesread result.totalforcing depth

Demanding the final total enters n deferred additions; at n = 100000, the recorded run overflows.

result.totalerror: stack overflow (possible infinite recursion)
Explore code

Each total needs the previous total before it can add the next memory request. The fold finishes, but the chain of additions keeps growing inside the records. Reading result.total forces the whole chain, one level per input, and overflows the stack.

Compute the total before returning the next record.

sumStep: replacement callback for foldl'
Code
acc: entry:
  let next = {
    total = acc.total + entry.memoryMiB;
    inherit diagnostics;
  };
  in builtins.seq next.total next
First call: compute 512 MiB before returning the record
recordtotal
00
1512
next?
  • computed number
  • deferred expression
  • evaluated record
Three records shown; total[k] is the total of record k.

sumStep computes next.total before returning next. The first call stores 512 MiB, while diagnostics still refers to the deferred report.

The next call adds its memory request to an already computed total.

sumStep: replacement callback for foldl'
Code
acc: entry:
  let next = {
    total = acc.total + entry.memoryMiB;
    inherit diagnostics;
  };
  in builtins.seq next.total next
Each call computes total; diagnostics stays deferred
recordtotal
00
1512
21536
  • computed number
  • deferred expression
  • evaluated record
Three records shown; total[k] is the total of record k.

The previous record holds 512, so adding the next 1024 MiB computes 1536. No chain builds up.

The final total now succeeds with the same input and limits.

fixed = builtins.foldl' sumStep initial entries
Code
acc: entry:
  let next = {
    total = acc.total + entry.memoryMiB;
    inherit diagnostics;
  };
  in builtins.seq next.total next
Each step computes its total before the next begins
Each step computes its total before the next beginsThe first peak labels the two nested evaluations: the step waits while seq forces one addition. Both finish before the next step begins. Reading the final total then uses an already computed number.1234stack limitforce additionstepread computed total76800000 MiB⋯evaluatorfold entriesread fixed.totalforcing depthEach step computes its total before the next beginsThe first peak labels the two nested evaluations: the step waits while seq forces one addition. Both finish before the next step begins. Reading the final total then uses an already computed number.1234stack limitforce additionstepread computed total76800000 MiB⋯evaluatorfold entriesread fixed.totalforcing depth

Each step computes its total before returning; the recorded run returns 76800000 MiB.

fixed.total76800000
Explore code

Each call does its one addition before returning. fixed.total returns 76800000 MiB with the same input and limits, without generating the diagnostic report.

deepSeq sums the memory and generates the report.

Another replacement callback
Code
acc: entry:
  let next = {
    total = acc.total + entry.memoryMiB;
    inherit diagnostics;
  };
  in builtins.deepSeq next next
DemandEvaluatesStill deferred
seq next nextthe recordtotal and diagnostics
seq next.total nexttotaldiagnostics
deepSeq next nexttotal and diagnosticsnothing
fixed.total: total from the deepSeq callback
fixed.total76800000
Explore code

deepSeq next next computes total, but also evaluates diagnostics. Our report is a shared string, so generation happens once when the first callback demands it. We wanted only the memory total. seq next.total next keeps the additions evaluated without asking for the report.

Which callback sums memory without generating the report?

Replace makeReport entries with a throw to detect report evaluation
Code
diagnostics = throw "diagnostics evaluated";
sumStep = acc: entry:
  let next = {
    total = acc.total + entry.memoryMiB;
    inherit diagnostics;
  };
  in ...;
  1. Abuiltins.seq next.total next
  2. Bbuiltins.deepSeq next next

Replace report generation with a throw. Both callbacks still run through the same fold over entries, starting from initial. Which callback returns the final memory total without reaching diagnostics?

The targeted callback sums requested memory without evaluating diagnostics.

A · seq next.total next
targeted.total76800000
Explore code
B · deepSeq next next
deep.totalerror: diagnostics evaluated
Explore code

targeted.total returns 76800000 MiB. Each callback computes its new total and leaves diagnostics deferred. deep.total throws during the first callback because deepSeq also demands the report. Both queries select only total; the callback determines whether diagnostics is evaluated.

Compute the fields you need before the next step.

sumStep
Code
acc: entry:
  let next = {
    total = acc.total + entry.memoryMiB;
    inherit diagnostics;
  };
  in builtins.seq next.total next
nixcon2026.bohinen.no

Runnable examples - reading - PDFs

sternenseemann: trampolining Nix (2022)

Compute each new total before returning the next record. The capacity-planning tool can then read its memory total without building a deferred chain of additions or generating the optional diagnostic report. Strictness belongs to the fields that need it; the rest can stay lazy.

Sum requested memory

Sum the memory requested by service configurations and keep a report of entries missing a port. The report is only needed when requested.

let
Definitions
n = 100000;
entries = builtins.genList
  (i: if builtins.bitAnd i 1 == 0
      then { memoryMiB = 512; }
      else { memoryMiB = 1024; port = 8080; }) n;
makeReport = entries: builtins.toJSON
  (builtins.filter (entry: !(entry ? port)) entries);
diagnostics = makeReport entries;
initial = { total = 0; inherit diagnostics; };
result = builtins.foldl'
  (acc: entry: { total = acc.total + entry.memoryMiB; inherit diagnostics; })
  initial entries;

Empty source

    Inspect the record's fields without asking for the memory total or the report.