lib¶
Reusable functions for parsing @nix protocol and running nix commands.
_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)
¶
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
|
Returns:
| Type | Description |
|---|---|
list[BuildEvent]
|
Build list padded to at least |
Source code in src/junix/lib.py
_normalize_flake_path(path)
¶
Normalize a flake path so Nix can resolve it.
Nix requires relative filesystem paths to start with ./ or /.
Bare paths like tests/sample-flake are not recognised as flake
references. This helper prepends ./ when the path looks like a
relative filesystem path (no scheme, not already prefixed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Raw flake path from the user (e.g. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Normalised path suitable for |
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
_setup_colors()
¶
Configure ANSI color output based on stderr and NO_COLOR.
Disables color when stderr is not a TTY or when the NO_COLOR
environment variable is set (per https://no-color.org/).
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)
async
¶
Discover flake check attribute URIs for path.
Uses nix flake show --json and filters by eval_arch
(default: current system only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Flake path (e.g. |
required |
eval_arch
|
list[str] | None
|
Architectures to evaluate (default: current system). |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of flake attribute URIs (e.g. |
Source code in src/junix/lib.py
print_summary(builds, downloaded=0)
¶
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
|
Source code in src/junix/lib.py
run_nix_build(attrs, suite_name='nix', store=None, default_name='nix build', log_level=0, expected_count=0)
async
¶
Run nix build and produce JUnit XML from its @nix protocol output.
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
list[str]
|
Flake attribute paths to build (e.g. |
required |
suite_name
|
str
|
Value for the |
'nix'
|
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
|
Returns:
| Type | Description |
|---|---|
tuple[str, int]
|
Tuple of (xml_string, exit_code). |
Source code in src/junix/lib.py
translate_stream(stream, suite_name='nix', process_successful=False, default_name='nix build', log_level=0)
¶
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
|
IOBase
|
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
|
Returns:
| Type | Description |
|---|---|
tuple[str, int]
|
Tuple of (xml_string, exit_code). |