diff --git a/flake.nix b/flake.nix index f591dc3fc..44286ff63 100644 --- a/flake.nix +++ b/flake.nix @@ -93,6 +93,52 @@ "payjoin-directory" = ""; }; + # Python-specific configuration + pythonVersion = pkgs.python3; + pythonEnv = pythonVersion.withPackages (ps: with ps; [ + virtualenv + pip + ]); + + # Determine platform for generate script + supportedPlatforms = { + "x86_64-linux" = "linux"; + "aarch64-linux" = "linux"; + "x86_64-darwin" = "macos"; + "aarch64-darwin" = "macos"; + }; + platform = supportedPlatforms.${system} or (throw "Unsupported platform: ${system}. Supported platforms: ${builtins.concatStringsSep ", " (builtins.attrNames supportedPlatforms)}"); + + # Python devShell + pythonDevShell = pkgs.mkShell { + name = "python-dev"; + buildInputs = with pkgs; [ + pythonEnv + bash + ]; + + # Environment variables and shell hook + shellHook = '' + export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [pkgs.openssl]}:$LD_LIBRARY_PATH + cd payjoin-ffi/python + # Create and activate virtual environment + python -m venv venv + source venv/bin/activate + # Install dependencies, allowing PyPI fetches for version mismatches + pip install --requirement requirements.txt --requirement requirements-dev.txt + # Generate bindings, setting PYBIN to the venv's python binary + export PYBIN=./venv/bin/python + bash ./scripts/generate_${platform}.sh + # Set CARGO_TOML_PATH for setup.py + export CARGO_TOML_PATH=${./.}/Cargo.toml + # Build the wheel + python setup.py bdist_wheel --verbose + + # Install payjoin + pip install ./dist/payjoin-*.whl + ''; + }; + devShells = builtins.mapAttrs (_name: craneLib: craneLib.devShell { packages = with pkgs; [ @@ -115,7 +161,10 @@ // args); in { packages = packages; - devShells = devShells // {default = devShells.nightly;}; + devShells = devShells // { + default = devShells.nightly; + python = pythonDevShell; + }; formatter = pkgs.alejandra; checks = packages diff --git a/payjoin-ffi/python/README.md b/payjoin-ffi/python/README.md index 600ccfcf5..77c85c489 100644 --- a/payjoin-ffi/python/README.md +++ b/payjoin-ffi/python/README.md @@ -4,18 +4,25 @@ Welcome to the Python language bindings for the [Payjoin Dev Kit](https://payjoi ## Install from PyPI -Grab the latest release with a simple: +To grab the latest release: -```shell +```sh pip install payjoin ``` -## Running Tests +## Building the Package + +If you have [nix](https://nixos.org/download/) installed, you can simply run: -Follow these steps to clone the repository and run the tests. +```sh +nix develop .#python +``` + +This will get you up and running with a shell containing the dependencies you need. +Otherwise, follow these steps to clone the repository and build the package: -```shell +```sh git clone https://github.com/payjoin/rust-payjoin.git cd rust-payjoin/payjoin-ffi/python @@ -23,7 +30,11 @@ cd rust-payjoin/payjoin-ffi/python python -m venv venv source venv/bin/activate -# Generate the bindings (use the script appropriate for your platform) +# Install dependencies +# NOTE: requirements-dev.txt only needed when running tests +pip install --requirement requirements.txt --requirement requirements-dev.txt + +# Generate the bindings (use the script appropriate for your platform (linux or macos)) PYBIN="./venv/bin/" bash ./scripts/generate_.sh # Build the wheel @@ -31,25 +42,21 @@ python setup.py bdist_wheel --verbose # Force reinstall payjoin pip install ./dist/payjoin-.whl --force-reinstall +``` + +If all goes well, you should be able to run the Python interpreter and import `payjoin`: + +```sh +python +import payjoin +``` + +## Running Tests +```sh # Run all tests python -m unittest --verbose ``` Note that you'll need Docker to run the integration tests. If you get a "Failed to start container" error, ensure the Docker engine is running on your machine. You can [filter which tests](https://docs.python.org/3/library/unittest.html#command-line-interface) to run by passing a file or test name as argument. - -## Building the Package - -```shell -# Setup a python virtual environment -python -m venv venv -source venv/bin/activate - -# Generate the bindings (use the script appropriate for your platform) -PYBIN="./venv/bin/" bash ./scripts/generate_.sh - -# Build the wheel -python setup.py --verbose bdist_wheel - -```