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

lib: add makeOverridableWithName + build*Package overrides #87394

Closed
wants to merge 6 commits into from
Closed
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
25 changes: 22 additions & 3 deletions lib/customisation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,33 @@ rec {
nix-repl> y = lib.makeOverridable x { a = 1; b = 2; }

nix-repl> y
{ override = «lambda»; overrideDerivation = «lambda»; result = 3; }
{ override = { ... }; overrideDerivation = «lambda»; result = 3; }

nix-repl> y.override { a = 10; }
{ override = «lambda»; overrideDerivation = «lambda»; result = 12; }
{ override = { ... }; overrideDerivation = «lambda»; result = 12; }

Please refer to "Nixpkgs Contributors Guide" section
"<pkg>.overrideDerivation" to learn about `overrideDerivation` and caveats
related to its use.
*/
makeOverridable = f: origArgs:
makeOverridable = makeOverridableWithName null;


/* `makeOverridableWithName` takes a function from attribute set to attribute
set and injects a `override` attribute and a optional `name` attribute
which can be used to override arguments of the function.

nix-repl> x = {a, b}: { result = a + b; }

nix-repl> y = lib.makeOverridableWithName "update" x { a = 1; b = 2; }

nix-repl> y
{ override = { ... }; overrideDerivation = «lambda»; result = 3; update = «repeated»; }

nix-repl> y.update { a = 10; }
{ override = { ... }; overrideDerivation = «lambda»; result = 12; }
*/
makeOverridableWithName = name: f: origArgs:
let
result = f origArgs;

Expand All @@ -81,6 +98,7 @@ rec {
if builtins.isAttrs result then
result // {
override = overrideArgs;
${name} = overrideArgs;
overrideDerivation = fdrv: overrideResult (x: overrideDerivation x fdrv);
${if result ? overrideAttrs then "overrideAttrs" else null} = fdrv:
overrideResult (x: x.overrideAttrs fdrv);
Expand All @@ -89,6 +107,7 @@ rec {
# Transform the result into a functor while propagating its arguments
lib.setFunctionArgs result (lib.functionArgs result) // {
override = overrideArgs;
${name} = overrideArgs;
}
else result;

Expand Down
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let
noDepEntry fullDepEntry packEntry stringAfter;
inherit (customisation) overrideDerivation makeOverridable
callPackageWith callPackagesWith extendDerivation hydraJob
makeScope;
makeScope makeOverridableWithName;
inherit (meta) addMetaAttrs dontDistribute setName updateName
appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio
hiPrioSet;
Expand Down
7 changes: 4 additions & 3 deletions pkgs/development/compilers/rust/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
inherit cargo;
};

buildRustPackage = callPackage ../../../build-support/rust {
inherit rustc cargo fetchCargoTarball;
};
buildRustPackage = lib.makeOverridableWithName "overrideRustAttrs"
(callPackage ../../../build-support/rust {
inherit rustc cargo fetchCargoTarball;
});

rustcSrc = callPackage ./rust-src.nix {
inherit rustc;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/ruby-modules/gem/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
, ruby, bundler
} @ defs:

lib.makeOverridable (
lib.makeOverridableWithName "overrideRubyGemAttrs" (

{ name ? null
, gemName
Expand Down
9 changes: 5 additions & 4 deletions pkgs/top-level/lua-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ let

buildLuaApplication = args: buildLuarocksPackage ({namePrefix="";} // args );

buildLuarocksPackage = with pkgs.lib; makeOverridable(callPackage ../development/interpreters/lua-5/build-lua-package.nix {
inherit toLuaModule;
inherit lua;
});
buildLuarocksPackage = lib.makeOverridableWithName "overrideLuarocksAttrs"
(callPackage ../development/interpreters/lua-5/build-lua-package.nix {
inherit toLuaModule;
inherit lua;
});
in
with self; {

Expand Down
7 changes: 4 additions & 3 deletions pkgs/top-level/perl-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ let
};
});

buildPerlPackage = callPackage ../development/perl-modules/generic {
inherit buildPerl;
};
buildPerlPackage = stdenv.lib.makeOverridableWithName "overridePerlAttrs"
(callPackage ../development/perl-modules/generic {
inherit buildPerl;
});

# Helper functions for packages that use Module::Build to build.
buildPerlModule = args:
Expand Down
34 changes: 10 additions & 24 deletions pkgs/top-level/python-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,17 @@ let

bootstrapped-pip = callPackage ../development/python-modules/bootstrapped-pip { };

# Derivations built with `buildPythonPackage` can already be overriden with `override`, `overrideAttrs`, and `overrideDerivation`.
# 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);
in
if builtins.isAttrs ff then (ff // {
overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
})
else if builtins.isFunction ff then {
overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
__functor = self: ff;
}
else ff;

buildPythonPackage = makeOverridablePythonPackage ( makeOverridable (callPackage ../development/interpreters/python/mk-python-derivation.nix {
inherit namePrefix; # We want Python libraries to be named like e.g. "python3.6-${name}"
inherit toPythonModule; # Libraries provide modules
}));
buildPythonPackage = makeOverridableWithName "overridePythonAttrs"
(callPackage ../development/interpreters/python/mk-python-derivation.nix {
inherit namePrefix; # We want Python libraries to be named like e.g. "python3.6-${name}"
inherit toPythonModule; # Libraries provide modules
});

buildPythonApplication = makeOverridablePythonPackage ( makeOverridable (callPackage ../development/interpreters/python/mk-python-derivation.nix {
namePrefix = ""; # Python applications should not have any prefix
toPythonModule = x: x; # Application does not provide modules.
}));
buildPythonApplication = makeOverridableWithName "overridePythonAttrs"
(callPackage ../development/interpreters/python/mk-python-derivation.nix {
namePrefix = ""; # Python applications should not have any prefix
toPythonModule = x: x; # Application does not provide modules.
});

# See build-setupcfg/default.nix for documentation.
buildSetupcfg = import ../build-support/build-setupcfg self;
Expand Down