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 |
Serge Bazanski | 27885a9 | 2020-10-03 16:54:39 +0200 | [diff] [blame] | 11 | # We use mkDerivation instead of writeScript or writeScriptBin as we need a |
| 12 | # derivation that both: |
| 13 | # - has a directory structure (for rules_nixpkgs to be able to use it) |
| 14 | # - has the Python interpreter directly in that structure and not in bin/, as |
| 15 | # rules_python's pip3_import interpreter_path requires a file target, and |
| 16 | # will not take an alias. Meanwhile, rules_nixpkgs only creates a BUILD file |
| 17 | # in the root path of the external repository (which is populated with a |
| 18 | # symlink tree from the nix derivation), so we can onlly directly reference |
| 19 | # file in the root of a Nix derivation. |
| 20 | generic = package: binary: stdenv.mkDerivation { |
| 21 | name = "${binary}-wrapper"; |
| 22 | version = "1.0"; |
| 23 | src = ./.; |
| 24 | unpackPhase = ""; |
| 25 | buildPhase = '' |
| 26 | mkdir -p $out |
| 27 | cat > $out/${binary} <<EOF |
Serge Bazanski | 194b1c8 | 2020-09-25 20:24:17 +0000 | [diff] [blame] | 28 | #!/bin/bash |
| 29 | |
| 30 | # pyscopg wants libpq, and uses pg_config to find paths. Inject pg_config into |
| 31 | # the Python interpreter's path. |
| 32 | export PATH="${pkgs.postgresql}/bin:\$PATH" |
| 33 | |
Serge Bazanski | 27885a9 | 2020-10-03 16:54:39 +0200 | [diff] [blame] | 34 | exec ${package}/bin/${binary} "\$@" |
Serge Bazanski | 194b1c8 | 2020-09-25 20:24:17 +0000 | [diff] [blame] | 35 | EOF |
Serge Bazanski | 27885a9 | 2020-10-03 16:54:39 +0200 | [diff] [blame] | 36 | ''; |
| 37 | installPhase = '' |
| 38 | chmod +x $out/${binary} |
| 39 | ''; |
| 40 | }; |
| 41 | |
| 42 | in { |
| 43 | # Add cffi for import _cffi_backend in `cryptography` to work. |
| 44 | python2 = generic (pkgs.python27.withPackages (ps: with ps; [ cffi ])) "python2"; |
| 45 | python3 = generic (pkgs.python37.withPackages (ps: with ps; [ cffi ])) "python3"; |
Serge Bazanski | 194b1c8 | 2020-09-25 20:24:17 +0000 | [diff] [blame] | 46 | } |