mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-12-06 05:43:17 +00:00
639edcb829
The previos version did a for loop over the output of set, which spits out _all_ defined variables and their contents. This not only is dangerous if there is a variable starting with CONFIG_ but also can't handle whitespace, as the IFS is set to any (horizontal _and_ vertical) whitespace by default. So, imagine (actually don't imagine, something like this is the case in a lot of kernel configuration files) you have the following variable: CONFIG_ARCH_HWEIGHT_CFLAGS="-fcall-saved-rdi -fcall-saved-rsi ..." A loop with for and the default IFS would result in the following variable pieces: 0: CONFIG_ARCH_HWEIGHT_CFLAGS="-fcall-saved-rdi 1: -fcall-saved-rsi 2: ..." This obviously leads to the problem that this config variable is being cut off at the first whitespace. Another downside of this approach is that set not only returns variables but functions as well. This could lead to quite a lot of unexpected behaviour and confusion. So the new approach doesn't source the kernel configuration anymore but uses `read` to parse the file line-by line, setting IFS to '=', thus splitting all configuration lines into key/value pairs. Using parameter expansion, we ensure that we only read lines starting with "CONFIG_". This particularily has the advantage of not being bash-specific, should we choose to change to a different default shell someday. Now, after we got a correct "CONFIG_" line, we're using a temporary variable to split off the first quote from the result. Particularily the reason behind this is shell compatibility again, as ${${foo#"}%"} only works in Bash, Zsh and whatnot but not in plain SH. And within the next line we obviously insert the no_firstquote variable without it's last quote removed. But, what about escaping? First of all, if we'd just eval the $val variable, we would correctly unescape the value, but this has the downside that variables within the content would be expanded, for example look at this: CONFIG_DEFCONFIG_LIST="/lib/modules/$UNAME_RELEASE/.config" Well, obviously this is a bad example at the Nix sense, but just to show that variables within kernel configuration entries aren't impossible. And second, which would have been a show stopper if \" would be within $val: It simply would end up being an invalid Nix expression, because \" would end up as a ploin " within a double quoted string. Signed-off-by: aszlig <aszlig@redmoonstudios.org>
165 lines
4.3 KiB
Nix
165 lines
4.3 KiB
Nix
{ stdenv, runCommand, nettools, perl, kmod, writeTextFile }:
|
|
|
|
with stdenv.lib;
|
|
|
|
let
|
|
|
|
# Function to parse the config file into a nix expression
|
|
readConfig = configFile:
|
|
let
|
|
configAttrs = import "${runCommand "config.nix" {} ''
|
|
echo "{" > "$out"
|
|
while IFS='=' read key val; do
|
|
[ "x''${key#CONFIG_}" != "x$key" ] || continue
|
|
no_firstquote="''${val#\"}";
|
|
echo ' "'"$key"'" = "'"''${no_firstquote%\"}"'";' >> "$out"
|
|
done < "${configFile}"
|
|
echo "}" >> $out
|
|
''}";
|
|
|
|
config = configAttrs // rec {
|
|
attrName = attr: "CONFIG_" + attr;
|
|
|
|
isSet = attr: hasAttr (attrName attr) config;
|
|
|
|
getValue = attr: if isSet attr then getAttr (attrName attr) config else null;
|
|
|
|
isYes = attr: (isSet attr) && ((getValue attr) == "y");
|
|
|
|
isNo = attr: (isSet attr) && ((getValue attr) == "n");
|
|
|
|
isModule = attr: (isSet attr) && ((getValue attr) == "m");
|
|
|
|
isEnabled = attr: (isModule attr) || (isYes attr);
|
|
|
|
isDisabled = attr: (!(isSet attr)) || (isNo attr);
|
|
};
|
|
in
|
|
config;
|
|
|
|
in
|
|
|
|
{
|
|
# The kernel version
|
|
version,
|
|
# The version of the kernel module directory
|
|
modDirVersion ? version,
|
|
# The kernel source (tarball, git checkout, etc.)
|
|
src,
|
|
# Any patches
|
|
kernelPatches ? [],
|
|
# The kernel .config file
|
|
configfile,
|
|
# Manually specified nixexpr representing the config
|
|
# If unspecified, this will be autodetected from the .config
|
|
config ? optionalAttrs allowImportFromDerivation (readConfig configfile),
|
|
# Whether to utilize the controversial import-from-derivation feature to parse the config
|
|
allowImportFromDerivation ? false
|
|
}:
|
|
|
|
let
|
|
installkernel = name: writeTextFile { name = "installkernel"; executable=true; text = ''
|
|
#!/bin/sh
|
|
mkdir $4
|
|
cp -av $2 $4/${name}
|
|
cp -av $3 $4
|
|
'';};
|
|
|
|
isModular = config.isYes "MODULES";
|
|
|
|
installsFirmware = (config.isEnabled "FW_LOADER") &&
|
|
(isModular || (config.isDisabled "FIRMWARE_IN_KERNEL"));
|
|
|
|
commonMakeFlags = [
|
|
"O=$(buildRoot)"
|
|
"INSTALL_PATH=$(out)"
|
|
] ++ (optional isModular "INSTALL_MOD_PATH=$(out)")
|
|
++ optional installsFirmware "INSTALL_FW_PATH=$(out)/lib/firmware";
|
|
in
|
|
|
|
stdenv.mkDerivation {
|
|
name = "linux-${version}";
|
|
|
|
enableParallelBuilding = true;
|
|
|
|
passthru = {
|
|
inherit version modDirVersion config kernelPatches src;
|
|
};
|
|
|
|
sourceRoot = stdenv.mkDerivation {
|
|
name = "linux-${version}-source";
|
|
|
|
inherit src;
|
|
|
|
patches = map (p: p.patch) kernelPatches;
|
|
|
|
phases = [ "unpackPhase" "patchPhase" "installPhase" ];
|
|
|
|
prePatch = ''
|
|
for mf in $(find -name Makefile -o -name Makefile.include -o -name install.sh); do
|
|
echo "stripping FHS paths in \`$mf'..."
|
|
sed -i "$mf" -e 's|/usr/bin/||g ; s|/bin/||g ; s|/sbin/||g'
|
|
done
|
|
sed -i Makefile -e 's|= depmod|= ${kmod}/sbin/depmod|'
|
|
'';
|
|
|
|
installPhase = ''
|
|
cd ..
|
|
mv $sourceRoot $out
|
|
'';
|
|
};
|
|
|
|
unpackPhase = ''
|
|
mkdir build
|
|
export buildRoot="$(pwd)/build"
|
|
ln -sv ${configfile} $buildRoot/.config
|
|
cd $sourceRoot
|
|
'';
|
|
|
|
configurePhase = ''
|
|
runHook preConfigure
|
|
make $makeFlags "''${makeFlagsArray[@]}" oldconfig
|
|
runHook postConfigure
|
|
'';
|
|
|
|
buildNativeInputs = [ perl nettools ];
|
|
|
|
makeFlags = commonMakeFlags ++ [
|
|
"INSTALLKERNEL=${installkernel stdenv.platform.kernelTarget}"
|
|
];
|
|
|
|
crossAttrs = {
|
|
makeFlags = commonMakeFlags ++ [
|
|
"INSTALLKERNEL=${installkernel stdenv.cross.platform.kernelTarget}"
|
|
];
|
|
};
|
|
|
|
postInstall = optionalString installsFirmware ''
|
|
mkdir -p $out/lib/firmware
|
|
'' + (if isModular then ''
|
|
make modules_install $makeFlags "''${makeFlagsArray[@]}" \
|
|
$installFlags "''${installFlagsArray[@]}"
|
|
rm -f $out/lib/modules/${modDirVersion}/build
|
|
mv $buildRoot $out/lib/modules/${modDirVersion}/build
|
|
'' else optionalString installsFirmware ''
|
|
make firmware_install $makeFlags "''${makeFlagsArray[@]}" \
|
|
$installFlags "''${installFlagsArray[@]}"
|
|
'');
|
|
|
|
postFixup = optionalString isModular ''
|
|
if [ -z "$dontStrip" ]; then
|
|
find $out -name "*.ko" -print0 | xargs -0 -r strip -S
|
|
fi
|
|
'';
|
|
|
|
meta = {
|
|
description = "The Linux kernel";
|
|
license = "GPLv2";
|
|
homepage = http://www.kernel.org/;
|
|
maintainers = [
|
|
maintainers.shlevy
|
|
];
|
|
platforms = lib.platforms.linux;
|
|
};
|
|
}
|