Added overrideScope for callPackageWith

Consequently removing several ad-hoc definitions of the same concept.
This commit is contained in:
Will Fancher 2017-07-12 23:58:54 -04:00
parent 5a5f8613e0
commit 8b764960e9
4 changed files with 52 additions and 64 deletions

View file

@ -50,13 +50,17 @@ rec {
}
else { }));
# Like `makeOverridable`, except a `self` argument is passed to `f`,
# which represents the fixed point result, even after using `extend`
# or `override`.
#
# Also, an `interface` function is taken as an argument, paralleling
# the `interface` argument to `makeExtensibleWithInterface`. This Is
# mostly useful for adding new `override` style functions,
# e.g. `overrideScope`.
makeOverridableWithInterface = interface: f: origArgs: let
# Like `makeOverridable`, but provides the function with the `self`
# argument. `f` is called with the new `self` whenever an override
# or extension is added.
makeOverridableWithSelf = f: origArgs: let
interface = {val, args, ...}: overridePackage:
addOverrideFuncs = {val, args, ...}: overridePackage:
(lib.optionalAttrs (builtins.isAttrs val) (val // {
extend = f: overridePackage (self: super: {
val = super.val // f self.val super.val;
@ -83,7 +87,7 @@ rec {
});
};
in lib.makeExtensibleWithInterface interface (self: {
in lib.makeExtensibleWithInterface (x: o: interface (addOverrideFuncs x o) o) (self: {
args = origArgs;
val = f self.args self.val;
});
@ -158,7 +162,22 @@ rec {
11
*/
makeOverridable = fn: makeOverridableWithSelf (args: _: fn args);
makeOverridable = fn: makeOverridableWithInterface (x: _: x) (args: _: fn args);
callPackageCommon = functionArgs: scope: f: args:
let
intersect = builtins.intersectAttrs functionArgs;
interface = val: overridePackage: val // {
overrideScope = newScope: overridePackage (self: super: {
scope = super.scope.extend newScope;
});
};
in (makeOverridableWithInterface interface f (intersect scope // args))
.overridePackage (self: super: {
inherit scope;
# Don't use super.args because that contains the original scope.
args = intersect self.scope // args;
});
/* Call the package function in the file `fn' with the required
@ -181,21 +200,34 @@ rec {
libfoo = null;
enableX11 = true;
};
On top of the additions from `makeOverridable`, an `overrideScope`
function is also added to the result. It is similar to `override`,
except that it provides `self` and `super` views to the
scope. This can't be done in `makeOverridable` because the scope
is filtered to just the arguments needed by the function before
entering `makeOverridable`. It is useful to have a view of the
scope before restriction; for example, to change versions for a
particular dependency.
foo.overrideScope (self: super: {
llvm = self.llvm_37;
})
`llvm_37` would not exist in the scope after restriction.
*/
callPackageWith = autoArgs: fn: args:
let
f = if builtins.isFunction fn then fn else import fn;
auto = builtins.intersectAttrs (builtins.functionArgs f) autoArgs;
in makeOverridable f (auto // args);
let f = if builtins.isFunction fn then fn else import fn;
in callPackageCommon (builtins.functionArgs f) autoArgs (x: _: f x) args;
# Like `callPackageWith`, but provides the function with the `self`
# argument. `fn` is called with the new `self` whenever an override
# or extension is added.
callPackageWithSelfWith = autoArgs: fn: args:
let
f = if builtins.isFunction fn then fn else import fn;
auto = builtins.intersectAttrs (builtins.functionArgs f) autoArgs;
in makeOverridableWithSelf f (auto // args);
let f = if builtins.isFunction fn then fn else import fn;
in callPackageCommon (builtins.functionArgs f) autoArgs f args;
/* Like callPackage, but for a function that returns an attribute

View file

@ -5,14 +5,9 @@ let
lib = pkgs.callPackage ./lib.nix {};
# FIXME: add support for overrideScope
callPackageWithScope = scope: drv: args: stdenv.lib.callPackageWith scope drv args;
mkScope = scope: pkgs // scope;
packages = self:
let
defaultScope = mkScope self;
callPackage = drv: args: callPackageWithScope defaultScope drv args;
callPackage = stdenv.lib.callPackageWith (pkgs // self);
in
import ./hex-packages.nix {
inherit pkgs stdenv callPackage;

View file

@ -13,7 +13,7 @@
# return value: a function from self to the package set
self: let
inherit (stdenv.lib) fix' extends makeOverridable;
inherit (stdenv.lib) fix' extends makeOverridable callPackageWith;
inherit (import ./lib.nix { inherit pkgs; }) overrideCabal;
mkDerivationImpl = pkgs.callPackage ./generic-builder.nix {
@ -45,39 +45,9 @@ self: let
mkDerivation = makeOverridable mkDerivationImpl;
# manualArgs are the arguments that were explictly passed to `callPackage`, like:
#
# callPackage foo { bar = null; };
#
# here `bar` is a manual argument.
callPackageWithScope = scope: fn: manualArgs:
let
# this code is copied from callPackage in lib/customisation.nix
#
# we cannot use `callPackage` here because we want to call `makeOverridable`
# on `drvScope` (we cannot add `overrideScope` after calling `callPackage` because then it is
# lost on `.override`) but determine the auto-args based on `drv` (the problem here
# is that nix has no way to "passthrough" args while preserving the reflection
# info that callPackage uses to determine the arguments).
drv = if builtins.isFunction fn then fn else import fn;
auto = builtins.intersectAttrs (builtins.functionArgs drv) scope;
# this wraps the `drv` function to add a `overrideScope` function to the result.
drvScope = allArgs: drv allArgs // {
overrideScope = f:
let newScope = mkScope (fix' (extends f scope.__unfix__));
# note that we have to be careful here: `allArgs` includes the auto-arguments that
# weren't manually specified. If we would just pass `allArgs` to the recursive call here,
# then we wouldn't look up any packages in the scope in the next interation, because it
# appears as if all arguments were already manually passed, so the scope change would do
# nothing.
in callPackageWithScope newScope drv manualArgs;
};
in stdenv.lib.makeOverridable drvScope (auto // manualArgs);
mkScope = scope: pkgs // pkgs.xorg // pkgs.gnome2 // scope;
defaultScope = mkScope self;
callPackage = drv: args: callPackageWithScope defaultScope drv args;
callPackage = drv: args: callPackageWith defaultScope drv args;
withPackages = packages: callPackage ./with-packages-wrapper.nix {
inherit (self) llvmPackages;

View file

@ -1,17 +1,8 @@
{ pkgs, idris, overrides ? (self: super: {}) }: let
inherit (pkgs.lib) callPackageWith fix' extends;
/* Taken from haskell-modules/default.nix, should probably abstract this away */
callPackageWithScope = scope: drv: args: (callPackageWith scope drv args) // {
overrideScope = f: callPackageWithScope (mkScope (fix' (extends f scope.__unfix__))) drv args;
};
mkScope = scope : pkgs // pkgs.xorg // pkgs.gnome2 // scope;
idrisPackages = self: let
defaultScope = mkScope self;
callPackage = callPackageWithScope defaultScope;
callPackage = callPackageWith (pkgs // pkgs.xorg // pkgs.gnome2 // self);
builtins_ = pkgs.lib.mapAttrs self.build-builtin-package {
prelude = [];