2025-03-03 17:19:12 -05:00
|
|
|
"""Provides components to evaluate nix components and process the result."""
|
|
|
|
|
2025-03-03 11:18:44 -05:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-08-02 09:13:06 -04:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
|
2025-03-03 17:19:12 -05:00
|
|
|
from flupdt.common import bash_wrapper
|
|
|
|
|
2024-08-03 00:21:44 -04:00
|
|
|
drv_re = re.compile(r".*(/nix/store/.*\.drv).*")
|
2024-08-02 09:13:06 -04:00
|
|
|
|
2024-08-03 00:21:44 -04:00
|
|
|
|
2025-03-03 17:19:12 -05:00
|
|
|
def evaluate_output(path: str, output: str) -> str | None:
|
|
|
|
"""Evaluates a given output in a flake.
|
|
|
|
|
|
|
|
:param path: path to flake
|
|
|
|
:param output: flake output to be evaluated
|
|
|
|
:returns the .drv path on success or None on failure
|
|
|
|
:raises RuntimeError: evaluation succeeds but no derivation is found
|
|
|
|
"""
|
2024-08-02 09:13:06 -04:00
|
|
|
logging.info(f"evaluating {output}")
|
|
|
|
out = bash_wrapper(f"nix eval {path}#{output}")
|
|
|
|
logging.debug(out[0])
|
|
|
|
logging.debug(out[1])
|
|
|
|
logging.debug(out[2])
|
|
|
|
if out[2] != 0:
|
|
|
|
logging.warning(f"output {output} did not evaluate correctly")
|
|
|
|
return None
|
2025-03-03 17:19:12 -05:00
|
|
|
drv_match = drv_re.match(out[0])
|
|
|
|
if drv_match is None:
|
|
|
|
out_msg = "derivation succeeded but output derivation does not contain a derivation"
|
|
|
|
raise RuntimeError(out_msg)
|
|
|
|
drv = drv_match.group(1)
|
|
|
|
logging.debug(f"derivation evaluated to {drv}")
|