Skip to content

Commit

Permalink
trying to be more flexible about config generation suing functions/sh…
Browse files Browse the repository at this point in the history
…ell etc wip in pkgs/os-specific/linux/kernel/config.nix
  • Loading branch information
teto committed Nov 20, 2017
1 parent ffb9a41 commit f226e94
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 74 deletions.
87 changes: 87 additions & 0 deletions pkgs/os-specific/linux/kernel/config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# {stdenv
# , buildRoot
# , srcRoot
# }:
{
patchKconfig = ''
# Patch kconfig to print "###" after every question so that
# generate-config.pl from the generic builder can answer them.
sed -e '/fflush(stdout);/i\printf("###");' -i scripts/kconfig/conf.c
'';


#
buildKernelConfig = arch: autoModules: ''
cd $buildRoot
# Get a basic config file for later refinement with $generateConfig.
make -C ../$sourceRoot O=$PWD $kernelBaseConfig ARCH=$arch
# Create the config file.
echo "generating kernel configuration..."
echo "$kernelConfig" > kernel-config
DEBUG=1 ARCH=$arch KERNEL_CONFIG=kernel-config AUTO_MODULES=$autoModules \
PREFER_BUILTIN=$preferBuiltin SRC=../$sourceRoot perl -w $generateConfig
'';

}
# TODO we should be able to generate standalone config and config on the go
# => we need functions with module etc
# espcially we need a bash builder
# TODO have a standalone
stdenv.mkDerivation {
inherit ignoreConfigErrors;
name = "linux-config-${version}";

generateConfig = ./generate-config.pl;

kernelConfig = kernelConfigFun config;

nativeBuildInputs = [ perl ];

platformName = stdenv.platform.name;
kernelBaseConfig = stdenv.platform.kernelBaseConfig;
kernelTarget = stdenv.platform.kernelTarget;
autoModules = stdenv.platform.kernelAutoModules;
preferBuiltin = stdenv.platform.kernelPreferBuiltin or false;
arch = stdenv.platform.kernelArch;

crossAttrs = let
cp = hostPlatform.platform;
in {
arch = cp.kernelArch;
platformName = cp.name;
kernelBaseConfig = cp.kernelBaseConfig;
kernelTarget = cp.kernelTarget;
autoModules = cp.kernelAutoModules;

# Just ignore all options that don't apply (We are lazy).
ignoreConfigErrors = true;

kernelConfig = kernelConfigFun configCross;

inherit (kernel.crossDrv) src patches preUnpack;
};

prePatch = kernel.prePatch + patchKconfig;
inherit (kernel) src patches preUnpack;

buildPhase = ''
cd $buildRoot
# Get a basic config file for later refinement with $generateConfig.
make -C ../$sourceRoot O=$PWD $kernelBaseConfig ARCH=$arch
# Create the config file.
echo "generating kernel configuration..."
echo "$kernelConfig" > kernel-config
DEBUG=1 ARCH=$arch KERNEL_CONFIG=kernel-config AUTO_MODULES=$autoModules \
PREFER_BUILTIN=$preferBuiltin SRC=../$sourceRoot perl -w $generateConfig
'';

installPhase = "mv .config $out";

enableParallelBuilding = true;
}


63 changes: 4 additions & 59 deletions pkgs/os-specific/linux/kernel/generic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,70 +57,15 @@ let
map ({extraConfig ? "", ...}: extraConfig) kernelPatches;
in lib.concatStringsSep "\n" ([baseConfig] ++ configFromPatches);

configfile = stdenv.mkDerivation {
inherit ignoreConfigErrors;
name = "linux-config-${version}";

generateConfig = ./generate-config.pl;

kernelConfig = kernelConfigFun config;

nativeBuildInputs = [ perl ];

platformName = stdenv.platform.name;
kernelBaseConfig = stdenv.platform.kernelBaseConfig;
kernelTarget = stdenv.platform.kernelTarget;
autoModules = stdenv.platform.kernelAutoModules;
preferBuiltin = stdenv.platform.kernelPreferBuiltin or false;
arch = stdenv.platform.kernelArch;

crossAttrs = let
cp = hostPlatform.platform;
in {
arch = cp.kernelArch;
platformName = cp.name;
kernelBaseConfig = cp.kernelBaseConfig;
kernelTarget = cp.kernelTarget;
autoModules = cp.kernelAutoModules;

# Just ignore all options that don't apply (We are lazy).
ignoreConfigErrors = true;

kernelConfig = kernelConfigFun configCross;

inherit (kernel.crossDrv) src patches preUnpack;
};

prePatch = kernel.prePatch + ''
# Patch kconfig to print "###" after every question so that
# generate-config.pl from the generic builder can answer them.
sed -e '/fflush(stdout);/i\printf("###");' -i scripts/kconfig/conf.c
'';

inherit (kernel) src patches preUnpack;

buildPhase = ''
cd $buildRoot
# Get a basic config file for later refinement with $generateConfig.
make -C ../$sourceRoot O=$PWD $kernelBaseConfig ARCH=$arch
# Create the config file.
echo "generating kernel configuration..."
echo "$kernelConfig" > kernel-config
DEBUG=1 ARCH=$arch KERNEL_CONFIG=kernel-config AUTO_MODULES=$autoModules \
PREFER_BUILTIN=$preferBuiltin SRC=../$sourceRoot perl -w $generateConfig
'';

installPhase = "mv .config $out";

enableParallelBuilding = true;
};
# TODO moved it
# configfile = callPackage

# TODO le transformer plutot en
kernel = buildLinux {
inherit version modDirVersion src kernelPatches stdenv;

configfile = configfile.nativeDrv or configfile;
# configfile = null;

crossConfigfile = configfile.crossDrv or configfile;

Expand Down
66 changes: 51 additions & 15 deletions pkgs/os-specific/linux/kernel/manual-config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ in {
# Patches for cross compiling only
crossKernelPatches ? [],
# The native kernel .config file
configfile,
configfile ? null,
# The cross kernel .config file
crossConfigfile ? configfile,
# Manually specified nixexpr representing the config
# If unspecified, this will be autodetected from the .config
config ? stdenv.lib.optionalAttrs allowImportFromDerivation (readConfig configfile),
# config ? stdenv.lib.optionalAttrs allowImportFromDerivation (readConfig configfile),
config ? null,
# Cross-compiling config
crossConfig ? if allowImportFromDerivation then (readConfig crossConfigfile) else config,
# Whether to utilize the controversial import-from-derivation feature to parse the config
Expand All @@ -52,6 +53,7 @@ let
cp -av $3 $4
''; };

# flags common to the cross-compiled and native builds
commonMakeFlags = [
"O=$(buildRoot)"

Expand Down Expand Up @@ -80,17 +82,23 @@ let

installsFirmware = (config.isEnabled "FW_LOADER") &&
(isModular || (config.isDisabled "FIRMWARE_IN_KERNEL"));
in (optionalAttrs isModular { outputs = [ "out" "dev" ]; }) // {
in (optionalAttrs isModular { outputs = [ "out" "dev" ]; }) // rec {
passthru = {
inherit version modDirVersion config kernelPatches configfile;
inherit version modDirVersion kernelPatches configfile;
# inherit config;
};

inherit src;

preUnpack = ''
mkdir build
export buildRoot="$PWD/build"
'';
# why this
# preUnpack = ''
# mkdir build
# export buildRoot="$PWD/build"
# '';
autoModules = stdenv.platform.kernelAutoModules;
preferBuiltin = stdenv.platform.kernelPreferBuiltin or false;
arch = stdenv.platform.kernelArch;
kernelBaseConfig = stdenv.platform.kernelBaseConfig;

patches = map (p: p.patch) kernelPatches;

Expand All @@ -100,20 +108,41 @@ let
sed -i "$mf" -e 's|/usr/bin/||g ; s|/bin/||g ; s|/sbin/||g'
done
sed -i Makefile -e 's|= depmod|= ${kmod}/bin/depmod|'
'' +
# Patch kconfig to print "###" after every question so that
# generate-config.pl from the generic builder can answer them.
''
sed -e '/fflush(stdout);/i\printf("###");' -i scripts/kconfig/conf.c
'';

# imported from generic.nix
buildConfigPhase = ''
echo "buildRoot set to $buildRoot"
cd $buildRoot
# Get a basic config file for later refinement with $generateConfig.
make -C ../$sourceRoot O=$PWD ${kernelBaseConfig} ARCH=$arch
# Create the config file.
echo "generating kernel configuration..."
echo "$kernelConfig" > kernel-config
DEBUG=1 ARCH=$arch KERNEL_CONFIG=kernel-config AUTO_MODULES=$autoModules \
PREFER_BUILTIN=$preferBuiltin SRC=../$sourceRoot perl -w $generateConfig
'';

preConfigure = buildConfigPhase;

configurePhase = ''
runHook preConfigure
if [ -z "$buildRoot" ]; then
# assume we are in the $sourceRoot folder
echo "buildRoot is not set"
mkdir build
export buildRoot="$PWD/build"
fi
runHook preConfigure
ln -sv ${configfile} $buildRoot/.config
# ln -sv ${configfile} ''${buildRoot:=$(pwd)/../build}/.config
make $makeFlags "''${makeFlagsArray[@]}" oldconfig
runHook postConfigure
Expand Down Expand Up @@ -208,6 +237,8 @@ let
make firmware_install $makeFlags "''${makeFlagsArray[@]}" \
$installFlags "''${installFlagsArray[@]}"
'');
# TODO add it as an output
# installPhase = "mv .config $out";

requiredSystemFeatures = [ "big-parallel" ];

Expand All @@ -228,8 +259,8 @@ let
};
};
in

stdenv.mkDerivation ((drvAttrs config stdenv.platform (kernelPatches ++ nativeKernelPatches) configfile) // {
# config ? stdenv.lib.optionalAttrs allowImportFromDerivation (readConfig configfile),
stdenv.mkDerivation ((drvAttrs config stdenv.platform (kernelPatches ++ nativeKernelPatches) configfile) // rec {
name = "linux-${version}";

enableParallelBuilding = true;
Expand All @@ -239,11 +270,16 @@ stdenv.mkDerivation ((drvAttrs config stdenv.platform (kernelPatches ++ nativeKe

hardeningDisable = [ "bindnow" "format" "fortify" "stackprotector" "pic" ];

# concatenated from drvAttrs
# configurePhase = ''
# '';

makeFlags = commonMakeFlags ++ [
"ARCH=${stdenv.platform.kernelArch}"
];
# ++ stdenv.lib.optional enableParallelBuilding "-j{}";
]
# ++ stdenv.lib.optional enableParallelBuilding "-j${cfg.buildCores}";
# cfg.buildCores # TODO set
;

karch = stdenv.platform.kernelArch;

Expand Down

0 comments on commit f226e94

Please sign in to comment.