Skip to content

Transpose

transpose swaps the outer two levels of a nested attribute set:

transpose { a.b.c = 1; }
# ⇒ { b.a.c = 1; }

Values below the second level are preserved as-is. Common children merge under one parent:

transpose { a.x = 1; b.x = 2; }
# ⇒ { x = { a = 1; b = 2; }; }

The implementation is a three-phase pipeline:

  1. DeconstructmapAttrsToList over all parents, producing { child, parent, value } items via emit.
  2. Flatten — collapse the nested lists into a flat list.
  3. Reconstructfoldl items back into the transposed structure.

emit is a function { child, parent, value } → [{ parent, child, value }].

Default is lib.singleton — identity transformation, one-to-one mapping.

You can use emit to:

  • Filter: return [] to drop items during transposition.
  • Modify: change the value, rename parent/child.
  • Multiply: return multiple items from a single input.

nix/aspects.nix supplies a custom emit that calls resolve on each item:

emit = transposed: [{
inherit (transposed) parent child;
value = aspects.${transposed.child}.resolve {
class = transposed.parent;
};
}];

This intercepts every <aspect>.<class><class>.<aspect> transposition and replaces the raw value with a fully-resolved module that includes all transitive dependencies.

Contribute Community Sponsor