blob: 5571a60430effa38c9b78b5e4fd1a13a78d83d5f [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
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.
23in 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.
35export 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.
39export LDFLAGS="-L${pkgs.ncurses}/lib"
40exec ${py}/bin/python3 "\$@"
41EOF
42 '';
43 installPhase = ''
44 chmod +x $out/python3
45 '';
46}