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?