Skip to content
1 change: 1 addition & 0 deletions lib/systems/inspect.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ rec {
isRiscV64 = { cpu = { family = "riscv"; bits = 64; }; };
isRx = { cpu = { family = "rx"; }; };
isSparc = { cpu = { family = "sparc"; }; };
isSparc64 = { cpu = { family = "sparc"; bits = 64; }; };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is independently useful; could you please split it out in a separate PR? I'll merge it. I'm surprised we don't already have this.

isWasm = { cpu = { family = "wasm"; }; };
isMsp430 = { cpu = { family = "msp430"; }; };
isVc4 = { cpu = { family = "vc4"; }; };
Expand Down
4 changes: 0 additions & 4 deletions lib/systems/platforms.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ rec {
baseConfig = "defconfig";
# Build whatever possible as a module, if not stated in the extra config.
autoModules = true;
target = "bzImage";
};
};

Expand All @@ -27,7 +26,6 @@ rec {
name = "PowerNV";

baseConfig = "powernv_defconfig";
target = "vmlinux";
autoModules = true;
# avoid driver/FS trouble arising from unusual page size
extraConfig = ''
Expand Down Expand Up @@ -384,7 +382,6 @@ rec {
# which our initrd builder can't currently do easily.
USB_XHCI_TEGRA m
'';
target = "Image";
};
gcc = {
arch = "armv8-a";
Expand Down Expand Up @@ -534,7 +531,6 @@ rec {
riscv-multiplatform = {
linux-kernel = {
name = "riscv-multiplatform";
target = "Image";
autoModules = true;
baseConfig = "defconfig";
DTB = true;
Expand Down
3 changes: 2 additions & 1 deletion pkgs/os-specific/linux/kernel/common-config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,8 @@ let

EFI_STUB = yes; # EFI bootloader in the bzImage itself
EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER =
whenOlder "6.2" (whenAtLeast "5.8" yes); # initrd kernel parameter for EFI
whenOlder "6.2" (whenAtLeast "5.8" yes); # initrd kernel parameter for EFI
EFI_ZBOOT = whenAtLeast "6.1" yes; # Generic compression support for EFI payloads
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be removed, this is only here for tests.

CGROUPS = yes; # used by systemd
FHANDLE = yes; # used by systemd
SECCOMP = yes; # used by systemd >= 231
Expand Down
56 changes: 56 additions & 0 deletions pkgs/os-specific/linux/kernel/generic.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
let
# Map of images representations to legacy Makefile targets.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Map of images representations to legacy Makefile targets.
# Map of images representations to Makefile targets; i.e. run
# `make ${defaultMapImageToTargets imageName}` to get `imageName`

defaultMapImageToTargets = {
"uImage" = "uinstall";
"zImage" = "zinstall";
"Image.gz" = "zinstall";
"vmlinux" = "install";
Comment on lines +4 to +7
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"uImage" = "uinstall";
"zImage" = "zinstall";
"Image.gz" = "zinstall";
"vmlinux" = "install";
"uImage" = "uinstall"; # U-Boot wrapped image
"zImage" = "zinstall"; # self-extracting images
"Image.gz" = "zinstall";
"vmlinux" = "install"; # the generic binary image

};
in
{ buildPackages
, callPackage
, perl
Expand Down Expand Up @@ -57,6 +66,11 @@
# easy overrides to stdenv.hostPlatform.linux-kernel members
, autoModules ? stdenv.hostPlatform.linux-kernel.autoModules or true
, preferBuiltin ? stdenv.hostPlatform.linux-kernel.preferBuiltin or false
, kernelTarget ? null
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, kernelTarget ? null
, kernelTarget ? defaultKernelTarget

# FIXME: this should go whenever we can rely on KBUILD_IMAGE to install
# our kernel targets, MIPS is probably the only recent kernel in-tree
# which does not support that yet.
, installTargets ? [ ]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, installTargets ? [ ]
, installTargets ? defaultKernelInstallTargets

, kernelArch ? stdenv.hostPlatform.linuxArch
, kernelTests ? []
, nixosTests
Expand Down Expand Up @@ -92,6 +106,41 @@ let
ia32Emulation = true;
} // features) kernelPatches;


# Legacy compatibility layer for
# systems relying on `installTarget`
# being a property of the platform.
defaultKernelInstallTargets = [
stdenv.hostPlatform.linux-kernel.installTarget or
(defaultMapImageToTargets.${stdenv.hostPlatform.linux-kernel.target or "vmlinux"})
];

# Default kernel target in the case no install target or kernelTarget is mentioned
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Default kernel target in the case no install target or kernelTarget is mentioned
# Default kernel target in the case no installTarget or kernelTarget is mentioned

# We aim to derive the "best" kernel given the current constraints.
defaultKernelTarget =
if stdenv.hostPlatform.isx86
then "bzImage"
else if stdenv.hostPlatform.isAarch64
then "Image.gz"
else if (stdenv.hostPlatform.isAarch || stdenv.hostPlatform.isArmv7)
then "vmlinuz"
else if stdenv.hostPlatform.isMips
then "vmlinux"
else if stdenv.hostPlatform.isRiscV
then "Image.xz"
else if stdenv.hostPlatform.isSparc64
then "Image.gz"
else if stdenv.hostPlatform.isSparc
then "vmlinux"
else if stdenv.hostPlatform.isS390
then "bzImage"
else if stdenv.hostplatform.isLoongArch64
then "Image.gz"
else "vmlinux";

finalKernelTarget = if kernelTarget == null then defaultKernelTarget else kernelTarget;
finalInstallTargets = if installTargets != [] then defaultKernelInstallTargets else installTargets;

commonStructuredConfig = import ./common-config.nix {
inherit lib stdenv version;

Expand Down Expand Up @@ -193,6 +242,9 @@ let

kernel = (callPackage ./manual-config.nix { inherit lib stdenv buildPackages; }) (basicArgs // {
inherit kernelPatches randstructSeed extraMakeFlags extraMeta configfile;
kernelTarget = finalKernelTarget;
# kernelTarget has precedence
installTargets = if installTargets != [ ] then finalInstallTargets else [ ];
pos = builtins.unsafeGetAttrPos "version" args;

config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; };
Expand All @@ -201,6 +253,10 @@ let
passthru = basicArgs // {
features = kernelFeatures;
inherit commonStructuredConfig structuredExtraConfig extraMakeFlags isZen isHardened isLibre;

kernelTarget = if kernelTarget == null then defaultKernelTarget else kernelTarget;
installTargets = if installTargets != [] then defaultKernelInstallTargets else installTargets;

isXen = lib.warn "The isXen attribute is deprecated. All Nixpkgs kernels that support it now have Xen enabled." true;

# Adds dependencies needed to edit the config:
Expand Down
29 changes: 19 additions & 10 deletions pkgs/os-specific/linux/kernel/manual-config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ in lib.makeOverridable ({
src,
# a list of { name=..., patch=..., extraConfig=...} patches
kernelPatches ? [],
# KBUILD_IMAGE
kernelTarget ? null,
# a list of install targets, e.g. :
# - uinstall for uImage, U-Boot wrapped image
# - zinstall for zImage, self-extracting images
# - install, the generic binary image
Comment on lines +36 to +38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# - uinstall for uImage, U-Boot wrapped image
# - zinstall for zImage, self-extracting images
# - install, the generic binary image

(see suggestion above where I relocated these comments)

# those targets are legacy and are present
# only for compatibility with old kernels or MIPS for now.
# prefer KBUILD_IMAGE instead.
installTargets ? [],
# The kernel .config file
configfile,
# Manually specified nixexpr representing the config
Expand Down Expand Up @@ -88,14 +98,16 @@ let
isModular = config.isYes "MODULES";

kernelConf = stdenv.hostPlatform.linux-kernel;
target = kernelConf.target or "vmlinux";

buildDTBs = kernelConf.DTB or false;
in

assert lib.versionOlder version "5.8" -> libelf != null;
assert lib.versionAtLeast version "5.8" -> elfutils != null;

# An kernel image target must be explicit as we cannot learn it
# from the defaults in the different Makefile.
assert kernelTarget != null || installTargets != [];

Comment on lines +107 to +110
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# An kernel image target must be explicit as we cannot learn it
# from the defaults in the different Makefile.
assert kernelTarget != null || installTargets != [];

I'm not sure what situation this assert was meant to catch.

stdenv.mkDerivation ({
pname = "linux";
inherit version src;
Expand Down Expand Up @@ -220,6 +232,7 @@ stdenv.mkDerivation ({
] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
"CROSS_COMPILE=${stdenv.cc.targetPrefix}"
] ++ (kernelConf.makeFlags or [])
++ lib.optional (kernelTarget != null) "KBUILD_IMAGE=${kernelTarget}"
++ extraMakeFlags;

karch = stdenv.hostPlatform.linuxArch;
Expand Down Expand Up @@ -296,12 +309,7 @@ stdenv.mkDerivation ({
'';

# Some image types need special install targets (e.g. uImage is installed with make uinstall)
installTargets = [
(kernelConf.installTarget or (
/**/ if target == "uImage" then "uinstall"
else if target == "zImage" || target == "Image.gz" then "zinstall"
else "install"))
];
installTargets = if installTargets == [ ] then [ "install" ] else installTargets;

postInstall = optionalString isModular ''
if [ -z "''${dontStrip-}" ]; then
Expand Down Expand Up @@ -332,7 +340,8 @@ stdenv.mkDerivation ({
cp -HR $buildRoot/$f $dev/lib/modules/${modDirVersion}/build/$f
fi
done
ln -s $dev/lib/modules/${modDirVersion}/build/vmlinux $dev
mkdir $debug/
ln -s $dev/lib/modules/${modDirVersion}/build/vmlinux $debug
Comment on lines -335 to +344
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going on here? Seems unrelated. Should this be part of a separate PR?


# !!! No documentation on how much of the source tree must be kept
# If/when kernel builds fail due to missing files, you can add
Expand Down Expand Up @@ -424,5 +433,5 @@ stdenv.mkDerivation ({
} // optionalAttrs (pos != null) {
inherit pos;
} // optionalAttrs isModular {
outputs = [ "out" "dev" ];
outputs = [ "out" "dev" "debug" ];
Comment on lines -427 to +436
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going on here? Seems unrelated. Should this be part of a separate PR?

}))