Skip to content

Commit 9965c61

Browse files
Add python devShell to nix flake
Adds a top-level python devShell to the nix flake which creates a python virtual environment with all necessary dependencies installed. Now, assuming Docker is running and nix is installed on the system, a developer can simply run `nix develop .#python` and have a shell with all the requirements to run `python -m unittest --verbose` setup, or import the `payjoin` package (locally built) to their project.
1 parent e3383d2 commit 9965c61

File tree

2 files changed

+76
-28
lines changed

2 files changed

+76
-28
lines changed

flake.nix

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,52 @@
9393
"payjoin-directory" = "";
9494
};
9595

96+
# Python-specific configuration
97+
pythonVersion = pkgs.python3;
98+
pythonEnv = pythonVersion.withPackages (ps: with ps; [
99+
virtualenv
100+
pip
101+
]);
102+
103+
# Determine platform for generate script
104+
supportedPlatforms = {
105+
"x86_64-linux" = "linux";
106+
"aarch64-linux" = "linux";
107+
"x86_64-darwin" = "macos";
108+
"aarch64-darwin" = "macos";
109+
};
110+
platform = supportedPlatforms.${system} or (throw "Unsupported platform: ${system}. Supported platforms: ${builtins.concatStringsSep ", " (builtins.attrNames supportedPlatforms)}");
111+
112+
# Python devShell
113+
pythonDevShell = pkgs.mkShell {
114+
name = "python-dev";
115+
buildInputs = with pkgs; [
116+
pythonEnv
117+
bash
118+
];
119+
120+
# Environment variables and shell hook
121+
shellHook = ''
122+
export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [pkgs.openssl]}:$LD_LIBRARY_PATH
123+
cd payjoin-ffi/python
124+
# Create and activate virtual environment
125+
python -m venv venv
126+
source venv/bin/activate
127+
# Install dependencies, allowing PyPI fetches for version mismatches
128+
pip install --requirement requirements.txt --requirement requirements-dev.txt
129+
# Generate bindings, setting PYBIN to the venv's python binary
130+
export PYBIN=./venv/bin/python
131+
bash ./scripts/generate_${platform}.sh
132+
# Set CARGO_TOML_PATH for setup.py
133+
export CARGO_TOML_PATH=${./.}/Cargo.toml
134+
# Build the wheel
135+
python setup.py bdist_wheel --verbose
136+
137+
# Install payjoin
138+
pip install ./dist/payjoin-*.whl
139+
'';
140+
};
141+
96142
devShells = builtins.mapAttrs (_name: craneLib:
97143
craneLib.devShell {
98144
packages = with pkgs; [
@@ -115,7 +161,10 @@
115161
// args);
116162
in {
117163
packages = packages;
118-
devShells = devShells // {default = devShells.nightly;};
164+
devShells = devShells // {
165+
default = devShells.nightly;
166+
python = pythonDevShell;
167+
};
119168
formatter = pkgs.alejandra;
120169
checks =
121170
packages

payjoin-ffi/python/README.md

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
# Payjoin
22

3-
Welcome to the Python language bindings for the [Payjoin Dev Kit](https://payjoindevkit.org/)! Let's get you up and running with some smooth transactions and a sprinkle of fun.
3+
Welcome to the Python language bindings for the [Payjoin Dev Kit](https://payjoindevkit.org/)!
44

55
## Install from PyPI
66

7-
Grab the latest release with a simple:
7+
To grab the latest release:
88

9-
```shell
9+
```sh
1010
pip install payjoin
1111
```
1212

13-
## Running Tests
13+
## Building the Package
1414

15-
Follow these steps to clone the repository and run the tests.
15+
If you have [nix](https://nixos.org/download/) installed, you can simply run:
16+
17+
```sh
18+
nix develop .#python
19+
```
1620

21+
This will get you up and running with a shell containing the dependencies you need.
1722

18-
```shell
23+
Otherwise, follow these steps to clone the repository and build the package:
24+
25+
```sh
1926
git clone https://github.com/payjoin/rust-payjoin.git
2027
cd rust-payjoin/payjoin-ffi/python
2128

@@ -24,40 +31,32 @@ python -m venv venv
2431
source venv/bin/activate
2532

2633
# Install dependencies
34+
# NOTE: requirements-dev.txt only needed when running tests
2735
pip install --requirement requirements.txt --requirement requirements-dev.txt
2836

29-
# Generate the bindings (use the script appropriate for your platform)
37+
# Generate the bindings (use the script appropriate for your platform (linux or macos))
3038
PYBIN="./venv/bin/" bash ./scripts/generate_<platform>.sh
3139

3240
# Build the wheel
3341
python setup.py bdist_wheel --verbose
3442

3543
# Force reinstall payjoin
3644
pip install ./dist/payjoin-<version>.whl --force-reinstall
45+
```
46+
47+
If all goes well, you should be able to run the Python interpreter and import `payjoin`:
3748

49+
```sh
50+
python
51+
import payjoin
52+
```
53+
54+
## Running Tests
55+
56+
```sh
3857
# Run all tests
3958
python -m unittest --verbose
4059
```
4160

4261
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.
4362
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.
44-
45-
## Building the Package
46-
47-
```shell
48-
# Setup a python virtual environment
49-
python -m venv venv
50-
source venv/bin/activate
51-
52-
# Install dependencies
53-
pip install --requirement requirements.txt
54-
55-
# Generate the bindings (use the script appropriate for your platform)
56-
PYBIN="./venv/bin/" bash ./scripts/generate_<platform>.sh
57-
58-
# Build the wheel
59-
python setup.py --verbose bdist_wheel
60-
61-
```
62-
We hope everything worked smoothly! Now go forth test, and may your test results be as reliable as the Bitcoin blockchain itself!
63-
₿🔒🤝

0 commit comments

Comments
 (0)