From 842746e2cd3592bf52b0a4c84c84f85f21d1d6bc Mon Sep 17 00:00:00 2001
From: John Carey <jcarey@arista.com>
Date: Tue, 14 Jan 2025 18:22:38 -0800
Subject: [PATCH 1/3] Support GHC 9.10

---
 .github/workflows/ci.yml            |  1 +
 CHANGELOG.md                        |  3 +++
 README.md                           |  4 +++
 nix/haskell-packages.nix            | 12 +++------
 nix/nixpkgs.nix                     |  9 ++++---
 nix/packages/free.nix               | 19 --------------
 nix/packages/hpack.nix              | 40 -----------------------------
 proto3-wire.cabal                   |  2 +-
 shell.nix                           |  2 +-
 src/Proto3/Wire/Decode.hs           |  3 +++
 src/Proto3/Wire/Reverse/Internal.hs |  6 ++---
 11 files changed, 25 insertions(+), 76 deletions(-)
 delete mode 100644 nix/packages/free.nix
 delete mode 100644 nix/packages/hpack.nix

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 6bab750..bcff2fd 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -18,6 +18,7 @@ jobs:
           - 948
           - 962
           - 981
+          - 9101
         exclude:
           - os: macos-latest
             ghc: 902
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 05ee2cd..4d0798d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,6 @@
+1.4.4
+  - Support GHC 9.10
+
 1.4.3
   - Support GHC 9.8
   - Support GHC 9.6
diff --git a/README.md b/README.md
index e77f8ad..c433faf 100644
--- a/README.md
+++ b/README.md
@@ -28,6 +28,10 @@ stack build [--test] [--haddock]
 
 ### GHC Versions
 
+#### GHC 9.10
+
+Supported on Linux and Darwin.
+
 #### GHC 9.8
 
 Supported on Linux and Darwin.
diff --git a/nix/haskell-packages.nix b/nix/haskell-packages.nix
index 0b17be9..6a251a7 100644
--- a/nix/haskell-packages.nix
+++ b/nix/haskell-packages.nix
@@ -14,14 +14,6 @@ pkgsNew: pkgsOld:
             adjunctions =
               pkgsNew.haskell.lib.dontCheck haskellPackagesPrev.adjunctions;
 
-            # With nixpkgs-23.11 and ghc981, aeson-2.1.2.1 thinks that th-abstraction is out of bounds.
-            # Also, in order to avoid the breaking change to package structure in aeson-2.2.0.0,
-            # we patch the import list of aeson-2.1.2.1.
-            aeson =
-              pkgsNew.haskell.lib.doJailbreak
-                ( pkgsNew.haskell.lib.appendPatches haskellPackagesPrev.aeson
-                    [ ./patches/aeson-2.1.2.1.patch ] );
-
             # With nixpkgs-23.11 and ghc981, base-compat-batteries wants hspec for testing,
             # which causes problems.
             base-compat-batteries =
@@ -102,6 +94,10 @@ pkgsNew: pkgsOld:
             iproute =
               pkgsNew.haskell.lib.dontCheck haskellPackagesPrev.iproute;
 
+            # With nixpkgs-24.11 and our overrides, lens thinks that template-haskell is out of bounds.
+            lens =
+              pkgsNew.haskell.lib.doJailbreak haskellPackagesPrev.lens;
+
             # With nixpkgs-23.11 and ghc981, monad-par wants test-framework for testing, which
             # wants language-haskell-extract, which does not support modern template-haskell.
             monad-par =
diff --git a/nix/nixpkgs.nix b/nix/nixpkgs.nix
index fdb1eac..f32361d 100644
--- a/nix/nixpkgs.nix
+++ b/nix/nixpkgs.nix
@@ -2,9 +2,10 @@ args:
 
 let
   nixpkgs = builtins.fetchTarball {
-    # build: https://hydra.nixos.org/build/258096332
-    # commit: 0638fe2715d998fa81d173aad264eb671ce2ebc1
-    url = "https://hydra.nixos.org/build/258096332/download/2/nixpkgs-23.11pre558121.0638fe2715d9.tar.xz";
-    sha256 = "1z3s1hqg3b72g608pf9sv474d4y9s00p86nsvfw5i9xgwhjncjjb";
+    # from: https://hydra.nixos.org/job/nixos/release-24.11/nixpkgs.tarball
+    # build: https://hydra.nixos.org/build/284195557
+    # commit: cbd8ec4de4469333c82ff40d057350c30e9f7d36
+    url = "https://hydra.nixos.org/build/284195557/download/2/nixpkgs-24.11pre712431.cbd8ec4de446.tar.xz";
+    sha256 = "0ljq084fq784fgvm7n9081dmnjhksz20vwzca2zics0kkkzjxh5k";
   };
 in import nixpkgs ({ config = { }; } // args)
diff --git a/nix/packages/free.nix b/nix/packages/free.nix
deleted file mode 100644
index f19700e..0000000
--- a/nix/packages/free.nix
+++ /dev/null
@@ -1,19 +0,0 @@
-{ mkDerivation, base, comonad, containers, distributive, exceptions
-, indexed-traversable, lib, mtl, profunctors, semigroupoids
-, template-haskell, th-abstraction, transformers, transformers-base
-}:
-mkDerivation {
-  pname = "free";
-  version = "5.2";
-  sha256 = "72867f7c89173263765736e8d395e94291f1aaea626ecb1d673d72ce90b94f89";
-  revision = "4";
-  editedCabalFile = "0vic3p2viip8gjww8fx19ax6ry7y34h7xclvhzkvmbspjh9d219x";
-  libraryHaskellDepends = [
-    base comonad containers distributive exceptions indexed-traversable
-    mtl profunctors semigroupoids template-haskell th-abstraction
-    transformers transformers-base
-  ];
-  homepage = "http://github.com/ekmett/free/";
-  description = "Monads for free";
-  license = lib.licenses.bsd3;
-}
diff --git a/nix/packages/hpack.nix b/nix/packages/hpack.nix
deleted file mode 100644
index 8b74b68..0000000
--- a/nix/packages/hpack.nix
+++ /dev/null
@@ -1,40 +0,0 @@
-{ mkDerivation, aeson, base, bifunctors, bytestring, Cabal
-, containers, crypton, deepseq, directory, filepath, Glob, hspec
-, hspec-discover, http-client, http-client-tls, http-types, HUnit
-, infer-license, interpolate, lib, mockery, mtl, pretty, QuickCheck
-, scientific, template-haskell, temporary, text, transformers
-, unordered-containers, vector, yaml
-}:
-mkDerivation {
-  pname = "hpack";
-  version = "0.36.0";
-  sha256 = "a0de4e1a0fe587030fa643cad99cd96de81e295923ffb57cfc7b1575f253ea7a";
-  revision = "1";
-  editedCabalFile = "1zh5rsf38xmwp7lf80iifrhnkl80lri4xzlhz2n5df3vc0dqzya8";
-  isLibrary = true;
-  isExecutable = true;
-  libraryHaskellDepends = [
-    aeson base bifunctors bytestring Cabal containers crypton deepseq
-    directory filepath Glob http-client http-client-tls http-types
-    infer-license mtl pretty scientific text transformers
-    unordered-containers vector yaml
-  ];
-  executableHaskellDepends = [
-    aeson base bifunctors bytestring Cabal containers crypton deepseq
-    directory filepath Glob http-client http-client-tls http-types
-    infer-license mtl pretty scientific text transformers
-    unordered-containers vector yaml
-  ];
-  testHaskellDepends = [
-    aeson base bifunctors bytestring Cabal containers crypton deepseq
-    directory filepath Glob hspec http-client http-client-tls
-    http-types HUnit infer-license interpolate mockery mtl pretty
-    QuickCheck scientific template-haskell temporary text transformers
-    unordered-containers vector yaml
-  ];
-  testToolDepends = [ hspec-discover ];
-  homepage = "https://github.com/sol/hpack#readme";
-  description = "A modern format for Haskell packages";
-  license = lib.licenses.mit;
-  mainProgram = "hpack";
-}
diff --git a/proto3-wire.cabal b/proto3-wire.cabal
index 1a27dfb..07384e1 100644
--- a/proto3-wire.cabal
+++ b/proto3-wire.cabal
@@ -1,7 +1,7 @@
 cabal-version: >=1.10
 
 name:         proto3-wire
-version:      1.4.3
+version:      1.4.4
 synopsis:     A low-level implementation of the Protocol Buffers (version 3) wire format
 license:      Apache-2.0
 license-file: LICENSE
diff --git a/shell.nix b/shell.nix
index 93c75c7..b74e293 100644
--- a/shell.nix
+++ b/shell.nix
@@ -1,4 +1,4 @@
-{ compiler ? "ghc8107", enableStack ? false }:
+{ compiler ? "ghc948", enableStack ? false }:
 
 let
   pkgs = import ./nix/pkgs.nix {
diff --git a/src/Proto3/Wire/Decode.hs b/src/Proto3/Wire/Decode.hs
index 20c1b04..2119a65 100644
--- a/src/Proto3/Wire/Decode.hs
+++ b/src/Proto3/Wire/Decode.hs
@@ -23,6 +23,7 @@
 -- from the untyped 'Map' representation obtained from 'decodeWire'.
 
 {-# LANGUAGE BangPatterns               #-}
+{-# LANGUAGE CPP                        #-}
 {-# LANGUAGE DeriveFunctor              #-}
 {-# LANGUAGE LambdaCase                 #-}
 {-# LANGUAGE OverloadedStrings          #-}
@@ -86,7 +87,9 @@ import           Data.Bits
 import qualified Data.ByteString         as B
 import qualified Data.ByteString.Lazy    as BL
 import qualified Data.ByteString.Short   as BS
+#if !MIN_VERSION_base(4,20,0)
 import           Data.Foldable           ( foldl' )
+#endif
 import qualified Data.IntMap.Strict      as M -- TODO intmap
 import           Data.Maybe              ( fromMaybe )
 import           Data.Serialize.Get      ( Get, getWord8, getInt32le
diff --git a/src/Proto3/Wire/Reverse/Internal.hs b/src/Proto3/Wire/Reverse/Internal.hs
index 25e0fbe..ba55552 100644
--- a/src/Proto3/Wire/Reverse/Internal.hs
+++ b/src/Proto3/Wire/Reverse/Internal.hs
@@ -462,7 +462,7 @@ sealBuffer# addr unused s0 =
       -- until a state action frees the stable pointer or modifies the state
       -- variable, the stable pointer will reference the state variable,
       -- which in turn will reference the current buffer.
-      let allocation = P.sizeofMutableByteArray buffer - metaDataSize
+      allocation <- subtract metaDataSize <$> P.getSizeofMutableByteArray buffer
       if allocation <= I# unused
         then
           pure (oldSealed, total, stateVar, statePtr, Just buffer)
@@ -573,8 +573,8 @@ afterPrependChunks# SealedState
       Just buf -> do
         -- Recycle the old current buffer, from which
         -- we already copied what we wished to keep.
-        let u1 = P.sizeofMutableByteArray buf - metaDataSize
-            !(PTR base) = P.mutableByteArrayContents buf
+        u1 <- subtract metaDataSize <$> P.getSizeofMutableByteArray buf
+        let !(PTR base) = P.mutableByteArrayContents buf
             !v1 = plusPtr (Ptr base) (metaDataSize + u1)
             !m = plusPtr (Ptr base) metaDataSize
         writeSpace m (u1 + total)

From 01b4b07e3cdd918a2b99e39ab57384fd82c0241a Mon Sep 17 00:00:00 2001
From: John Carey <jcarey@arista.com>
Date: Tue, 14 Jan 2025 18:28:41 -0800
Subject: [PATCH 2/3] CI: 9.6.2 -> 9.6.5, 9.8.1 -> 9.8.2

---
 .github/workflows/ci.yml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bcff2fd..28f362c 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -16,8 +16,8 @@ jobs:
           - 902
           - 928
           - 948
-          - 962
-          - 981
+          - 965
+          - 982
           - 9101
         exclude:
           - os: macos-latest

From 892417807d752ac03ca0d3f9e70d91d65c1a6030 Mon Sep 17 00:00:00 2001
From: John Carey <jcarey@arista.com>
Date: Wed, 15 Jan 2025 10:49:51 -0800
Subject: [PATCH 3/3] Jailbreak aeson

---
 nix/haskell-packages.nix | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/nix/haskell-packages.nix b/nix/haskell-packages.nix
index 6a251a7..b32bb36 100644
--- a/nix/haskell-packages.nix
+++ b/nix/haskell-packages.nix
@@ -14,6 +14,11 @@ pkgsNew: pkgsOld:
             adjunctions =
               pkgsNew.haskell.lib.dontCheck haskellPackagesPrev.adjunctions;
 
+            # With nixpkgs-24.11 and our overrides, and when building with GHC 8.10,
+            # aeson thinks that th-abstraction is out of bounds.
+            aeson =
+              pkgsNew.haskell.lib.doJailbreak haskellPackagesPrev.aeson;
+
             # With nixpkgs-23.11 and ghc981, base-compat-batteries wants hspec for testing,
             # which causes problems.
             base-compat-batteries =