Nix kernel debug symbols

I’m doing doing some kernel hacking using Nix, for which nix looks great. I’m using vmTools to spin up my in-development kernel module in a vm.

Of course I need debug symbols. So the first thing I reach for is:

linux = enableDebugging pkgs.linux_5_5;

But I end up with a bzImage which has been stripped. How do I get a debug build of the kernel? (not just symbols, but dwarf, etc).

I include my full default.nix which lives along side my kernel module, in case anyone cares to share other hints/simplifications/advice. I’m using the nixpkgs-unstable from Ubuntu. I tried switching to nixos-19.09, but then import <nixpkgs/pkgs/build-support/vm> stopped working.

Thanks in advance :slight_smile:

let
  pkgs = import <nixpkgs> {};

in
with pkgs;
let

  origKernel = pkgs.buildLinux {
    inherit (pkgs.linux_5_5) src version stdenv kernelPatches modDirVersion;

    structuredExtraConfig = with import <nixpkgs/lib/kernel.nix> { inherit (stdenv) lib; version = null; }; {
      # Issue with kernel build: "bad copy" (no log fragment to hand).
      KVM = no;
    };
  };

  linux = enableDebugging origKernel;

  vmTools = import <nixpkgs/pkgs/build-support/vm> { inherit pkgs; kernel = linux; };
  
  moduleBuildDir = "${linux.dev}/lib/modules/${linux.modDirVersion}/build";

  kernhack_module = runCommandCC "kernhack_module" {} ''
    cp ${./kernhack.c} kernhack.c
    cp ${./Makefile} Makefile

    ${gnumake}/bin/make \
      -C ${moduleBuildDir} \
      M=$PWD \
      V=1 \
      modules
    
    cp kernhack.ko $out
  '';

  kernhack = runCommand "kernhack_output" {} ''
    echo "Image is ${linux}/bzImage"
    cp ${kernhack_module} kernhack.ko
    ${kmod}/bin/insmod ./kernhack.ko
    ${kmod}/bin/rmmod kernhack
  '';
in
  vmTools.runInLinuxVM kernhack
1 Like