NixOS and raspberry pi zero 2w + pi camera module v3

Hello.

Does anyone have a working Pi Camera Module v3 on rpi zero 2w on NixOS?
If so, could you please share your setup?

I’ve been working on this recently. It works well on my Pi 3, which is very similar to the Zero 2 W, but I haven’t yet been able to test as the camera connector is different and I’m missing the adapter.

Unfortunately I am unable to share my complete configuration (I generate SD card images with a minimal configuration to stream the camera over WebRTC), but this is the part that makes the camera available over libcamera:

{ config, pkgs, ... }:

{
  imports = [
    <nixos-hardware/raspberry-pi/4/pkgs-overlays.nix>
  ];

  nixpkgs = {
    overlays = [
      (self: super: {
        # https://patchwork.libcamera.org/patch/19420
        libcamera = super.libcamera.overrideAttrs ({ patches ? [ ], ... }: {
          patches = patches ++ [
            (self.fetchpatch {
              url = "https://patchwork.libcamera.org/patch/19420/raw";
              hash = "sha256-xJ8478CAKvyo2k1zrfIytDxFQ1Qdd8ilMdABQoNcdPU=";
            })
          ];
        });
      })
    ];
  };

  # https://github.com/Electrostasy/dots/blob/3b81723feece67610a252ce754912f6769f0cd34/hosts/phobos/klipper.nix#L11
  hardware.raspberry-pi."4".apply-overlays-dtmerge.enable = true;
  hardware.deviceTree = {
    enable = true;
    filter = "bcm2837-rpi*.dtb";
    overlays =
      let
        # https://github.com/Electrostasy/dots/blob/3b81723feece67610a252ce754912f6769f0cd34/hosts/phobos/klipper.nix#L17-L42
        mkCompatibleDtsFile = dtbo:
          let
            # TODO: Make compatible with more than just bcm2837 (Raspberry Pi 3, Zero 2 W), e.g. Raspberry Pi 4 or 5.
            drv = (pkgs.runCommand (builtins.replaceStrings [ ".dtbo" ] [ ".dts" ] (baseNameOf dtbo)) {
              nativeBuildInputs = with pkgs; [ dtc gnused ];
            }) ''
              mkdir "$out"
              dtc -I dtb -O dts '${dtbo}' | sed -e 's/bcm2835/bcm2837/' > "$out/overlay.dts"
            '';
          in
          "${drv}/overlay.dts";
      in
      [
        # TODO: Support more cameras.
        {
          name = "imx708";
          dtsFile =
            mkCompatibleDtsFile "${config.boot.kernelPackages.kernel}/dtbs/overlays/imx708.dtbo";
        }

        # TODO: Find a better way to increase the CMA.
        # Real KMS does not work on the Raspberry Pi Zero 2 W.
        {
          name = "vc4-fkms-v3d";
          dtsFile = mkCompatibleDtsFile "${config.boot.kernelPackages.kernel}/dtbs/overlays/vc4-fkms-v3d.dtbo";
        }
      ];
  };

  services.udev.extraRules = ''
    # https://raspberrypi.stackexchange.com/a/141107
    SUBSYSTEM=="dma_heap", GROUP="video", MODE="0660"
  '';
}
1 Like

Hi! Thank you very much.

I was able to add provided module/code to my flake.
However, just for future me:

6.9 kernel runs fine on rpi zero 2w, but this libcamera stuff expect rpi kernel kernelPackages = pkgs.linuxKernel.packages.linux_rpi3; or similar, due to provided overlays.

I’m going to generate new SD image and test it soon.

1 Like

Ok. First try ended with:
no device tree found.
:confused: