Fixed kernel compile sccache caching

a little bit of chatgpt5.4 slop has fixed the kernel caching with sccache. Good stuff.
This commit is contained in:
TheK0tYaRa 2026-03-09 02:34:29 +02:00
parent dd1c091312
commit bbc6f283ed
2 changed files with 57 additions and 21 deletions

View file

@ -33,6 +33,7 @@ pkgs.callPackage (
{ buildLinux, ... }@args:
let
llvm = pkgs.llvmPackages_latest;
rust = pkgs.rustc-unwrapped;
# buildLinux uses common-flags.nix which sets CC to the *unwrapped* compiler:
# CC=${lib.getExe stdenv.cc.cc}
@ -43,31 +44,54 @@ pkgs.callPackage (
buildPkgs = args.buildPackages or pkgs.buildPackages;
realHostCC = lib.getExe' buildPkgs.stdenv.cc "${buildPkgs.stdenv.cc.targetPrefix}cc";
realHostCXX = lib.getExe' buildPkgs.stdenv.cc "${buildPkgs.stdenv.cc.targetPrefix}c++";
realRustc = lib.getExe rust;
realHostRustc = realRustc;
clangSccache = pkgs.writeShellScriptBin "clang" ''
set -euo pipefail
: "''${SCCACHE_ENFORCE_MARKER:?SCCACHE_ENFORCE_MARKER is not set}"
: > "''${SCCACHE_ENFORCE_MARKER}"
export SCCACHE_DIR=${lib.escapeShellArg sccacheDir}
if [ -n "''${SCCACHE_ENFORCE_MARKER-}" ]; then
: > "''${SCCACHE_ENFORCE_MARKER}"
fi
exec ${pkgs.sccache}/bin/sccache ${realClang} "$@"
'';
hostccSccache = pkgs.writeShellScriptBin "cc" ''
set -euo pipefail
: "''${SCCACHE_ENFORCE_MARKER:?SCCACHE_ENFORCE_MARKER is not set}"
: > "''${SCCACHE_ENFORCE_MARKER}"
export SCCACHE_DIR=${lib.escapeShellArg sccacheDir}
if [ -n "''${SCCACHE_ENFORCE_MARKER-}" ]; then
: > "''${SCCACHE_ENFORCE_MARKER}"
fi
exec ${pkgs.sccache}/bin/sccache ${realHostCC} "$@"
'';
hostcxxSccache = pkgs.writeShellScriptBin "c++" ''
set -euo pipefail
: "''${SCCACHE_ENFORCE_MARKER:?SCCACHE_ENFORCE_MARKER is not set}"
: > "''${SCCACHE_ENFORCE_MARKER}"
export SCCACHE_DIR=${lib.escapeShellArg sccacheDir}
if [ -n "''${SCCACHE_ENFORCE_MARKER-}" ]; then
: > "''${SCCACHE_ENFORCE_MARKER}"
fi
exec ${pkgs.sccache}/bin/sccache ${realHostCXX} "$@"
'';
rustcSccache = pkgs.writeShellScriptBin "rustc" ''
set -euo pipefail
export SCCACHE_DIR=${lib.escapeShellArg sccacheDir}
if [ -n "''${SCCACHE_ENFORCE_MARKER-}" ]; then
: > "''${SCCACHE_ENFORCE_MARKER}"
fi
exec ${pkgs.sccache}/bin/sccache ${realRustc} "$@"
'';
hostrustcSccache = pkgs.writeShellScriptBin "rustc" ''
set -euo pipefail
export SCCACHE_DIR=${lib.escapeShellArg sccacheDir}
if [ -n "''${SCCACHE_ENFORCE_MARKER-}" ]; then
: > "''${SCCACHE_ENFORCE_MARKER}"
fi
exec ${pkgs.sccache}/bin/sccache ${realHostRustc} "$@"
'';
structuredExtraConfig' =
(with lib.kernel; {
LTO_CLANG_THIN = yes;
@ -97,29 +121,41 @@ pkgs.callPackage (
"CC=${lib.getExe clangSccache}"
"HOSTCC=${lib.getExe hostccSccache}"
"HOSTCXX=${lib.getExe hostcxxSccache}"
"RUSTC=${lib.getExe rustcSccache}"
"HOSTRUSTC=${lib.getExe hostrustcSccache}"
];
# Enforce that the wrappers were actually invoked at least once.
# sccache is a compiler wrapper; this is the contract were enforcing. :contentReference[oaicite:4]{index=4}
# Start with clean stats so the build-phase check reports only this build.
preBuild =
(args.preBuild or "")
+ lib.optionalString enforceSccache ''
export SCCACHE_ENFORCE_MARKER="$NIX_BUILD_TOP/.sccache-used"
rm -f "$SCCACHE_ENFORCE_MARKER"
${pkgs.sccache}/bin/sccache --start-server || true
${pkgs.sccache}/bin/sccache --zero-stats || true
'';
postBuild =
(args.postBuild or "")
+ lib.optionalString enforceSccache ''
if [ ! -e "$SCCACHE_ENFORCE_MARKER" ]; then
echo "FATAL: sccache enforcement failed: compiler wrappers were not invoked."
echo "This means buildLinux did not use your CC/HOSTCC overrides."
exit 1
fi
${pkgs.sccache}/bin/sccache --show-stats --stats-format text || true
'';
buildPhase =
if enforceSccache then
''
runHook preBuild
export SCCACHE_ENFORCE_MARKER="$NIX_BUILD_TOP/.sccache-used"
rm -f "$SCCACHE_ENFORCE_MARKER"
make "''${makeFlags[@]}" "''${buildFlags[@]}"
if [ ! -e "$SCCACHE_ENFORCE_MARKER" ]; then
echo "FATAL: sccache enforcement failed during buildPhase: compiler wrappers were not invoked."
echo "This means the build stage did not use your CC/HOSTCC/HOSTCXX/RUSTC/HOSTRUSTC overrides."
exit 1
fi
${pkgs.sccache}/bin/sccache --show-stats --stats-format text || true
runHook postBuild
''
else
(args.buildPhase or ''
runHook preBuild
make "''${makeFlags[@]}" "''${buildFlags[@]}"
runHook postBuild
'');
postBuild = args.postBuild or "";
structuredExtraConfig = structuredExtraConfig';

View file

@ -61,7 +61,7 @@
kernel-src = inputs.kernel-src;
};
modules = [
# ./custom/modules
./custom/modules
ragenix.nixosModules.default
niri-flake.nixosModules.niri
nix-index-database.nixosModules.default