From 08021dd8253b6fa6ab625cd925e4bd3f09f1d8b4 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Tue, 11 Jul 2017 21:33:26 -0400 Subject: [PATCH 1/7] Added `makeExtensibleWithInterface` --- lib/fixed-points.nix | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/fixed-points.nix b/lib/fixed-points.nix index a11b5a6f4bd..5039a45cc3f 100644 --- a/lib/fixed-points.nix +++ b/lib/fixed-points.nix @@ -71,8 +71,19 @@ rec { # Same as `makeExtensible` but the name of the extending attribute is # customized. - makeExtensibleWithCustomName = extenderName: rattrs: - fix' rattrs // { - ${extenderName} = f: makeExtensibleWithCustomName extenderName (extends f rattrs); - }; + makeExtensibleWithCustomName = extenderName: makeExtensibleWithInterface + (fixedPoint: extend: fixedPoint // { ${extenderName} = extend; }); + + # Similar to `makeExtensible`, but expects you to implement the + # final interface for the result. Specifically, it takes an extra + # argument: a function that takes the final result and the `extend` + # function as arguments, and returns a transformed result + # (preferably one that contains the `extend` function). This is + # mainly useful for getting to choose what to name the `extend` + # function in the resulting attribute set. But it's also useful for + # having an internal structure that extensions can see, but the user + # facing code cannot. + makeExtensibleWithInterface = interface: fext: interface + (fix' fext) + (f: makeExtensibleWithInterface interface (extends f fext)); } From 927c4f83d8d62b45229a931d9c295ad9a2d6d541 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Tue, 11 Jul 2017 22:12:32 -0400 Subject: [PATCH 2/7] Improved `makeOverridable` with `extend` and `overridePackage` --- lib/customisation.nix | 115 +++++++++++++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 24 deletions(-) diff --git a/lib/customisation.nix b/lib/customisation.nix index 98a46ca6c61..124e8596f49 100644 --- a/lib/customisation.nix +++ b/lib/customisation.nix @@ -51,9 +51,46 @@ rec { else { })); - /* `makeOverridable` takes a function from attribute set to attribute set and - injects `override` attibute which can be used to override arguments of - the function. + # 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: + (lib.optionalAttrs (builtins.isAttrs val) (val // { + extend = f: overridePackage (self: super: { + val = super.val // f self.val super.val; + }); + + overrideDerivation = newArgs: overridePackage (self: super: { + val = lib.overrideDerivation super.val newArgs; + }); + })) // (lib.optionalAttrs (builtins.isFunction val) { + __functor = _: val; + extend = throw "extend not yet supported for functors"; + overrideDerivation = throw "overrideDerivation not yet supported for functors"; + }) // { + inherit overridePackage; + + override = newArgs: overridePackage (self: super: { + args = super.args // + (if builtins.isFunction newArgs then newArgs super.args else newArgs); + }); + }; + + in lib.makeExtensibleWithInterface interface (self: { + args = origArgs; + val = f self.args self.val; + }); + + + /* `makeOverridable` takes a function from attribute set to + attribute set and injects 4 attributes which can be used to + override arguments and return values of the function. + + + 1. `override` allows you to change what arguments were passed to + the function and acquire the new result. nix-repl> x = {a, b}: { result = a + b; } @@ -65,28 +102,58 @@ rec { nix-repl> y.override { a = 10; } { override = «lambda»; overrideDerivation = «lambda»; result = 12; } - Please refer to "Nixpkgs Contributors Guide" section - ".overrideDerivation" to learn about `overrideDerivation` and caveats - related to its use. + + 2. `extend` changes the results of the function, giving you a + view of the original result and a view of the eventual final + result. It is meant to do the same thing as + `makeExtensible`. That is, it lets you add to or change the + return value, such that previous extensions are consistent with + the final view, rather than being based on outdated + values. "Outdated" values come from the `super` argument, which + must be used when you are attempting to modify and old value. And + the final values come from the `self` argument, which recursively + refers to what all extensions combined return. + + nix-repl> obj = makeOverridable (args: { }) { } + + nix-repl> obj = obj.extend (self: super: { foo = "foo"; }) + + nix-repl> obj.foo + "foo" + + nix-repl> obj = obj.extend (self: super: { foo = super.foo + " + "; bar = "bar"; foobar = self.foo + self.bar; }) + + nix-repl> obj + { bar = "bar"; foo = "foo + "; foobar = "foo + bar"; ... } # Excess omitted + + + 3. `overrideDerivation`: Please refer to "Nixpkgs Contributors + Guide" section ".overrideDerivation" to learn about + `overrideDerivation` and caveats related to its use. + + + 4. `overridePackage` is by far the most powerful of the four, as + it exposes a deeper structure. It provides `self` and `super` + views of both the arguments and return value of the function, + allowing you to change both in one override; you can even have + overrides for one based on overrides for the other. The type of + `self`, `super`, and the return value are all: + `{ args :: argumentsToF, val :: returnValueOfF }` + + nix-repl> obj = makeOverridable ({a, b}: {inherit a b;}) {a = 1; b = 3;} + + nix-repl> obj = obj.overridePackage (self: super: { args = super.args // {b = self.val.a;}; }) + + nix-repl> obj.b + 1 + + nix-repl> obj = obj.overridePackage (self: super: { val = super.val // {a = self.args.a + 10;}; }) + + nix-repl> obj.b + 11 + */ - makeOverridable = f: origArgs: - let - ff = f origArgs; - overrideWith = newArgs: origArgs // (if builtins.isFunction newArgs then newArgs origArgs else newArgs); - in - if builtins.isAttrs ff then (ff // { - override = newArgs: makeOverridable f (overrideWith newArgs); - overrideDerivation = fdrv: - makeOverridable (args: overrideDerivation (f args) fdrv) origArgs; - ${if ff ? overrideAttrs then "overrideAttrs" else null} = fdrv: - makeOverridable (args: (f args).overrideAttrs fdrv) origArgs; - }) - else if builtins.isFunction ff then { - override = newArgs: makeOverridable f (overrideWith newArgs); - __functor = self: ff; - overrideDerivation = throw "overrideDerivation not yet supported for functors"; - } - else ff; + makeOverridable = fn: makeOverridableWithSelf (args: _: fn args); /* Call the package function in the file `fn' with the required From af479c182ff0391aefd070f5219e79d15d655c75 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Tue, 11 Jul 2017 22:27:05 -0400 Subject: [PATCH 3/7] Added `callPackageWithSelfWith` and `callPackageWithSelf` --- lib/customisation.nix | 9 +++++++++ pkgs/top-level/splice.nix | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/lib/customisation.nix b/lib/customisation.nix index 124e8596f49..66260c8a486 100644 --- a/lib/customisation.nix +++ b/lib/customisation.nix @@ -183,6 +183,15 @@ rec { auto = builtins.intersectAttrs (builtins.functionArgs f) autoArgs; in makeOverridable f (auto // 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); + /* Like callPackage, but for a function that returns an attribute set of derivations. The override function is added to the diff --git a/pkgs/top-level/splice.nix b/pkgs/top-level/splice.nix index 43951100de3..329a83e5a31 100644 --- a/pkgs/top-level/splice.nix +++ b/pkgs/top-level/splice.nix @@ -79,7 +79,11 @@ in # `newScope' for sets of packages in `pkgs' (see e.g. `gnome' below). callPackage = pkgs.newScope {}; + callPackageWithSelf = pkgs.newScopeWithSelf {}; + callPackages = lib.callPackagesWith splicedPackages; newScope = extra: lib.callPackageWith (splicedPackages // extra); + + newScopeWithSelf = extra: lib.callPackageWithSelfWith (splicedPackages // extra); } From e11dbc37103687fb0c57dce85209086b0d691a34 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Tue, 11 Jul 2017 22:27:44 -0400 Subject: [PATCH 4/7] Update `haskellPackages` to use new features of `makeOverridable` Instead of manually using `makeExtensible`, which broke `override`. Fixes #26561 --- pkgs/development/haskell-modules/default.nix | 20 +++++----- pkgs/top-level/all-packages.nix | 4 +- pkgs/top-level/haskell-packages.nix | 40 ++++++++++---------- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/pkgs/development/haskell-modules/default.nix b/pkgs/development/haskell-modules/default.nix index 32fa46f111d..d68b10b386e 100644 --- a/pkgs/development/haskell-modules/default.nix +++ b/pkgs/development/haskell-modules/default.nix @@ -7,6 +7,8 @@ , configurationNix ? import ./configuration-nix.nix }: +self: # Provided by `callPackageWithSelf` + let inherit (stdenv.lib) extends makeExtensible; @@ -14,19 +16,15 @@ let haskellPackages = makePackageSet { package-set = initialPackages; - inherit ghc extensible-self; + extensible-self = self; + inherit ghc; }; commonConfiguration = configurationCommon { inherit pkgs; }; nixConfiguration = configurationNix { inherit pkgs; }; - extensible-self = makeExtensible - (extends overrides - (extends packageSetConfig - (extends compilerConfig - (extends commonConfiguration - (extends nixConfiguration haskellPackages))))); - -in - - extensible-self +in (extends overrides + (extends packageSetConfig + (extends compilerConfig + (extends commonConfiguration + (extends nixConfiguration haskellPackages))))) self diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index c783b3cb916..30e9d327c6e 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -5481,9 +5481,7 @@ with pkgs; haskell = callPackage ./haskell-packages.nix { }; - haskellPackages = haskell.packages.ghc802.override { - overrides = config.haskellPackageOverrides or (self: super: {}); - }; + haskellPackages = haskell.packages.ghc802.extend (config.haskellPackageOverrides or (self: super: {})); inherit (haskellPackages) ghc; diff --git a/pkgs/top-level/haskell-packages.nix b/pkgs/top-level/haskell-packages.nix index 099e45f1022..36ae1979ba0 100644 --- a/pkgs/top-level/haskell-packages.nix +++ b/pkgs/top-level/haskell-packages.nix @@ -1,4 +1,4 @@ -{ pkgs, callPackage, stdenv, buildPlatform, targetPlatform }: +{ pkgs, callPackage, callPackageWithSelf, stdenv, buildPlatform, targetPlatform }: let # These are attributes in compiler and packages that don't support integer-simple. integerSimpleExcludes = [ @@ -118,79 +118,79 @@ in rec { packages = { # Support for this compiler is broken, because it can't deal with directory-based package databases. - # ghc6104 = callPackage ../development/haskell-modules { ghc = compiler.ghc6104; }; - ghc6123 = callPackage ../development/haskell-modules { + # ghc6104 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc6104; }; + ghc6123 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc6123; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-6.12.x.nix { }; }; - ghc704 = callPackage ../development/haskell-modules { + ghc704 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc704; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.0.x.nix { }; }; - ghc722 = callPackage ../development/haskell-modules { + ghc722 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc722; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.2.x.nix { }; }; - ghc742 = callPackage ../development/haskell-modules { + ghc742 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc742; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.4.x.nix { }; }; - ghc763 = callPackage ../development/haskell-modules { + ghc763 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc763; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.6.x.nix { }; }; - ghc783 = callPackage ../development/haskell-modules { + ghc783 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc783; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.8.x.nix { }; }; - ghc784 = callPackage ../development/haskell-modules { + ghc784 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc784; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.8.x.nix { }; }; - ghc7102 = callPackage ../development/haskell-modules { + ghc7102 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc7102; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.10.x.nix { }; }; - ghc7103 = callPackage ../development/haskell-modules { + ghc7103 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc7103; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.10.x.nix { }; }; - ghc801 = callPackage ../development/haskell-modules { + ghc801 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc801; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-8.0.x.nix { }; }; - ghc802 = callPackage ../development/haskell-modules { + ghc802 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc802; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-8.0.x.nix { }; }; - ghc821 = callPackage ../development/haskell-modules { + ghc821 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc821; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-8.2.x.nix { }; }; - ghcHEAD = callPackage ../development/haskell-modules { + ghcHEAD = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghcHEAD; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-head.nix { }; }; # TODO Support for multiple variants here - ghcCross = callPackage ../development/haskell-modules { + ghcCross = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghcHEAD.crossCompiler; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-head.nix { }; }; - ghcCross821 = callPackage ../development/haskell-modules { + ghcCross821 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc821.crossCompiler; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-8.2.x.nix { }; }; - ghcjs = callPackage ../development/haskell-modules { + ghcjs = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghcjs; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.10.x.nix { }; packageSetConfig = callPackage ../development/haskell-modules/configuration-ghcjs.nix { }; }; - ghcjsHEAD = callPackage ../development/haskell-modules { + ghcjsHEAD = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghcjsHEAD; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-8.0.x.nix { }; packageSetConfig = callPackage ../development/haskell-modules/configuration-ghcjs.nix { }; }; - ghcHaLVM240 = callPackage ../development/haskell-modules { + ghcHaLVM240 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghcHaLVM240; compilerConfig = callPackage ../development/haskell-modules/configuration-halvm-2.4.0.nix { }; }; From 5a5f8613e09b9a94466e564b0d8fa18da022c34d Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Wed, 12 Jul 2017 18:38:22 -0400 Subject: [PATCH 5/7] Re-added `overrideAttrs` in `makeOverridable` --- lib/customisation.nix | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/customisation.nix b/lib/customisation.nix index 66260c8a486..35538641aa8 100644 --- a/lib/customisation.nix +++ b/lib/customisation.nix @@ -65,6 +65,11 @@ rec { overrideDerivation = newArgs: overridePackage (self: super: { val = lib.overrideDerivation super.val newArgs; }); + + ${if val ? overrideAttrs then "overrideAttrs" else null} = fdrv: + overridePackage (self: super: { + val = super.val.overrideAttrs fdrv; + }); })) // (lib.optionalAttrs (builtins.isFunction val) { __functor = _: val; extend = throw "extend not yet supported for functors"; From 8b764960e93f60cb0896a8866f8e53e331726985 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Wed, 12 Jul 2017 23:58:54 -0400 Subject: [PATCH 6/7] Added `overrideScope` for `callPackageWith` Consequently removing several ad-hoc definitions of the same concept. --- lib/customisation.nix | 64 ++++++++++++++----- pkgs/development/beam-modules/default.nix | 7 +- .../haskell-modules/make-package-set.nix | 34 +--------- pkgs/development/idris-modules/default.nix | 11 +--- 4 files changed, 52 insertions(+), 64 deletions(-) diff --git a/lib/customisation.nix b/lib/customisation.nix index 35538641aa8..425c4523354 100644 --- a/lib/customisation.nix +++ b/lib/customisation.nix @@ -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 diff --git a/pkgs/development/beam-modules/default.nix b/pkgs/development/beam-modules/default.nix index f0a049bb4b2..78ab61f73a4 100644 --- a/pkgs/development/beam-modules/default.nix +++ b/pkgs/development/beam-modules/default.nix @@ -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; diff --git a/pkgs/development/haskell-modules/make-package-set.nix b/pkgs/development/haskell-modules/make-package-set.nix index 6ed8d0864ca..b259192ce24 100644 --- a/pkgs/development/haskell-modules/make-package-set.nix +++ b/pkgs/development/haskell-modules/make-package-set.nix @@ -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; diff --git a/pkgs/development/idris-modules/default.nix b/pkgs/development/idris-modules/default.nix index 3d0ea511e10..9d2ebda00c4 100644 --- a/pkgs/development/idris-modules/default.nix +++ b/pkgs/development/idris-modules/default.nix @@ -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 = []; From 05f9db601abd582be51f096affbb97eff49f2ccb Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Thu, 13 Jul 2017 18:54:04 -0400 Subject: [PATCH 7/7] Added `self` views of the interface in `makeExtensibleWithInterface` Fixing the `overrideScope` in `haskellpackages`. --- lib/customisation.nix | 51 +++++++++----------- lib/fixed-points.nix | 43 +++++++++++------ pkgs/development/haskell-modules/default.nix | 2 +- pkgs/top-level/haskell-packages.nix | 40 +++++++-------- pkgs/top-level/splice.nix | 4 +- 5 files changed, 76 insertions(+), 64 deletions(-) diff --git a/lib/customisation.nix b/lib/customisation.nix index 425c4523354..91aed8b9ca8 100644 --- a/lib/customisation.nix +++ b/lib/customisation.nix @@ -50,28 +50,22 @@ 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`. + # A more powerful version of `makeOverridable` with features similar + # to `makeExtensibleWithInterface`. makeOverridableWithInterface = interface: f: origArgs: let addOverrideFuncs = {val, args, ...}: overridePackage: (lib.optionalAttrs (builtins.isAttrs val) (val // { - extend = f: overridePackage (self: super: { + extend = f: overridePackage (_: self: super: { val = super.val // f self.val super.val; }); - overrideDerivation = newArgs: overridePackage (self: super: { + overrideDerivation = newArgs: overridePackage (_: self: super: { val = lib.overrideDerivation super.val newArgs; }); ${if val ? overrideAttrs then "overrideAttrs" else null} = fdrv: - overridePackage (self: super: { + overridePackage (_: self: super: { val = super.val.overrideAttrs fdrv; }); })) // (lib.optionalAttrs (builtins.isFunction val) { @@ -81,15 +75,15 @@ rec { }) // { inherit overridePackage; - override = newArgs: overridePackage (self: super: { + override = newArgs: overridePackage (_: self: super: { args = super.args // (if builtins.isFunction newArgs then newArgs super.args else newArgs); }); }; - in lib.makeExtensibleWithInterface (x: o: interface (addOverrideFuncs x o) o) (self: { + in lib.makeExtensibleWithInterface (x: o: interface (addOverrideFuncs x o) o) (output: self: { args = origArgs; - val = f self.args self.val; + val = f output self.args self.val; }); @@ -145,35 +139,37 @@ rec { it exposes a deeper structure. It provides `self` and `super` views of both the arguments and return value of the function, allowing you to change both in one override; you can even have - overrides for one based on overrides for the other. The type of - `self`, `super`, and the return value are all: - `{ args :: argumentsToF, val :: returnValueOfF }` + overrides for one based on overrides for the other. It also + provides the `output` view, which is the view of `self` after + passing it through the `makeOverridable` interface and adding all + the `overrideX` functions. `output` is necessary when your + overrides depend on the overridable structure of `output`. nix-repl> obj = makeOverridable ({a, b}: {inherit a b;}) {a = 1; b = 3;} - nix-repl> obj = obj.overridePackage (self: super: { args = super.args // {b = self.val.a;}; }) + nix-repl> obj = obj.overridePackage (output: self: super: { args = super.args // {b = self.val.a;}; }) nix-repl> obj.b 1 - nix-repl> obj = obj.overridePackage (self: super: { val = super.val // {a = self.args.a + 10;}; }) + nix-repl> obj = obj.overridePackage (output: self: super: { val = super.val // {a = self.args.a + 10;}; }) nix-repl> obj.b 11 */ - makeOverridable = fn: makeOverridableWithInterface (x: _: x) (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: { + overrideScope = newScope: overridePackage (_: self: super: { scope = super.scope.extend newScope; }); }; in (makeOverridableWithInterface interface f (intersect scope // args)) - .overridePackage (self: super: { + .overridePackage (output: self: super: { inherit scope; # Don't use super.args because that contains the original scope. args = intersect self.scope // args; @@ -219,15 +215,16 @@ rec { */ callPackageWith = autoArgs: fn: args: let f = if builtins.isFunction fn then fn else import fn; - in callPackageCommon (builtins.functionArgs f) autoArgs (x: _: f x) args; + in callPackageCommon (builtins.functionArgs f) autoArgs (output: x: _: f x) args; - # Like `callPackageWith`, but provides the function with the `self` - # argument. `fn` is called with the new `self` whenever an override + # Like `callPackageWith`, but provides the function with a `self` + # view of the output, which has the override functions + # injected. `fn` is called with the new output whenever an override # or extension is added. - callPackageWithSelfWith = autoArgs: fn: args: + callPackageWithOutputWith = autoArgs: fn: args: let f = if builtins.isFunction fn then fn else import fn; - in callPackageCommon (builtins.functionArgs f) autoArgs f args; + in callPackageCommon (builtins.functionArgs f) autoArgs (output: args: _: f args output ) args; /* Like callPackage, but for a function that returns an attribute diff --git a/lib/fixed-points.nix b/lib/fixed-points.nix index 5039a45cc3f..910dd24b202 100644 --- a/lib/fixed-points.nix +++ b/lib/fixed-points.nix @@ -71,19 +71,34 @@ rec { # Same as `makeExtensible` but the name of the extending attribute is # customized. - makeExtensibleWithCustomName = extenderName: makeExtensibleWithInterface - (fixedPoint: extend: fixedPoint // { ${extenderName} = extend; }); + makeExtensibleWithCustomName = extenderName: f: makeExtensibleWithInterface + (fixedPoint: extend: fixedPoint // { ${extenderName} = ext: extend (_: ext); }) + (_: f); - # Similar to `makeExtensible`, but expects you to implement the - # final interface for the result. Specifically, it takes an extra - # argument: a function that takes the final result and the `extend` - # function as arguments, and returns a transformed result - # (preferably one that contains the `extend` function). This is - # mainly useful for getting to choose what to name the `extend` - # function in the resulting attribute set. But it's also useful for - # having an internal structure that extensions can see, but the user - # facing code cannot. - makeExtensibleWithInterface = interface: fext: interface - (fix' fext) - (f: makeExtensibleWithInterface interface (extends f fext)); + # A version of `makeExtensible` that allows the function being fixed + # to return a different interface than the interface returned to the + # user. Along with `self` and `super` views of the internal + # interface, a `self` view of the output interface is also + # provided. `extend` is not added to the output by default. This is + # the job of the interface. + # + # nix-repl> foo = {a, b}: {c = a + b;} + # + # nix-repl> interface = {args, val, ...}: extend: val // {inherit extend;} + # + # nix-repl> obj = makeExtensibleWithInterface interface (output: self: { args = {a = 1; b = 2;}; val = foo self.args; }) + # + # nix-repl> obj.c + # 3 + # + # nix-repl> obj = obj.extend (output: self: super: { args = super.args // { b = output.d; }; }) + # + # nix-repl> obj = obj.extend (output: self: super: { val = super.val // { d = 10; }; }) + # + # nix-repl> { inherit (obj) c d; } + # { c = 11; d = 10; } + makeExtensibleWithInterface = interface: f: let i = interface + (fix' (f i)) + (fext: makeExtensibleWithInterface interface (i': (extends (fext i') (f i')))); + in i; } diff --git a/pkgs/development/haskell-modules/default.nix b/pkgs/development/haskell-modules/default.nix index d68b10b386e..9a67adf57ac 100644 --- a/pkgs/development/haskell-modules/default.nix +++ b/pkgs/development/haskell-modules/default.nix @@ -7,7 +7,7 @@ , configurationNix ? import ./configuration-nix.nix }: -self: # Provided by `callPackageWithSelf` +self: # Provided by `callPackageWithOutput` let diff --git a/pkgs/top-level/haskell-packages.nix b/pkgs/top-level/haskell-packages.nix index 36ae1979ba0..dde5aa52278 100644 --- a/pkgs/top-level/haskell-packages.nix +++ b/pkgs/top-level/haskell-packages.nix @@ -1,4 +1,4 @@ -{ pkgs, callPackage, callPackageWithSelf, stdenv, buildPlatform, targetPlatform }: +{ pkgs, callPackage, callPackageWithOutput, stdenv, buildPlatform, targetPlatform }: let # These are attributes in compiler and packages that don't support integer-simple. integerSimpleExcludes = [ @@ -118,79 +118,79 @@ in rec { packages = { # Support for this compiler is broken, because it can't deal with directory-based package databases. - # ghc6104 = callPackageWithSelf ../development/haskell-modules { ghc = compiler.ghc6104; }; - ghc6123 = callPackageWithSelf ../development/haskell-modules { + # ghc6104 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc6104; }; + ghc6123 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc6123; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-6.12.x.nix { }; }; - ghc704 = callPackageWithSelf ../development/haskell-modules { + ghc704 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc704; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.0.x.nix { }; }; - ghc722 = callPackageWithSelf ../development/haskell-modules { + ghc722 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc722; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.2.x.nix { }; }; - ghc742 = callPackageWithSelf ../development/haskell-modules { + ghc742 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc742; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.4.x.nix { }; }; - ghc763 = callPackageWithSelf ../development/haskell-modules { + ghc763 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc763; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.6.x.nix { }; }; - ghc783 = callPackageWithSelf ../development/haskell-modules { + ghc783 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc783; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.8.x.nix { }; }; - ghc784 = callPackageWithSelf ../development/haskell-modules { + ghc784 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc784; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.8.x.nix { }; }; - ghc7102 = callPackageWithSelf ../development/haskell-modules { + ghc7102 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc7102; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.10.x.nix { }; }; - ghc7103 = callPackageWithSelf ../development/haskell-modules { + ghc7103 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc7103; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.10.x.nix { }; }; - ghc801 = callPackageWithSelf ../development/haskell-modules { + ghc801 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc801; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-8.0.x.nix { }; }; - ghc802 = callPackageWithSelf ../development/haskell-modules { + ghc802 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc802; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-8.0.x.nix { }; }; - ghc821 = callPackageWithSelf ../development/haskell-modules { + ghc821 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc821; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-8.2.x.nix { }; }; - ghcHEAD = callPackageWithSelf ../development/haskell-modules { + ghcHEAD = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghcHEAD; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-head.nix { }; }; # TODO Support for multiple variants here - ghcCross = callPackageWithSelf ../development/haskell-modules { + ghcCross = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghcHEAD.crossCompiler; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-head.nix { }; }; - ghcCross821 = callPackageWithSelf ../development/haskell-modules { + ghcCross821 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghc821.crossCompiler; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-8.2.x.nix { }; }; - ghcjs = callPackageWithSelf ../development/haskell-modules { + ghcjs = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghcjs; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-7.10.x.nix { }; packageSetConfig = callPackage ../development/haskell-modules/configuration-ghcjs.nix { }; }; - ghcjsHEAD = callPackageWithSelf ../development/haskell-modules { + ghcjsHEAD = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghcjsHEAD; compilerConfig = callPackage ../development/haskell-modules/configuration-ghc-8.0.x.nix { }; packageSetConfig = callPackage ../development/haskell-modules/configuration-ghcjs.nix { }; }; - ghcHaLVM240 = callPackageWithSelf ../development/haskell-modules { + ghcHaLVM240 = callPackageWithOutput ../development/haskell-modules { ghc = compiler.ghcHaLVM240; compilerConfig = callPackage ../development/haskell-modules/configuration-halvm-2.4.0.nix { }; }; diff --git a/pkgs/top-level/splice.nix b/pkgs/top-level/splice.nix index 329a83e5a31..36596ec5961 100644 --- a/pkgs/top-level/splice.nix +++ b/pkgs/top-level/splice.nix @@ -79,11 +79,11 @@ in # `newScope' for sets of packages in `pkgs' (see e.g. `gnome' below). callPackage = pkgs.newScope {}; - callPackageWithSelf = pkgs.newScopeWithSelf {}; + callPackageWithOutput = pkgs.newScopeWithOutput {}; callPackages = lib.callPackagesWith splicedPackages; newScope = extra: lib.callPackageWith (splicedPackages // extra); - newScopeWithSelf = extra: lib.callPackageWithSelfWith (splicedPackages // extra); + newScopeWithOutput = extra: lib.callPackageWithOutputWith (splicedPackages // extra); }