liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

default.nix (3421B)


      1 {  stdenv
      2  , buildPackages
      3  , runCommand
      4  , writeText
      5  , lib
      6 
      7  , config
      8  , src
      9  , version ? "0"
     10  , extraPatchPhase ? "echo"
     11  , targets ? ["vmlinux"]
     12 } :
     13 let
     14   writeConfig = import ./write-kconfig.nix { inherit lib writeText; };
     15   kconfigFile = writeConfig "kconfig" config;
     16   arch = stdenv.hostPlatform.linuxArch;
     17   targetNames =  map baseNameOf targets;
     18   inherit lib; in
     19 stdenv.mkDerivation rec {
     20   name = "kernel";
     21   inherit src extraPatchPhase;
     22   hardeningDisable = ["all"];
     23   nativeBuildInputs = [buildPackages.stdenv.cc] ++
     24                       (with buildPackages.pkgs; [
     25                         rsync bc bison flex pkg-config
     26                         openssl ncurses.all perl
     27                       ]);
     28   CC = "${stdenv.cc.bintools.targetPrefix}gcc";
     29   HOSTCC = with buildPackages.pkgs;
     30     "gcc -I${openssl}/include -I${ncurses}/include";
     31   HOST_EXTRACFLAGS = with buildPackages.pkgs;
     32     "-I${openssl.dev}/include -L${openssl.out}/lib -L${ncurses.out}/lib";
     33   PKG_CONFIG_PATH = "./pkgconfig";
     34   CROSS_COMPILE = stdenv.cc.bintools.targetPrefix;
     35   ARCH = arch;
     36   KBUILD_BUILD_HOST = "liminix.builder";
     37 
     38   dontStrip = true;
     39   dontPatchELF = true;
     40   outputs = ["out" "headers" "modulesupport"] ++ targetNames;
     41   phases = [
     42     "unpackPhase"
     43     "butcherPkgconfig"
     44     "extraPatchPhase"
     45     "patchPhase"
     46     "patchScripts"
     47     "configurePhase"
     48     "checkConfigurationPhase"
     49     "buildPhase"
     50     "installPhase"
     51   ];
     52 
     53   patches = [
     54     ./cmdline-cookie.patch
     55     ./mips-malta-fdt-from-bootloader.patch
     56   ] ++ lib.optional (lib.versionOlder version "5.18.0")
     57     ./phram-allow-cached-mappings.patch;
     58 
     59   # this is here to work around what I think is a bug in nixpkgs
     60   # packaging of ncurses: it installs pkg-config data files which
     61   # don't produce any -L options when queried with "pkg-config --lib
     62   # ncurses".  For a regular build you'll never even notice, this only
     63   # becomes an issue if you do a nix-shell in this derivation and
     64   # expect "make nconfig" to work.
     65   butcherPkgconfig = ''
     66     cp -r ${buildPackages.pkgs.ncurses.dev}/lib/pkgconfig .
     67     chmod +w pkgconfig pkgconfig/*.pc
     68     for i in pkgconfig/*.pc; do test -f $i && sed -i 's/^Libs:/Libs: -L''${libdir} /'  $i;done
     69   '';
     70 
     71   patchScripts = ''
     72     # Make kexec pass dtb in register when invoking new kernel. The
     73     # code to do this is already present, but bracketed by UHI_BOOT
     74     # which we can't enable.
     75     sed -i arch/mips/kernel/machine_kexec.c -e 's/CONFIG_UHI_BOOT/CONFIG_MIPS/g'
     76 
     77     patchShebangs scripts/
     78   '';
     79 
     80   configurePhase = ''
     81     export KBUILD_OUTPUT=`pwd`
     82     cp ${kconfigFile} .config
     83     cp ${kconfigFile} .config.orig
     84     make V=1 olddefconfig
     85   '';
     86 
     87   checkConfigurationPhase = ''
     88     echo Checking required config items:
     89     if comm -2 -3 <(grep 'CONFIG' ${kconfigFile} |sort) <(grep 'CONFIG' .config|sort) |grep '.'    ; then
     90       echo -e "^^^ Some configuration lost :-(\nPerhaps you have mutually incompatible settings, or have disabled options on which these depend.\n"
     91       exit 0
     92     fi
     93     echo "OK"
     94   '';
     95 
     96   buildPhase = ''
     97     make ${lib.concatStringsSep " " targetNames} modules_prepare -j$NIX_BUILD_CORES
     98   '';
     99 
    100   installPhase = ''
    101     ${CROSS_COMPILE}strip -d vmlinux
    102     ${lib.concatStringsSep "\n" (map (f: "cp ${f} \$${baseNameOf f}") targets)}
    103     cp vmlinux $out
    104     mkdir -p $headers
    105     cp -a include .config $headers/
    106     mkdir -p $modulesupport
    107     make modules
    108     cp -a . $modulesupport
    109   '';
    110 }