{ stdenv, fetchFromGitHub, perl, perlPackages, makeWrapper }:

stdenv.mkDerivation rec {
  pname = "sanoid";
  version = "git";
  
  src = fetchFromGitHub {
    owner = "jimsalterjrs";
    repo = "sanoid";
    rev = "ea90108eab3f6db7cc78395e80ac7aa27e130d6f";
    sha256 = "05z3n8828yibdynqgf6hj3j5kwjbw1ybbw1h53kxwqllnbj9cgm1";
  };

  # Should these be pointing to /run/booted-system instead?
  patches = [ ./paths.patch ];

  nativeBuildInputs = [ makeWrapper ];
  buildInputs = [ perl ];
  perlInputs = with perlPackages; [ ConfigIniFiles ];
  PERL5LIB = perlPackages.makePerlPath perlInputs;

  installPhase = ''
    mkdir -p $out/bin
    cp sanoid syncoid findoid sleepymutex $out/bin/

    mkdir $out/etc
    cp sanoid.defaults.conf $out/etc/sanoid

    mkdir -p $out/share/doc/sanoid
    cp sanoid.conf $out/share/doc/sanoid/sanoid.conf.example
  '';

  postFixup = ''
    for f in sanoid syncoid findoid; do
      wrapProgram $out/bin/$f --prefix PERL5LIB : $PERL5LIB
    done
  '';

  meta = with stdenv.lib; {
    homepage = http://www.openoid.net/products/;
    description = "A policy-driven snapshot management tool for ZFS filesystems";
    platforms = platforms.linux;
    license = licenses.gpl3;
  };
}