Serge Bazanski | 194b1c8 | 2020-09-25 20:24:17 +0000 | [diff] [blame] | 1 | # This is a Python interpreter wrapper that's passed to pip3_import under |
| 2 | # NixOS. |
| 3 | # It allows us to build some pip wheels under NixOS that require special |
| 4 | # system libraries. This is quite hacky, it would be much better if we could |
| 5 | # somehow tell pip3_import that a given package needs to be built within a |
| 6 | # given environment. |
| 7 | |
| 8 | with import <nixpkgs> {}; |
| 9 | |
| 10 | let |
| 11 | # Add cffi for import _cffi_backend in `cryptography` to work. |
| 12 | py = pkgs.python37.withPackages (ps: with ps; [ cffi ]); |
| 13 | |
| 14 | # We use mkDerivation instead of writeScript or writeScriptBin as we need a |
| 15 | # derivation that both: |
| 16 | # - has a directory structure (for rules_nixpkgs to be able to use it) |
| 17 | # - has the Python interpreter directly in that structure and not in bin/, as |
| 18 | # rules_python's pip3_import interpreter_path requires a file target, and |
| 19 | # will not take an alias. Meanwhile, rules_nixpkgs only creates a BUILD file |
| 20 | # in the root path of the external repository (which is populated with a |
| 21 | # symlink tree from the nix derivation), so we can onlly directly reference |
| 22 | # file in the root of a Nix derivation. |
| 23 | in stdenv.mkDerivation { |
| 24 | name = "py-wrapper"; |
| 25 | version = "1.0"; |
| 26 | src = ./.; |
| 27 | unpackPhase = ""; |
| 28 | buildPhase = '' |
| 29 | mkdir -p $out |
| 30 | cat > $out/python3 <<EOF |
| 31 | #!/bin/bash |
| 32 | |
| 33 | # pyscopg wants libpq, and uses pg_config to find paths. Inject pg_config into |
| 34 | # the Python interpreter's path. |
| 35 | export PATH="${pkgs.postgresql}/bin:\$PATH" |
| 36 | |
| 37 | # uWSGI has a truly cheese-grade build system, and this is the only way to let |
| 38 | # it know where to find ncurses. |
| 39 | export LDFLAGS="-L${pkgs.ncurses}/lib" |
| 40 | exec ${py}/bin/python3 "\$@" |
| 41 | EOF |
| 42 | ''; |
| 43 | installPhase = '' |
| 44 | chmod +x $out/python3 |
| 45 | ''; |
| 46 | } |