How to build arbitrary kernel from local source tree on NixOS?

The title is the question I guess: How to build any arbitrary kernel from a local git repo of the kernel source tree? I need to find a bug and git bisect through it. And I am contemplating whether I can do this reasonably well on NixOS or I need go grab something else for this…

Some time ago there was a topic on this: Building arbitrary kernels

But it was never answered. And I haven’t found anything else on it.

Any ideas?

Have you read Linux kernel - NixOS Wiki? Might contain some useful information. Hard to say without more information on on your end

I’ve used buildFHSUserEnv in the past.

{ pkgs ? import <nixpkgs> {} }:
(pkgs.buildFHSUserEnv {
  name = "kernel-env";
  targetPkgs = pkgs: with pkgs; [
    bc
    gcc
    flex
    bison
    openssl.dev
    elfutils.dev
    libelf
    ncurses.dev
  ];
}).env

Builds v6.1-rc4 just fine.

1 Like

@danielbarter
Which additional information would you require?

I really mean just this: git clone the linux kernel sources and change something and build this changed kernel source, and the boot NixOS with it.

I am looking for some set of instructions like these here on Fedora for building vanilla upsteam kernels:

The NixOS wiki does only appear to cover building a custom kernel from a packaged archive.

@ius

That looks a bit more what I was looking for. How do you integrate that into your system config so you can build the kernel from a local git repo and boot it?

Assuming you want to bisect Kernel 6.1, you can simply check out nixpkgs, edit pkgs/os-specific/linux/kernel/linux-6.1.nix to point src to your checkout of the Kernel git and then build from your local nixpkgs checkout. That way you can bisect in your local kernel checkout and rebuild with the settings NixOS uses.

1 Like

That is actually a really good idea, I will give it a try and see how it goes. It should also work if I just override the attribute set of my kernel 6.1 to pick up the local git repo, right? I will try using fetchgit and point it to my local path with url being set to something like url = "file:///path/to/linux/source/";

Yeah I kinda was suspecting that this would have been too easy… I tried to point my kernel to the local source tree, but that simply doesn’t work

In this case I have to build kernel 5.16.10 to start bisecting. This is the first version that I know off which had the bug I am looking for.

{ pkgs, ... }:

{
  boot.kernelPackages =
    let
      linux_pkg = { fetchurl, buildLinux, ... } @ args:

        buildLinux (args // rec {
          version = "5.16.10";
          modDirVersion = version;

          src = "file:///home/ap/Coding/c/linux-stable";

          kernelPatches = [ ];

          extraMeta.branch = "5.16";
        } // (args.argsOverride or { }));
      linux_5-16-10 = pkgs.callPackage linux_pkg { };
    in
    pkgs.recurseIntoAttrs (pkgs.linuxPackagesFor linux_5-16-10);
}

However I get the following error message:

error: builder for '/nix/store/w96g4jncqalh1ihyxqrhnzxhgkk7qkbx-linux-config-5.16.10.drv' failed with exit code 1;
       last 3 log lines:
       > unpacking sources
       > unpacking source archive file:///home/ap/Coding/c/linux-stable
       > do not know how to unpack source archive file:///home/ap/Coding/c/linux-stable
       For full logs, run 'nix log /nix/store/w96g4jncqalh1ihyxqrhnzxhgkk7qkbx-linux-config-5.16.10.drv'.
error: 1 dependencies of derivation '/nix/store/b277d2qzk2kcmc524dyav54y7lxbwbp1-linux-5.16.10.drv' failed to build
error: 1 dependencies of derivation '/nix/store/3ylrjx5y5lsfb88iwb8fhhrgrkljyvsm-nixos-system-archon-22.11.20230122.ab12540.drv' failed to build

Which is kinda what I was expecting, namely that it doesn’t want to take the git repository directly but is looking for an archive instead. Which of course does not exist. So yeah…

Anyone any other ideas?

Alright, another try, this time I am almost there. I just decided to grab the exact commit I need from the remote repo.

{
  boot.kernelPackages =
    let
      linux_pkg = { fetchgit, buildLinux, ... } @ args:

        buildLinux (args // rec {
          version = "5.16.9";
          modDirVersion = version;

          src = fetchgit
            {
              url = "https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/";
              rev = "91fc147";
              sha256 = "Dz+CpecLcVU/jA00gCnOenDhL8yYzMxM58uCXcTtzj0=";
            };

          kernelPatches = [ ];

          extraMeta.branch = "5.16";
        } // (args.argsOverride or { }));
      linux_5-16-9 = pkgs.callPackage linux_pkg { };
    in
    pkgs.recurseIntoAttrs (pkgs.linuxPackagesFor linux_5-16-9);
}

But now the build itself fails. Which is another problem entirely and I will open a new thread for this one I guess. But getting the right sources works now. Even though fetchgit did not want to fetch my local source tree for whatever reason…