replaceDirectDependencies: split off from replaceDependencies

This allows both swapping out and reusing the rewrite machinery.
This commit is contained in:
Alois Wohlschlager 2024-01-12 10:14:38 +01:00 committed by Yureka
parent fee5c7e1c2
commit 59ca239d1a
4 changed files with 40 additions and 22 deletions

View File

@ -74,7 +74,9 @@ let
baseSystemAssertWarn
else
(pkgs.replaceDependencies.override {
replaceDirectDependencies = pkgs.replaceDirectDependencies.override {
nix = config.nix.package;
};
}) {
drv = baseSystemAssertWarn;
inherit replacements cutoffPackages;

View File

@ -1,7 +1,7 @@
{
runCommandLocal,
nix,
lib,
runCommandLocal,
replaceDirectDependencies,
}:
# Replace some dependencies in the requisites tree of drv, propagating the change all the way up the tree, even within other replacements, without a full rebuild.
@ -43,15 +43,13 @@ let
inherit (builtins) unsafeDiscardStringContext appendContext;
inherit (lib)
trace
substring
stringLength
concatStringsSep
mapAttrsToList
listToAttrs
attrValues
mapAttrs
filter
hasAttr
mapAttrsToList
all
;
inherit (lib.attrsets) mergeAttrsList;
@ -90,21 +88,6 @@ let
echo }) > $out
''
).outPath;
rewriteHashes =
drv: rewrites:
if rewrites == { } 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 " (
mapAttrsToList (name: value: "'s|${baseNameOf name}|${baseNameOf value}|g'") rewrites
)
} | $nixStore --restore $out
'';
knownDerivations = [ drv ] ++ map ({ newDependency, ... }: newDependency) replacements;
referencesMemo = listToAttrs (
@ -162,7 +145,13 @@ let
}) rewrittenReferences
);
in
rewriteHashes storePathOrKnownDerivationMemo.${drv} rewrites
replaceDirectDependencies {
drv = storePathOrKnownDerivationMemo.${drv};
replacements = mapAttrsToList (name: value: {
oldDependency = name;
newDependency = value;
}) rewrites;
}
) relevantReferences
// listToAttrs (
map (drv: {

View File

@ -0,0 +1,25 @@
{
lib,
runCommandLocal,
nix,
}:
# 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
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'")
replacements)
} | $nixStore --restore $out
''

View File

@ -1319,6 +1319,8 @@ with pkgs;
replaceVars = callPackage ../build-support/replace-vars { };
replaceDirectDependencies = callPackage ../build-support/replace-direct-dependencies.nix { };
nukeReferences = callPackage ../build-support/nuke-references {
inherit (darwin) signingUtils;
};