make kernel builder callPackage'able

This commit is contained in:
Daniel Barlow
2022-09-25 22:02:45 +01:00
parent a7e54c087c
commit e29d009b2f
3 changed files with 20 additions and 4 deletions

43
kernel/default.nix Normal file
View File

@@ -0,0 +1,43 @@
{
callPackage
, buildPackages
, stdenvNoCC
, fetchFromGitHub
, config
}:
let
source = fetchFromGitHub {
owner = "torvalds";
repo = "linux";
rev = "3d7cb6b04c3f3115719235cc6866b10326de34cd"; # v5.19
hash = "sha256-OVsIRScAnrPleW1vbczRAj5L/SGGht2+GnvZJClMUu4=";
};
# The kernel is huge and takes a long time just to
# download and unpack. This derivation creates
# a source tree in a suitable shape to build from -
# today it just patches some scripts but as we add
# support for boards/SoCs we expect the scope of
# "pre-treatment" to grow
tree = stdenvNoCC.mkDerivation {
name = "spindled-kernel-tree";
src = source;
phases = [ "unpackPhase" "patchScripts" "installPhase" ];
patchScripts = ''
patchShebangs scripts/
# substituteInPlace Makefile --replace /bin/pwd ${buildPackages.pkgs.coreutils}/bin/pwd
'';
installPhase = ''
mkdir -p $out
cp -a . $out
'';
};
in
{
vmlinux = callPackage ./vmlinux.nix {
inherit tree;
inherit config;
};
}

90
kernel/vmlinux.nix Normal file
View File

@@ -0,0 +1,90 @@
{ stdenv
, buildPackages
, runCommand
, writeText
, lib
, config
, checkedConfig ? {}
, tree
} :
let writeConfig = name : config: writeText name
(builtins.concatStringsSep
"\n"
(lib.mapAttrsToList
(name: value: (if value == "n" then "# CONFIG_${name} is not set" else "CONFIG_${name}=${value}"))
config
));
kconfigFile = writeConfig "nixwrt_kconfig" config;
checkedConfigFile = writeConfig "checked_kconfig" checkedConfig ;
inherit lib; in
stdenv.mkDerivation rec {
name = "vmlinux";
hardeningDisable = ["all"];
nativeBuildInputs = [buildPackages.stdenv.cc] ++
(with buildPackages.pkgs;
[rsync bc bison flex pkgconfig openssl ncurses.all perl]);
CC = "${stdenv.cc.bintools.targetPrefix}gcc";
HOSTCC = "gcc -I${buildPackages.pkgs.openssl}/include -I${buildPackages.pkgs.ncurses}/include";
HOST_EXTRACFLAGS = "-I${buildPackages.pkgs.openssl.dev}/include -L${buildPackages.pkgs.openssl.out}/lib -L${buildPackages.pkgs.ncurses.out}/lib " ;
PKG_CONFIG_PATH = "./pkgconfig";
CROSS_COMPILE = stdenv.cc.bintools.targetPrefix;
ARCH = "mips"; # kernel uses "mips" here for both mips and mipsel
dontStrip = true;
dontPatchELF = true;
outputs = ["out" "modulesupport"];
phases = ["butcherPkgconfig"
# "patchScripts"
"configurePhase"
"checkConfigurationPhase"
"buildPhase"
"installPhase"
];
# this is here to work around what I think is a bug in nixpkgs packaging
# of ncurses: it installs pkg-config data files which don't produce
# any -L options when queried with "pkg-config --lib ncurses". For a
# regular nixwrt compilation you'll never even notice, this only becomes
# an issue if you do a nix-shell in this derivation and expect "make nconfig"
# to work.
butcherPkgconfig = ''
cp -r ${buildPackages.pkgs.ncurses.dev}/lib/pkgconfig .
chmod +w pkgconfig pkgconfig/*.pc
for i in pkgconfig/*.pc; do test -f $i && sed -i 's/^Libs:/Libs: -L''${libdir} /' $i;done
'';
# patchScripts = ''
# patchShebangs --build scripts/
# '';
configurePhase = ''
export KBUILD_OUTPUT=`pwd`
cp ${kconfigFile} .config
cp ${kconfigFile} .config.orig
( cd ${tree} && make V=1 olddefconfig )
'';
checkConfigurationPhase = ''
echo Checking required config items:
if comm -2 -3 <(grep 'CONFIG' ${checkedConfigFile} |sort) <(grep 'CONFIG' .config|sort) |grep '.' ; then
echo -e "^^^ Some configuration lost :-(\nPerhaps you have mutually incompatible settings, or have disabled options on which these depend.\n"
exit 0
fi
echo "OK"
'';
KBUILD_BUILD_HOST = "nixwrt.builder";
buildPhase = ''
make -C ${tree} vmlinux
'';
installPhase = ''
${CROSS_COMPILE}strip -d vmlinux
cp vmlinux $out
make clean
mkdir -p $modulesupport
cp -a . $modulesupport
'';
}