Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix splicing in overrideAttrs #201734

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkgs/test/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ with pkgs;

cross = callPackage ./cross {};

splicing = callPackage ./splicing {};

php = recurseIntoAttrs (callPackages ./php {});

pkg-config = recurseIntoAttrs (callPackage ../top-level/pkg-config/tests.nix { });
Expand Down
40 changes: 40 additions & 0 deletions pkgs/test/splicing/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{ pkgs, stdenvNoCC, lib, }:
let
pkgsCross = pkgs.pkgsCross.aarch64-multiplatform.__splicedPackages;
tests =
let
pythonInNativeBuildInputs = lib.elemAt pkgsCross.python3Packages.xpybutil.nativeBuildInputs 0;
overridenAttr = pkgsCross.hello.overrideAttrs (_: { something = "hello"; });
in
[
({
name = "pythonInNativeBuildInputsShouldBeNative";
expr = pythonInNativeBuildInputs.stdenv.hostPlatform.system == "x86_64-linux";
expected = true;
})
({
name = "overridenAttrShouldHaveSplicedAndSomething";
expr = if overridenAttr ? __spliced then overridenAttr.something == overridenAttr.__spliced.buildHost.something else false;
expected = true;
})
({
name = "splicedShouldBeOverriden";
expr = pkgsCross.nix.aws-sdk-cpp.cmakeFlags == pkgsCross.nix.aws-sdk-cpp.__spliced.hostTarget.cmakeFlags;
expected = true;
})
({
name = "notCrossOverriden";
expr = (pkgs.hello.overrideAttrs (_: _: { passthru = { o = "works"; }; })) ? o;
expected = true;
})
];
in
{
test-splicing = stdenvNoCC.mkDerivation {
name = "test-splicing";
passthru = { inherit tests; };
buildCommand = ''
touch $out
'' + lib.concatMapStringsSep "\n" (t: "([[ ${lib.boolToString t.expr} == ${lib.boolToString t.expected} ]] && echo '${t.name} success') || (echo '${t.name} fail' && exit 1)") tests;
};
}
39 changes: 35 additions & 4 deletions pkgs/top-level/splice.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,36 @@ let
valueHostHost = pkgsHostHost.${name} or { };
valueHostTarget = pkgsHostTarget.${name} or { };
valueTargetTarget = pkgsTargetTarget.${name} or { };
augmentedValue = defaultValue

addEntangled = origOverrideAttrs: f0:
let
# copied from make-derivation.nix, check there for the comment.
f = self: super:
let x = f0 super;
in
if builtins.isFunction x
then
f0 self super
else x;
in
origOverrideAttrs (
lib.composeExtensions f (self: super: {
passthru = (super.passthru or { }) // {
crossDrv = super.passthru.crossDrv.overrideAttrs f;
nativeDrv = super.passthru.nativeDrv.overrideAttrs f;
__spliced = { } // (lib.mapAttrs (_: sDrv: sDrv.overrideAttrs f) splicingAttrs.__spliced);
overrideAttrs = addEntangled self.overrideAttrs;
};
})
);

entangle = pkg1: splicingAttrs: pkg1.overrideAttrs (self: super: {
passthru = (super.passthru or { }) // {
overrideAttrs = addEntangled self.overrideAttrs;
} // splicingAttrs;
});

splicingAttrs = { }
# TODO(@Artturin): remove before release 23.05 and only have __spliced.
// (lib.optionalAttrs (pkgsBuildHost ? ${name}) { nativeDrv = lib.warn "use ${name}.__spliced.buildHost instead of ${name}.nativeDrv" valueBuildHost; })
// (lib.optionalAttrs (pkgsHostTarget ? ${name}) { crossDrv = lib.warn "use ${name}.__spliced.hostTarget instead of ${name}.crossDrv" valueHostTarget; })
Expand All @@ -55,10 +84,12 @@ let
// (lib.optionalAttrs (pkgsBuildTarget ? ${name}) { buildTarget = valueBuildTarget; })
// (lib.optionalAttrs (pkgsHostHost ? ${name}) { hostHost = valueHostHost; })
// (lib.optionalAttrs (pkgsHostTarget ? ${name}) { hostTarget = valueHostTarget; })
// (lib.optionalAttrs (pkgsTargetTarget ? ${name}) {
targetTarget = valueTargetTarget;
});
// (lib.optionalAttrs (pkgsTargetTarget ? ${name}) { targetTarget = valueTargetTarget; });
};
augmentedValue =
if (defaultValue ? overrideAttrs)
then entangle defaultValue splicingAttrs
else defaultValue // splicingAttrs;
# Get the set of outputs of a derivation. If one derivation fails to
# evaluate we don't want to diverge the entire splice, so we fall back
# on {}
Expand Down