Skip to content

Commit a830625

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 2a6dbba commit a830625

File tree

2 files changed

+78
-22
lines changed

2 files changed

+78
-22
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: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,59 @@ Welcome to the Python language bindings for the [Payjoin Dev Kit](https://payjoi
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
14+
15+
If you have [nix](https://nixos.org/download/) installed, you can simply run:
1416

15-
Follow these steps to clone the repository and run the tests.
17+
```sh
18+
nix develop .#python
19+
```
20+
21+
This will get you up and running with a shell containing the dependencies you need.
1622

23+
Otherwise, follow these steps to clone the repository and build the package:
1724

18-
```shell
25+
```sh
1926
git clone https://github.com/payjoin/rust-payjoin.git
2027
cd rust-payjoin/payjoin-ffi/python
2128

2229
# Setup a python virtual environment
2330
python -m venv venv
2431
source venv/bin/activate
2532

26-
# Generate the bindings (use the script appropriate for your platform)
33+
# Install dependencies
34+
# NOTE: requirements-dev.txt only needed when running tests
35+
pip install --requirement requirements.txt --requirement requirements-dev.txt
36+
37+
# Generate the bindings (use the script appropriate for your platform (linux or macos))
2738
PYBIN="./venv/bin/" bash ./scripts/generate_<platform>.sh
2839

2940
# Build the wheel
3041
python setup.py bdist_wheel --verbose
3142

3243
# Force reinstall payjoin
3344
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`:
48+
49+
```sh
50+
python
51+
import payjoin
52+
```
53+
54+
## Running Tests
3455

56+
```sh
3557
# Run all tests
3658
python -m unittest --verbose
3759
```
3860

3961
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.
4062
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.
41-
42-
## Building the Package
43-
44-
```shell
45-
# Setup a python virtual environment
46-
python -m venv venv
47-
source venv/bin/activate
48-
49-
# Generate the bindings (use the script appropriate for your platform)
50-
PYBIN="./venv/bin/" bash ./scripts/generate_<platform>.sh
51-
52-
# Build the wheel
53-
python setup.py --verbose bdist_wheel
54-
55-
```

0 commit comments

Comments
 (0)