summary refs log tree commit diff
path: root/pkgs/os-specific/linux/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/os-specific/linux/kernel')
-rw-r--r--pkgs/os-specific/linux/kernel/builder.sh149
-rw-r--r--pkgs/os-specific/linux/kernel/generate-config.pl5
-rw-r--r--pkgs/os-specific/linux/kernel/generic.nix168
-rw-r--r--pkgs/os-specific/linux/kernel/manual-config.nix322
-rw-r--r--pkgs/os-specific/linux/kernel/patches.nix46
-rw-r--r--pkgs/os-specific/linux/kernel/perf.nix7
6 files changed, 273 insertions, 424 deletions
diff --git a/pkgs/os-specific/linux/kernel/builder.sh b/pkgs/os-specific/linux/kernel/builder.sh
deleted file mode 100644
index 8fb5e9f91eb4..000000000000
--- a/pkgs/os-specific/linux/kernel/builder.sh
+++ /dev/null
@@ -1,149 +0,0 @@
-source $stdenv/setup
-
-
-makeFlags="ARCH=$arch SHELL=/bin/sh KBUILD_BUILD_VERSION=1-NixOS $makeFlags"
-if [ -n "$crossConfig" ]; then
-  makeFlags="$makeFlags CROSS_COMPILE=$crossConfig-"
-fi
-
-postPatch() {
-    # Makefiles are full of /bin/pwd, /bin/false, /bin/bash, etc.
-    # Patch these away, assuming the tools are in $PATH.
-    for mf in $(find -name Makefile); do
-        echo "stripping FHS paths in \`$mf'..."
-        sed -i "$mf" -e 's|/usr/bin/||g ; s|/bin/||g'
-    done
-}
-
-configurePhase() {
-    if test -n "$preConfigure"; then
-        eval "$preConfigure"
-    fi
-
-    export INSTALL_PATH=$out
-    export INSTALL_MOD_PATH=$out
-
-    # Set our own localversion, if specified.
-    rm -f localversion*
-    if test -n "$localVersion"; then
-        echo "$localVersion" > localversion-nix
-    fi
-
-    # Patch kconfig to print "###" after every question so that
-    # generate-config.pl can answer them.
-    sed -e '/fflush(stdout);/i\printf("###");' -i scripts/kconfig/conf.c
-
-    # Get a basic config file for later refinement with $generateConfig.
-    make $kernelBaseConfig ARCH=$arch
-
-    # Create the config file.
-    echo "generating kernel configuration..."
-    echo "$kernelConfig" > kernel-config
-    DEBUG=1 ARCH=$arch KERNEL_CONFIG=kernel-config AUTO_MODULES=$autoModules \
-        perl -w $generateConfig
-}
-
-
-installPhase() {
-
-    mkdir -p $out
-
-    # New kernel versions have a combined tree for i386 and x86_64.
-    archDir=$arch
-    if test -e arch/x86 -a \( "$arch" = i386 -o "$arch" = x86_64 \); then
-        archDir=x86
-    fi
-
-
-    # Copy the bzImage and System.map.
-    cp System.map $out
-    if test "$arch" = um; then
-        mkdir -p $out/bin
-        cp linux $out/bin
-    elif test "$kernelTarget" != "vmlinux"; then
-        # In any case we copy the 'vmlinux' ELF in the next lines
-        cp arch/$archDir/boot/$kernelTarget $out
-    fi
-
-    cp vmlinux $out
-
-    if grep -q "CONFIG_MODULES=y" .config; then
-        # Install the modules in $out/lib/modules.
-        make modules_install \
-            DEPMOD=$kmod/sbin/depmod \
-            $makeFlags "${makeFlagsArray[@]}" \
-            $installFlags "${installFlagsArray[@]}"
-
-        if test -z "$dontStrip"; then
-            # Strip the kernel modules.
-        echo "Stripping kernel modules..."
-        if [ -z "$crossConfig" ]; then
-                find $out -name "*.ko" -print0 | xargs -0 strip -S
-        else
-                find $out -name "*.ko" -print0 | xargs -0 $crossConfig-strip -S
-        fi
-        fi
-
-        # move this to install later on
-        # largely copied from early FC3 kernel spec files
-        version=$(cd $out/lib/modules && ls -d *)
-
-        # remove symlinks and create directories
-        rm -f $out/lib/modules/$version/build
-        rm -f $out/lib/modules/$version/source
-        mkdir $out/lib/modules/$version/build
-
-        # copy config
-        cp .config $out/lib/modules/$version/build/.config
-        ln -s $out/lib/modules/$version/build/.config $out/config
-
-        if test "$arch" != um; then
-            # copy all Makefiles and Kconfig files
-            ln -s $out/lib/modules/$version/build $out/lib/modules/$version/source
-            cp --parents `find  -type f -name Makefile -o -name "Kconfig*"` $out/lib/modules/$version/build
-            cp Module.symvers $out/lib/modules/$version/build
-
-        if test "$dontStrip" = "1"; then
-            # copy any debugging info that can be found
-            cp --parents -rv `find -name \*.debug -o -name debug.a`     \
-               "$out/lib/modules/$version/build"
-        fi
-
-            # weed out unneeded stuff
-            rm -rf $out/lib/modules/$version/build/Documentation
-            rm -rf $out/lib/modules/$version/build/scripts
-            rm -rf $out/lib/modules/$version/build/include
-
-            # copy architecture dependent files
-            cp -a arch/$archDir/scripts $out/lib/modules/$version/build/ || true
-            cp -a arch/$archDir/*lds $out/lib/modules/$version/build/ || true
-            cp -a arch/$archDir/Makefile*.cpu $out/lib/modules/$version/build/arch/$archDir/ || true
-            cp -a --parents arch/$archDir/kernel/asm-offsets.s $out/lib/modules/$version/build/arch/$archDir/kernel/ || true
-
-            # copy scripts
-            rm -f scripts/*.o
-            rm -f scripts/*/*.o
-            cp -a scripts $out/lib/modules/$version/build
-
-            # copy include files
-            includeDir=$out/lib/modules/$version/build/include
-            mkdir -p $includeDir
-            (cd include && cp -a * $includeDir)
-        (cd arch/$archDir/include && cp -a * $includeDir || true)
-        (cd arch/$archDir/include && cp -a asm/* $includeDir/asm/ || true)
-        (cd arch/$archDir/include && cp -a generated/asm/* $includeDir/asm/ || true)
-        (cd arch/$archDir/include/asm/mach-generic && cp -a * $includeDir/ || true)
-            # include files for special arm architectures 
-            if [ "$archDir" == "arm" ]; then
-                cp -a --parents arch/arm/mach-*/include $out/lib/modules/$version/build
-            fi
-        fi
-    fi
-
-    if test -n "$postInstall"; then
-        eval "$postInstall";
-    fi
-}
-
-
-genericBuild
diff --git a/pkgs/os-specific/linux/kernel/generate-config.pl b/pkgs/os-specific/linux/kernel/generate-config.pl
index 78663098fb31..20abe1015c3f 100644
--- a/pkgs/os-specific/linux/kernel/generate-config.pl
+++ b/pkgs/os-specific/linux/kernel/generate-config.pl
@@ -11,6 +11,9 @@
 
 use strict;
 use IPC::Open2;
+use Cwd;
+
+my $wd = getcwd;
 
 my $debug = $ENV{'DEBUG'};
 my $autoModules = $ENV{'AUTO_MODULES'};
@@ -36,7 +39,7 @@ close ANSWERS;
 sub runConfig {
 
     # Run `make config'.
-    my $pid = open2(\*IN, \*OUT, "make config SHELL=bash ARCH=$ENV{ARCH}");
+    my $pid = open2(\*IN, \*OUT, "make -C $ENV{SRC} O=$wd config SHELL=bash ARCH=$ENV{ARCH}");
 
     # Parse the output, look for questions and then send an
     # appropriate answer.
diff --git a/pkgs/os-specific/linux/kernel/generic.nix b/pkgs/os-specific/linux/kernel/generic.nix
index 3e1fc920a59b..42ec3de93691 100644
--- a/pkgs/os-specific/linux/kernel/generic.nix
+++ b/pkgs/os-specific/linux/kernel/generic.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, perl, mktemp, kmod, bc
+{ stdenv, perl, linuxManualConfig
 
 , # The kernel source tarball.
   src
@@ -23,20 +23,7 @@
   # symbolic name and `patch' is the actual patch.  The patch may
   # optionally be compressed with gzip or bzip2.
   kernelPatches ? []
-
-, # Allows you to set your own kernel version suffix (e.g.,
-  # "-my-kernel").
-  localVersion ? ""
-
-, preConfigure ? ""
 , extraMeta ? {}
-, ubootChooser ? null
-, postInstall ? ""
-
-, # After the builder did a 'make all' (kernel + modules)
-  # we force building the target asked: bzImage/zImage/uImage/...
-  postBuild ? "make $makeFlags $kernelTarget; make $makeFlags -C scripts unifdef"
-
 , ...
 }:
 
@@ -52,93 +39,90 @@ let
         map ({extraConfig ? "", ...}: extraConfig) kernelPatches;
     in lib.concatStringsSep "\n" ([baseConfig] ++ configFromPatches);
 
+  configfile = stdenv.mkDerivation {
+    name = "linux-config-${version}";
+
+    generateConfig = ./generate-config.pl;
+
+    kernelConfig = kernelConfigFun config;
+
+    ignoreConfigErrors = stdenv.platform.name != "pc";
+
+    nativeBuildInputs = [ perl ];
+
+    platformName = stdenv.platform.name;
+    kernelBaseConfig = stdenv.platform.kernelBaseConfig;
+    kernelTarget = stdenv.platform.kernelTarget;
+    autoModules = stdenv.platform.kernelAutoModules;
+    arch = stdenv.platform.kernelArch;
+
+    crossAttrs = let
+        cp = stdenv.cross.platform;
+      in {
+        arch = cp.kernelArch;
+        platformName = cp.name;
+        kernelBaseConfig = cp.kernelBaseConfig;
+        kernelTarget = cp.kernelTarget;
+        autoModules = cp.kernelAutoModules;
+
+        # Just ignore all options that don't apply (We are lazy).
+        ignoreConfigErrors = true;
+
+        kernelConfig = kernelConfigFun configCross;
+
+        inherit (kernel.crossDrv) src patches preUnpack;
+      };
+
+    prePatch = kernel.prePatch + ''
+      # Patch kconfig to print "###" after every question so that
+      # generate-config.pl from the generic builder can answer them.
+      sed -e '/fflush(stdout);/i\printf("###");' -i scripts/kconfig/conf.c
+    '';
+
+    inherit (kernel) src patches preUnpack;
+
+    buildPhase = ''
+      cd $buildRoot
+
+      # Get a basic config file for later refinement with $generateConfig.
+      make -C ../$sourceRoot O=$PWD $kernelBaseConfig ARCH=$arch
+
+      # Create the config file.
+      echo "generating kernel configuration..."
+      echo "$kernelConfig" > kernel-config
+      DEBUG=1 ARCH=$arch KERNEL_CONFIG=kernel-config AUTO_MODULES=$autoModules \
+           SRC=../$sourceRoot perl -w $generateConfig
+    '';
+
+    installPhase = "mv .config $out";
+  };
+
+  kernel = linuxManualConfig {
+    inherit version modDirVersion src kernelPatches;
+
+    configfile = configfile.nativeDrv or configfile;
+
+    crossConfigfile = configfile.crossDrv or configfile;
+
+    config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; };
+
+    crossConfig = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; };
+  };
+
   configWithPlatform = kernelPlatform:
     import ./common-config.nix { inherit stdenv version kernelPlatform extraConfig; };
 
   config = configWithPlatform stdenv.platform;
   configCross = configWithPlatform stdenv.cross.platform;
 
-in
-
-stdenv.mkDerivation {
-  name = "linux-${version}";
-
-  enableParallelBuilding = true;
-
   passthru = {
-    inherit version modDirVersion kernelPatches;
     # Combine the `features' attribute sets of all the kernel patches.
     features = lib.fold (x: y: (x.features or {}) // y) features kernelPatches;
+
+    meta = kernel.meta // extraMeta;
   };
 
-  builder = ./builder.sh;
-
-  generateConfig = ./generate-config.pl;
-
-  inherit preConfigure src kmod localVersion postInstall postBuild;
-
-  patches = map (p: p.patch) kernelPatches;
-
-  kernelConfig = kernelConfigFun config;
-
-  # For UML and non-PC, just ignore all options that don't apply (We are lazy).
-  ignoreConfigErrors = stdenv.platform.name != "pc";
-
-  nativeBuildInputs = [ perl mktemp bc ];
-
-  buildInputs = lib.optional (stdenv.platform.uboot != null)
-    (ubootChooser stdenv.platform.uboot);
-
-  platformName = stdenv.platform.name;
-  kernelBaseConfig = stdenv.platform.kernelBaseConfig;
-  kernelTarget = stdenv.platform.kernelTarget;
-  autoModules = stdenv.platform.kernelAutoModules;
-
-  # Should we trust platform.kernelArch? We can only do
-  # that once we differentiate i686/x86_64 in platforms.
-  arch =
-    if stdenv.system == "i686-linux" then "i386" else
-    if stdenv.system == "x86_64-linux" then "x86_64" else
-    if stdenv.isArm then "arm" else
-    if stdenv.system == "mips64el-linux" then "mips" else
-    abort "Platform ${stdenv.system} is not supported.";
-
-  crossAttrs = let
-      cp = stdenv.cross.platform;
-    in
-      assert cp.name == "sheevaplug" -> cp.uboot != null;
-    {
-      arch = cp.kernelArch;
-      platformName = cp.name;
-      kernelBaseConfig = cp.kernelBaseConfig;
-      kernelTarget = cp.kernelTarget;
-      autoModules = cp.kernelAutoModules;
-
-      # Just ignore all options that don't apply (We are lazy).
-      ignoreConfigErrors = true;
-
-      kernelConfig = kernelConfigFun configCross;
-
-      # The substitution of crossAttrs happens *after* the stdenv cross adapter sets
-      # the parameters for the usual stdenv. Thus, we need to specify
-      # the ".crossDrv" in the buildInputs here.
-      buildInputs = lib.optional (cp.uboot != null) (ubootChooser cp.uboot).crossDrv;
-    };
-
-  meta = {
-    description =
-      "The Linux kernel" +
-      (if kernelPatches == [] then "" else
-        " (with patches: "
-        + lib.concatStrings (lib.intersperse ", " (map (x: x.name) kernelPatches))
-        + ")");
-    license = "GPLv2";
-    homepage = http://www.kernel.org/;
-    maintainers = [
-      lib.maintainers.eelco
-      lib.maintainers.chaoflow
-    ];
-    platforms = lib.platforms.linux;
-  } // extraMeta;
-}
+  nativeDrv = lib.addPassthru kernel.nativeDrv passthru;
 
+  crossDrv = lib.addPassthru kernel.crossDrv passthru;
+in if kernel ? crossDrv then nativeDrv // { inherit nativeDrv crossDrv; } else lib.addPassthru kernel passthru
diff --git a/pkgs/os-specific/linux/kernel/manual-config.nix b/pkgs/os-specific/linux/kernel/manual-config.nix
index f097f2562d02..206f95661e8e 100644
--- a/pkgs/os-specific/linux/kernel/manual-config.nix
+++ b/pkgs/os-specific/linux/kernel/manual-config.nix
@@ -1,45 +1,16 @@
-{ stdenv, runCommand, nettools, bc, perl, kmod, writeTextFile }:
+{ stdenv, runCommand, nettools, bc, perl, kmod, writeTextFile, ubootChooser }:
 
 let
-  inherit (stdenv.lib)
-    hasAttr getAttr optionalAttrs optional optionalString maintainers platforms;
-
-  # Function to parse the config file into a nix expression
-  readConfig = configFile:
-    let
-      configAttrs = import "${runCommand "config.nix" {} ''
-        echo "{" > "$out"
-        while IFS='=' read key val; do
-          [ "x''${key#CONFIG_}" != "x$key" ] || continue
-          no_firstquote="''${val#\"}";
-          echo '  "'"$key"'" = "'"''${no_firstquote%\"}"'";' >> "$out"
-        done < "${configFile}"
-        echo "}" >> $out
-      ''}";
-
-      config = configAttrs // rec {
-        attrName = attr: "CONFIG_" + attr;
-
-        isSet = attr: hasAttr (attrName attr) config;
-
-        getValue = attr: if isSet attr then getAttr (attrName attr) config else null;
-
-        isYes = attr: (isSet attr) && ((getValue attr) == "y");
-
-        isNo = attr: (isSet attr) && ((getValue attr) == "n");
-
-        isModule = attr: (isSet attr) && ((getValue attr) == "m");
-
-        isEnabled = attr: (isModule attr) || (isYes attr);
-
-        isDisabled = attr: (!(isSet attr)) || (isNo attr);
-      };
-    in
-      config;
-
-in
-
-{
+  readConfig = configfile: import (runCommand "config.nix" {} ''
+    echo "{" > "$out"
+    while IFS='=' read key val; do
+      [ "x''${key#CONFIG_}" != "x$key" ] || continue
+      no_firstquote="''${val#\"}";
+      echo '  "'"$key"'" = "'"''${no_firstquote%\"}"'";' >> "$out"
+    done < "${configfile}"
+    echo "}" >> $out
+  '').outPath;
+in {
   # The kernel version
   version,
   # The version of the kernel module directory
@@ -48,127 +19,212 @@ in
   src,
   # Any patches
   kernelPatches ? [],
-  # The kernel .config file
+  # Patches for native compiling only
+  nativeKernelPatches ? [],
+  # Patches for cross compiling only
+  crossKernelPatches ? [],
+  # The native kernel .config file
   configfile,
+  # The cross kernel .config file
+  crossConfigfile ? configfile,
   # Manually specified nixexpr representing the config
   # If unspecified, this will be autodetected from the .config
-  config ? optionalAttrs allowImportFromDerivation (readConfig configfile),
+  config ? stdenv.lib.optionalAttrs allowImportFromDerivation (readConfig configfile),
+  # Cross-compiling config
+  crossConfig ? if allowImportFromDerivation then (readConfig crossConfigfile) else config,
   # Whether to utilize the controversial import-from-derivation feature to parse the config
   allowImportFromDerivation ? false
 }:
 
 let
-  installkernel = name: writeTextFile { name = "installkernel"; executable=true; text = ''
-    #!/bin/sh
-    mkdir $4
-    cp -av $2 $4/${name}
-    cp -av $3 $4
-  '';};
-
-  isModular = config.isYes "MODULES";
+  inherit (stdenv.lib)
+    hasAttr getAttr optional optionalString maintainers platforms;
 
-  installsFirmware = (config.isEnabled "FW_LOADER") &&
-    (isModular || (config.isDisabled "FIRMWARE_IN_KERNEL"));
+  installkernel = writeTextFile { name = "installkernel"; executable=true; text = ''
+    #!${stdenv.shell} -e
+    mkdir -p $4
+    cp -av $2 $4
+    cp -av $3 $4
+  ''; };
 
   commonMakeFlags = [
     "O=$(buildRoot)"
-    "INSTALL_PATH=$(out)"
-  ] ++ (optional isModular "INSTALL_MOD_PATH=$(out)")
-  ++ optional installsFirmware "INSTALL_FW_PATH=$(out)/lib/firmware";
+    "DEPMOD=${kmod}/bin/depmod"
+  ];
 
-  sourceRoot = stdenv.mkDerivation {
-    name = "linux-${version}-source";
+  drvAttrs = config_: platform: kernelPatches: configfile:
+    let
+      config = let attrName = attr: "CONFIG_" + attr; in {
+        isSet = attr: hasAttr (attrName attr) config;
 
-    inherit src;
+        getValue = attr: if config.isSet attr then getAttr (attrName attr) config else null;
 
-    patches = map (p: p.patch) kernelPatches;
+        isYes = attr: (config.getValue attr) == "y";
 
-    phases = [ "unpackPhase" "patchPhase" "installPhase" ]; 
+        isNo = attr: (config.getValue attr) == "n";
 
-    prePatch = ''
-      for mf in $(find -name Makefile -o -name Makefile.include -o -name install.sh); do
-          echo "stripping FHS paths in \`$mf'..."
-          sed -i "$mf" -e 's|/usr/bin/||g ; s|/bin/||g ; s|/sbin/||g'
-      done
-      sed -i Makefile -e 's|= depmod|= ${kmod}/sbin/depmod|'
-    '';
+        isModule = attr: (config.getValue attr) == "m";
 
-    installPhase = ''
-      cd ..
-      mv $sourceRoot $out
-    '';
-  };
-in
+        isEnabled = attr: (config.isModule attr) || (config.isYes attr);
 
-stdenv.mkDerivation {
-  name = "linux-${version}";
+        isDisabled = attr: (!(config.isSet attr)) || (config.isNo attr);
+      } // config_;
 
-  enableParallelBuilding = true;
+      isModular = config.isYes "MODULES";
 
-  outputs = if isModular then [ "out" "dev" ] else null;
+      installsFirmware = (config.isEnabled "FW_LOADER") &&
+        (isModular || (config.isDisabled "FIRMWARE_IN_KERNEL"));
+    in {
+      outputs = if isModular then [ "out" "dev" ] else null;
 
-  passthru = {
-    inherit version modDirVersion config kernelPatches src;
-  };
+      passthru = {
+        inherit version modDirVersion config kernelPatches;
+      };
 
-  inherit sourceRoot;
+      inherit src;
+
+      preUnpack = ''
+        mkdir build
+        export buildRoot="$(pwd)/build"
+      '';
+
+      patches = map (p: p.patch) kernelPatches;
+
+      prePatch = ''
+        for mf in $(find -name Makefile -o -name Makefile.include -o -name install.sh); do
+            echo "stripping FHS paths in \`$mf'..."
+            sed -i "$mf" -e 's|/usr/bin/||g ; s|/bin/||g ; s|/sbin/||g'
+        done
+      '';
+
+      configurePhase = ''
+        runHook preConfigure
+        ln -sv ${configfile} $buildRoot/.config
+        make $makeFlags "''${makeFlagsArray[@]}" oldconfig
+        runHook postConfigure
+      '';
+
+      buildFlags = [ "KBUILD_BUILD_VERSION=1-NixOS" platform.kernelTarget ] ++ optional isModular "modules";
+
+      installFlags = [
+        "INSTALLKERNEL=${installkernel}"
+        "INSTALL_PATH=$(out)"
+      ] ++ (optional isModular "INSTALL_MOD_PATH=$(out)")
+      ++ optional installsFirmware "INSTALL_FW_PATH=$(out)/lib/firmware";
+
+      # Some image types need special install targets (e.g. uImage is installed with make uinstall)
+      installTargets = [ (if platform.kernelTarget == "uImage" then "uinstall" else "install") ];
+
+      postInstall = optionalString installsFirmware ''
+        mkdir -p $out/lib/firmware
+      '' + (if isModular then ''
+        make modules_install $makeFlags "''${makeFlagsArray[@]}" \
+          $installFlags "''${installFlagsArray[@]}"
+        unlink $out/lib/modules/${modDirVersion}/build
+        unlink $out/lib/modules/${modDirVersion}/source
+
+        mkdir -p $dev/lib/modules/${modDirVersion}
+        cd ..
+        mv $sourceRoot $dev/lib/modules/${modDirVersion}/source
+        cd $dev/lib/modules/${modDirVersion}/source
+
+        mv $buildRoot/.config $buildRoot/Module.symvers $TMPDIR
+        rm -fR $buildRoot
+        mkdir $buildRoot
+        mv $TMPDIR/.config $TMPDIR/Module.symvers $buildRoot
+        make modules_prepare $makeFlags "''${makeFlagsArray[@]}"
+        mv $buildRoot $dev/lib/modules/${modDirVersion}/build
+
+        # !!! No documentation on how much of the source tree must be kept
+        # If/when kernel builds fail due to missing files, you can add
+        # them here. Note that we may see packages requiring headers
+        # from drivers/ in the future; it adds 50M to keep all of its
+        # headers on 3.10 though.
+
+        chmod +w -R ../source
+        arch=`cd $dev/lib/modules/${modDirVersion}/build/arch; ls`
+
+        # Remove unusued arches
+        mv arch/$arch .
+        rm -fR arch
+        mkdir arch
+        mv $arch arch
+
+        # Remove all driver-specific code (50M of which is headers)
+        rm -fR drivers
+
+        # Keep all headers
+        find .  -type f -name '*.h' -print0 | xargs -0 chmod -w
+
+        # Keep root and arch-specific Makefiles
+        chmod -w Makefile
+        chmod -w arch/$arch/Makefile
+
+        # Keep whole scripts dir
+        chmod -w -R scripts
+
+        # Delete everything not kept
+        find . -type f -perm -u=w -print0 | xargs -0 rm
+
+        # Delete empty directories
+        find -empty -type d -delete
+      '' else optionalString installsFirmware ''
+        make firmware_install $makeFlags "''${makeFlagsArray[@]}" \
+          $installFlags "''${installFlagsArray[@]}"
+      '');
+
+      # !!! This leaves references to gcc in $dev
+      # that we might be able to avoid
+      postFixup = if isModular then ''
+        if [ -z "$dontStrip" ]; then
+            find $out -name "*.ko" -print0 | xargs -0 -r ''${crossConfig+$crossConfig-}strip -S
+        fi
+        # !!! Should this be part of stdenv? Also patchELF should take an argument...
+        prefix=$dev
+        patchELF
+        prefix=$out
+      '' else null;
+
+      meta = {
+        description =
+          "The Linux kernel" +
+          (if kernelPatches == [] then "" else
+            " (with patches: "
+            + stdenv.lib.concatStrings (stdenv.lib.intersperse ", " (map (x: x.name) kernelPatches))
+            + ")");
+        license = "GPLv2";
+        homepage = http://www.kernel.org/;
+        maintainers = [
+          maintainers.shlevy
+        ];
+        platforms = platforms.linux;
+      };
+    };
+in
 
-  unpackPhase = ''
-    mkdir build
-    export buildRoot="$(pwd)/build"
-    cd ${sourceRoot}
-  '';
+stdenv.mkDerivation ((drvAttrs config stdenv.platform (kernelPatches ++ nativeKernelPatches) configfile) // {
+  name = "linux-${version}";
 
-  configurePhase = ''
-    runHook preConfigure
-    ln -sv ${configfile} $buildRoot/.config
-    make $makeFlags "''${makeFlagsArray[@]}" oldconfig
-    runHook postConfigure
-  '';
+  enableParallelBuilding = true;
 
-  nativeBuildInputs = [ perl bc nettools ];
+  nativeBuildInputs = [ perl bc nettools ] ++ optional (stdenv.platform.uboot != null)
+    (ubootChooser stdenv.platform.uboot);
 
   makeFlags = commonMakeFlags ++ [
-   "INSTALLKERNEL=${installkernel stdenv.platform.kernelTarget}"
+    "ARCH=${stdenv.platform.kernelArch}"
   ];
 
-  crossAttrs = {
-    makeFlags = commonMakeFlags ++ [
-     "INSTALLKERNEL=${installkernel stdenv.cross.platform.kernelTarget}"
-    ];
-  };
-
-  postInstall = optionalString installsFirmware ''
-    mkdir -p $out/lib/firmware
-  '' + (if isModular then ''
-    make modules_install $makeFlags "''${makeFlagsArray[@]}" \
-      $installFlags "''${installFlagsArray[@]}"
-    rm -f $out/lib/modules/${modDirVersion}/build
-    mkdir -p $dev/lib/modules/${modDirVersion}
-    mv $out/lib/modules/${modDirVersion}/source $dev/lib/modules/${modDirVersion}/source
-    mv $buildRoot $dev/lib/modules/${modDirVersion}/build
-  '' else optionalString installsFirmware ''
-    make firmware_install $makeFlags "''${makeFlagsArray[@]}" \
-      $installFlags "''${installFlagsArray[@]}"
-  '');
-
-  postFixup = if isModular then ''
-    if [ -z "$dontStrip" ]; then
-        find $out -name "*.ko" -print0 | xargs -0 -r strip -S
-        # Remove all references to the source directory to avoid unneeded
-        # runtime dependencies
-        find $out -name "*.ko" -print0 | xargs -0 -r sed -i \
-          "s|${sourceRoot}|$NIX_STORE/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-${sourceRoot.name}|g"
-    fi
-  '' else null;
-
-  meta = {
-    description = "The Linux kernel";
-    license = "GPLv2";
-    homepage = http://www.kernel.org/;
-    maintainers = [
-      maintainers.shlevy
-    ];
-    platforms = platforms.linux;
+  crossAttrs = let cp = stdenv.cross.platform; in
+    (drvAttrs crossConfig cp (kernelPatches ++ crossKernelPatches) crossConfigfile) // {
+      makeFlags = commonMakeFlags ++ [
+        "ARCH=${cp.kernelArch}"
+        "CROSS_COMPILE=$(crossConfig)-"
+      ];
+
+      # !!! uboot has messed up cross-compiling, nativeDrv builds arm tools on x86,
+      # crossDrv builds x86 tools on x86 (but arm uboot). If this is fixed, uboot
+      # can just go into buildInputs (but not nativeBuildInputs since cp.uboot
+      # may be different from stdenv.platform.uboot)
+      buildInputs = optional (cp.uboot != null) (ubootChooser cp.uboot).crossDrv;
   };
-}
+})
diff --git a/pkgs/os-specific/linux/kernel/patches.nix b/pkgs/os-specific/linux/kernel/patches.nix
index b55312fe5609..efa23f67846b 100644
--- a/pkgs/os-specific/linux/kernel/patches.nix
+++ b/pkgs/os-specific/linux/kernel/patches.nix
@@ -18,24 +18,6 @@ let
       };
     };
 
-  makeAufs3StandalonePatch = {version, rev, sha256}:
-
-    stdenv.mkDerivation {
-      name = "aufs3-standalone-${version}.patch";
-
-      src = fetchgit {
-        url = git://aufs.git.sourceforge.net/gitroot/aufs/aufs3-standalone.git;
-        inherit sha256 rev;
-      };
-
-      phases = [ "unpackPhase" "installPhase" ];
-
-      # Instructions from http://aufs.git.sourceforge.net/git/gitweb.cgi?p=aufs/aufs3-standalone.git;a=blob;f=Documentation/filesystems/aufs/README;h=b8cf077635b323d1b454266366f05f476bbd09cb;hb=1067b9d8d64d23c70d905c9cd3c90a669e39c4d4
-      installPhase = ''
-        cat aufs3-base.patch aufs3-proc_map.patch aufs3-standalone.patch > $out
-      '';
-    };
-
   makeAppArmorPatch = {apparmor, version}:
     stdenv.mkDerivation {
       name = "apparmor-${version}.patch";
@@ -68,34 +50,6 @@ rec {
       features.secPermPatch = true;
     };
 
-  aufs3_2 = rec {
-    name = "aufs3.2";
-    version = "3.2.20121210";
-    utilRev = "91af15f977d12e02165759620005f6ce1a4d7602";
-    utilHash = "dda4df89828dcf0e4012d88b4aa3eda8c30af69d6530ff5fedc2411de872c996";
-    patch = makeAufs3StandalonePatch {
-      inherit version;
-      rev = "0bf50c3b82f98e2ddc4c9ba0657f28ebfa8d15cb";
-      sha256 = "bc4b65cb77c62744db251da98488fdf4962f14a144c045cea6cbbbd42718ff89";
-    };
-    features.aufsBase = true;
-    features.aufs3 = true;
-  };
-
-  aufs3_4 = rec {
-    name = "aufs3.4";
-    version = "3.4.20121210";
-    utilRev = "91af15f977d12e02165759620005f6ce1a4d7602";
-    utilHash = "dda4df89828dcf0e4012d88b4aa3eda8c30af69d6530ff5fedc2411de872c996";
-    patch = makeAufs3StandalonePatch {
-      inherit version;
-      rev = "2faacd9baffb37df3b9062cc554353eebe68df1e";
-      sha256 = "3ecf97468f5e85970d9fd2bfc61e38c7f5ae2c6dde0045d5a17de085c411d452";
-    };
-    features.aufsBase = true;
-    features.aufs3 = true;
-  };
-
   no_xsave =
     { name = "no-xsave";
       patch = ./no-xsave.patch;
diff --git a/pkgs/os-specific/linux/kernel/perf.nix b/pkgs/os-specific/linux/kernel/perf.nix
index 04924f013a45..3fb18a234729 100644
--- a/pkgs/os-specific/linux/kernel/perf.nix
+++ b/pkgs/os-specific/linux/kernel/perf.nix
@@ -1,13 +1,13 @@
-{ stdenv, kernelDev, elfutils, python, perl, newt, slang, asciidoc, xmlto
+{ stdenv, kernel, elfutils, python, perl, newt, slang, asciidoc, xmlto
 , docbook_xsl, docbook_xml_dtd_45, libxslt, flex, bison, pkgconfig
 , withGtk ? false, gtk ? null }:
 
 assert withGtk -> gtk != null;
 
 stdenv.mkDerivation {
-  name = "perf-linux-${kernelDev.version}";
+  name = "perf-linux-${kernel.version}";
 
-  inherit (kernelDev) src patches;
+  inherit (kernel) src patches;
 
   preConfigure = ''
     cd tools/perf
@@ -31,6 +31,7 @@ stdenv.mkDerivation {
     propagatedBuildInputs = [ elfutils.crossDrv newt.crossDrv ];
     makeFlags = "CROSS_COMPILE=${stdenv.cross.config}-";
     elfutils = elfutils.crossDrv;
+    inherit (kernel.crossDrv) src patches;
   };
 
   meta = {