2024-07-28 02:59:40 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import json
|
2024-07-29 18:29:54 -04:00
|
|
|
from flupdt.common import bash_wrapper
|
2024-07-28 02:59:40 -04:00
|
|
|
import shutil
|
|
|
|
|
2024-07-28 14:15:06 -04:00
|
|
|
|
|
|
|
def traverse_json_base(json_dict: dict, path: list[str]):
|
2024-07-28 02:59:40 -04:00
|
|
|
final_paths = []
|
2024-07-28 14:15:06 -04:00
|
|
|
for key, value in json_dict.items():
|
|
|
|
if isinstance(value, dict):
|
2024-07-28 02:59:40 -04:00
|
|
|
keys = value.keys()
|
2024-07-28 14:15:06 -04:00
|
|
|
if "type" in keys and value["type"] in [
|
|
|
|
"nixos-configuration",
|
|
|
|
"derivation",
|
|
|
|
]:
|
|
|
|
final_paths += [".".join(path + [key])]
|
2024-07-28 02:59:40 -04:00
|
|
|
else:
|
2024-07-28 14:15:06 -04:00
|
|
|
final_paths += traverse_json_base(value, path + [key])
|
2024-07-28 02:59:40 -04:00
|
|
|
return final_paths
|
|
|
|
|
|
|
|
|
|
|
|
def traverse_json(json_dict: dict):
|
2024-07-28 14:15:06 -04:00
|
|
|
return traverse_json_base(json_dict, [])
|
|
|
|
|
2024-07-28 02:59:40 -04:00
|
|
|
|
|
|
|
def get_derivations(path_to_flake: str):
|
|
|
|
nix_path = shutil.which("nix")
|
2024-07-28 14:15:06 -04:00
|
|
|
flake_show = bash_wrapper(f"{nix_path} flake show --json", path=path_to_flake)
|
2024-07-28 02:59:40 -04:00
|
|
|
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
|