blob: 563127eec825bfe2bf67221d3d41b0f5e3f4faf5 [file] [log] [blame]
Serge Bazanski194b1c82020-09-25 20:24:17 +00001# 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
8with import <nixpkgs> {};
9
10let
Serge Bazanski27885a92020-10-03 16:54:39 +020011 # 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 Bazanski194b1c82020-09-25 20:24:17 +000028#!/bin/bash
29
30# pyscopg wants libpq, and uses pg_config to find paths. Inject pg_config into
31# the Python interpreter's path.
32export PATH="${pkgs.postgresql}/bin:\$PATH"
33
Serge Bazanski27885a92020-10-03 16:54:39 +020034exec ${package}/bin/${binary} "\$@"
Serge Bazanski194b1c82020-09-25 20:24:17 +000035EOF
Serge Bazanski27885a92020-10-03 16:54:39 +020036 '';
37 installPhase = ''
38 chmod +x $out/${binary}
39 '';
40 };
41
42in {
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 Bazanski194b1c82020-09-25 20:24:17 +000046}