Interactively build custom kernel via shell.nix

Using the custom_kernel.nix acting as a shell.nix below I would like to nix-shell ./custom_kernel.nix and then the steps as listed in Cross-compiling Linux from source:

Click to see custom_kernel.nix listed
{ pkgs ? import <nixpkgs> {}, ... }:

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

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

          src = fetchurl {
            url = "https://github.com/microsoft/WSL2-Linux-Kernel/tarball/linux-msft-wsl-${version}";
            sha256 = "0000000000000000000000000000000000000000000000000000";
          };
          kernelPatches = [];

          /*extraConfig = ''
	    CONFIG_USB_ACM y
            CONFIG_USBIP_CORE y
            CONFIG_USBIP_VHCI_HCD y
            CONFIG_USBIP_VHCI_HC_PORTS freefrom "8"
            CONFIG_USBIP_VHCI_NR_HCS 1
            CONFIG_USBIP_DEBUG y
	    CONFIG_USB_SERIAL y
          '';*/
        
	  structuredExtraConfig = with pkgs.lib.kernel; {
            USB_AC = yes;
            USBIP_CORE = yes;
            USBIP_VHCI_HCD = yes;
            USBIP_VHCI_HC_PORTS = freeform "8";
            USBIP_VHCI_NR_HCS = freeform "1";
            USBIP_DEBUG = yes;
            USB_SERIAL = yes;
         };

	  ignoreConfigErrors = true;

          extraMeta.branch = "5.10";
        } // (args.argsOverride or {}));
      linux_wsl = pkgs.callPackage linux_wsl_pkg{};
    in 
      pkgs.recurseIntoAttrs (pkgs.linuxPackagesFor linux_wsl);
}

For now I get

error: nix-shell requires a single derivation
Try ‘nix-shell --help’ for more information.

Tried nix-shell custom_kernel.nix -A boot.kernelPackages IDK if that makes sense.

Now I get

error: ati drivers are no longer supported by any kernel >=4.1

You can’t easily. I’d would recommend to just use the standard kernel configuration tools in Nixos.

Is there beginner documentation for this ?

I must admit I’m just starting and don’t know yet where to begin searching. Also it is hard to grasp which of all the examples in NixOS wiki fit which use case.

I have an alternate shell.nix for now that let’s me nix-shell and therein genericBuild but what I’m still missing is:

  1. How to generate a .config from the config.gz in use currently at least from the point where I already copied / gunzipped / moved the former to the folder with shell.nix
  2. Toggle some kernel settings via make menuconfig or similar
  3. Use the resulting .config from step 2) to do step 4)
  4. Just build not even install the built kernel image

I’m sure this is doable all in a default.nix or nix expression but for straters would be happy to have just the environment set up using nix, hence my interactive approach.

Might be completely naiive though.

Click for shell.nix listing
{ pkgs ? import <nixpkgs> {} }:

pkgs.stdenv.mkDerivation rec {
  name = "linux-kernel-build";
          version = "5.10.102.1";
          modDirVersion = version;
          src = builtins.fetchTarball {
            url = "https://github.com/microsoft/WSL2-Linux-Kernel/tarball/linux-msft-wsl-${version}";
            sha256 = "18b5s6kxjnxnfhhh5ywgnxrj4pi8jr0g0bdd9y3vj7qi1q2nz7zw";
          };
    postUnpack = ''
      (cd $sourceRoot
       TOP=$(cat scripts/ld-version.sh|grep ^#\!.*bin|awk -F\  '{ print $1 }')
       substituteInPlace scripts/ld-version.sh --replace "$TOP" "${pkgs.gawk}/bin/awk")
    '';
  nativeBuildInputs = with pkgs; [
    getopt
    flex
    bison
    gcc
    gnumake
    bc
    pkg-config
    binutils
  ];
  buildInputs = with pkgs; [
    elfutils
    ncurses
    openssl
    zlib
  ];
}
1 Like

Linux kernel - NixOS Wiki should answer most of your questions.

Apperantly you can use menuconfig.

Following make_menuconfig and with my custom_kernel.nix above (as in Booting_a_kernel_from_a_custom_source) how do I invoke the nix-shell i. e. what is attribute to use instead of linux(.overrideAttrs) there ?

nix-shell -E 'with import ./custom_kernel.nix {}; linux.overrideAttrs (o: {nativeBuildInputs=o.nativeBuildInputs ++ [ pkg-config ncurses ];})'

Solved in follow-up post here