Skip to content

Commit

Permalink
replaceDependencies: add support for ca-derivations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alois31 committed Apr 3, 2024
1 parent 1926d77 commit da79a19
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 30 deletions.
1 change: 1 addition & 0 deletions nixos/tests/replace-dependencies/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ../make-test-python.nix (
nodes.machine =
{ ... }:
{
nix.settings.experimental-features = [ "ca-derivations" ];
system.extraDependencies = [ pkgs.stdenvNoCC ];
};

Expand Down
17 changes: 17 additions & 0 deletions nixos/tests/replace-dependencies/guest.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ let
oldDependency = writeShellScriptBin "dependency" ''
echo "got old dependency"
'';
oldDependency-ca = oldDependency.overrideAttrs { __contentAddressed = true; };
newDependency = writeShellScriptBin "dependency" ''
echo "got new dependency"
'';
newDependency-ca = newDependency.overrideAttrs { __contentAddressed = true; };
basic = writeShellScriptBin "test" ''
${oldDependency}/bin/dependency
'';
basic-ca = writeShellScriptBin "test" ''
${oldDependency-ca}/bin/dependency
'';
transitive = writeShellScriptBin "test" ''
${basic}/bin/test
'';
Expand Down Expand Up @@ -58,6 +63,18 @@ in
inherit oldDependency newDependency;
}) "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 {
drv = transitive;
inherit oldDependency newDependency;
Expand Down
54 changes: 36 additions & 18 deletions pkgs/build-support/replace-dependencies.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ let
inherit (builtins) unsafeDiscardStringContext appendContext;
inherit (lib)
trace
stringLength
listToAttrs
isStorePath
readFile
attrValues
mapAttrs
filter
hasAttr
mapAttrsToList
all
;
inherit (lib.attrsets) mergeAttrsList;

Expand Down Expand Up @@ -84,17 +84,38 @@ let
''
).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 (
map (drv: {
name = toContextlessString drv;
name = realisation drv;
value = referencesOf drv;
}) knownDerivations
}) targetDerivations
);
relevantReferences = mergeAttrsList (attrValues referencesMemo);
# 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.
storePathOrKnownDerivationMemo =
storePathOrKnownTargetDerivationMemo =
mapAttrs (
drv: _references:
# builtins.storePath does not work in pure evaluation mode, even though it is not impure.
Expand All @@ -104,9 +125,9 @@ let
) relevantReferences
// listToAttrs (
map (drv: {
name = toContextlessString drv;
name = realisation drv;
value = drv;
}) knownDerivations
}) targetDerivations
);

relevantReplacements = filter (
Expand All @@ -116,7 +137,7 @@ let
# 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.
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}"
# 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.
Expand All @@ -141,7 +162,7 @@ let
);
in
replaceDirectDependencies {
drv = storePathOrKnownDerivationMemo.${drv};
drv = storePathOrKnownTargetDerivationMemo.${drv};
replacements = mapAttrsToList (name: value: {
oldDependency = name;
newDependency = value;
Expand All @@ -150,21 +171,18 @@ let
) relevantReferences
// listToAttrs (
map (drv: {
name = toContextlessString drv;
value = storePathOrKnownDerivationMemo.${toContextlessString drv};
name = realisation drv;
value = storePathOrKnownTargetDerivationMemo.${realisation drv};
}) cutoffPackages
)
// listToAttrs (
map (
{ oldDependency, newDependency }:
{
name = toContextlessString oldDependency;
value = rewriteMemo.${toContextlessString newDependency};
name = realisation oldDependency;
value = rewriteMemo.${realisation newDependency};
}
) relevantReplacements
);
in
assert all (
{ oldDependency, newDependency }: stringLength oldDependency == stringLength newDependency
) replacements;
rewriteMemo.${toContextlessString drv}
rewriteMemo.${realisation drv}
44 changes: 32 additions & 12 deletions pkgs/build-support/replace-direct-dependencies.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,39 @@
# 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.
{ drv, replacements ? [ ] }:
let inherit (lib) all stringLength substring concatStringsSep;
in assert all ({ oldDependency, newDependency }:
stringLength oldDependency == stringLength newDependency) replacements;
if replacements == [ ] then
let
inherit (lib)
isStorePath substring stringLength optionalString escapeShellArgs concatMap;
in if replacements == [ ] then
drv
else
let drvName = substring 33 (stringLength (baseNameOf drv)) (baseNameOf drv);
in runCommandLocal drvName { nixStore = "${nix}/bin/nix-store"; } ''
$nixStore --dump ${drv} | sed 's|${
baseNameOf drv
}|'$(basename $out)'|g' | sed -e ${
concatStringsSep " -e " (map ({ oldDependency, newDependency }:
"'s|${baseNameOf oldDependency}|${baseNameOf newDependency}|g'")
let
drvName = if isStorePath drv then
# Reconstruct the name from the actual store path if available.
substring 33 (stringLength (baseNameOf drv)) (baseNameOf drv)
else if drv ? drvAttrs.name then
# Try to get the name from the derivation arguments otherwise (for floating or deferred derivations).
drv.drvAttrs.name + (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)
} | $nixStore --restore $out
}
nix-store --dump ${drv} | sed -f rewrite.sed | nix-store --restore $out
''

0 comments on commit da79a19

Please sign in to comment.