From bc5534def37c702ad09da57f0f5b4fd5c09f7783 Mon Sep 17 00:00:00 2001 From: ahuston-0 Date: Sun, 28 Jul 2024 02:59:40 -0400 Subject: [PATCH] add nix-flake-show utils Signed-off-by: ahuston-0 --- app/common.py | 39 +++++++++++++++++++++++++++++++++++++++ app/flake_show.py | 29 +++++++++++++++++++++++++++++ main.py | 5 +++++ 3 files changed, 73 insertions(+) create mode 100644 app/common.py create mode 100644 app/flake_show.py create mode 100644 main.py diff --git a/app/common.py b/app/common.py new file mode 100644 index 0000000..aad88b1 --- /dev/null +++ b/app/common.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +"""common.""" + +import logging +import sys +from subprocess import PIPE, Popen + + +def configure_logger(level: str = "INFO") -> None: + """Configure the logger. + + Args: + level (str, optional): The logging level. Defaults to "INFO". + """ + logging.basicConfig( + level=level, + datefmt="%Y-%m-%dT%H:%M:%S%z", + format="%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s", + handlers=[logging.StreamHandler(sys.stdout)], + ) + + +def bash_wrapper(command: str, path:str = '.') -> tuple[str, int]: + """Execute a bash command and capture the output. + + Args: + command (str): The bash command to be executed. + path (str): The current working directory, '.' by default + + Returns: + Tuple[str, int]: A tuple containing the output of the command (stdout) as a string, + the error output (stderr) as a string (optional), and the return code as an integer. + """ + # This is a acceptable risk + process = Popen(command.split(), stdout=PIPE, stderr=PIPE,cwd = path) # noqa: S603 + output, _ = process.communicate() + + return output.decode(), process.returncode diff --git a/app/flake_show.py b/app/flake_show.py new file mode 100644 index 0000000..abfa6a0 --- /dev/null +++ b/app/flake_show.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import json +from app.common import bash_wrapper +import shutil + +def traverse_json_base(json_dict:dict ,path: list[str]): + final_paths = [] + for (key,value) in json_dict.items(): + if isinstance(value,dict): + keys = value.keys() + if 'type' in keys and value['type'] in ['nixos-configuration','derivation']: + final_paths += ['.'.join(path + [key])] + else: + final_paths += traverse_json_base(value,path+[key]) + return final_paths + + +def traverse_json(json_dict: dict): + return traverse_json_base(json_dict,[]) + +def get_derivations(path_to_flake: str): + nix_path = shutil.which("nix") + flake_show = bash_wrapper(f"{nix_path} flake show --json", path = path_to_flake) + if flake_show[1] != 0: + raise RuntimeError("flake show returned non-zero exit code") + flake_show_json = json.loads(flake_show[0]) + derivations = traverse_json(flake_show_json) + return derivations diff --git a/main.py b/main.py new file mode 100644 index 0000000..1a85479 --- /dev/null +++ b/main.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 + +from app.flake_show import get_derivations + +print(get_derivations("/home/alice/.gitprojects/nix-dotfiles"))