diff --git a/lib/customisation.nix b/lib/customisation.nix index dc5dd76919765..bee7b2de0aff3 100644 --- a/lib/customisation.nix +++ b/lib/customisation.nix @@ -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 ".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; @@ -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); @@ -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; diff --git a/lib/default.nix b/lib/default.nix index d00c4abec0a96..908dcbba29a39 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -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; diff --git a/pkgs/development/compilers/rust/default.nix b/pkgs/development/compilers/rust/default.nix index d08b63dd643f9..feafeeea05d0f 100644 --- a/pkgs/development/compilers/rust/default.nix +++ b/pkgs/development/compilers/rust/default.nix @@ -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; diff --git a/pkgs/development/ruby-modules/gem/default.nix b/pkgs/development/ruby-modules/gem/default.nix index 158d5891ec3ac..952e421f3b13a 100644 --- a/pkgs/development/ruby-modules/gem/default.nix +++ b/pkgs/development/ruby-modules/gem/default.nix @@ -22,7 +22,7 @@ , ruby, bundler } @ defs: -lib.makeOverridable ( +lib.makeOverridableWithName "overrideRubyGemAttrs" ( { name ? null , gemName diff --git a/pkgs/top-level/lua-packages.nix b/pkgs/top-level/lua-packages.nix index 7cf47c26c6fc4..b71157362454e 100644 --- a/pkgs/top-level/lua-packages.nix +++ b/pkgs/top-level/lua-packages.nix @@ -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; { diff --git a/pkgs/top-level/perl-packages.nix b/pkgs/top-level/perl-packages.nix index 17d605742154a..e5248dc6a35f6 100644 --- a/pkgs/top-level/perl-packages.nix +++ b/pkgs/top-level/perl-packages.nix @@ -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: diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix index e54a4e9fe4564..785c7f445f132 100644 --- a/pkgs/top-level/python-packages.nix +++ b/pkgs/top-level/python-packages.nix @@ -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;