From 629f1acafcc4ae578ad5889ab2a2f71ea57519f9 Mon Sep 17 00:00:00 2001
From: Benjamin Koch <bbbsnowball@gmail.com>
Date: Mon, 29 May 2023 17:49:39 +0200
Subject: [PATCH] add modbus-tk and newer version of pymodbus to our Nix shell

---
 flake.nix         |  4 +++
 nix/modbus-tk.nix | 24 ++++++++++++++++
 nix/pymodbus3.nix | 73 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 101 insertions(+)
 create mode 100644 nix/modbus-tk.nix
 create mode 100644 nix/pymodbus3.nix

diff --git a/flake.nix b/flake.nix
index 6cd4659..d09bb70 100644
--- a/flake.nix
+++ b/flake.nix
@@ -13,6 +13,9 @@
             qrcodegen = python-final.callPackage nix/qrcodegen.nix { };
             svgutils = python-final.callPackage nix/svgutils.nix { };
             pcbnewTransition = python-final.callPackage nix/pcbnewTransition.nix { };
+
+            modbus-tk = python-final.callPackage nix/modbus-tk.nix { };
+            pymodbus3 = python-final.callPackage nix/pymodbus3.nix { };
           })
         ];
         other.python3Packages = self.packages.${system}.python3.pkgs;
@@ -153,6 +156,7 @@
         devShells.rust = with self.packages.${system}; pkgs.mkShell {
           packages = with pkgs; [
             rustup udev.dev pkg-config openssl.dev picotool
+            (self.packages.${system}.python3.withPackages (p: with p; [ pymodbus3 pyserial modbus-tk ]))
           ];
         };
       }
diff --git a/nix/modbus-tk.nix b/nix/modbus-tk.nix
new file mode 100644
index 0000000..e91b96d
--- /dev/null
+++ b/nix/modbus-tk.nix
@@ -0,0 +1,24 @@
+{ lib
+, buildPythonPackage
+, fetchFromGitHub
+, pyserial
+}:
+
+buildPythonPackage rec {
+  pname = "modbus-tk";
+  version = "git";
+
+  src = fetchFromGitHub {
+    owner = "ljean";
+    repo = pname;
+    rev = "f32bb42cda8be3122fefad59a35b5c5fe6bcf567";
+    hash = "sha256-wKFRi6xcStuQURJ3Lo48P0lFDfg33Zt3NLJzqvu138U=";
+  };
+
+  # Twisted asynchronous version is not supported due to a missing dependency
+  propagatedBuildInputs = [
+    pyserial
+  ];
+
+  pythonImportsCheck = [ "modbus_tk" ];
+}
\ No newline at end of file
diff --git a/nix/pymodbus3.nix b/nix/pymodbus3.nix
new file mode 100644
index 0000000..990fc33
--- /dev/null
+++ b/nix/pymodbus3.nix
@@ -0,0 +1,73 @@
+# based on https://github.com/NixOS/nixpkgs/blob/c8a17ce7abc03c50cd072e9e6c9b389c5f61836b/pkgs/development/python-modules/pymodbus/default.nix
+{ lib
+, aiohttp
+, asynctest
+, buildPythonPackage
+, click
+, fetchFromGitHub
+, mock
+, prompt-toolkit
+, pygments
+, pyserial
+, pyserial-asyncio
+, pytestCheckHook
+, redis
+, sqlalchemy
+, tornado
+, twisted
+, pytest-asyncio
+, pytest-xdist
+}:
+
+buildPythonPackage rec {
+  pname = "pymodbus";
+  version = "3.2.2";
+
+  src = fetchFromGitHub {
+    owner = "pymodbus-dev";
+    repo = pname;
+    rev = "v${version}";
+    hash = "sha256-Hw7oGYzjcHBTBjE9vf+3D8DzXdqVuHbJmIMH0W3mwE4=";
+  };
+
+  # Twisted asynchronous version is not supported due to a missing dependency
+  propagatedBuildInputs = [
+    aiohttp
+    click
+    prompt-toolkit
+    pygments
+    pyserial
+    pyserial-asyncio
+    tornado
+  ];
+
+  checkInputs = [
+    asynctest
+    mock
+    pytestCheckHook
+    redis
+    sqlalchemy
+    twisted
+    pytest-asyncio
+    pytest-xdist
+  ];
+
+  pythonImportsCheck = [ "pymodbus" ];
+
+  # many tests fail because self.loop is None in TestAsyncioServer
+  #FIXME We should figure this out but that's not a priority here because we don't use async, yet.
+  doCheck = false;
+
+  meta = with lib; {
+    description = "Python implementation of the Modbus protocol";
+    longDescription = ''
+      Pymodbus is a full Modbus protocol implementation using twisted,
+      torndo or asyncio for its asynchronous communications core. It can
+      also be used without any third party dependencies if a more
+      lightweight project is needed.
+    '';
+    homepage = "https://github.com/riptideio/pymodbus";
+    license = with licenses; [ bsd3 ];
+    maintainers = with maintainers; [ fab ];
+  };
+}
-- 
GitLab