How to get a certain file from the linux kernel source?

Is it possible to add a file from the linux kernel source (e.g. a certain .dts file) to the $out of buildLinux? I tried to use overrideAttrs and add cp arch/arm/boot/dts/vendor/file.dts $out to postInstall and it does not find it. I don’t know if I’m doing this correctly. Please help.

In general, you should be able to reference the directory of a derivations output from the outPath variable. Technically some derivations have multiple outputs, but I’m ignoring that for now for brevity.

Since the src attribute is actually just another derivation, this fact applies to it as well. For convenience sake, nix automagically iterpolates a derivation to it’s default output path when referenced in a string, i.e. "${myPackage.src.outPath}/some/path/in/the/source" is the same as "{myPackage.src}/...".

Keep in mind that if you actually read the contents of any file from the output of another derivation, that this is considered import from derivation, which has extra evaluation overhead, since Nix cannot calculate the final hash without building the input. For simple derivations like a src fetcher, that overhead might not be so bad, but it is worth mentioning that IFD is disallowed in nixpkgs.

1 Like

Do you mean if use some other package src in my derivation? What if I am trying to reference my package own src (i.e. linux kernel). To have a context, here’s a sample code from my derivation:

  customKernel = (crosspkgs.linux.override {
    extraConfig = ''
    ...
    '';
  }).overrideAttrs (oldAttrs: {
    postInstall = ''
      cp ${oldAttrs.src.outPath}/arch/arm/boot/dts/<vendor>/<file>.dts $out
      ${oldAttrs.postInstall}
    '';

Upon building, the src.outPath refers to the compressed linux-kernel package.

 cp: cannot stat '/nix/store/3id7by3l9bdahwgkyf2l9pjl0ccswa6m-linux-6.1.69.tar.xz/arch/arm/boot/dts/<vendor>/<file>.dts': Not a directory

I wonder how can I get to the default sourceRoot of the package so I can copy the file in the $out variable.

I used the find command inside postUnpack to determine if the file exist. Turns out I got the path wrong. It should be arch/arm/boot/dts/<file>.dts. :sweat_smile: