8000 Dicts can cross the wire: Nix attribute keys are strings · Issue #567 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

Dicts can cross the wire: Nix attribute keys are strings #567

Closed

Description

@Lillecarl

DONE. Closed before these files carried a status word. The .done
suffix is the record; this line is that same answer in the second place
the check reads, and no new judgement.

Carl, 2026-08-25: "All dict keys in Nix are strings."

Why that matters

gc_stats is the only binding function with no RPC surface, and the
build says why:

warning: gc_stats has no RPC surface - return type: dict has no
wire representation; the schema has no map or struct type yet

That blocker was written when the answer was unknown. It is known now.
An attribute set has string keys, so map<string, V> covers every dict
this API will ever return, and protobuf has had map fields since
proto3's first release.

It is not only gc_stats. An attribute set is the shape Nix evaluation
hands back for most things worth asking for, so an evaluation server
(016) that cannot put one on the wire is missing its main return type.

What it takes

  • _msg_arg_type learns a map case, keyed by string, valued by
    whatever the declared value type is.
  • The value type has to come from somewhere. dict alone says
    nothing, so the binding declares dict[str, int] the way gc_stats
    now does for the typechecker's benefit - which means the same
    annotation feeds both, and _annotation_name has to stop flattening
    a subscripted generic to its head.
  • wire_blocker keeps reporting an unparameterised dict, because
    that one genuinely cannot cross: the schema needs a value type.
  • The codec grows an encode/decode pair for maps, recursing into the
    value type the same way value_to_msg already recurses into a
    nested wire-value.

Watch out for

A map value that is itself a proxy. map<string, Handle> is
representable, but the lifetime rules for a handle arriving inside a
collection are not written down anywhere - each one is a lease, and
nothing currently grants leases in bulk. Start with scalar values and
say so.

Nested dicts. map<string, X> cannot hold another map directly in
proto3; it needs a wrapper message.

Decided 2026-08-25

Carl: a recursive Value message is the correct way to do it, not a
map of maps.

That settles the shape and makes the flat-map version a stepping
stone rather than the destination:

message NixValue {
  oneof v {
    string  s     = 1;
    sint64  i     = 2;
    bool    b     = 3;
    double  f     = 4;
    Handle  proxy = 5;   // a Value that stays remote
    NixList list  = 6;
    NixAttrs attrs = 7;  // map<string, NixValue>
  }
}

Which changes the order of work. A recursive message carrying a Handle
in one of its arms means proxies appear at arbitrary depth, so 031
(recursive handle tracking, and the identity-mapping that has to
precede it) is a prerequisite rather than a parallel concern.

It also means the proxy arm is where laziness lives: a thunk cannot
be serialized, so an unforced value crosses as a handle and the client
forces it with another call. That is the same value/proxy split the
whole design already rests on, one level deeper - and it is why this
message cannot be generated from a _wire_fields declaration the way
StorePath's is. It is recursive, and its arms are the wire kinds
themselves.

Done 2026-08-25: the flat map

dict[str, V] crosses as map<string, V>. gc_stats was the last
function with no RPC surface and now has one, proven end to end and
through grpcurl - which only has the descriptor this build emitted, so
a wrong synthesised entry message fails there even when it parses in
Python.

What it took, against the sketch above:

  • _annotation_name needed no change. It already renders a
    subscripted generic in full, so dict[str, int] reached the
    manifest intact and the emitter, which parses annotations with
    ast, already wrote it into the async form and the stub.
  • _msg_arg_type was the wrong place. proto3 spells a map as a
    repeated field of a message the containing type carries, so the
    builder has to CREATE something, not classify. _add_field does
    that and every field now goes through it.
  • wire_blocker reports what a map cannot do in its own words: a bare
    dict, a non-string key, a map of maps, a map of proxies.
  • The codec grew map_to_msg / map_from_msg beside the wire-value
    pair they mirror.

New: codegen/wiretypes.py, copied into the package as
_wiretypes.py the way _runtime.py is. Reading dict[str, int] as
a map is the one piece of knowledge the manifest cannot carry, because
it is about how an annotation is SPELLED rather than about the types.
The schema builder needs it at build time and the codec at run time,
so they read one definition instead of two.

Two things stay blocked on purpose, both waiting on the recursive
value message:

  • a map of maps. proto3 will not synthesise the wrapper.
  • a map of proxies. Every entry would be a lease, and nothing grants
    leases in bulk (Bind API for creating gcroots #31).

Done 2026-08-25: the mock has collections

A value can now be a list or an attribute set holding other values, so
a value is a tree and any node of it may still be a thunk. Built, not
parsed (Carl: "just builder methods, we don't want to reimplement
Nix"). Attributes live in a sorted array like nix::Bindings, because
Carl confirmed Nix attribute sets are alphabetical - so an index walk
IS the listing order and dict[str, V] is the right Python shape.

The binding surface is index-based and container-free:
size/at/name_at/value_at/has/get to read, make_list plus list_append
and make_attrs plus attrs_set to build. That is not a style choice.
The pxd declares this API to Cython, and a template type there maps to
nothing, so a declared surface is scalars and Value pointers.

Two things fell out of it:

  • the pxd parser answered "void" for any type it could not render, so
    a vector[string] return would have become None with nothing
    reporting it. Fixed and gated.
  • a RETURNED type producing another returned type was not adopted into
    its async wrapper. A Value that holds Values is the first thing with
    that shape, and the conformance gate named all three methods.

Was left: the recursive message

Value.attrs() -> dict[str, Value] and Value.items() -> list[Value]
are deliberately absent. They need a collection of PROXIES, which is
the NixValue message, and that message needs a decision this file
cannot make on its own:

What does a client ask for? The message shape is settled. The rpc
that carries it is not. A tree that forces everything can be unbounded
and can raise halfway down; a tree that forces nothing is a handle the
client already had. Somewhere between them is a Realize(handle, depth) that serializes what is forced, leaves a thunk as a proxy, and
stops at a depth the caller names.

And every proxy in that tree takes a lease. A realize over a large
attribute set hands the client hundreds of handles to release. The
client's finalizers do balance it (#28), but the round trip that
saved N calls costs N handles, which may argue for the depth limit
being small by default.

Done 2026-08-25: Realize

Carl settled the two open points. Trees are not that deep in practice
as long as a value is visited once, and Nix's own JSON printer already
declines to dig into a derivation. He also asked the sharper question:
is depth root-to-branch, or does sideways expansion count?

Sideways counts, and it is the one that bites. nixpkgs holds a
hundred thousand attributes one level down, so a depth bound says
nothing about the size of the answer. Realize takes both:

  • depth counts levels EXPANDED, 1 being the root alone. It says how
    much shape the caller wants. Zero would be the natural spelling for
    "the root alone" and proto3 cannot tell zero from unset, which is
    why it is 1-based;
  • budget is the hard stop, in nodes. It bounds the message and the
    lease count together, whatever shape the tree has.

Both frontiers answer the same way - a proxy - so a bounded answer, a
lazy one and a shared one all have one shape.

It forces NOTHING. What is already forced serializes; a thunk crosses
as a handle and the caller forces it with the rpc that already exists.
So the answer cannot raise halfway down a half-built message, and it
composes with force rather than duplicating it. Real Nix's
printValueAsJSON is the other choice: strict forces everything and is
bounded only by max-call-depth, and non-strict ERRORS on the first
thunk rather than handing one back. The proxy arm is what lets this
one do better than either.

Verified against upstream

src/libexpr/value-to-json.cc, in the source read earlier:

  • attributes serialize in lexicographicOrder, which is Carl's
    alphabetical point again;
  • an attribute set carrying outPath serializes AS that attribute -
    that is the "does not dig into derivations" rule. The mock has no
    analogue, because a Derivation here is its own proxy class rather
    than an attribute set. Real Nix will need it, and it wants to be a
    declaration next to the binding like the rest of the walk;
  • there is NO visit tracking upstream. A shared subtree is serialized
    once per occurrence, so Carl's suggestion is an improvement on
    upstream rather than a copy of it.

Two things that only showed up when it ran

id() is not identity. A binding builds a fresh Python wrapper per
access, so two wrappers over one value differ - and a wrapper that
dies hands its id() to the next one, which reads as "already seen"
and truncates a tree that was never visited. That is a live bug that
looked like a working feature: the first realize returned a proxy
where a list belonged. Identity is now declared (_tree["identity"]),
and only the binding can say where it lives.

Walking from the client is a call per node, and every one is a network
round trip plus a thread handover. BaseRunner.run submits one
function to the target's own executor, so the whole tree costs one
hop. call dispatches a method by name and cannot express that.

What the generator still does not know

Nothin 8000 g about values. The walk is declared next to the binding as
_tree - which accessor says what a node is, which reads each scalar
kind, how to reach list elements and attribute pairs, and what makes
two nodes the same node - and reaches the server through the manifest.
server.py names no type and no method, and the no-hardcoded-domain-
types gate still passes.

Also closed by it: a hole the same shape

Realize made it obvious that a collection of wrapped types has no
owner. dict[str, Value] as a method return type-checked, built,
emitted map<string, Handle> - and then handed back bare sync objects
in process while failing on the first call over the wire. Every layer
attaches a runner to ONE object: the async wrapper writes
AsyncX(result, self._runner), the server puts one handle, the client
builds one proxy. None of them walks a container.

check_collection_contract refuses it at build time and says what to
do instead. It is not an oversight to fix later: a collection of
remote objects IS a value tree, which is what Realize answers, and
that is protocol rather than something a return annotation can ask
for.

Finding it turned up a second inconsistency. _annotation_name used
__name__, which answers with the head of a subscripted generic, so
dict[str, int] became dict. Whether that happened depended on
which path resolved the annotation: a free function's stayed the
written string and kept its parameters, a method's resolved to a real
generic and lost them. The same declaration meant two different things
depending on where it appeared. gc_stats worked only because it is a
free function.

Migrated from tasks/030-dicts-on-the-wire.md.done.


Assisted-By: Opus 5

Activity

  1. added
    source: huggorm
    on Oct 19, 2026
  2. transferred this issue fromnixidae/huggormon Oct 19, 2026
  3. added theissue type on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    source: huggorm

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0