Custom kernel expression stuck at ati drivers

Related: Interactively build custom kernel via shell.nix

custom kernel expression
# default.nix
# borrowed here https://sourcegraph.com/github.com/tpwrules/nixos-m1/-/blob/nix/m1-support/kernel/package.nix
{ pkgs ? import <nixpkgs> {}}: let

  localPkgs = import (pkgs.path) { system = builtins.currentSystem; };
  readConfig = configfile: import (localPkgs.runCommand "config.nix" {} ''
    echo "{" > "$out"
    while IFS='=' read key val; do
      [ "x''${key#CONFIG_}" != "x$key" ] || continue
      no_firstquote="''${val#\"}";
      echo '  "'"$key"'" = "'"''${no_firstquote%\"}"'";' >> "$out"
    done < "${configfile}"
    echo "}" >> $out
  '').outPath;

    linux_wsl_pkg =  { stdenv, lib, fetchurl, fetchFromGitHub, fetchpatch, linuxKernel, ... } @ args:

	linuxKernel.manualConfig rec {
          inherit stdenv lib;
          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";
          };
 
          kernelPatches = [];

          configfile = ./source/Microsoft/config-wsl;
          config = readConfig configfile;


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

I wonder how to get rid of ati_drivers_x11 when trying to build a custom kernel:

nix --extra-experimental-features "nix-command flakes" repl
:l default.nix
ati_drivers_x11
error: ati drivers are no longer supported by any kernel >=4.1

`

Found this https://github.com/mudrii/nixlenovodot/blob/2b60a89cf6c4875332c542b317e4c8cc327efb1c/configuration.nix#L14:

overlays = [
   (_: prev: {
      linuxPackagesFor = kernel:
      (prev.linuxPackagesFor kernel).extend (_: _: { ati_drivers_x11 = null; });
   })
];

How do I apply it in my expression?

# file default.nix
# try nix repl > :l default.nix > :b kernel
# borrowed here https://sourcegraph.com/github.com/tpwrules/nixos-m1/-/blob/nix/m1-support/kernel/package.nix
{ pkgs ? import <nixpkgs> {
overlays = [
   (_: prev: {
     linuxPackagesFor = kernel: (prev.linuxPackagesFor kernel).extend (_: _: {
        ati_drivers_x11 = null;
	extraConfig = '' 
	    USB_ACM y
            USBIP_CORE y
            USBIP_VHCI_HCD y
            USBIP_VHCI_HC_PORTS 8
            USBIP_VHCI_NR_HCS 1
            USBIP_DEBUG y
	    USB_SERIAL y
	'';
      });
   })
];
}}: let
          version = "5.10.102.1";
	  src = fetchTarball {
            url = "https://github.com/microsoft/WSL2-Linux-Kernel/tarball/linux-msft-wsl-${version}";
            sha256 = "18b5s6kxjnxnfhhh5ywgnxrj4pi8jr0g0bdd9y3vj7qi1q2nz7zw";
          };

    # manualConfig is basicall buildLinux, see https://git.sharcnet.ca/nix/nixpkgs/blob/a26c5e5c43c36267643dc48458f5ffd61a478a1a/pkgs/top-level/all-packages.nix
    linux_wsl_pkg =  { stdenv, lib, fetchurl, fetchFromGitHub, fetchpatch, linuxKernel, buildLinux, version, src,  ... } @ args:

        buildLinux (args // rec {
	  inherit src version;
          modDirVersion = version;

          kernelPatches = [];

          extraMeta.branch = "5.10";
        } // (args.argsOverride or {}));

      linux_wsl = pkgs.callPackage linux_wsl_pkg {
          inherit version src; 
          allowImportFromDerivation = true;
          configfile = ./source/build/config-wsl-usb;
      };
    in  pkgs.recurseIntoAttrs (pkgs.linuxPackagesFor linux_wsl)

:b kernel after :l default.nix in a nix repl session starts a build at least.

Still I think this involves manually running nix-shell default.nix -A kernel and therein make KCONFIG_CONFIG=source/build/config-wsl-usb menuconfig.

Also I’m not really sure if the config saved concluding menuconfig (./source/build/config-wsl-usb in default.nix) is picked up correctly then in the build.

This is the expression I use for now.
With it I can

  • nix-shell default.nix -A kernel to have the dependencies for running make menuconfig
  • run make KCONFIG_CONFIG=source/Microsoft/config-wsl menuconfig -C source in above nix-shell to configure kernel build based on configuration provided with kernel linux-msft-wsl-5.10.102.1
  • when ready editing configuration save as source/Microsoft/config-wsl-usb in menuconfig or use edited configuration in configfile below
  • i. e. nix repl and therein :l default.nix (file attached to this post) and :b kernel
  • run nix-store --read-log /nix/store/*** to inspect the logs of the built derivation accordingly replacing *** with output of step beyond (or equivalent nix log /nix/store/***)
Click for listing of the kernel builder expression
# default.nix
# try 
# 1) nix-shell default.nix -A kernel
# 2) Therein make KCONFIG_CONFIG=source/Microsoft/config-wsl menuconfig -C source using the config default 
# 3) When ready editing config save as source/Microsoft/config-wsl-usb or use your value in configfile below
# 4) I. e. nix repl and therein :l default.nix :b kernel
# 5) nix-store --read-log /nix/store/*** # to inspect the logs of the built derivation replacing *** or just
# 6)  nix log /nix/store***
# borrowed here https://sourcegraph.com/github.com/tpwrules/nixos-m1/-/blob/nix/m1-support/kernel/package.nix
{ pkgs ? import <nixpkgs> {
overlays = [
   (_: prev: {
     buildLinux = kernel: (prev.buildLinux kernel).overrideAttrs (o: {nativeBuildInputs=o.nativeBuildInputs ++ [ prev.pkg-config prev.ncurses ];});
     linuxPackagesFor = kernel: (prev.linuxPackagesFor kernel).extend (_: _: {
        ati_drivers_x11 = null;
      });
   })
];
}}: let

          
    # manualConfig is basically buildLinux, see https://github.com/NixOS/nixpkgs/blob/a26c5e5c43c36267643dc48458f5ffd61a478a1a/pkgs/top-level/all-packages.nix#L11630
    linux_wsl_pkg =  { stdenv, lib, fetchurl, fetchFromGitHub, fetchpatch, linuxKernel, buildLinux, version, src, ... } @ args:

      buildLinux (args // rec {
          version = "5.10.102.1";
	  src = fetchTarball {
            url = "https://github.com/microsoft/WSL2-Linux-Kernel/tarball/linux-msft-wsl-${version}";
            sha256 = "18b5s6kxjnxnfhhh5ywgnxrj4pi8jr0g0bdd9y3vj7qi1q2nz7zw";
          };
          modDirVersion = version;

          kernelPatches = [];

          # source: https://sourcegraph.com/search?q=context:global+file:.*%5C.nix+%28content:%22structuredExtraConfig%22+AND+content:%22%28_:+value:+lib.mkForce+value%29%22%29&patternType=literal
          structuredExtraConfig = with pkgs.lib.kernel; builtins.mapAttrs (_: value: lib.mkForce value) {
            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;
        autoModules = false;
        kernelPreferBuiltin = true;
        enableParallelBuilding = true;

          extraMeta.branch = "5.10";
        } // (args.argsOverride or {}));

      linux_wsl = pkgs.callPackage linux_wsl_pkg rec {
          inherit (linux_wsl_pkg) version src structuredExtraConfig;
          allowImportFromDerivation = true;
          # when running manual kernel config use file I saved here i. e. ./source/Microsoft/config-wsl-usb
          configfile = ./source/Microsoft/config-wsl;
      };
    in  pkgs.recurseIntoAttrs (pkgs.linuxPackagesFor linux_wsl)

Open question: Still would be good to know if just the default config could be used here combined with extraConfig to reliably do without the (interactive) menuconfig step, i. e. extraConfig just modifying what default kernel configuration is serving already. Follow up question then: When would I need SETTING = lib.mkForce yes; ?
I would like to answer this myself as well: See the edits in the above kernel builder expression. Correctly applying structuredExtraConfig seems to lead to (excerpt on an already manually edited configuration - hence the y all over there) this output when nix-shell default.nix -A kernel is first run:

QUESTION: USB/IP support, NAME: USBIP_CORE, ALTS: N/m/y/?, ANSWER: y
GOT: y
QUESTION: VHCI hcd, NAME: USBIP_VHCI_HCD, ALTS: N/m/y/?, ANSWER: y
GOT: y
QUESTION: Number of ports per USB/IP virtual host controller, NAME: USBIP_VHCI_HC_PORTS, ALTS: 8, ANSWER: 8
GOT: 8
QUESTION: Number of USB/IP virtual host controllers, NAME: USBIP_VHCI_NR_HCS, ALTS: 1, ANSWER: 1
GOT: 1
QUESTION: Host driver, NAME: USBIP_HOST, ALTS: N/m/y/?, ANSWER:
GOT:
QUESTION: Debug messages for USB/IP, NAME: USBIP_DEBUG, ALTS: N/y/?, ANSWER: y
GOT: y

So I dare saying success.

EDIT: Removed the extraStructuredConfig related stuff as it was misleading and extraStructuredConfig belongs to and only to the kernelPatches closure.

EDIT: WSL2 doesn’t seem to like bzImage so I figured the vmlinuz location.

nix-repl> kernel.dev.outPath