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

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