about summary refs log tree commit diff
path: root/nix/dependency-analyzer/default.nix
diff options
context:
space:
mode:
authorsterni <sternenseemann@systemli.org>2022-10-01T18·09+0200
committersterni <sternenseemann@systemli.org>2022-10-08T10·59+0000
commit3fab3b780b04f6cbddf6cdef625adb095c8702e3 (patch)
tree105fd2b80b0c4af8e2f132162c218e75335ba896 /nix/dependency-analyzer/default.nix
parent57d5988b340ec1b799882f00323010d9435892ca (diff)
feat(nix/dependency-analyzer): improved directDrvDeps for Nix >= 2.6 r/5061
This codepath will basically never be used in depot, but I want to add
it as kind of a note to myself. It's kind of a neat feature, although
I'm not quite sure it is going to stick around.

Change-Id: If0e26ef47bdedc6dbf3d048ad4fc9a3a1fd6c5a2
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6833
Reviewed-by: grfn <grfn@gws.fyi>
Tested-by: BuildkiteCI
Diffstat (limited to 'nix/dependency-analyzer/default.nix')
-rw-r--r--nix/dependency-analyzer/default.nix36
1 files changed, 25 insertions, 11 deletions
diff --git a/nix/dependency-analyzer/default.nix b/nix/dependency-analyzer/default.nix
index 4ced173eafab..c988d9409da4 100644
--- a/nix/dependency-analyzer/default.nix
+++ b/nix/dependency-analyzer/default.nix
@@ -23,19 +23,33 @@ let
     in
     appendContext drvPath' { ${drvPath'} = { path = true; }; };
 
-  # Find all quoted references to a derivation path in the specified drv file.
-  # Should correspond to the list of input derivations, but is obviously a big
-  # HACK as we just grep for store paths that look right. This should eventually
-  # be solved properly by parsing the drv file.
+  # Determine all paths a derivation depends on, i.e. input derivations and
+  # files imported into the Nix store.
+  #
+  # Implementation for Nix < 2.6 is quite hacky at the moment.
   #
   # Type: str -> [str]
-  directDrvDeps = drvPath: builtins.concatLists (
-    builtins.filter builtins.isList (
-      builtins.split
-        "\"(${lib.escapeRegex builtins.storeDir}/[[:alnum:]+._?=-]+.drv)\""
-        (builtins.readFile drvPath)
-    )
-  );
+  #
+  # TODO(sterni): clean this up and expose it
+  directDrvDeps =
+    if lib.versionAtLeast builtins.nixVersion "2.6"
+    then
+    # Since https://github.com/NixOS/nix/pull/1643, Nix apparently »preserves
+    # string context« through a readFile invocation. This has the side effect
+    # that it becomes possible to query the actual references a store path has.
+    # Not a 100% sure this is intended, but _very_ convenient for us here.
+      drvPath: builtins.attrNames (builtins.getContext (builtins.readFile drvPath))
+    else
+    # For Nix < 2.6 we have to rely on HACK, namely grepping for quoted store
+    # path references in the file. In the future this should be replaced by
+    # a proper derivation parser.
+      drvPath: builtins.concatLists (
+        builtins.filter builtins.isList (
+          builtins.split
+            "\"(${lib.escapeRegex builtins.storeDir}/[[:alnum:]+._?=-]+.drv)\""
+            (builtins.readFile drvPath)
+        )
+      );
 
   # Maps a list of derivation to the list of corresponding `drvPath`s.
   #