Mika Bohinen / NixCon 2026

Worked examples

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.