]> git.scottworley.com Git - nix-pin-deps/blobdiff - nix_pin_deps.py
nix show-derivation → nix derivation show
[nix-pin-deps] / nix_pin_deps.py
index 2b88634f687f97044170bb9e72c799cc77fa2836..0b83d5c189fed1198cd4abc67725e4f6c7b57e13 100644 (file)
@@ -1,3 +1,10 @@
+# nix-pin-deps: gc-pin dependencies of a partially-built derivation
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation, version 3.
+
+
 from contextlib import contextmanager
 import json
 import os
 from contextlib import contextmanager
 import json
 import os
@@ -20,6 +27,7 @@ def log(msg: str) -> Iterator[None]:
 
 class ParseNixStoreQueryGraphML(xml.sax.handler.ContentHandler):
     def __init__(self) -> None:
 
 class ParseNixStoreQueryGraphML(xml.sax.handler.ContentHandler):
     def __init__(self) -> None:
+        super().__init__()
         self.roots: Set[str] = set()
         self.non_roots: Set[str] = set()
         self.deps: Dict[str, List[str]] = {}
         self.roots: Set[str] = set()
         self.non_roots: Set[str] = set()
         self.deps: Dict[str, List[str]] = {}
@@ -38,26 +46,24 @@ class ParseNixStoreQueryGraphML(xml.sax.handler.ContentHandler):
 
 def getDeps(drv: str) -> ParseNixStoreQueryGraphML:
     with log("Loading dependency tree..."):
 
 def getDeps(drv: str) -> ParseNixStoreQueryGraphML:
     with log("Loading dependency tree..."):
-        process = subprocess.Popen(
-            ["nix-store", "--query", "--graphml", drv], stdout=subprocess.PIPE)
-        parser = ParseNixStoreQueryGraphML()
-        assert process.stdout
-        xml.sax.parse(process.stdout, parser)
-        assert process.wait() == 0
+        with subprocess.Popen(
+                ["nix-store", "--query", "--graphml", drv], stdout=subprocess.PIPE) as process:
+            parser = ParseNixStoreQueryGraphML()
+            assert process.stdout
+            xml.sax.parse(process.stdout, parser)
+            assert process.wait() == 0
         return parser
 
 
 def getDrvInfo(drv: str) -> Any:
         return parser
 
 
 def getDrvInfo(drv: str) -> Any:
-    with log("Loading %s..." % drv):
-        process = subprocess.Popen(["nix",
-                                    "--experimental-features",
-                                    "nix-command",
-                                    "show-derivation",
-                                    "/nix/store/" + drv],
-                                   stdout=subprocess.PIPE)
-        assert process.stdout
-        info = json.load(process.stdout)
-        assert process.wait() == 0
+    with log(f"Loading {drv}..."):
+        with subprocess.Popen(
+                ["nix", "--experimental-features", "nix-command",
+                    "derivation", "show", f"/nix/store/{drv}^*"],
+                stdout=subprocess.PIPE) as process:
+            assert process.stdout
+            info = json.load(process.stdout)
+            assert process.wait() == 0
         return info
 
 
         return info
 
 
@@ -80,12 +86,8 @@ def removesuffix(s: str, suf: str) -> str:
 def pin(drv: str) -> None:
     outPath = os.path.join(sys.argv[2], removesuffix(drv, ".drv"))
     if not os.path.exists(outPath):
 def pin(drv: str) -> None:
     outPath = os.path.join(sys.argv[2], removesuffix(drv, ".drv"))
     if not os.path.exists(outPath):
-        process = subprocess.run(["nix-store",
-                                  "--realise",
-                                  "--add-root",
-                                  outPath,
-                                  "/nix/store/" + drv],
-                                 check=True)
+        subprocess.run(["nix-store", "--realise", "--add-root",
+                       outPath, "/nix/store/" + drv], check=True)
 
 
 def pinBuiltThings(thing: str,
 
 
 def pinBuiltThings(thing: str,
@@ -101,6 +103,11 @@ def pinBuiltThings(thing: str,
         pinBuiltThings(dep, done, deps)
 
 
         pinBuiltThings(dep, done, deps)
 
 
-dep_graph = getDeps(sys.argv[1])
-for root in dep_graph.roots:
-    pinBuiltThings(root, set(), dep_graph.deps)
+def main() -> None:
+    dep_graph = getDeps(sys.argv[1])
+    for root in dep_graph.roots:
+        pinBuiltThings(root, set(), dep_graph.deps)
+
+
+if __name__ == '__main__':
+    main()