liminix

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

default.nix (3726B)


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