dotfiles/common/update-local-flakes/update-local-flakes.py

63 lines
1.8 KiB
Python
Raw Permalink Normal View History

2024-01-07 12:54:43 +01:00
import argparse
import json
import os
import subprocess
GET_INPUTS_CMD = [
"nix-instantiate",
"--eval",
"--json", # This parser is stupid, better provide it with pre-eaten stuff
"--expr",
"builtins.fromJSON (builtins.toJSON (import ./flake.nix).inputs)",
]
def process_flake(flakeUri: str) -> None:
2024-01-07 12:54:43 +01:00
# get full path
flakeUri = os.path.normpath(flakeUri)
flakeFile = os.path.join(flakeUri, "flake.nix")
if not os.path.isfile(flakeFile):
raise FileNotFoundError(f"Flake not found: {flakeUri}")
2024-01-07 12:54:43 +01:00
# import dependencies
p = subprocess.run(GET_INPUTS_CMD, cwd=flakeUri, stdout=subprocess.PIPE)
2024-01-07 12:54:43 +01:00
deps = json.loads(p.stdout)
p.check_returncode()
# for each dependency
for dep_name, dep in deps.items():
dep_url = dep["url"]
# if not local path, continue
if not (
2024-01-07 22:38:42 +01:00
dep_url.startswith("path:")
or dep_url.startswith("git+file:")
2024-01-07 12:54:43 +01:00
):
continue
if dep.get("flake", True):
# get flake file corresponding
dep_path = dep_url.split(":")[1]
if not dep_path.startswith("/"):
dep_path = os.path.join(flakeUri, dep_path)
process_flake(dep_path)
2024-01-07 12:54:43 +01:00
# update lockfile
2024-01-07 19:37:06 +01:00
cmd = [
"nix",
"--extra-experimental-features",
"nix-command",
"--extra-experimental-features",
"flakes",
"flake",
2024-05-08 13:24:03 +02:00
"update",
2024-01-07 19:37:06 +01:00
dep_name,
]
p = subprocess.run(cmd, cwd=flakeUri)
2024-02-18 14:12:32 +01:00
p.check_returncode()
2024-01-07 12:54:43 +01:00
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Recursively update lockfiles "
"of flakes located on the system"
)
parser.add_argument("flake", help="Starting flake", default="/")
2024-01-07 12:54:43 +01:00
args = parser.parse_args()
process_flake(args.flake)