Building kernel module

I have been trying to build the wm6980 audio hat driver into a nix package (hopefully to use on a Raspberry Pi with wyoming-satellite.) I keep running into a lot Permission denied errors. I think this outlines a huge knowledge gap I have with nix. Since my source is set to fetchFromGitHub, my understanding is that the store path will be used within the flake, but then the store path is read-only. I hope someone here can set me straight, because by this logic no nix package should build.

Here are the errors:

error: builder for '/nix/store/p78pj1ii3fim40ij2aybp10gkacjwf41-wm6980-audio-hat.drv' failed with exit code 2;
       last 10 log lines:
       > mkdir: cannot create directory '/nix/store/0ns9h5r5d7k0z4brzf4d8kl0ap2kq5c1-source/.tmp_139': Permission denied
       > mkdir: cannot create directory '/nix/store/0ns9h5r5d7k0z4brzf4d8kl0ap2kq5c1-source/.tmp_142': Permission denied
       > mkdir: cannot create directory '/nix/store/0ns9h5r5d7k0z4brzf4d8kl0ap2kq5c1-source/.tmp_145': Permission denied
       > mkdir: cannot create directory '/nix/store/0ns9h5r5d7k0z4brzf4d8kl0ap2kq5c1-source/.tmp_148': Permission denied
       > mkdir: cannot create directory '/nix/store/0ns9h5r5d7k0z4brzf4d8kl0ap2kq5c1-source/.tmp_151': Permission denied
       > /nix/store/9vafkkic27k7m4934fpawl6yip3a6k4h-bash-5.2-p21/bin/bash: line 1: /nix/store/0ns9h5r5d7k0z4brzf4d8kl0ap2kq5c1-source/snd-soc-wm8960.mod: Permission denied
       > make[2]: *** [/nix/store/8qsg7kkmxwyva5ns6wikm1ppbcx4j93n-linux-6.7-dev/lib/modules/6.7.0/source/scripts/Makefile.build:252: /nix/store/0ns9h5r5d7k0z4brzf4d8kl0ap2kq5c1-source/snd-soc-wm8960.mod] Error 1
       > make[1]: *** [/nix/store/8qsg7kkmxwyva5ns6wikm1ppbcx4j93n-linux-6.7-dev/lib/modules/6.7.0/source/Makefile:1911: /nix/store/0ns9h5r5d7k0z4brzf4d8kl0ap2kq5c1-source] Error 2
       > make: *** [/nix/store/8qsg7kkmxwyva5ns6wikm1ppbcx4j93n-linux-6.7-dev/lib/modules/6.7.0/source/Makefile:234: __sub-make] Error 2
       > make: Leaving directory '/nix/store/8qsg7kkmxwyva5ns6wikm1ppbcx4j93n-linux-6.7-dev/lib/modules/6.7.0/build'
       For full logs, run 'nix log /nix/store/p78pj1ii3fim40ij2aybp10gkacjwf41-wm6980-audio-hat.drv'.

And here is the current iteration of my flake.nix:

{
  description = "wm6980 audio hat";

  inputs = {
    # nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    nixpkgs.url = "git+file:///home/myuser/Projects/nixpkgs";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, flake-utils, nixpkgs }:

  flake-utils.lib.eachSystem [ "x86_64-linux" "aarch64-linux" ] (system:
    let
      pkgs = nixpkgs.legacyPackages.${system};
      kernel = pkgs.linux_6_7;
    in rec  {
      packages  = rec {
        wm6980-audio-hat = pkgs.stdenv.mkDerivation rec {
          name = "wm6980-audio-hat";
          inherit system;          

          buildInputs = with pkgs; [
            stdenv
            nukeReferences
          ];

          src = pkgs.fetchFromGitHub {
            owner = "waveshare";
            repo = "WM8960-Audio-HAT";
            rev = "rpi-4.9.y";
            hash = "sha256-EZkR/lZIORYP8qpsDcrdMQCSToVTrif042BBynBpXV4=";
            # url = "github:rhasspy/wyoming-satellite";
          };

          makeFlags = kernel.makeFlags ++ [
            "-C"
            "${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
            "M=${src}"
          ];
          
          buildFlags = [ "modules" ];
          installFlags = [ "INSTALL_MOD_PATH=${placeholder "out"}" ];

        };

        default = packages.wm6980-audio-hat;
      };
    }
  );
}

1 Like

I’m not kernel wizard but I would bet on this, change by $(pwd)
${src} is the other “package” the is the result of the download of the source.
Or unset it.

1 Like

Nice catch! There are still problems, but less. I wonder why it can’t make a temp directory in its own store path?!?

Thank you for helping. I don’t expect you to debug this line by line with me, but I am posting the result of that last change (to $(pwd) ) in case it is obvious to someone.

error: builder for '/nix/store/qw8rmr4hpam4i7f2hcbqf8pnsrfy454j-wm8960-audio-hat.drv' failed with exit code 2;
       last 10 log lines:
       > mkdir: cannot create directory '.tmp_204': Permission denied
       > ln: failed to create symbolic link 'source': Permission denied
       > make[2]: *** [/nix/store/8qsg7kkmxwyva5ns6wikm1ppbcx4j93n-linux-6.7-dev/lib/modules/6.7.0/source/Makefile:640: outputmakefile] Error 1
       > make[1]: *** [/nix/store/8qsg7kkmxwyva5ns6wikm1ppbcx4j93n-linux-6.7-dev/lib/modules/6.7.0/source/Makefile:786: include/config/auto.conf] Error 2
       > make[1]: *** [include/config/auto.conf] Deleting file 'include/generated/rustc_cfg'
       > make[1]: unlink: include/generated/rustc_cfg: Permission denied
       > make[1]: *** [include/config/auto.conf] Deleting file 'include/generated/autoconf.h'
       > make[1]: unlink: include/generated/autoconf.h: Permission denied
       > make: *** [/nix/store/8qsg7kkmxwyva5ns6wikm1ppbcx4j93n-linux-6.7-dev/lib/modules/6.7.0/source/Makefile:234: __sub-make] Error 2
       > make: Leaving directory '/nix/store/8qsg7kkmxwyva5ns6wikm1ppbcx4j93n-linux-6.7-dev/lib/modules/6.7.0/build'
       For full logs, run 'nix log /nix/store/qw8rmr4hpam4i7f2hcbqf8pnsrfy454j-wm8960-audio-hat.drv'.

This probably going to boil down to problems with the Makefile itself. This doesn’t appear to want to work for anything but the most generic debian based system.