{
  description = "Cross compile libcamera app for Raspberry Pi Zero";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };

  outputs = { self, nixpkgs }:
    let
      system = "x86_64-linux";

      pkgs = import nixpkgs {
        inherit system;

        crossSystem = {
          config = "armv6l-unknown-linux-gnueabihf";
        };
      };
    in
    {
      devShells.${system}.default = pkgs.mkShell {
        nativeBuildInputs = [
          pkgs.pkgsBuildBuild.pkg-config
        ];

        buildInputs = with pkgs; [
          libcamera
          libGL
          libglvnd
          mesa
        ];

        shellHook = ''
          export CC=${pkgs.stdenv.cc.targetPrefix}gcc
          export CXX=${pkgs.stdenv.cc.targetPrefix}g++

          export PKG_CONFIG_ALLOW_CROSS=1

          echo "Cross shell ready"
        '';
      };

      packages.${system}.default = pkgs.stdenv.mkDerivation {
        pname = "rpi-zero-libcamera-app";
        version = "0.1.0";

        src = ./.;

        nativeBuildInputs = [
          pkgs.pkgsBuildBuild.pkg-config
        ];

        buildInputs = with pkgs; [
          libcamera
          libGL
          libglvnd
          mesa
        ];

        buildPhase = ''
          runHook preBuild

          export PKG_CONFIG_ALLOW_CROSS=1

          mkdir -p build

          ${pkgs.stdenv.cc.targetPrefix}g++ \
            main.cpp \
            -o build/app \
            $(pkg-config --cflags --libs libcamera)

          runHook postBuild
        '';

        installPhase = ''
          mkdir -p $out/bin
          cp build/app $out/bin/app
        '';
      };

      apps.${system}.default = {
        type = "app";
        program = "${self.packages.${system}.default}/bin/app";
      };
    };
}
