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