From 38fa082e79751534f40fdb8f4226019258a1c176 Mon Sep 17 00:00:00 2001
From: Vincent Laporte <Vincent.Laporte@gmail.com>
Date: Mon, 23 Jul 2018 17:16:34 +0000
Subject: [PATCH 1/2] ocamlPackages.piqi: fix build with OCaml 4.06

---
 pkgs/development/ocaml-modules/piqi/default.nix     |  6 +++---
 .../ocaml-modules/piqi/safe-string.patch            | 13 +++++++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)
 create mode 100644 pkgs/development/ocaml-modules/piqi/safe-string.patch

diff --git a/pkgs/development/ocaml-modules/piqi/default.nix b/pkgs/development/ocaml-modules/piqi/default.nix
index f8126f91d876..c7baa87a3314 100644
--- a/pkgs/development/ocaml-modules/piqi/default.nix
+++ b/pkgs/development/ocaml-modules/piqi/default.nix
@@ -1,4 +1,4 @@
-{stdenv, fetchurl, ocaml, findlib, camlp4, which, ulex, easy-format, ocaml_optcomp, xmlm, base64}:
+{ stdenv, fetchurl, ocaml, findlib, which, ulex, easy-format, ocaml_optcomp, xmlm, base64 }:
 
 stdenv.mkDerivation rec {
   version = "0.6.13";
@@ -9,10 +9,10 @@ stdenv.mkDerivation rec {
     sha256 = "1whqr2bb3gds2zmrzqnv8vqka9928w4lx6mi6g244kmbwb2h8d8l";
   };
 
-  buildInputs = [ocaml findlib camlp4 which ocaml_optcomp];
+  buildInputs = [ ocaml findlib which ocaml_optcomp ];
   propagatedBuildInputs = [ulex xmlm easy-format base64];
 
-  patches = [ ./no-ocamlpath-override.patch ];
+  patches = [ ./no-ocamlpath-override.patch ./safe-string.patch ];
 
   createFindlibDestdir = true;
 
diff --git a/pkgs/development/ocaml-modules/piqi/safe-string.patch b/pkgs/development/ocaml-modules/piqi/safe-string.patch
new file mode 100644
index 000000000000..fbc2864d5348
--- /dev/null
+++ b/pkgs/development/ocaml-modules/piqi/safe-string.patch
@@ -0,0 +1,13 @@
+--- a/piqilib/piqi_json_parser.mll
++++ b/piqilib/piqi_json_parser.mll
+@@ -189,8 +189,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+     let len = lexbuf.lex_curr_pos - lexbuf.lex_start_pos in
+     let s = lexbuf.lex_buffer in
+     let start = lexbuf.lex_start_pos in
+-    check_adjust_utf8 v lexbuf s start len;
+-    Buffer.add_substring v.buf s start len
++    check_adjust_utf8 v lexbuf (Bytes.unsafe_to_string s) start len;
++    Buffer.add_subbytes v.buf s start len
+ 
+   let map_lexeme f lexbuf =
+     let len = lexbuf.lex_curr_pos - lexbuf.lex_start_pos in

From c63e5c13d271dbdc534d477c494a08463d8f53f9 Mon Sep 17 00:00:00 2001
From: Vincent Laporte <Vincent.Laporte@gmail.com>
Date: Mon, 23 Jul 2018 17:23:07 +0000
Subject: [PATCH 2/2] ocamlPackages.piqi-ocaml: fix build with OCaml 4.06

---
 pkgs/development/ocaml-modules/piqi-ocaml/default.nix | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/ocaml-modules/piqi-ocaml/default.nix b/pkgs/development/ocaml-modules/piqi-ocaml/default.nix
index 38b56ac7beb1..bbe38ac89942 100644
--- a/pkgs/development/ocaml-modules/piqi-ocaml/default.nix
+++ b/pkgs/development/ocaml-modules/piqi-ocaml/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, ocaml, findlib, piqi, camlp4 }:
+{ stdenv, fetchurl, fetchpatch, ocaml, findlib, piqi, camlp4 }:
 
 stdenv.mkDerivation rec {
   version = "0.7.5";
@@ -9,6 +9,11 @@ stdenv.mkDerivation rec {
     sha256 = "0ngz6y8i98i5v2ma8nk6mc83pdsmf2z0ks7m3xi6clfg3zqbddrv";
   };
 
+  patches = [ (fetchpatch {
+    url = https://github.com/alavrik/piqi-ocaml/commit/336e8fdb84e77f4105e9bbb5ab545b8729101308.patch;
+    sha256 = "071s4xjyr6xx95v6az2lbl2igc87n7z5jqnnbhfq2pidrxakd0la";
+  })];
+
   buildInputs = [ ocaml findlib piqi camlp4 ];
 
   createFindlibDestdir = true;