replaceDependencies: add support for ca-derivations

Unlike regular input-addressed or fixed-output derivations, floating and
deferred derivations do not have their store path available at evaluation time,
so their outPath is a placeholder. The following changes are needed for
replaceDependencies to continue working:
* Detect the placeholder and retrieve the store path using another IFD hack
  when collecting the rewrite plan.
* Try to obtain the derivation name needed for replaceDirectDependencies from
  the derivation arguments if a placeholder is detected.
* Move the length mismatch detection to build time, since the placeholder has a
  fixed length which is unrelated to the store path.
This commit is contained in:
Alois Wohlschlager 2024-01-27 11:59:26 +01:00 committed by Yureka
parent 59ca239d1a
commit 3616cfb8d9
4 changed files with 114 additions and 31 deletions

View File

@ -7,6 +7,7 @@ import ../make-test-python.nix (
nodes.machine = nodes.machine =
{ ... }: { ... }:
{ {
nix.settings.experimental-features = [ "ca-derivations" ];
system.extraDependencies = [ pkgs.stdenvNoCC ]; system.extraDependencies = [ pkgs.stdenvNoCC ];
}; };

View File

@ -21,12 +21,17 @@ let
oldDependency = writeShellScriptBin "dependency" '' oldDependency = writeShellScriptBin "dependency" ''
echo "got old dependency" echo "got old dependency"
''; '';
oldDependency-ca = oldDependency.overrideAttrs { __contentAddressed = true; };
newDependency = writeShellScriptBin "dependency" '' newDependency = writeShellScriptBin "dependency" ''
echo "got new dependency" echo "got new dependency"
''; '';
newDependency-ca = newDependency.overrideAttrs { __contentAddressed = true; };
basic = writeShellScriptBin "test" '' basic = writeShellScriptBin "test" ''
${oldDependency}/bin/dependency ${oldDependency}/bin/dependency
''; '';
basic-ca = writeShellScriptBin "test" ''
${oldDependency-ca}/bin/dependency
'';
transitive = writeShellScriptBin "test" '' transitive = writeShellScriptBin "test" ''
${basic}/bin/test ${basic}/bin/test
''; '';
@ -58,6 +63,18 @@ in
inherit oldDependency newDependency; inherit oldDependency newDependency;
}) "got new dependency"; }) "got new dependency";
replacedependency-basic-old-ca = mkCheckOutput "replacedependency-basic" (replaceDependency {
drv = basic-ca;
oldDependency = oldDependency-ca;
inherit newDependency;
}) "got new dependency";
replacedependency-basic-new-ca = mkCheckOutput "replacedependency-basic" (replaceDependency {
drv = basic;
inherit oldDependency;
newDependency = newDependency-ca;
}) "got new dependency";
replacedependency-transitive = mkCheckOutput "replacedependency-transitive" (replaceDependency { replacedependency-transitive = mkCheckOutput "replacedependency-transitive" (replaceDependency {
drv = transitive; drv = transitive;
inherit oldDependency newDependency; inherit oldDependency newDependency;

View File

@ -43,14 +43,14 @@ let
inherit (builtins) unsafeDiscardStringContext appendContext; inherit (builtins) unsafeDiscardStringContext appendContext;
inherit (lib) inherit (lib)
trace trace
stringLength
listToAttrs listToAttrs
isStorePath
readFile
attrValues attrValues
mapAttrs mapAttrs
filter filter
hasAttr hasAttr
mapAttrsToList mapAttrsToList
all
; ;
inherit (lib.attrsets) mergeAttrsList; inherit (lib.attrsets) mergeAttrsList;
@ -89,17 +89,38 @@ let
'' ''
).outPath; ).outPath;
knownDerivations = [ drv ] ++ map ({ newDependency, ... }: newDependency) replacements; targetDerivations = [ drv ] ++ map ({ newDependency, ... }: newDependency) replacements;
realisation =
drv:
if isStorePath drv then
# Input-addressed and fixed-output derivations have their realisation as outPath.
toContextlessString drv
else
# Floating and deferred derivations have a placeholder outPath.
# The realisation can only be obtained by performing an actual build.
unsafeDiscardStringContext (
readFile (
runCommandLocal "realisation"
{
env = {
inherit drv;
};
}
''
echo -n "$drv" > $out
''
)
);
referencesMemo = listToAttrs ( referencesMemo = listToAttrs (
map (drv: { map (drv: {
name = toContextlessString drv; name = realisation drv;
value = referencesOf drv; value = referencesOf drv;
}) knownDerivations }) targetDerivations
); );
relevantReferences = mergeAttrsList (attrValues referencesMemo); relevantReferences = mergeAttrsList (attrValues referencesMemo);
# Make sure a derivation is returned even when no replacements are actually applied. # Make sure a derivation is returned even when no replacements are actually applied.
# Yes, even in the stupid edge case where the root derivation itself is replaced. # Yes, even in the stupid edge case where the root derivation itself is replaced.
storePathOrKnownDerivationMemo = storePathOrKnownTargetDerivationMemo =
mapAttrs ( mapAttrs (
drv: _references: drv: _references:
# builtins.storePath does not work in pure evaluation mode, even though it is not impure. # builtins.storePath does not work in pure evaluation mode, even though it is not impure.
@ -109,9 +130,9 @@ let
) relevantReferences ) relevantReferences
// listToAttrs ( // listToAttrs (
map (drv: { map (drv: {
name = toContextlessString drv; name = realisation drv;
value = drv; value = drv;
}) knownDerivations }) targetDerivations
); );
relevantReplacements = filter ( relevantReplacements = filter (
@ -121,7 +142,7 @@ let
# Attempting to replace a dependency by itself is completely useless, and would only lead to infinite recursion. # Attempting to replace a dependency by itself is completely useless, and would only lead to infinite recursion.
# Hence it must not be attempted to apply this replacement in any case. # Hence it must not be attempted to apply this replacement in any case.
false false
else if !hasAttr (toContextlessString oldDependency) referencesMemo.${toContextlessString drv} then else if !hasAttr (realisation oldDependency) referencesMemo.${realisation drv} then
warn "replaceDependencies: ${drv} does not depend on ${oldDependency}" warn "replaceDependencies: ${drv} does not depend on ${oldDependency}"
# Handle the corner case where one of the other replacements introduces the dependency. # Handle the corner case where one of the other replacements introduces the dependency.
# It would be more correct to not show the warning in this case, but the added complexity is probably not worth it. # It would be more correct to not show the warning in this case, but the added complexity is probably not worth it.
@ -146,7 +167,7 @@ let
); );
in in
replaceDirectDependencies { replaceDirectDependencies {
drv = storePathOrKnownDerivationMemo.${drv}; drv = storePathOrKnownTargetDerivationMemo.${drv};
replacements = mapAttrsToList (name: value: { replacements = mapAttrsToList (name: value: {
oldDependency = name; oldDependency = name;
newDependency = value; newDependency = value;
@ -155,21 +176,18 @@ let
) relevantReferences ) relevantReferences
// listToAttrs ( // listToAttrs (
map (drv: { map (drv: {
name = toContextlessString drv; name = realisation drv;
value = storePathOrKnownDerivationMemo.${toContextlessString drv}; value = storePathOrKnownTargetDerivationMemo.${realisation drv};
}) cutoffPackages }) cutoffPackages
) )
// listToAttrs ( // listToAttrs (
map ( map (
{ oldDependency, newDependency }: { oldDependency, newDependency }:
{ {
name = toContextlessString oldDependency; name = realisation oldDependency;
value = rewriteMemo.${toContextlessString newDependency}; value = rewriteMemo.${realisation newDependency};
} }
) relevantReplacements ) relevantReplacements
); );
in in
assert all ( rewriteMemo.${realisation drv}
{ oldDependency, newDependency }: stringLength oldDependency == stringLength newDependency
) replacements;
rewriteMemo.${toContextlessString drv}

View File

@ -6,20 +6,67 @@
# Replace some direct dependencies of drv, not recursing into the dependency tree. # Replace some direct dependencies of drv, not recursing into the dependency tree.
# You likely want to use replaceDependencies instead, unless you plan to implement your own recursion mechanism. # You likely want to use replaceDependencies instead, unless you plan to implement your own recursion mechanism.
{ drv, replacements ? [ ] }: {
let inherit (lib) all stringLength substring concatStringsSep; drv,
in assert all ({ oldDependency, newDependency }: replacements ? [ ],
stringLength oldDependency == stringLength newDependency) replacements; }:
let
inherit (lib)
isStorePath
substring
stringLength
optionalString
escapeShellArgs
concatMap
;
in
if replacements == [ ] then if replacements == [ ] then
drv drv
else else
let drvName = substring 33 (stringLength (baseNameOf drv)) (baseNameOf drv); let
in runCommandLocal drvName { nixStore = "${nix}/bin/nix-store"; } '' drvName =
$nixStore --dump ${drv} | sed 's|${ if isStorePath drv then
baseNameOf drv # Reconstruct the name from the actual store path if available.
}|'$(basename $out)'|g' | sed -e ${ substring 33 (stringLength (baseNameOf drv)) (baseNameOf drv)
concatStringsSep " -e " (map ({ oldDependency, newDependency }: else if drv ? drvAttrs.name then
"'s|${baseNameOf oldDependency}|${baseNameOf newDependency}|g'") # Try to get the name from the derivation arguments otherwise (for floating or deferred derivations).
replacements) drv.drvAttrs.name
} | $nixStore --restore $out + (
let
outputName = drv.outputName or "out";
in
optionalString (outputName != "out") "-${outputName}"
)
else
throw "cannot reconstruct the derivation name from ${drv}";
in
runCommandLocal drvName { nativeBuildInputs = [ nix.out ]; } ''
createRewriteScript() {
while [ $# -ne 0 ]; do
oldBasename="$(basename "$1")"
newBasename="$(basename "$2")"
shift 2
if [ ''${#oldBasename} -ne ''${#newBasename} ]; then
echo "cannot rewrite $oldBasename to $newBasename: length does not match" >&2
exit 1
fi
echo "s|$oldBasename|$newBasename|g" >> rewrite.sed
done
}
createRewriteScript ${
escapeShellArgs (
[
drv
(placeholder "out")
]
++ concatMap (
{ oldDependency, newDependency }:
[
oldDependency
newDependency
]
) replacements
)
}
nix-store --dump ${drv} | sed -f rewrite.sed | nix-store --restore $out
'' ''