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

Fixes for overrideAttrs (split from #201734) #211685

Merged
merged 5 commits into from
Feb 3, 2023
Merged
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
9 changes: 8 additions & 1 deletion lib/customisation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,14 @@ rec {
outputSpecified = true;
drvPath = assert condition; drv.${outputName}.drvPath;
outPath = assert condition; drv.${outputName}.outPath;
};
} //
# TODO: give the derivation control over the outputs.
# `overrideAttrs` may not be the only attribute that needs
# updating when switching outputs.
lib.optionalAttrs (passthru?overrideAttrs) {
# TODO: also add overrideAttrs when overrideAttrs is not custom, e.g. when not splicing.
overrideAttrs = f: (passthru.overrideAttrs f).${outputName};
};
};

outputsList = map outputToAttrListElement outputs;
Expand Down
21 changes: 13 additions & 8 deletions pkgs/development/interpreters/python/python-packages-base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ let
# This function introduces `overridePythonAttrs` and it overrides the call to `buildPythonPackage`.
makeOverridablePythonPackage = f: origArgs:
let
ff = f origArgs;
overrideWith = newArgs: origArgs // (if pkgs.lib.isFunction newArgs then newArgs origArgs else newArgs);
args = lib.fix (lib.extends
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By inlining fix and extends, and then simplifying the expression, you'll improve performance, but the gain will be marginal, as the performance stats indicate that this addition is in fact quite cheap. Readability is more important for now, as you're still working on this code.

(_: previousAttrs: {
passthru = (previousAttrs.passthru or { }) // {
overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
};
})
(_: origArgs));
result = f args;
overrideWith = newArgs: args // (if pkgs.lib.isFunction newArgs then newArgs args else newArgs);
in
if builtins.isAttrs ff then (ff // {
if builtins.isAttrs result then result
else if builtins.isFunction result then {
overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
})
else if builtins.isFunction ff then {
overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
__functor = self: ff;
__functor = self: result;
}
else ff;
else result;

buildPythonPackage = makeOverridablePythonPackage (lib.makeOverridable (callPackage ./mk-python-derivation.nix {
inherit namePrefix; # We want Python libraries to be named like e.g. "python3.6-${name}"
Expand Down
49 changes: 25 additions & 24 deletions pkgs/stdenv/generic/make-derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,34 @@ let
# separate lines, because Nix would only show the last line of the comment.

# An infinite recursion here can be caused by having the attribute names of expression `e` in `.overrideAttrs(finalAttrs: previousAttrs: e)` depend on `finalAttrs`. Only the attribute values of `e` can depend on `finalAttrs`.
args = rattrs (args // { inherit finalPackage; });
args = rattrs (args // { inherit finalPackage overrideAttrs; });
# ^^^^

finalPackage =
mkDerivationSimple
(f0:
let
f = self: super:
# Convert f0 to an overlay. Legacy is:
# overrideAttrs (super: {})
# We want to introduce self. We follow the convention of overlays:
# overrideAttrs (self: super: {})
# Which means the first parameter can be either self or super.
# This is surprising, but far better than the confusion that would
# arise from flipping an overlay's parameters in some cases.
let x = f0 super;
in
if builtins.isFunction x
then
# Can't reuse `x`, because `self` comes first.
# Looks inefficient, but `f0 super` was a cheap thunk.
f0 self super
else x;
overrideAttrs = f0:
let
f = self: super:
# Convert f0 to an overlay. Legacy is:
# overrideAttrs (super: {})
# We want to introduce self. We follow the convention of overlays:
# overrideAttrs (self: super: {})
# Which means the first parameter can be either self or super.
# This is surprising, but far better than the confusion that would
# arise from flipping an overlay's parameters in some cases.
let x = f0 super;
in
makeDerivationExtensible
(self: let super = rattrs self; in super // f self super))
args;
if builtins.isFunction x
then
# Can't reuse `x`, because `self` comes first.
# Looks inefficient, but `f0 super` was a cheap thunk.
f0 self super
else x;
in
makeDerivationExtensible
(self: let super = rattrs self; in super // f self super);

finalPackage =
mkDerivationSimple overrideAttrs args;

in finalPackage;

# makeDerivationExtensibleConst == makeDerivationExtensible (_: attrs),
Expand Down
2 changes: 2 additions & 0 deletions pkgs/test/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ with pkgs;

nixos-functions = callPackage ./nixos-functions {};

overriding = callPackage ./overriding.nix { };

patch-shebangs = callPackage ./patch-shebangs {};

texlive = callPackage ./texlive {};
Expand Down
56 changes: 56 additions & 0 deletions pkgs/test/overriding.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{ lib, pkgs, stdenvNoCC }:

let
tests =
let
p = pkgs.python3Packages.xpybutil.overridePythonAttrs (_: { dontWrapPythonPrograms = true; });
in
[
({
name = "overridePythonAttrs";
expr = !lib.hasInfix "wrapPythonPrograms" p.postFixup;
expected = true;
})
({
name = "repeatedOverrides-pname";
expr = repeatedOverrides.pname == "a-better-hello-with-blackjack";
expected = true;
})
({
name = "repeatedOverrides-entangled-pname";
expr = repeatedOverrides.entangled.pname == "a-better-figlet-with-blackjack";
expected = true;
})
];

addEntangled = origOverrideAttrs: f:
origOverrideAttrs (
lib.composeExtensions f (self: super: {
passthru = super.passthru // {
entangled = super.passthru.entangled.overrideAttrs f;
overrideAttrs = addEntangled self.overrideAttrs;
};
})
);

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

example = entangle pkgs.hello pkgs.figlet;

overrides1 = example.overrideAttrs (_: super: { pname = "a-better-${super.pname}"; });

repeatedOverrides = overrides1.overrideAttrs (_: super: { pname = "${super.pname}-with-blackjack"; });
in

stdenvNoCC.mkDerivation {
name = "test-overriding";
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;
}