Trampolining Nix
How I nearly lost my mind fighting the evaluator
Mika Bohinen - NixCon 2026

Connecting presentation...
Choose where to resume.
How I nearly lost my mind fighting the evaluator
Mika Bohinen - NixCon 2026

I wanted to build effects, a type system and an evaluator in Nix.
getgetrequest the state21the current states: put (s * 2)s = 21put (s * 2)state = 42bind get (s:
bind (put (s * 2)) (_:
pure s))s = 21state = 4221the original valueThe state ends at 42; the program returns 21.
run = state:
let next = step state;
in
if next.done
then next.value
else run next.state;Each call to step returns before run calls itself.
run = state:
let next = step state;
in
if next.done
then next.value
else run next.state;The earlier calls cannot return until they have the answer.
run = state:
let next = step state;
in
if next.done
then next.value
else run next.state;The final value travels back through the waiting calls.
run = state:
let next = step state;
in
if next.done
then next.value
else run next.state;Tail-call elimination could discard these waiting callers. Nix keeps them.
The driver has called the first step. It is the only call above the driver.
The first step has returned its reply. Its call is gone before the next one starts.
Each step returns before the next call; the depth stays at one.
total: _: total + 1total = 0the initial valuetotal + 11builtins.foldl'
(total: _: total + 1)
0
ticksscalarFold?builtins.genericClosure {
startSet = [{ key = app.id; package = app; }];
operator = item:
map (package: {
key = package.id;
inherit package;
}) item.package.dependencies;
}Each box is one call to operator; its arrows are the dependencies it returns.
Illustrative traversal; each key is expanded once, in visit order.
startSet = [{ key = 0; total = 0; }];key = 0Identity used by the worklist
total = 0Payload used by our counter
{
key = item.key + 1;
total = item.total + 1;
}key = 0total = 0the seedoperatorkey = item.key + 1total = item.total + 1fields can remain unevaluatedoperator = item:
if item.key >= n then [] else| Condition | operator returns | Effect | |
|---|---|---|---|
item.key < n | [ successor ] | one more state | |
item.key >= n | [] | this branch stops |
n = 100000. The seed has key 0; the last visited record has key n.
naive = builtins.genericClosure {
inherit startSet operator;
};
builtins.length naive?(last naive).total?Counting the visited states does not compute their totals.

{
key = item.key + 1;
total = item.total + 1;
}{
key = item.key + 1;
total = item.total + 1;
}builtins.length naive?{
key = item.key + 1;
total = item.total + 1;
}Demanding the final total enters n deferred additions; at n = 100000, the recorded run overflows.
(last naive).total?let next = { total = item.total + 1; };
in {
key = builtins.seq next (item.key + 1);
inherit (next) total;
}(last shallow).total?let next = { total = item.total + 1; };
in {
key = builtins.seq next.total (item.key + 1);
inherit (next) total;
}let next = { total = item.total + 1; };
in {
key = builtins.seq next.total (item.key + 1);
inherit (next) total;
}let next = { total = item.total + 1; };
in {
key = builtins.seq next.total (item.key + 1);
inherit (next) total;
}Each step computes its total before returning; the recorded run returns 100000.
(last targeted).total?let next = { stats.total = item.stats.total + 1; };
in {
key = item.key + 1;
inherit (next) stats;
}let next = { stats.total = item.stats.total + 1; };
in {
key = builtins.seq next.stats (item.key + 1);
inherit (next) stats;
}nestedShallowResult?let next = { stats.total = item.stats.total + 1; };
in {
key = builtins.seq next.stats.total (item.key + 1);
inherit (next) stats;
}nestedTargetedResult?k = builtins.deepSeq newState (step.key + 1);We know to force total in our counter. The interpreter accepts differently shaped program state, so it chooses recursive forcing.
(last deep).total?history = {
head = item.key;
tail = item.history;
};head = item.keytail = item.historytailitem.historyshared, not copiedtail…next = {
total = item.total + 1;
history = { head = item.key; tail = item.history; };
};| Demand | Evaluates | Still deferred |
|---|---|---|
seq next | the record | total, history |
seq next.total | total | history |
deepSeq next | total and every history cell | nothing |
3 new cells. 1 + 2 + 3 = 6 visits.
Stored values can already be evaluated. Each traversal walks the links again.
n steps cost n(n + 1) / 2 visits: about 5 billion at n = 100000.
seq next.totalComputes the number; leaves the program log alone.
deepSeq nextRecursive forcing also walks this program's growing log.
The interpreter cannot hard-code total. Its forcing cost depends on what the program carries.
Π (x₁ : A₁).
Π (x₂ : A₂).
…
Π (xₙ : Aₙ). RWhat about 10,000 binders?
A Π type describes a function whose result type may depend on its argument.
The driver can take another step only after this operation returns.
f0 = x: x;
step = previous: x: previous x + 1;step f0x: previous x + 1remembers previous = f0f1 = step f0;
f2 = step f1;
f3 = step f2;f3adds 1 after previouspreviousf2adds 1 after previouspreviousf1adds 1 after previouspreviousf0returns xbuiltins.deepSeq closureChain truebuiltins.deepSeq closureChain true?closureChain 0Illustrative trace; spacing shows order, not elapsed time.
closureChain 0?
And then I called it.
x: previous x + 1previous is the remembered function. The pending operation is + 1.
{ tag = "add"; amount = 1; }Store the operation for the dispatcher to perform. Written add 1 below.
Where did previous go? Its links become the order of the frame list.
add 1add 1add 1apply = value: frame:
if frame.tag == "add"
then value + frame.amount
else throw "Unknown frame";0tag = "add"amount = 11value + frame.amountbuiltins.foldl' apply 0 frames0add 11add 12add 13f3 0?builtins.foldl' apply 0 frames?Each transition consumes the top frame; the remaining frames keep their order.
Deep terms require control over each recursive walk.
Π₁ = Π (x₁ : A₁). Π₂
Π₁ is the outer type; Π₂ is its body: Π (x₂ : A₂). …
Each arrow returns to the driver. Open both bodies with the same stand-in for x₁, then continue at the next binder.

I'm the evaluator now.
{ key = 0; total = 0; }Each held state keeps the data it references reachable.
Each held state keeps the data it references reachable.

while holding every visited state
chunk = builtins.genList (i: i) size;
after = builtins.foldl' stepIf state chunk;s₀ → s₁ → … → sₖsₖ → … → sₘFinished: return the answer. Each recursive call leaves one driver call waiting.

All that for a while loop.
run = state:
let next = step state;
in
if next.done
then next.value
else run next.state;builtins.trampoline step initialStateinitialStatestep currentone call, then a replyresultThe machine's frames and forcing policy stay in the program.
Nix already hosts languages with their own semantics.
The module system collects, merges and checks option definitions.
Should every interpreter have to build its own loop?
Native trampoline discussion: NixOS/nix #8430 / hsjobeki's prototype
scalarFoldbuiltins.length naive(last naive).totalbuiltins.length naive(last naive).total(last shallow).total(last targeted).totalnestedShallowResultnestedTargetedResult(last deep).totalbuiltins.deepSeq closureChain trueclosureChain 0f3 0builtins.foldl' apply 0 framesHow I nearly lost my mind fighting the evaluator
Mika Bohinen - NixCon 2026

I wanted to build effects, a type system and an evaluator in Nix.
getgetrequest the state21the current states: put (s * 2)s = 21put (s * 2)state = 42bind get (s:
bind (put (s * 2)) (_:
pure s))s = 21state = 4221the original valueThe state ends at 42; the program returns 21.
run = state:
let next = step state;
in
if next.done
then next.value
else run next.state;Each call to step returns before run calls itself.
run = state:
let next = step state;
in
if next.done
then next.value
else run next.state;The earlier calls cannot return until they have the answer.
run = state:
let next = step state;
in
if next.done
then next.value
else run next.state;The final value travels back through the waiting calls.
run = state:
let next = step state;
in
if next.done
then next.value
else run next.state;Tail-call elimination could discard these waiting callers. Nix keeps them.
The driver has called the first step. It is the only call above the driver.
The first step has returned its reply. Its call is gone before the next one starts.
Each step returns before the next call; the depth stays at one.
total: _: total + 1total = 0the initial valuetotal + 11builtins.foldl'
(total: _: total + 1)
0
ticksscalarFold?builtins.genericClosure {
startSet = [{ key = app.id; package = app; }];
operator = item:
map (package: {
key = package.id;
inherit package;
}) item.package.dependencies;
}Each box is one call to operator; its arrows are the dependencies it returns.
Illustrative traversal; each key is expanded once, in visit order.
startSet = [{ key = 0; total = 0; }];key = 0Identity used by the worklist
total = 0Payload used by our counter
{
key = item.key + 1;
total = item.total + 1;
}key = 0total = 0the seedoperatorkey = item.key + 1total = item.total + 1fields can remain unevaluatedoperator = item:
if item.key >= n then [] else| Condition | operator returns | Effect | |
|---|---|---|---|
item.key < n | [ successor ] | one more state | |
item.key >= n | [] | this branch stops |
n = 100000. The seed has key 0; the last visited record has key n.
naive = builtins.genericClosure {
inherit startSet operator;
};
builtins.length naive?(last naive).total?Counting the visited states does not compute their totals.

{
key = item.key + 1;
total = item.total + 1;
}{
key = item.key + 1;
total = item.total + 1;
}builtins.length naive?{
key = item.key + 1;
total = item.total + 1;
}Demanding the final total enters n deferred additions; at n = 100000, the recorded run overflows.
(last naive).total?let next = { total = item.total + 1; };
in {
key = builtins.seq next (item.key + 1);
inherit (next) total;
}(last shallow).total?let next = { total = item.total + 1; };
in {
key = builtins.seq next.total (item.key + 1);
inherit (next) total;
}let next = { total = item.total + 1; };
in {
key = builtins.seq next.total (item.key + 1);
inherit (next) total;
}let next = { total = item.total + 1; };
in {
key = builtins.seq next.total (item.key + 1);
inherit (next) total;
}Each step computes its total before returning; the recorded run returns 100000.
(last targeted).total?let next = { stats.total = item.stats.total + 1; };
in {
key = item.key + 1;
inherit (next) stats;
}let next = { stats.total = item.stats.total + 1; };
in {
key = builtins.seq next.stats (item.key + 1);
inherit (next) stats;
}nestedShallowResult?let next = { stats.total = item.stats.total + 1; };
in {
key = builtins.seq next.stats.total (item.key + 1);
inherit (next) stats;
}nestedTargetedResult?k = builtins.deepSeq newState (step.key + 1);We know to force total in our counter. The interpreter accepts differently shaped program state, so it chooses recursive forcing.
(last deep).total?history = {
head = item.key;
tail = item.history;
};head = item.keytail = item.historytailitem.historyshared, not copiedtail…next = {
total = item.total + 1;
history = { head = item.key; tail = item.history; };
};| Demand | Evaluates | Still deferred |
|---|---|---|
seq next | the record | total, history |
seq next.total | total | history |
deepSeq next | total and every history cell | nothing |
3 new cells. 1 + 2 + 3 = 6 visits.
Stored values can already be evaluated. Each traversal walks the links again.
n steps cost n(n + 1) / 2 visits: about 5 billion at n = 100000.
seq next.totalComputes the number; leaves the program log alone.
deepSeq nextRecursive forcing also walks this program's growing log.
The interpreter cannot hard-code total. Its forcing cost depends on what the program carries.
Π (x₁ : A₁).
Π (x₂ : A₂).
…
Π (xₙ : Aₙ). RWhat about 10,000 binders?
A Π type describes a function whose result type may depend on its argument.
The driver can take another step only after this operation returns.
f0 = x: x;
step = previous: x: previous x + 1;step f0x: previous x + 1remembers previous = f0f1 = step f0;
f2 = step f1;
f3 = step f2;f3adds 1 after previouspreviousf2adds 1 after previouspreviousf1adds 1 after previouspreviousf0returns xbuiltins.deepSeq closureChain truebuiltins.deepSeq closureChain true?closureChain 0Illustrative trace; spacing shows order, not elapsed time.
closureChain 0?
And then I called it.
x: previous x + 1previous is the remembered function. The pending operation is + 1.
{ tag = "add"; amount = 1; }Store the operation for the dispatcher to perform. Written add 1 below.
Where did previous go? Its links become the order of the frame list.
add 1add 1add 1apply = value: frame:
if frame.tag == "add"
then value + frame.amount
else throw "Unknown frame";0tag = "add"amount = 11value + frame.amountbuiltins.foldl' apply 0 frames0add 11add 12add 13f3 0?builtins.foldl' apply 0 frames?Each transition consumes the top frame; the remaining frames keep their order.
Deep terms require control over each recursive walk.
Π₁ = Π (x₁ : A₁). Π₂
Π₁ is the outer type; Π₂ is its body: Π (x₂ : A₂). …
Each arrow returns to the driver. Open both bodies with the same stand-in for x₁, then continue at the next binder.

I'm the evaluator now.
{ key = 0; total = 0; }Each held state keeps the data it references reachable.
Each held state keeps the data it references reachable.

while holding every visited state
chunk = builtins.genList (i: i) size;
after = builtins.foldl' stepIf state chunk;s₀ → s₁ → … → sₖsₖ → … → sₘFinished: return the answer. Each recursive call leaves one driver call waiting.

All that for a while loop.
run = state:
let next = step state;
in
if next.done
then next.value
else run next.state;builtins.trampoline step initialStateinitialStatestep currentone call, then a replyresultThe machine's frames and forcing policy stay in the program.
Nix already hosts languages with their own semantics.
The module system collects, merges and checks option definitions.
Should every interpreter have to build its own loop?
Native trampoline discussion: NixOS/nix #8430 / hsjobeki's prototype
End of presentation
Thank the organizers. Introduce nix-effects as a deliberate exploration of Nix as a programming language: effects, a type system and an evaluator.
Introduce effects, an MLTT-inspired type checker and an evaluator in Nix. Focus on running programs.
Introduce a request and a handler. The get request receives the current state, 21. The handler gives the operation its meaning.
Follow the reply into s. The continuation requests put with twice s: 42. Name bind as the connection to the next computation.
Trace get, double, put, return. Final state 42; result 21. The interpreter must carry state and keep running continuations.
Introduce step's two possible replies. run s0 needs run s1's answer before it can return. Point to both open calls. Each step call has already returned.
Add run s2. The first two calls are still waiting. Height counts calls that have started but not returned: the call stack. More transitions add more waiting callers.
Follow value down from run s2 to run s1 to run s0. Each return closes a call. The answer is unchanged all the way back.
Nothing remains except passing back the answer: this is a tail call. Tail-call elimination could discard the waiting caller. Nix keeps it, so enough transitions exhaust call depth or stack. Forcing state does not change this.
Move repetition into a native driver. step describes the next work instead of calling run. Keep the same vertical scale; one step call is open above the driver.
The step returns a description of the next work. The call ends before another begins; point back to the baseline.
More steps do not add nested driver calls. The next work can be a function or explicit data. Now find a driver in Nix.
Show the smallest callback: a scalar plus one. The list supplies iterations; its element values are irrelevant.
Run the strict scalar fold. It returns 100000 without accumulating driver calls. Compare for each item in a supplied list with while the computation is unfinished. The interpreter needs the step to decide when iteration ends.
Start at app. operator returns direct dependencies; lib-a and lib-b both return lib-c. Point to the dashed copy: its key is already seen, so it is skipped. One operator call per key. The result lists every reachable package.
Reinterpret packages as execution states. Return one successor to continue or none to finish. Fresh keys distinguish successive steps even if payload values repeat. We inherit the visited-record collection.
Use total as a small model of interpreter state: data carried between steps. key identifies records; total carries the answer.
Construct one successor. Writing the addition into total describes a value; it does not establish when the addition runs.
Use key to stop at n. Below n, return one successor; at n, return an empty list.
Name the returned list naive. Prepare two questions: how many states were visited, and what is the last total?
Pause on the apparent success: the borrowed worklist supplies a while loop.
Run the visited count: 100001 including the seed. Then run the final total and pause on its stack overflow.
Leave a short beat for the setback. Move from whether traversal finished to what work remains in the answer.
Follow key and total as separate dependency paths. Their values advance together in source, but demanding one does not demand the other.
Reuse the successful count. Keys let the driver traverse the worklist; the count says nothing about the deferred totals.
Follow each total to its predecessor. Building the records stayed shallow; demanding the final number enters all the deferred additions.
Name the carried data next to compare demanding the record with demanding its total. Try seq next. A ready record can still contain a deferred total, so the scalar dependency chain survives.
Make the demanded next key depend on seq next.total. The first transition computes one before handing control back.
The preceding total is already a number. Compute two with one addition, preserving the invariant for every following transition.
Run the targeted repair with unchanged input and limits. Connect the counter's computed total to the interpreter's carried state.
Move total inside stats. Ask whether seq next.stats reaches the number. Pause for a prediction before revealing the answer.
Run the shallow nested case. stats is another record boundary; its total can still be deferred.
Run the targeted nested repair. We can follow the scalar dependency to next.stats.total because we know this state. A general interpreter accepts other programs' state too.
Programs choose the carried data; the interpreter cannot hard-code total. Explain its chosen deepSeq policy before exposing the key. Run the unchanged counter with deepSeq and show success. Ask what that policy costs when a program's state grows.
Add a program log to the working counter. One new history cell shares the old tail; the next addition still needs only total.
Compare demands on the extended record. deepSeq reaches the required total and also every log cell. The log is unrelated to the next addition.
Follow the oldest cell through all three traversals. Three new cells yield six visits. Evaluated values are shared, but structural visits still total N(N+1)/2 for this growing log.
Close the policy tradeoff: we know total matters for this counter; the interpreter accepts other state. Then introduce a different question about the terms the checker must process.
Explain Π in one sentence: a function type whose result may depend on its argument. Point down the nested bindings. Ask about comparing a type with 10000 binders.
Height means open Nix calls. Follow the comparison into nested bodies while earlier calls wait. The native driver cannot take another step until this operation returns.
Use add-one wrappers as a small model of nested execution. Construct the closure around previous before testing forcing and application.
Trace f3 to f2 to f1 to f0 as captured references. The final function has not received an argument.
Run deepSeq closureChain true. Let the successful result stand. Explain that true is deepSeq's second argument; the function has not received an argument. Pause before asking what happens when we supply zero.
Apply the closure chain and pause on the overflow. Each wrapper waits for its predecessor before adding one. This nesting is inside the callback, separate from the tail-recursive driver's problem.
Let Pikachu follow the overflow. Deeply nested terms belong to the intended input. The outer loop cannot control work hidden inside one call; introduce explicit pending work.
Lead with returning after one transition. Map the pending addition to an add frame and the previous links to frame order. Name defunctionalization after showing the mapping. The dispatcher must return between jobs.
Apply one frame to one value. The dispatcher performs one operation and returns; the driver, not the dispatcher, owns repetition.
Compare f3 0 with folding apply over the three frames: both return three. The fold regains control between scalar additions.
Trace zero with three frames through three with none. Then explain C: current expression, E: bindings, K: pending evaluator work. Return to the deep-type question. Connect the same control discipline to the other recursive walks in the checker.
Return to the nested Π type. Circles show the current comparison; the frame holds the body comparison, Π₂, while comparing A₁. Once the argument types match, the body comparison becomes current. Open both bodies with the same stand-in for x₁. Point to the returns to the driver, then continue at the next binder.
Pause on choosing the next transition. The evaluator was intentional; handling deep terms expanded the work required. Now return to the borrowed driver and states it retains.
Return to the counter with key and total. genericClosure retains earlier records. With machine states, old references can keep finished work reachable; frames may be shared. Explain references before the skeleton.
Reveal the skeleton after explaining retention. Pause. Waiting cannot help while the returned list keeps every visited record reachable, even when its fields are evaluated.
Trace the current state through a fold, then the recursive call to another chunk. Finished states pass through unchanged. The public implementation uses both drivers. Brief aside: the unpublished Lean rewrite generates a common bounded chunked-fold driver for evaluation, quotation and comparison. Its outer fold schedules the budget without recursive chunk calls, but still visits unused outer ticks. Return to the cost of supplying repetition.
Let the Thanos caption land. The interpreter has plenty of interesting problems; running its next step should be straightforward. Return to the original run function.
Return to the original tail-recursive run. The worklist collects states; the illustrated chunk driver leaves recursive calls; bounded nested folds visit their scheduled budget. Ask for the operation directly: take a step, carry its state forward, repeat until finished.
Present the call as an interface sketch. Native repetition removes the visited collection and recursion between chunks. Frames and forcing policy remain useful parts of interpreter design.
Start with familiar module declarations: the module system collects, merges and checks them under its own rules. That is interpretation. Move to interpreters that discover more work while running: they need state-dependent repetition, and can meet the same stack and memory constraints. Do not claim the module system has the demonstrated failure. Ask whether every DSL implementation should reconstruct a driver. Tail-call support is another route; forcing and representation remain design responsibilities.
Return to the opening subtitle: nearly losing my mind writing a loop in Nix. Pause, then point to examples and thank contributors. Keep project-status questions for Q&A; the unpublished rewrite accounts for much of the quieter public development.
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.