Patching nixpkgs sources


Does nixpkgs contain some mechanism for patching .nix source files directly and not just modules or derivations?

I’d like to patch modDirVersion and tag in nixpkgs/linux-rpi.nix at f0ba8235153dd2e25cf06cbf70d43efdd4443592 · NixOS/nixpkgs · GitHub

and easiest would be to patch it directly at source file level because those things I want to override are not just some attributes positioned directly in some attrset at the top of the file but they are in an attrset passed to buildLinux which is passed to overrideDerivation etc…

No. You can either write a derivation that creates a patched nixpkgs source and import that (which likely requires an extra bootstrapping nixpkgs instance). In most cases it is easier to have an alternative .nix file instead of a patch and just switch out the attribute via an overlay, i.e. importing your file using callPackage instead of overriding what be normally there.

You can’t patch nixpkgs sources but in your case the writer of that .nix source has provided a way that makes it possible to change modDirVersion and tag indirectly using .override.

The file you want to change has an argsOverride argument that you can use to override all the arguments to buildLinux. In your case you would want to override modDirVersion, version and src since those are the attributes where modDirVersion and tag are used.

You would use this approach like this:

pkgs.linuxKernel.kernels.linux_rpi4.override (let
  modDirVersion = "6.1.21";
  tag = "1.20230405";
  hash = "sha256-ILwecHZ1BN6GhZAUB6/UwiN/rZ8gHndKON6DUhidtxI=";
in {
  argsOverride = {
    version = "${modDirVersion}-${tag}";
    inherit modDirVersion;
    src = pkgs.fetchFromGitHub {
      owner = "raspberrypi";
      repo = "linux";
      rev = tag;
      inherit hash;