lib¶
Reusable functions for parsing @nix protocol and running nix commands.
ArchChecks
dataclass
¶
Checks discovered for a single architecture.
Attributes:
| Name | Type | Description |
|---|---|---|
arch |
str
|
Architecture identifier (e.g. |
checks |
list[CheckResult]
|
One CheckResult per check in this arch. |
error |
str | None
|
Error message if the entire arch eval failed, else None. |
Source code in src/junix/lib.py
CheckResult
dataclass
¶
Result of evaluating a single check.
Attributes:
| Name | Type | Description |
|---|---|---|
arch |
str
|
Architecture identifier (e.g. |
name |
str
|
Check name (e.g. |
drv_path |
str | None
|
Derivation path (or |
error |
str | None
|
Error message (or |
Source code in src/junix/lib.py
EvalResult
dataclass
¶
Result of evaluating flake checks across architectures.
Attributes:
| Name | Type | Description |
|---|---|---|
arches |
list[ArchChecks]
|
Per-architecture results, one entry per evaluated arch. |
path |
str
|
Normalised flake path used for building URIs. |
Source code in src/junix/lib.py
all_attrs
property
¶
Flatten all successful check attrs across all arches.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of flake attribute URIs (e.g. |
has_errors
property
¶
Whether any arch evaluation failed.
Returns:
| Type | Description |
|---|---|
bool
|
True if at least one arch has an error. |
_apply_color_mode(mode)
¶
Apply the resolved color mode to plumbum's ANSIStyle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
One of |
required |
Source code in src/junix/lib.py
_attribute_eval_time(arches, eval_elapsed, re_eval_seconds)
¶
Split the eval phase wall-clock across per-arch suite times.
Failing checks triggered a per-check nix eval --show-trace
round trip; those round trips are measured and assigned to the
arch where the check lives. The remaining bulk-eval time is
split evenly across the healthy checks in all arches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arches
|
list[ArchChecks]
|
Per-architecture check lists, post-recovery. |
required |
eval_elapsed
|
float
|
Wall-clock of the bulk |
required |
re_eval_seconds
|
dict[tuple[str, str], float]
|
Map of |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Per-arch seconds to assign to that arch's eval suite |
dict[str, float]
|
attribute. |
Source code in src/junix/lib.py
_current_nix_system()
cached
¶
Return the Nix system identifier for the current host machine.
Delegates to nix eval --impure --json --expr builtins.currentSystem.
The result is cached because it never changes during a process lifetime.
Returns:
| Type | Description |
|---|---|
str
|
Nix system string (e.g. |
Source code in src/junix/lib.py
_ensure_builds(builds, process_successful, default_name='nix build', expected_count=0, expected_names=None, expected_pairs=None, unbuildable_drv_paths=None, real_failure_drv_paths=None)
¶
Ensure the build list has at least expected_count entries.
Nix only emits actBuild protocol activities when it actually
compiles something. Cached/substituted builds produce no actBuild
events, so the build list may be shorter than the number of
attributes requested. This helper pads with synthetic skipped
builds so the JUnit report reflects every requested attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
builds
|
list[BuildEvent]
|
Collected build events from the protocol handler. |
required |
process_successful
|
bool
|
Whether the overall Nix process succeeded. |
required |
default_name
|
str
|
Name to use for the synthetic build when no builds exist and no expected_count is given. |
'nix build'
|
expected_count
|
int
|
Minimum number of builds to return. When 0 (default), at least one build is guaranteed. |
0
|
expected_names
|
list[str] | None
|
Names for synthetic entries. When provided,
padded entries use these names instead of |
None
|
expected_pairs
|
list[tuple[str, str]] | None
|
|
None
|
unbuildable_drv_paths
|
set[str] | None
|
Drv paths that Nix explicitly reported
as "Cannot build" (either real failure or dep-failed).
Synthetic entries whose |
None
|
real_failure_drv_paths
|
list[str] | None
|
Drv paths of builds that failed for
their own reasons (e.g. builder exit code, output
rejected). When |
None
|
Returns:
| Type | Description |
|---|---|
list[BuildEvent]
|
Build list padded to at least |
Source code in src/junix/lib.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | |
_eval_check_trace(path, arch, name)
async
¶
Re-evaluate a single failing check to capture its Nix error trace.
builtins.tryEval swallows the error message, so the bulk
nix eval --json ...#checks call only ever reports a generic
"evaluation failed" placeholder for failing checks. This helper
runs nix eval --show-trace <path>#checks.<arch>.<name>.drvPath
and returns the stderr verbatim so the JUnit report can show the
real trace to the user. The round-trip wall-clock is also returned
so the CLI can include it in the per-arch suite time — this is
the only honest per-check timing available, since @nix carries
no timestamps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Flake path the user passed (e.g. |
required |
arch
|
str
|
Architecture (e.g. |
required |
name
|
str
|
Check name (e.g. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Tuple of (Nix error trace or None, elapsed_seconds). The trace |
float
|
is the stderr stripped, or |
tuple[str | None, float]
|
produced no useful output. Callers should keep the original |
tuple[str | None, float]
|
bulk error in that case. |
Source code in src/junix/lib.py
_overall_success(builds)
¶
Return True when every build completed successfully.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
builds
|
list[BuildEvent]
|
List of build events to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if all builds stopped and succeeded. |
Source code in src/junix/lib.py
_parse_nix_stderr(stderr, handler, log_level=0)
¶
Feed stderr lines to handler, forwarding non-@nix lines to stderr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stderr
|
str
|
Raw stderr text from a nix invocation. |
required |
handler
|
NixEventHandler
|
The event handler to feed |
required |
log_level
|
int
|
Maximum Nix log level to forward to stderr (default 0 = errors only; higher = more verbose). |
0
|
Source code in src/junix/lib.py
_phase_count(events, overall_success, label)
¶
Build the "N evals" or "F/T evals" fragment for one phase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
list[BuildEvent]
|
Phase's BuildEvents. |
required |
overall_success
|
bool
|
Whether everything passed. |
required |
label
|
str
|
The phase label ( |
required |
Returns:
| Type | Description |
|---|---|
str
|
A string like |
Source code in src/junix/lib.py
_print_build_list(builds, downloaded=0, header=None)
¶
Print the downloaded line, a header (if any), and one line per build.
Used by subcommands that have no per-arch sections (i.e. build).
For multi-arch flows (check), the per-arch :func:_print_build_results
and :func:_print_eval_results already print the listing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
builds
|
list[BuildEvent]
|
Build events to list individually. |
required |
downloaded
|
int
|
Number of paths downloaded from binary caches. |
0
|
header
|
str | None
|
Optional info line to print before the listing, e.g.
|
None
|
Source code in src/junix/lib.py
_print_build_results(arch, build_builds, color_mode='auto')
¶
Print build phase results for one architecture.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arch
|
str
|
Architecture identifier. |
required |
build_builds
|
list[BuildEvent]
|
Build events (real + synthetic) for this arch. |
required |
color_mode
|
str
|
One of |
'auto'
|
Source code in src/junix/lib.py
_print_check_summary(evals=None, builds=None, overall_success=True, color_mode='auto')
¶
Print the final summary line used by every junix subcommand.
The format is consistent across commands: PASSED (...) or
FAILED (...) with separate counts for evals and builds. Each
phase is only mentioned if it has entries, in the order
evals, builds.
The per-phase listing (with ✓/✗/(cached)) is the caller's
responsibility — check uses :func:_print_eval_results and
:func:_print_build_results per arch, and build uses
:func:_print_build_list directly. This keeps the visual layout
consistent with how each subcommand presents its per-arch sections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
evals
|
list[BuildEvent] | None
|
Synthetic BuildEvents from the eval phase. Pass an empty
list (or |
None
|
builds
|
list[BuildEvent] | None
|
BuildEvents from the build phase. Pass an empty list
(or |
None
|
overall_success
|
bool
|
Whether everything passed. Drives the PASS/FAIL prefix. |
True
|
color_mode
|
str
|
One of |
'auto'
|
Source code in src/junix/lib.py
_print_eval_results(eval_result, color_mode='auto')
¶
Print eval phase results in pytest style.
Each check is printed with the exact nix eval command that would
reproduce it, so CI failures are copy-pasteable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eval_result
|
EvalResult
|
The result from discover_checks(). |
required |
color_mode
|
str
|
One of |
'auto'
|
Source code in src/junix/lib.py
_rename_builds(builds_list, attrs, drv_to_name, expected_names, expected_drv_paths)
¶
Rename actBuild events and build the expected (name, drv) pairs.
The renames happen on the BuildEvent objects in place
(mutating b.name) so _ensure_builds sees the right names
and does not create duplicate synthetic entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
builds_list
|
list[BuildEvent]
|
Collected |
required |
attrs
|
list[str]
|
Original attrs the user passed to |
required |
drv_to_name
|
dict[str, str] | None
|
Caller-supplied drv_path → name map (e.g. from
|
required |
expected_names
|
list[str] | None
|
User-facing testcase names parallel to
|
required |
expected_drv_paths
|
list[str] | list[str | None] | None
|
Real |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str]]
|
|
list[tuple[str, str]]
|
|
list[tuple[str, str]]
|
entries stripped out (since those attrs have no drv path to |
list[tuple[str, str]]
|
match against |
list[tuple[str, str]]
|
resolved drv paths are available, so the caller falls back |
list[tuple[str, str]]
|
to positional pairing. |
Source code in src/junix/lib.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 | |
_resolve_color_mode(cli_value)
¶
Resolve the effective color mode from the CLI value.
This is a no-op pass-through: the precedence (CLI > JUNIX_COLOR >
"auto") is handled by the --color switch's default= in
cli.py, which feeds the resolved value here. This function exists
only for historical symmetry with the call sites.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cli_value
|
str | None
|
The raw CLI value (one of |
required |
Returns:
| Type | Description |
|---|---|
str
|
The resolved color mode, always one of |
Source code in src/junix/lib.py
_resolve_drv_paths(attrs)
async
¶
Resolve the drv path of each requested attr via nix eval.
Used by junix build so run_nix_build can pair
actBuild events with the user's attrs by drv path (more
reliable than positional pairing) and mark dep-failed targets as
failures instead of cached/skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
list[str]
|
Flake attribute paths the user passed to
|
required |
Returns:
| Type | Description |
|---|---|
list[str | None] | None
|
List parallel to |
list[str | None] | None
|
(resolved successfully) or |
list[str | None] | None
|
attr — e.g. not a flake attr, or no |
list[str | None] | None
|
|
list[str | None] | None
|
|
Source code in src/junix/lib.py
_write_report(xml, output_path)
¶
Write the JUnit XML to output_path or stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml
|
str
|
The JUnit XML string to write. |
required |
output_path
|
str | None
|
Path to write to, or None for stdout. |
required |
Source code in src/junix/lib.py
discover_checks(path, eval_arch=None, on_trace_recovery=None)
async
¶
Discover flake check attribute URIs for path.
Uses a single nix eval invocation with --apply to evaluate
each check with builtins.tryEval, returning a list of objects
with arch, name, drvPath and error fields. When
eval_arch is not provided, all architectures are returned.
Per-arch wall-clock attribution (seconds) is returned for the
CLI to populate <testsuite time="...">. Each arch's value
includes the wall-clock of any per-check nix eval --show-trace
round trips fired for failing checks plus a proportional share
of the bulk nix eval for its healthy checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Flake path (e.g. |
required |
eval_arch
|
list[str] | None
|
Architectures to evaluate (default: all). |
None
|
on_trace_recovery
|
Callable[[str], None] | None
|
Optional callback invoked once per failing
check just before the second |
None
|
Returns:
| Type | Description |
|---|---|
EvalResult
|
Tuple of (EvalResult, eval_elapsed_per_arch). The |
dict[str, float]
|
|
tuple[EvalResult, dict[str, float]]
|
tuple element is a |
Source code in src/junix/lib.py
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 | |
discover_checks_individually(path, eval_arch=None, on_progress=None)
async
¶
Discover flake checks by running one nix eval per check.
The bulk :func:discover_checks evaluates every check × arch of
the flake in a single Nix process, which is memory-efficient for
small flakes but blows the RAM budget of CI runners on large ones
(e.g. flakes that pull in Odoo's full closure across four
architectures).
This helper trades that for per-check isolation: each check
runs in its own nix eval <path>#checks.<arch>.<name>.drvPath
process, so the RAM peak per process is bounded by the closure
of that single check — not the sum of all checks. Cost: one Nix
process per check (~hundreds of milliseconds each), so a flake
with hundreds of checks pays noticeable wall-clock overhead. Use
this only when the bulk path OOMs.
Algorithm (cheap listing, then per-check eval):
- List arches. One cheap
nix eval --json <path>#checks --apply builtins.attrNamesto enumerate the flake's check architectures. Skipped ifeval_archis provided by the caller. - List checks per arch. One
nix eval --json <path>#checks.<arch> --apply builtins.attrNamesper arch. This is the second-cheapest Nix call (just attribute names, no closure evaluation). - Per-check drvPath. One
nix eval --raw <path>#checks.<arch>.<name>.drvPathper check. This is the step that actually loads the check's closure — but each call only sees its own check.
Each per-check call surfaces the real Nix error trace in stderr on
failure, so unlike the bulk path no second nix eval --show-trace
round trip is needed to recover the trace.
Total nix eval invocations: 1 + A + A·C (no -e) or
A + A·C (-e given), where A = arches, C = checks
per arch. Bulk path: 1 + F (bulk + per-failing-check
trace). Per-check path is slower in wall-clock but bounded in
memory by the largest single check.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Flake path (e.g. |
required |
eval_arch
|
list[str] | None
|
Architectures to evaluate (default: all). When provided, the arch-listing step is skipped. |
None
|
on_progress
|
Callable[[str], None] | None
|
Optional callback invoked once per check just
before its per-check eval fires, receiving
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
EvalResult
|
Tuple of (EvalResult, eval_elapsed_per_arch) — same shape |
|
as |
dict[str, float]
|
func: |
tuple[EvalResult, dict[str, float]]
|
(suite structure, JUnit, summary) is unchanged. |
Source code in src/junix/lib.py
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 | |
print_summary(builds, downloaded=0, color_mode='auto')
¶
Print a human-readable, color-coded summary of build results to stderr.
Output includes the number of paths downloaded from cache, cached builds, passed builds, and failed builds. On failure the names of failing builds are listed individually.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
builds
|
list[BuildEvent]
|
List of build events to summarize. |
required |
downloaded
|
int
|
Number of paths downloaded from binary caches. |
0
|
color_mode
|
str
|
One of |
'auto'
|
Source code in src/junix/lib.py
run_nix_build(attrs, store=None, default_name='nix build', log_level=0, expected_count=0, expected_names=None, expected_drv_paths=None, drv_to_name=None, extra_flags=None)
async
¶
Run nix build and return collected build events.
Uses --keep-going so Nix attempts every attribute even if some
fail. Cached/substituted builds produce no actBuild protocol
events; when expected_count is set, the report is padded with
synthetic skipped entries so every requested attribute is accounted
for.
Per-build wall-clock duration is measured by
NixEventHandler between each actBuild start and
stop event as the protocol stream is consumed, so each
returned BuildEvent.duration reflects the real build
wall-clock. The <testsuite time="..."> and
<testcase time="..."> attributes in the JUnit report come
from those per-build measurements (summed for the suite total).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
list[str]
|
Flake attribute paths to build (e.g. |
required |
store
|
str | None
|
Remote store URL (e.g. |
None
|
default_name
|
str
|
Name for the synthetic build when no |
'nix build'
|
log_level
|
int
|
Maximum Nix log level to forward to stderr (default 0 = errors only; higher = more verbose). |
0
|
expected_count
|
int
|
Minimum number of builds to report. When Nix caches some builds they produce no protocol events, so this pads the report with synthetic skipped entries. |
0
|
expected_names
|
list[str] | None
|
Names for synthetic entries. When provided,
padded entries use these names instead of |
None
|
expected_drv_paths
|
list[str] | list[str | None] | None
|
Real |
None
|
drv_to_name
|
dict[str, str] | None
|
Mapping from drv_path to check name. When provided, build events are renamed before padding so that synthetic entries don't duplicate real ones. |
None
|
extra_flags
|
list[str] | None
|
Extra flags to pass to |
None
|
Returns:
| Type | Description |
|---|---|
list[BuildEvent]
|
Tuple of (builds, error_messages, exit_code, downloaded_count). |
list[str]
|
|
int
|
binary caches during this build. |
Source code in src/junix/lib.py
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 | |
translate_stream(stream, suite_name='nix', process_successful=False, default_name='nix build', log_level=0, color_mode='auto')
¶
Read @nix JSON lines from stream and produce JUnit XML.
Non-@nix lines and Nix log messages are forwarded to stderr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
IO[str]
|
Text stream to read (e.g. sys.stdin). |
required |
suite_name
|
str
|
Value for the |
'nix'
|
process_successful
|
bool
|
Passed to NixEventHandler.finalize. |
False
|
default_name
|
str
|
Name for the synthetic build when no |
'nix build'
|
log_level
|
int
|
Maximum Nix log level to forward to stderr (default 0 = errors only; higher = more verbose). |
0
|
color_mode
|
str
|
One of |
'auto'
|
Returns:
| Type | Description |
|---|---|
tuple[str, int]
|
Tuple of (xml_string, exit_code). |