liminix

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

default.nix (3758B)


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