feat(nix/utils): add onlyDrvPath to get the drvPath w/o the outputs

I want to use this utility in a deploy script where the .drv is
nix-copy-closure-d to a remote host and realized there. Consequently it
doesn't make sense that the local deploy script depends on the
derivation's outputs which drvPath does by default.

This also came up when working on //nix/buildkite, although we didn't
end up using it there.

Change-Id: I952bbfd4d7e9de212569d5ee12182eb50d360f53
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5767
Tested-by: BuildkiteCI
Autosubmit: sterni <sternenseemann@systemli.org>
Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
sterni 2022-05-28 13:49:34 +02:00 committed by clbot
parent 6c3465dc59
commit 6813598c17
2 changed files with 27 additions and 0 deletions

View file

@ -43,6 +43,21 @@ let
else builtins.throw "Don't know how to get (base)name of " else builtins.throw "Don't know how to get (base)name of "
+ lib.generators.toPretty { } p; + lib.generators.toPretty { } p;
/* Retrieves the drvPath attribute from a given derivation, but ensures that
the resulting string only depends on the `.drv` file in the nix store and
not on its realised outputs as well.
Type: drv -> string
*/
onlyDrvPath = drv:
let
inherit (drv) drvPath;
unsafeDrvPath = builtins.unsafeDiscardStringContext drvPath;
in
builtins.appendContext unsafeDrvPath {
${unsafeDrvPath} = { path = true; };
};
/* Query the type of a path exposing the same information as would be by /* Query the type of a path exposing the same information as would be by
`builtins.readDir`, but for a single, specific target path. `builtins.readDir`, but for a single, specific target path.
@ -152,6 +167,7 @@ in
{ {
inherit inherit
storePathName storePathName
onlyDrvPath
pathType pathType
isDirectory isDirectory
isRegularFile isRegularFile

View file

@ -15,6 +15,7 @@ let
isSymlink isSymlink
pathType pathType
storePathName storePathName
onlyDrvPath
; ;
assertUtilsPred = msg: act: exp: [ assertUtilsPred = msg: act: exp: [
@ -91,9 +92,19 @@ let
(storePathName cleanedSource) (storePathName cleanedSource)
cleanedSource.name) cleanedSource.name)
]; ];
onlyDrvPathTests = it "correctly updates the string context of drvPath" [
(assertEq "onlyDrvPath only produces path dependencies"
(builtins.all
(dep: dep.path or false)
(builtins.attrValues
(builtins.getContext (onlyDrvPath depot.tools.cheddar))))
true)
];
in in
runTestsuite "nix.utils" [ runTestsuite "nix.utils" [
pathPredicates pathPredicates
storePathNameTests storePathNameTests
onlyDrvPathTests
] ]