Refactor to allow overriding for customizing kernel

This commit is contained in:
Lan Tian 2025-12-09 21:08:14 -08:00
parent 2e4f520495
commit 88572ff3a0
No known key found for this signature in database
GPG Key ID: 04E66B6B25A0862B
6 changed files with 187 additions and 128 deletions

View File

@ -109,7 +109,7 @@ If you want to construct your own `linuxPackages` attrset with `linuxKernel.pack
{ pkgs, ... }: { pkgs, ... }:
{ {
nixpkgs.overlays = [ self.overlay ]; nixpkgs.overlays = [ self.overlay ];
boot.kernelPackages = pkgs.cachyosKernels.linuxPackages-cachyos-latest; boot.kernelPackages = pkgs.linuxKernel.packagesFor pkgs.cachyosKernels.linux-cachyos-latest;
# ZFS config # ZFS config
boot.supportedFilesystems.zfs = true; boot.supportedFilesystems.zfs = true;
@ -124,3 +124,31 @@ If you want to construct your own `linuxPackages` attrset with `linuxKernel.pack
}; };
} }
``` ```
## How to apply CachyOS patches on your own kernel
The kernels provided in this flake can be overridden to use your own kernel source. This is helpful if you want to use a kernel version not available in Nixpkgs.
```nix
{
kernel = pkgs.cachyosKernels.linux-cachyos-latest.override {
pname = "linux-cachyos-with-custom-source";
version = "6.12.34";
src = pkgs.fetchurl {
# ...
};
# Additional args are available. See kernel-cachyos/mkCachyKernel.nix
};
# For non-LTO kernels
kernelPackages = pkgs.linuxKernel.packagesFor kernel;
# helpers.nix provides a few utilities for building kernel with LTO.
# I haven't figured out a clean way to expose it in flakes.
helpers = pkgs.callPackage "${inputs.nix-cachyos-kernel.outPath}/helpers.nix" {};
# For LTO kernels, helpers.kernelModuleLLVMOverride fixes compilation for some
# out-of-tree modules in nixpkgs.
kernelPackagesWithLTOFix = helpers.kernelModuleLLVMOverride (pkgs.linuxKernel.packagesFor kernel);
}
```

View File

@ -28,17 +28,21 @@
loadPackages = loadPackages =
pkgs: pkgs:
let let
kernels = load =
path:
lib.removeAttrs lib.removeAttrs
(pkgs.callPackage ./kernel-cachyos { (pkgs.callPackage path {
inherit inputs; inherit inputs;
}) })
[ [
"override" "override"
"overrideDerivation" "overrideDerivation"
]; ];
kernels = load ./kernel-cachyos;
packages = load ./kernel-cachyos/packages.nix;
in in
kernels kernels
// packages
// { // {
zfs-cachyos = pkgs.callPackage ./zfs-cachyos { zfs-cachyos = pkgs.callPackage ./zfs-cachyos {
inherit inputs; inherit inputs;

View File

@ -77,5 +77,4 @@ rec {
v v
) prev ) prev
); );
} }

View File

@ -8,39 +8,32 @@
}: }:
let let
mkCachyKernel = callPackage ./mkCachyKernel.nix { inherit inputs; }; mkCachyKernel = callPackage ./mkCachyKernel.nix { inherit inputs; };
in
batch = builtins.listToAttrs (
{ builtins.map (v: lib.nameValuePair v.pname v) [
pnameSuffix, (mkCachyKernel {
version, pname = "linux-cachyos-latest";
src,
configVariant,
...
}:
[
(mkCachyKernel {
pnameSuffix = "${pnameSuffix}";
inherit version src configVariant;
lto = false;
})
(mkCachyKernel {
pnameSuffix = "${pnameSuffix}-lto";
inherit version src configVariant;
lto = true;
})
];
batches = [
(batch {
pnameSuffix = "latest";
inherit (linux_latest) version src; inherit (linux_latest) version src;
configVariant = "linux-cachyos"; configVariant = "linux-cachyos";
lto = false;
}) })
(batch { (mkCachyKernel {
pnameSuffix = "lts"; pname = "linux-cachyos-latest-lto";
inherit (linux_latest) version src;
configVariant = "linux-cachyos";
lto = true;
})
(mkCachyKernel {
pname = "linux-cachyos-lts";
inherit (linux) version src; inherit (linux) version src;
configVariant = "linux-cachyos-lts"; configVariant = "linux-cachyos-lts";
lto = false;
}) })
]; (mkCachyKernel {
in pname = "linux-cachyos-lts-lto";
builtins.listToAttrs (lib.flatten batches) inherit (linux) version src;
configVariant = "linux-cachyos-lts";
lto = true;
})
]
)

View File

@ -5,102 +5,120 @@
buildLinux, buildLinux,
stdenv, stdenv,
kernelPatches, kernelPatches,
linuxKernel,
... ...
}: }:
{ lib.makeOverridable (
pnameSuffix, {
version, pname,
src, version,
configVariant, src,
lto,
}:
let
helpers = callPackage ../helpers.nix { };
inherit (helpers) stdenvLLVM ltoMakeflags kernelModuleLLVMOverride;
splitted = lib.splitString "-" version; # Kernel config variant to be used as defconfig, e.g. "linux-cachyos-lts".
ver0 = builtins.elemAt splitted 0; # See https://github.com/CachyOS/linux-cachyos for available values.
major = lib.versions.pad 2 ver0; configVariant,
cachyosConfigFile = "${inputs.cachyos-kernel.outPath}/${configVariant}/config"; # Set to true to enable Clang+ThinLTO.
lto,
# buildLinux doesn't accept postPatch, so adding config file early here # Patches to be applied in patchedSrc phase. This is different from buildLinux's kernelPatches.
patchedSrc = stdenv.mkDerivation { prePatch ? "",
pname = "linux-cachyos-${pnameSuffix}-src"; patches ? [ ],
inherit version src; postPatch ? "",
patches = [
kernelPatches.bridge_stp_helper.patch
kernelPatches.request_key_helper.patch
"${inputs.cachyos-kernel-patches.outPath}/${major}/all/0001-cachyos-base-all.patch"
];
postPatch = ''
for DIR in arch/*/configs; do
install -Dm644 ${cachyosConfigFile} $DIR/cachyos_defconfig
done
'';
dontConfigure = true;
dontBuild = true;
dontFixup = true;
installPhase = ''
mkdir -p $out
cp -r * $out/
'';
};
kernelPackage = buildLinux { # See nixpkgs/pkgs/os-specific/linux/kernel/generic.nix for additional options.
pname = "linux-cachyos-${pnameSuffix}"; # Additional args are passed to buildLinux.
inherit version; ...
src = patchedSrc; }@args:
stdenv = if lto then stdenvLLVM else stdenv; let
helpers = callPackage ../helpers.nix { };
inherit (helpers) stdenvLLVM ltoMakeflags;
extraMakeFlags = lib.optionals lto ltoMakeflags; splitted = lib.splitString "-" version;
ver0 = builtins.elemAt splitted 0;
major = lib.versions.pad 2 ver0;
defconfig = "cachyos_defconfig"; cachyosConfigFile = "${inputs.cachyos-kernel.outPath}/${configVariant}/config";
cachyosPatch = "${inputs.cachyos-kernel-patches.outPath}/${major}/all/0001-cachyos-base-all.patch";
# Clang has some incompatibilities with NixOS's default kernel config # buildLinux doesn't accept postPatch, so adding config file early here
ignoreConfigErrors = lto; patchedSrc = stdenv.mkDerivation {
pname = "${pname}-src";
structuredExtraConfig = inherit version src prePatch;
with lib.kernel; patches = [
( kernelPatches.bridge_stp_helper.patch
{ kernelPatches.request_key_helper.patch
NR_CPUS = lib.mkForce (option (freeform "8192")); cachyosPatch
]
# Follow NixOS default config to not break etc overlay ++ patches;
OVERLAY_FS = module; postPatch = ''
OVERLAY_FS_REDIRECT_DIR = no; for DIR in arch/*/configs; do
OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW = yes; install -Dm644 ${cachyosConfigFile} $DIR/cachyos_defconfig
OVERLAY_FS_INDEX = no; done
OVERLAY_FS_XINO_AUTO = no; ''
OVERLAY_FS_METACOPY = no; + postPatch;
OVERLAY_FS_DEBUG = no; dontConfigure = true;
} dontBuild = true;
// lib.optionalAttrs lto { dontFixup = true;
LTO_NONE = no; installPhase = ''
LTO_CLANG_THIN = yes; mkdir -p $out
} cp -r * $out/
); '';
extraMeta = {
description = "Linux CachyOS Kernel" + lib.optionalString lto " with Clang+ThinLTO";
}; };
}; in
buildLinux (
(lib.removeAttrs args [
"pname"
"version"
"src"
"configVariant"
"lto"
"prePatch"
"patches"
"postPatch"
])
// {
inherit pname version;
src = patchedSrc;
stdenv = args.stdenv or (if lto then stdenvLLVM else stdenv);
zfsPackage = callPackage ../zfs-cachyos { extraMakeFlags = (lib.optionals lto ltoMakeflags) ++ (args.extraMakeFlags or [ ]);
inherit inputs;
kernel = kernelPackage; defconfig = args.defconfig or "cachyos_defconfig";
};
in # Clang has some incompatibilities with NixOS's default kernel config
[ ignoreConfigErrors = args.ignoreConfigErrors or lto;
(lib.nameValuePair "linux-cachyos-${pnameSuffix}" kernelPackage)
(lib.nameValuePair "linuxPackages-cachyos-${pnameSuffix}" ( structuredExtraConfig =
kernelModuleLLVMOverride ( with lib.kernel;
(linuxKernel.packagesFor kernelPackage).extend ( (
final: prev: { {
zfs_cachyos = zfsPackage; NR_CPUS = lib.mkForce (option (freeform "8192"));
}
) # Follow NixOS default config to not break etc overlay
) OVERLAY_FS = module;
)) OVERLAY_FS_REDIRECT_DIR = no;
] OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW = yes;
OVERLAY_FS_INDEX = no;
OVERLAY_FS_XINO_AUTO = no;
OVERLAY_FS_METACOPY = no;
OVERLAY_FS_DEBUG = no;
}
// (lib.optionalAttrs lto {
LTO_NONE = no;
LTO_CLANG_THIN = yes;
})
// (args.structuredExtraConfig or { })
);
extraMeta = {
description = "Linux CachyOS Kernel" + lib.optionalString lto " with Clang+ThinLTO";
}
// (args.extraMeta or { });
extraPassthru = {
inherit cachyosConfigFile cachyosPatch;
}
// (args.extraPassthru or { });
}
)
)

View File

@ -1,14 +1,31 @@
{ {
mode ? null, inputs,
callPackage, callPackage,
lib, lib,
linuxKernel, linuxKernel,
sources,
... ...
}: }:
let let
kernels = callPackage ./default.nix { inherit mode sources; }; helpers = callPackage ../helpers.nix { };
inherit (helpers) kernelModuleLLVMOverride;
kernels = lib.filterAttrs (_: lib.isDerivation) (callPackage ./. { inherit inputs; });
in in
lib.mapAttrs (n: v: linuxKernel.packagesFor v) ( lib.mapAttrs' (
lib.filterAttrs (n: nv: !lib.hasSuffix "configfile" n) kernels n: v:
) let
zfsPackage = callPackage ../zfs-cachyos {
inherit inputs;
kernel = v;
};
packages = kernelModuleLLVMOverride (
(linuxKernel.packagesFor v).extend (
final: prev: {
zfs_cachyos = zfsPackage;
}
)
);
in
lib.nameValuePair "linuxPackages-${lib.removePrefix "linux-" n}" packages
) kernels