Skip to content

Instantly share code, notes, and snippets.

@GuillaumeDesforges
Last active January 26, 2024 16:03
Show Gist options
  • Save GuillaumeDesforges/c6a4d2799758cae7856e019dd8e9f909 to your computer and use it in GitHub Desktop.
Save GuillaumeDesforges/c6a4d2799758cae7856e019dd8e9f909 to your computer and use it in GitHub Desktop.
Local development setup for python-nix

First, let's prepare Nix.

Get Nix source code and the C API branch:

git clone https://github.com/NixOS/nix.git
cd nix
git remote add tweag https://github.com/tweag/nix.git
git checkout nix-c-bindings

Build it once locally.

nix develop .#native-clangStdenvPackages --override-input nixpkgs nixpkgs
[nix] $ autoreconfPhase
[nix] $ configurePhase
[nix] $ make clean && bear -- make -j$NIX_BUILD_CORES default check install

Now, let's get the python bindings.

In another terminal, get the python bindings source code:

git clone https://github.com/tweag/python-nix.git
cd python-nix

You need gcc and pkgconfig

nix shell nixpkgs#gcc nixpkgs#pkg-config

We need to tell pkgconfig where it can find our version of Nix:

export PKG_CONFIG_PATH="../nix/outputs/out/lib/pkgconfig/"

Use a Python virtual environment:

nix --offline run nixpkgs#python311 -- -m venv .venv --copies

We can now install the Python package:

pip install . --verbose

at the end, you should see it output messages such as:

  adding 'nix/_nix_api_expr.abi3.so'

If you don't, something went wrong.

You should be now able to test it:

python
>>> import nix
>>> nix.eval("1")
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment