flake-update-diff/flupdt/flake_eval.py
ahuston-0 607f3f9f76
All checks were successful
Check Nix flake / Perform Nix flake checks (ubuntu-latest) (pull_request) Successful in 3m26s
Check flake.lock / Check health of `flake.lock` (pull_request) Successful in 20s
Check Nix flake / Build nix outputs (ubuntu-latest) (pull_request) Successful in 2m21s
Check Nix formatting / Perform Nix format checks (pull_request) Successful in 1m41s
exit on failure, add per-class logging
Signed-off-by: ahuston-0 <aliceghuston@gmail.com>
2025-03-26 11:24:16 -04:00

39 lines
1.1 KiB
Python

"""Provides components to evaluate nix components and process the result."""
from __future__ import annotations
import logging
import re
from flupdt.common import bash_wrapper
drv_re = re.compile(r".*(/nix/store/.*\.drv).*")
logger = logging.getLogger(__name__)
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
"""
logger.info(f"evaluating {output}")
out = bash_wrapper(f"nix eval {path}#{output}")
logger.debug(out[0])
logger.debug(out[1])
logger.debug(out[2])
if out[2] != 0:
logger.warning(f"output {output} did not evaluate correctly")
return None
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)
logger.debug(f"derivation evaluated to {drv}")
return drv