Any PyPy3 users here? (Lib dep prb)

I tried to use PyPy3 and the available helper scripts, but faced quite quickly difficulties. Please do find below smallish flake.nix that shows the problem.

{
  description = "Try pypy3";
  outputs = { self, nixpkgs }:
  let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
    py3pkgs = pkgs.python3.pkgs;
  in {
    # For the following, use: nix run 
    # Fails, because doesn't find the requests module.
    packages.${system}.default = pkgs.writers.writePyPy3Bin "say_foo" { libraries = [ py3pkgs.requests ]; } ''
       import requests
       status = requests.get("https://www.example.com").status_code
       print(status)
    '';
    # The following works (with `nix run`)
    # This works fine.
    # packages.${system}.default = pkgs.writers.writePython3Bin "say_foo" { libraries = [ py3pkgs.requests ]; } ''
    #    import requests
    #    status = requests.get("https://www.example.com").status_code
    #    print(status)
    # '';
  };
}

There are couple of discussions that (almost) refer to this, like Basic flake: run existing (python, bash) script - #6 by n8henrie. Further, Python - NixOS Wiki says that

you can use the same example with other versions of Python by replacing python3 with python2 or pypy3

Apparently it is quite that easy with PyPy3. Do we have PyPy3 users here & if yes, how have you solved the dependency issues? Could you please share?

My hunch says that maybe some path needs to be set, but I’m novice w.r.t. PyPy3: it could be that pypy3 needs to somehow compile or install the libs on its own way?? Not sure.


Late additions.

Docs at Downloading and Installing PyPy — PyPy documentation say

Installing more modules¶

If you want to install 3rd party libraries, the most convenient way is to install pip using ensurepip (unless you want to install virtualenv as explained below; then you can directly use pip inside virtualenvs):

 ./pypy-xxx/bin/pypy -m ensurepip

 ./pypy-xxx/bin/pypy -mpip install -U pip wheel # to upgrade to the latest versions

 ./pypy-xxx/bin/pypy -mpip install pygments  # for example

Third party libraries will be installed in pypy-xxx/site-packages. As with CPython, scripts on linux and macOS will be in pypy-xxx/bin, and on windows they will be in pypy-xxx/Scripts

A small step taken forward. In a dev shell, by setting the PYTHONPATH, pypy is able to find the imports:

 let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
    py3pkgs = pkgs.python3.pkgs;
    py-libs = pkgs.python310.withPackages(ps: with ps; [
      requests
      ]);
    py-with-pkgs = with pkgs; [
      pypy310
      py-libs
    ];
    pysitepath = "${py-libs}/lib/python3.10/site-packages";
  in {
    devShells.${system}.default = pkgs.mkShell {
      buildInputs = py-with-pkgs; 
      shellHook = ''
          export PYTHONPATH="./src/":${pysitepath};
      '';
    };
    # For the following, use: nix run 
    # Fails, because doesn't find the requests module.
    packages.${system}.default = pkgs.writers.writePyPy3Bin "say_foo" { 
      libraries = [ py3pkgs.requests ]; 
      PYTHONPATH="./src/":${pysitepath};
    } ''
       import requests
       status = requests.get("https://www.example.com").status_code
       print(status)
    '';
   ...

So the next question is, how to actually tell that path in the script so that nix run would work?!?!
With a “writeScript” function and using just shell scripts, this seems to be doable. Anyhow, as the PyPy writer is there, it would feel better to use it.

Another things that feels problematic with this approach is the use of PYTHONPATH env var. E.g. Run shell commands in nix expressions recommends against using it. What would be the canonical way of solving this knot?

The line after “libraries =” having that shell variable, would be nice to use. Is there something similar available?