"""Provides components to build 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).*")


def build_output(path: str, output: str) -> str | None:
    """Builds a given output in a flake.

    :param path: path to flake
    :param output: flake output to be built
    :returns the .drv path on success or None on failure
    """
    logging.info(f"build {output}")
    out = bash_wrapper(f"nix build {path}#{output} -o {output}.nixoutput")
    logging.debug("output")
    logging.debug(out[0])
    logging.debug("error")
    logging.debug(out[1])
    logging.debug("statuscode")
    logging.debug(out[2])
    if out[2] != 0:
        logging.warning(f"output {output} did not build correctly")
        return None
    return ""