Older kernels compilation

Hello, I need to compile a specific kernel version. The configuration I normally use is based on the documentation. I’m able to compile newer kernels without a problem.
The following case make the compilation brake.

boot.kernelPackages = pkgs.linuxPackagesFor (pkgs.linux_5_4.override {
    argsOverride = rec {
      src = pkgs.fetchurl {
        url = "mirror://kernel/linux/kernel/v5.x/linux-${version}.tar.xz";
        sha256 = "0985fcf6cdcebceca63cb70abab66d12e75fac61014a796ce71272b33f831515";
      };
      version = "5.4.84";
      modDirVersion = "5.4.84";
    };
  });

This is the error I’m getting:
subcmd-util.h:56:23: error: pointer may be used after 'realloc' [-Werror=use-after-free]
If i was doing manually I would set the option on make.
How can I set this option in the declarative way? Or even set an older gcc version, that seems to be more stable to solve this kind of problems.

Updating the above info.
I kept trying to fix this issue and found this git GitHub - nix4noobs/nixosvm-custom-kernel: example showing how to use a custom kernel with a custom .config, which did something similar to what I want to do, but not quite. It uses the manual-config, which I assume it’s this file https://github.com/NixOS/nixpkgs/blob/de47b2e8a5e13afe8581f76bcb59ac5f17b4ac57/pkgs/os-specific/linux/kernel/manual-config.nix.
I modified my kernel configuration to this:

boot.kernelPackages = let
    my-kernel-pkg = { stdenv, lib, linuxKernel , ...} @ args:
      (linuxKernel.manualConfig rec {
        inherit stdenv lib;

        allowImportFromDerivation = true;
        configfile = ./kernel_config;

        extraMakeFlags = [ "KCFLAGS=-Wno-error=use-after-free -Wno-error=deprecated-declarations" ];

        version = "5.4.84";
        modDirVersion = "5.4.84";
        src = pkgs.fetchurl {
          url = "mirror://kernel/linux/kernel/v5.x/linux-${version}.tar.xz";
          sha256 = "0985fcf6cdcebceca63cb70abab66d12e75fac61014a796ce71272b33f831515";
        };
        kernelPatches = [ ];
      });
    my-kernel = pkgs.callPackage my-kernel-pkg {};
  in
    pkgs.recurseIntoAttrs (pkgs.linuxPackagesFor my-kernel);

I still got the same error, which I found it weird.
Then, I tried to set the stdenv to use a custom gcc version.

boot.kernelPackages = let
    my-kernel-pkg = { stdenv, lib, linuxKernel , ...} @ args:
      (linuxKernel.manualConfig rec {
        inherit lib;
        stdenv = pkgs.gcc10Stdenv;

        allowImportFromDerivation = true;
        configfile = ./kernel_config;
        
        version = "5.4.84";
        modDirVersion = "5.4.84";
        src = pkgs.fetchurl {
          url = "mirror://kernel/linux/kernel/v5.x/linux-${version}.tar.xz";
          sha256 = "0985fcf6cdcebceca63cb70abab66d12e75fac61014a796ce71272b33f831515";
        };
        kernelPatches = [ ];
      });
    my-kernel = pkgs.callPackage my-kernel-pkg {};
  in
    pkgs.recurseIntoAttrs (pkgs.linuxPackagesFor my-kernel);

This doesn’t fix either.
I’m somewhat new to nix, so I can’t understand how can I fix this.

Note: in both of these configurations I don’t use the kernel patches as before, the configurations were set in the config file.

Did you ever find a solution to get your build working?

I’m struggling with this same issue now, I’ve tried setting hardeningDisable to all, thinking that would turn off WERROR, but it seems to have no effect.

Some older kernels can’t be built without patching… For others, it turns out you can just override any kernel that’s not been disabled as “End of Life”.

For anyone wondering why someone would do this, in my case its to try to bisect kernel commits to determine when amdgpu MST broke with multiple displays (USB-C dock with two 2 monitors crashes X-Server (Ryzen 3500U, HP EliteBook 735 G6, Linux >= 6.1 regression) (#2680) · Issues · drm / amd · GitLab).

So for example, using the 6.1 kernel package name, which is still supported (for now), one can build 6.0 with this:

	# 6.0.19
	boot.kernelPackages = pkgs.linuxPackagesFor (pkgs.linux_6_1.override {
			argsOverride = rec {
				src = pkgs.fetchurl {
							url = "https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${version}.tar.xz";
							sha256 = "sha256-q+N+sOLjMb3HxBEBFWZOGA59Q7czbea0zSvRsSPTAgc=";
				};
				version = "6.0.19";
				modDirVersion = "6.0.19";
			};
	});

Or an old RC kernel like this:

	# 6.1-rc1
	boot.kernelPackages = pkgs.linuxPackagesFor (pkgs.linux_6_1.override {
			argsOverride = rec {
				src = pkgs.fetchurl {
							url = "https://git.kernel.org/torvalds/t/linux-${version}.tar.gz";
							sha256 = "sha256-xGoC/eHYs3P8zOQY98p0eoFXXDHN3/h8MgEscc6hMnQ=";
				};
				version = "6.1-rc1";
				modDirVersion = "6.1.0-rc1";
				ignoreConfigErrors = true; # error: unused option: X86_PLATFORM_DRIVERS_HP
			};
	});