protocol¶
Parse the @nix JSON protocol emitted by --log-format internal-json.
The protocol is a line-based JSON stream where each line starts with
@nix followed by a JSON object.
resFileLinked = 100
resBuildLogLine = 101
resUntrustedPath = 102
resCorruptedPath = 103
resSetPhase = 104
resProgress = 105
resSetExpected = 106
resPostBuildLogLine = 107
Built = 0
Substituted = 1
AlreadyValid = 2
PermanentFailure = 3
InputRejected = 4
OutputRejected = 5
TransientFailure = 6
CachedFailure = 7
TimedOut = 8
MiscFailure = 9
DependencyFailed = 10
LogLimitExceeded = 11
NotDeterministic = 12
ResolvesToAlreadyValid = 13
NoSubstituters = 14
BuildEvent
dataclass
¶
Collected information about one derivation build.
Source code in src/junix/protocol.py
cached = False
class-attribute
instance-attribute
¶
Whether the build was served from cache (substituted / already-valid).
True when Nix did not actually compile anything.
drv_path = ''
class-attribute
instance-attribute
¶
Store path of the derivation being built.
log_lines = field(default_factory=list)
class-attribute
instance-attribute
¶
Build log lines captured during the build.
name = ''
class-attribute
instance-attribute
¶
Human-readable name derived from drv_path.
phase = ''
class-attribute
instance-attribute
¶
Last reported build phase (e.g. unpackPhase, buildPhase).
result_code = None
class-attribute
instance-attribute
¶
Build result code (0=Built, 1=Substituted, 2=AlreadyValid, etc.).
None when not received (unknown / not reported by protocol).
started = False
class-attribute
instance-attribute
¶
Whether the activity start event was received.
stopped = False
class-attribute
instance-attribute
¶
Whether the activity stop event was received.
success = True
class-attribute
instance-attribute
¶
Whether the build completed successfully.
Defaults to True and set to False only if a failure is detected (e.g. process exits non-zero and build never stopped).
NixEventHandler
¶
Consume @nix protocol lines and produce a list of BuildEvent objects.
Typical usage:
handler = NixEventHandler()
for line in source:
handler.handle_line(line)
handler.finalize(process_successful=True)
for build in handler.builds:
...
Source code in src/junix/protocol.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 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 | |
builds
property
¶
Return the collected build events.
Call finalize() first to properly close any in-flight builds.
downloaded_count
property
¶
Return the number of paths that were downloaded from cache.
Counts actCopyPath (type 100) start events, which Nix emits
for each individual path it downloads from a binary cache.
_mark_failed_from_error(error_msg)
¶
Parse a build error message and mark the matching build as failed.
Nix error messages for build failures contain the derivation path::
error: Cannot build '/nix/store/xxx-yyy.drv'.
We extract the derivation path and mark the corresponding build
activity as failed, since the stop event for builds does not
carry the result code in Nix 2.34.x.
Source code in src/junix/protocol.py
_strip_ansi(text)
staticmethod
¶
Remove ANSI escape sequences from text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text that may contain ANSI escape codes. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Clean text with ANSI escapes removed. |
Source code in src/junix/protocol.py
finalize(process_successful=True)
¶
Mark any builds that never received a stop as failed.
Call this once the input stream is exhausted. If the overall Nix process failed (process_successful=False), any unstopped build is marked as a failure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process_successful
|
bool
|
Whether the overall Nix process succeeded. |
True
|
Source code in src/junix/protocol.py
get_messages(threshold=0)
¶
Return log messages whose level is at or below the threshold.
Nix log levels (lower = more important):
0 = lvlError
1 = lvlWarn
2 = lvlNotice
3 = lvlInfo
4 = lvlTalkative
5 = lvlChatty
6 = lvlDebug
7 = lvlVomit
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
int
|
Maximum level to include (default 0 = errors only). |
0
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of message strings at or below the threshold. |
Source code in src/junix/protocol.py
handle_line(line)
¶
Process one line of text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line
|
str
|
A single line of text (without trailing newline). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the line was a recognised |
bool
|
(caller should forward it to stderr). |
Source code in src/junix/protocol.py
handle_stream(stream)
¶
Read an entire text stream (e.g. stdin) line by line.
Non-@nix lines are collected and returned so the caller can
forward them to stderr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
IO[str]
|
A text-mode iterable (e.g. sys.stdin). |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of lines that did not start with |
Source code in src/junix/protocol.py
_Activity
dataclass
¶
An in-flight protocol activity being tracked.
Source code in src/junix/protocol.py
result_code = None
class-attribute
instance-attribute
¶
Build result code captured from the stop event fields.
store_path_to_name(store_path)
¶
Extract the human-readable derivation name from a Nix store path.
/nix/store/<hash>-<name>[-<version>] -> <name>[-<version>]
/nix/store/<hash>-<name>.drv -> <name>.drv
Uses the same logic as Nix's storePathToName().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store_path
|
str
|
A Nix store path (e.g. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The human-readable name portion of the path. |