{ lib, pkgs, fetchFromGitHub, makeWrapper, stb }:
let stdenv = pkgs.llvmPackages.stdenv;
in stdenv.mkDerivation rec {
  pname = "repalette";
  version = "849e1857cc985968c5dd08377fff27b258931937";

  src = fetchFromGitHub {
    owner = "ziap";
    repo = pname;
    rev = version;
    sha256 = "sha256-Q86W9/xwFjViIyvnCQfwyQjzrfC122+KK6A3mwJPXOg=";
  };

  nativeBuildInputs = [ makeWrapper ];

  makeFlags = [ "CC=${stdenv.cc.targetPrefix}cc" ];

  configurePhase = ''
    cp ${stb.src}/stb_image.h stb_image.h
    cp ${stb.src}/stb_image_write.h stb_image_write.h
  '';

  buildPhase = ''
    make ${pname}
  '';

  installPhase = ''
    mkdir -p $out/bin
    mv repalette $out/bin
  '';
}
