From eb90f5c08e3741c42655b250c19fcd0243a472b1 Mon Sep 17 00:00:00 2001
From: Matthew Bauer <mjbauer95@gmail.com>
Date: Thu, 14 Jun 2018 22:17:01 -0400
Subject: [PATCH 1/5] all-packages: add pkgsCross

---
 pkgs/top-level/all-packages.nix | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index d3d40662053e..86b5ee6a8b08 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -31,6 +31,11 @@ with pkgs;
     then pkgsi686Linux.callPackage
     else throw "callPackage_i686 not supported on system '${stdenv.system}'";
 
+  # Useful attribute of systems to cross compile to.
+  pkgsCross = lib.mapAttrs (n: crossSystem:
+              nixpkgsFun { inherit crossSystem; })
+              lib.systems.examples;
+
   # A stdenv capable of building 32-bit binaries.  On x86_64-linux,
   # it uses GCC compiled with multilib support; on i686-linux, it's
   # just the plain stdenv.

From d665e80450efcfeed9b245aecb6218cb82e1a24c Mon Sep 17 00:00:00 2001
From: Matthew Bauer <mjbauer95@gmail.com>
Date: Mon, 25 Jun 2018 14:18:51 -0400
Subject: [PATCH 2/5] top-level: move cross pkgs to overlay

---
 pkgs/top-level/all-packages.nix | 5 -----
 pkgs/top-level/stage.nix        | 7 +++++++
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 86b5ee6a8b08..d3d40662053e 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -31,11 +31,6 @@ with pkgs;
     then pkgsi686Linux.callPackage
     else throw "callPackage_i686 not supported on system '${stdenv.system}'";
 
-  # Useful attribute of systems to cross compile to.
-  pkgsCross = lib.mapAttrs (n: crossSystem:
-              nixpkgsFun { inherit crossSystem; })
-              lib.systems.examples;
-
   # A stdenv capable of building 32-bit binaries.  On x86_64-linux,
   # it uses GCC compiled with multilib support; on i686-linux, it's
   # just the plain stdenv.
diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix
index f9941a1c500d..ecb3541cbb3d 100644
--- a/pkgs/top-level/stage.nix
+++ b/pkgs/top-level/stage.nix
@@ -117,6 +117,12 @@ let
     lib.optionalAttrs allowCustomOverrides
       ((config.packageOverrides or (super: {})) super);
 
+  # Convenient way to reference cross
+  # Used in aliases for now but should not be used in Nixpkgs.
+  cross = self: super: { pkgsCross = lib.mapAttrs (n: crossSystem:
+                                     nixpkgsFun { inherit crossSystem; })
+                                     lib.systems.examples; };
+
   # The complete chain of package set builders, applied from top to bottom.
   # stdenvOverlays must be last as it brings package forward from the
   # previous bootstrapping phases which have already been overlayed.
@@ -127,6 +133,7 @@ let
     trivialBuilders
     splice
     allPackages
+    cross
     aliases
     configOverrides
   ] ++ overlays ++ [

From b6b195059468d610309d70db99690111cc4a6714 Mon Sep 17 00:00:00 2001
From: Matthew Bauer <mjbauer95@gmail.com>
Date: Mon, 25 Jun 2018 14:22:51 -0400
Subject: [PATCH 3/5] top-level: add extraPkgs to stage.nix

---
 pkgs/top-level/stage.nix | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix
index ecb3541cbb3d..a9d1988ad5f4 100644
--- a/pkgs/top-level/stage.nix
+++ b/pkgs/top-level/stage.nix
@@ -117,11 +117,17 @@ let
     lib.optionalAttrs allowCustomOverrides
       ((config.packageOverrides or (super: {})) super);
 
-  # Convenient way to reference cross
-  # Used in aliases for now but should not be used in Nixpkgs.
-  cross = self: super: { pkgsCross = lib.mapAttrs (n: crossSystem:
-                                     nixpkgsFun { inherit crossSystem; })
-                                     lib.systems.examples; };
+  # Convenience attributes for instantitating nixpkgs. Each of these
+  # will instantiate a new version of allPackages. They map example
+  # attributes to their own thing.
+  extraPkgs = self: super: {
+     pkgsCross = lib.mapAttrs (n: crossSystem:
+                              nixpkgsFun { inherit crossSystem; })
+                              lib.systems.examples;
+     pkgsLocal = lib.mapAttrs (n: localSystem:
+                              nixpkgsFun { inherit localSystem; })
+                              lib.systems.examples;
+  };
 
   # The complete chain of package set builders, applied from top to bottom.
   # stdenvOverlays must be last as it brings package forward from the
@@ -133,7 +139,7 @@ let
     trivialBuilders
     splice
     allPackages
-    cross
+    extraPkgs
     aliases
     configOverrides
   ] ++ overlays ++ [

From d1bd9faf6199096adc547ccd60ca850ef18324e4 Mon Sep 17 00:00:00 2001
From: Matthew Bauer <mjbauer95@gmail.com>
Date: Mon, 25 Jun 2018 14:32:35 -0400
Subject: [PATCH 4/5] stage: move old stuff to stage.nix

---
 pkgs/top-level/all-packages.nix | 17 -----------------
 pkgs/top-level/stage.nix        | 15 +++++++++++++++
 2 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index d3d40662053e..f8a814ebf056 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -12,25 +12,8 @@ with pkgs;
 
 {
 
-  # Allow callPackage to fill in the pkgs argument
   inherit pkgs;
 
-
-  # Override system. This is useful to build i686 packages on x86_64-linux.
-  forceSystem = system: kernel: nixpkgsFun {
-    localSystem = {
-      inherit system;
-      platform = platform // { kernelArch = kernel; };
-    };
-  };
-
-  # Used by wine, firefox with debugging version of Flash, ...
-  pkgsi686Linux = forceSystem "i686-linux" "i386";
-
-  callPackage_i686 = if stdenv.system == "i686-linux" || stdenv.system == "x86_64-linux"
-    then pkgsi686Linux.callPackage
-    else throw "callPackage_i686 not supported on system '${stdenv.system}'";
-
   # A stdenv capable of building 32-bit binaries.  On x86_64-linux,
   # it uses GCC compiled with multilib support; on i686-linux, it's
   # just the plain stdenv.
diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix
index a9d1988ad5f4..551a4a6945be 100644
--- a/pkgs/top-level/stage.nix
+++ b/pkgs/top-level/stage.nix
@@ -117,6 +117,14 @@ let
     lib.optionalAttrs allowCustomOverrides
       ((config.packageOverrides or (super: {})) super);
 
+  # Override system. This is useful to build i686 packages on x86_64-linux.
+  forceSystem = system: kernel: nixpkgsFun {
+    localSystem = {
+      inherit system;
+      platform = stdenv.hostPlatform.platform // { kernelArch = kernel; };
+    };
+  };
+
   # Convenience attributes for instantitating nixpkgs. Each of these
   # will instantiate a new version of allPackages. They map example
   # attributes to their own thing.
@@ -127,6 +135,13 @@ let
      pkgsLocal = lib.mapAttrs (n: localSystem:
                               nixpkgsFun { inherit localSystem; })
                               lib.systems.examples;
+
+     # Used by wine, firefox with debugging version of Flash, ...
+     pkgsi686Linux = forceSystem "i686-linux" "i386";
+
+     callPackage_i686 = if stdenv.system == "i686-linux" || stdenv.system == "x86_64-linux"
+       then self.pkgsi686Linux.callPackage
+       else throw "callPackage_i686 not supported on system '${stdenv.system}'";
   };
 
   # The complete chain of package set builders, applied from top to bottom.

From 18af089de75270523a127a76b62da32165f1f548 Mon Sep 17 00:00:00 2001
From: Matthew Bauer <mjbauer95@gmail.com>
Date: Mon, 25 Jun 2018 14:50:50 -0400
Subject: [PATCH 5/5] stage: remove nixpkgsFun

also inherit forceSystem for some GNU Hurd stuff
---
 pkgs/top-level/all-packages.nix | 18 ++++++------------
 pkgs/top-level/stage.nix        |  8 +++-----
 2 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index f8a814ebf056..9224793c8c01 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -5,13 +5,14 @@
  * to merges. Please use the full-text search of your editor. ;)
  * Hint: ### starts category names.
  */
-{ lib, nixpkgsFun, noSysDirs, config}:
+{ lib, noSysDirs, config}:
 self: pkgs:
 
 with pkgs;
 
 {
 
+  # Allow callPackage to fill in the pkgs argument
   inherit pkgs;
 
   # A stdenv capable of building 32-bit binaries.  On x86_64-linux,
@@ -2404,7 +2405,7 @@ with pkgs;
   fluentd = callPackage ../tools/misc/fluentd { };
 
   flvstreamer = callPackage ../tools/networking/flvstreamer { };
-  
+
   hmetis = callPackage_i686 ../applications/science/math/hmetis { };
 
   libbsd = callPackage ../development/libraries/libbsd { };
@@ -3703,7 +3704,7 @@ with pkgs;
   lzip = callPackage ../tools/compression/lzip { };
 
   luxcorerender = callPackage ../tools/graphics/luxcorerender { };
-  
+
   xz = callPackage ../tools/compression/xz { };
 
   lz4 = callPackage ../tools/compression/lz4 { };
@@ -4968,7 +4969,7 @@ with pkgs;
   securefs = callPackage ../tools/filesystems/securefs { };
 
   seexpr = callPackage ../development/compilers/seexpr { };
- 
+
   setroot = callPackage  ../tools/X11/setroot { };
 
   setserial = callPackage ../tools/system/setserial { };
@@ -5873,14 +5874,7 @@ with pkgs;
   xbursttools = callPackage ../tools/misc/xburst-tools {
     # It needs a cross compiler for mipsel to build the firmware it will
     # load into the Ben Nanonote
-    gccCross =
-      let
-        pkgsCross = nixpkgsFun {
-          # Ben Nanonote system
-          crossSystem = lib.systems.examples.ben-nanonote;
-        };
-      in
-        pkgsCross.buildPackages.gccCrossStageStatic;
+    gccCross = pkgsCross.ben-nanonote.buildPackages.gccCrossStageStatic;
   };
 
   xclip = callPackage ../tools/misc/xclip { };
diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix
index 551a4a6945be..c209b05b09c5 100644
--- a/pkgs/top-level/stage.nix
+++ b/pkgs/top-level/stage.nix
@@ -93,7 +93,7 @@ let
 
   allPackages = self: super:
     let res = import ./all-packages.nix
-      { inherit lib nixpkgsFun noSysDirs config; }
+      { inherit lib noSysDirs config; }
       res self;
     in res;
 
@@ -138,10 +138,8 @@ let
 
      # Used by wine, firefox with debugging version of Flash, ...
      pkgsi686Linux = forceSystem "i686-linux" "i386";
-
-     callPackage_i686 = if stdenv.system == "i686-linux" || stdenv.system == "x86_64-linux"
-       then self.pkgsi686Linux.callPackage
-       else throw "callPackage_i686 not supported on system '${stdenv.system}'";
+     callPackage_i686 = self.pkgsi686Linux.callPackage;
+     inherit forceSystem;
   };
 
   # The complete chain of package set builders, applied from top to bottom.