The thunk trap

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

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

What happens when we ask this fold for its result?

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

Which fields are in the result?
builtins.attrNames result?
Explore code
What is the final total?
result.total?
Explore code

What does this strict fold evaluate?

initial = { total = 0; inherit diagnostics; }
Code
result = builtins.foldl'
  (acc: entry: {
    total = acc.total + entry.memoryMiB;
    inherit diagnostics;
  })
  initial entries;

Empty source

    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.
    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.

    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.
    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.
    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.

    builtins.attrNames result?
    Explore code

    What else does deepSeq demand?

    Another replacement callback
    Code
    acc: entry:
      let next = {
        total = acc.total + entry.memoryMiB;
        inherit diagnostics;
      };
      in builtins.deepSeq next next
    fixed.total: total from the deepSeq callback
    fixed.total?
    Explore code

    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

    What happens when each fold asks for total?

    A · seq next.total next
    targeted.total?
    Explore code
    B · deepSeq next next
    deep.total?
    Explore code
    Overview
    1. The thunk trap
    2. Sum requested memory. Generate the report when requested.
    3. What happens when we ask this fold for its result?
    4. What does this strict fold evaluate?
    5. What happens when we read result.total?
    6. Compute the total before returning the next record.
    7. The next call adds its memory request to an already computed total.
    8. Does computing total at each step repair the fold?
    9. What else does deepSeq demand?
    10. Which callback sums memory without generating the report?
    11. What happens when each fold asks for total?
    12. Compute the fields you need before the next step.

    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.