commit 0279735e4327c70fc1da94c5c90339c9d68df5c6 Author: Lan Tian Date: Mon Dec 8 19:05:27 2025 -0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5c1327 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +result +result* +trace.txt* +.gcroots +.vscode +.direnv +*.log +.pre-commit-config.yaml +update-git-commits.txt +*.cmp +*.fetchlog diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..5a021c4 --- /dev/null +++ b/flake.lock @@ -0,0 +1,95 @@ +{ + "nodes": { + "cachyos-kernel": { + "flake": false, + "locked": { + "lastModified": 1764950048, + "narHash": "sha256-Dgc/8jbcMYeOr8xgd/yVFTL+8uNmAHAPyTsVu6G4jeA=", + "owner": "CachyOS", + "repo": "linux-cachyos", + "rev": "b76feee309500d7ba6a955760ed4a8e698902ba3", + "type": "github" + }, + "original": { + "owner": "CachyOS", + "repo": "linux-cachyos", + "type": "github" + } + }, + "cachyos-kernel-patches": { + "flake": false, + "locked": { + "lastModified": 1765190459, + "narHash": "sha256-TJJKd86jDyighG3Jx8MNyiuQTpEIMAsA2GkWpqttwFg=", + "owner": "CachyOS", + "repo": "kernel-patches", + "rev": "6dfdbc7f8f3ee4d9f5dc8adfb0672ef5d8e1e3d5", + "type": "github" + }, + "original": { + "owner": "CachyOS", + "repo": "kernel-patches", + "type": "github" + } + }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1763759067, + "narHash": "sha256-LlLt2Jo/gMNYAwOgdRQBrsRoOz7BPRkzvNaI/fzXi2Q=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "2cccadc7357c0ba201788ae99c4dfa90728ef5e0", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1765183668, + "narHash": "sha256-TBA7CE44IHYfvOPBWcyLncpVrrKEiXWPdOrF8CD6W84=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "fc2de1563f89f0843eba27f14576d261df0e3b80", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable-small", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1761765539, + "narHash": "sha256-b0yj6kfvO8ApcSE+QmA6mUfu8IYG6/uU28OFn4PaC8M=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "719359f4562934ae99f5443f20aa06c2ffff91fc", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "root": { + "inputs": { + "cachyos-kernel": "cachyos-kernel", + "cachyos-kernel-patches": "cachyos-kernel-patches", + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..01687ac --- /dev/null +++ b/flake.nix @@ -0,0 +1,44 @@ +{ + description = "CachyOS Kernels"; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small"; + flake-parts.url = "github:hercules-ci/flake-parts"; + + cachyos-kernel = { + url = "github:CachyOS/linux-cachyos"; + flake = false; + }; + cachyos-kernel-patches = { + url = "github:CachyOS/kernel-patches"; + flake = false; + }; + }; + outputs = + { self, flake-parts, ... }@inputs: + flake-parts.lib.mkFlake { inherit inputs; } ( + { + lib, + ... + }: + { + systems = [ + "x86_64-linux" + "aarch64-linux" + ]; + + perSystem = + { + pkgs, + ... + }: + rec { + # Legacy packages contain linux-cachyos-* and linuxPackages-cachyos-* + legacyPackages = pkgs.callPackage ./kernel-cachyos { + inherit inputs; + }; + # Packages only contain linux-cachyos-* due to Flake schema requirements + packages = lib.filterAttrs (_: lib.isDerivation) legacyPackages; + }; + } + ); +} diff --git a/helpers.nix b/helpers.nix new file mode 100644 index 0000000..e1a66e2 --- /dev/null +++ b/helpers.nix @@ -0,0 +1,81 @@ +{ + pkgs, + lib, + ... +}: +rec { + # https://github.com/NixOS/nixpkgs/pull/129806 + # https://github.com/lovesegfault/nix-config/blob/master/nix/overlays/linux-lto.nix + + noBintools = { + bootBintools = null; + bootBintoolsNoLibc = null; + }; + hostLLVM = pkgs.pkgsBuildHost.llvmPackages.override noBintools; + buildLLVM = pkgs.pkgsBuildBuild.llvmPackages.override noBintools; + + ltoMakeflags = [ + "LLVM=1" + "LLVM_IAS=1" + "CC=${buildLLVM.clangUseLLVM}/bin/clang" + "LD=${buildLLVM.lld}/bin/ld.lld" + "HOSTLD=${hostLLVM.lld}/bin/ld.lld" + "AR=${buildLLVM.llvm}/bin/llvm-ar" + "HOSTAR=${hostLLVM.llvm}/bin/llvm-ar" + "NM=${buildLLVM.llvm}/bin/llvm-nm" + "STRIP=${buildLLVM.llvm}/bin/llvm-strip" + "OBJCOPY=${buildLLVM.llvm}/bin/llvm-objcopy" + "OBJDUMP=${buildLLVM.llvm}/bin/llvm-objdump" + "READELF=${buildLLVM.llvm}/bin/llvm-readelf" + "HOSTCC=${hostLLVM.clangUseLLVM}/bin/clang" + "HOSTCXX=${hostLLVM.clangUseLLVM}/bin/clang++" + ]; + + stdenvLLVM = + let + mkLLVMPlatform = + platform: + platform + // { + linux-kernel = platform.linux-kernel // { + makeFlags = (platform.linux-kernel.makeFlags or [ ]) ++ ltoMakeflags; + }; + }; + + stdenv' = pkgs.overrideCC hostLLVM.stdenv hostLLVM.clangUseLLVM; + in + stdenv'.override (old: { + hostPlatform = mkLLVMPlatform old.hostPlatform; + buildPlatform = mkLLVMPlatform old.buildPlatform; + extraNativeBuildInputs = [ + hostLLVM.lld + pkgs.patchelf + ]; + }); + + kernelModuleLLVMOverride = + kernelPackages_: + kernelPackages_.extend ( + _final: prev: + lib.mapAttrs ( + n: v: + if + builtins.elem "LLVM=1" kernelPackages_.kernel.commonMakeFlags + && !(builtins.elem n [ "kernel" ]) + && lib.isDerivation v + && ((v.overrideAttrs or null) != null) + then + v.overrideAttrs (old: { + makeFlags = (old.makeFlags or [ ]) ++ kernelPackages_.kernel.commonMakeFlags; + postPatch = (if (old.postPatch or null) == null then "" else old.postPatch) + '' + if [ -f Makefile ]; then + substituteInPlace Makefile --replace "gcc" "cc" + fi + ''; + }) + else + v + ) prev + ); + +} diff --git a/kernel-cachyos/default.nix b/kernel-cachyos/default.nix new file mode 100644 index 0000000..9acb4e8 --- /dev/null +++ b/kernel-cachyos/default.nix @@ -0,0 +1,46 @@ +{ + inputs, + callPackage, + lib, + linux_latest, + linux, + ... +}: +let + mkCachyKernel = callPackage ./mkCachyKernel.nix { inherit inputs; }; + + batch = + { + pnameSuffix, + version, + 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; + configVariant = "linux-cachyos"; + }) + (batch { + pnameSuffix = "lts"; + inherit (linux) version src; + configVariant = "linux-cachyos-lts"; + }) + ]; +in +builtins.listToAttrs (lib.flatten batches) diff --git a/kernel-cachyos/mkCachyKernel.nix b/kernel-cachyos/mkCachyKernel.nix new file mode 100644 index 0000000..be9af2c --- /dev/null +++ b/kernel-cachyos/mkCachyKernel.nix @@ -0,0 +1,88 @@ +{ + inputs, + lib, + callPackage, + buildLinux, + stdenv, + kernelPatches, + linuxKernel, + ... +}: +{ + pnameSuffix, + version, + src, + configVariant, + lto, +}: +let + helpers = callPackage ../helpers.nix { }; + inherit (helpers) stdenvLLVM ltoMakeflags kernelModuleLLVMOverride; + + splitted = lib.splitString "-" version; + ver0 = builtins.elemAt splitted 0; + major = lib.versions.pad 2 ver0; + + cachyosConfigFile = "${inputs.cachyos-kernel.outPath}/${configVariant}/config"; + + # buildLinux doesn't accept postPatch, so adding config file early here + patchedSrc = stdenv.mkDerivation { + pname = "linux-cachyos-${pnameSuffix}-src"; + inherit version src; + 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 { + pname = "linux-cachyos-${pnameSuffix}"; + inherit version; + src = patchedSrc; + stdenv = if lto then stdenvLLVM else stdenv; + + extraMakeFlags = lib.optionals lto ltoMakeflags; + + defconfig = "cachyos_defconfig"; + + modDirVersion = "${ver0}"; + + # Clang has some incompatibilities with NixOS's default kernel config + ignoreConfigErrors = lto; + + structuredExtraConfig = + with lib.kernel; + ( + { + NR_CPUS = lib.mkForce (option (freeform "8192")); + } + // lib.optionalAttrs lto { + LTO_NONE = no; + LTO_CLANG_THIN = yes; + } + ); + + extraMeta = { + description = "Linux CachyOS Kernel" + lib.optionalString lto " with Clang+ThinLTO"; + }; + }; +in +[ + (lib.nameValuePair "linux-cachyos-${pnameSuffix}" kernelPackage) + (lib.nameValuePair "linuxPackages-cachyos-${pnameSuffix}" ( + kernelModuleLLVMOverride (linuxKernel.packagesFor kernelPackage) + )) +] diff --git a/kernel-cachyos/packages.nix b/kernel-cachyos/packages.nix new file mode 100644 index 0000000..b6a3ed4 --- /dev/null +++ b/kernel-cachyos/packages.nix @@ -0,0 +1,14 @@ +{ + mode ? null, + callPackage, + lib, + linuxKernel, + sources, + ... +}: +let + kernels = callPackage ./default.nix { inherit mode sources; }; +in +lib.mapAttrs (n: v: linuxKernel.packagesFor v) ( + lib.filterAttrs (n: nv: !lib.hasSuffix "configfile" n) kernels +)