1{
2 autoPatchelfHook,
3 buildPythonPackage,
4 cmake,
5 cython,
6 fetchFromGitHub,
7 h3,
8 lib,
9 numpy,
10 pytestCheckHook,
11 scikit-build,
12 stdenv,
13}:
14
15buildPythonPackage rec {
16 pname = "h3";
17 version = "3.7.7";
18 format = "setuptools";
19
20 # pypi version does not include tests
21 src = fetchFromGitHub {
22 owner = "uber";
23 repo = "h3-py";
24 rev = "refs/tags/v${version}";
25 hash = "sha256-wXQaSMXQI0f7zfyj37mubxdqGFv7vhHQd6rH08H57d4=";
26 };
27
28 dontConfigure = true;
29
30 nativeCheckInputs = [ pytestCheckHook ];
31
32 nativeBuildInputs =
33 [
34 scikit-build
35 cmake
36 cython
37 ]
38 ++ lib.optionals stdenv.hostPlatform.isLinux [
39 # On Linux the .so files ends up referring to libh3.so instead of the full
40 # Nix store path. I'm not sure why this is happening! On Darwin it works
41 # fine.
42 autoPatchelfHook
43 ];
44
45 # This is not needed per-se, it's only added for autoPatchelfHook to work
46 # correctly. See the note above ^^
47 buildInputs = lib.optionals stdenv.hostPlatform.isLinux [ h3 ];
48
49 propagatedBuildInputs = [ numpy ];
50
51 # The following prePatch replaces the h3lib compilation with using the h3 packaged in nixpkgs.
52 #
53 # - Remove the h3lib submodule.
54 # - Patch CMakeLists to avoid building h3lib, and use h3 instead.
55 prePatch =
56 let
57 cmakeCommands = ''
58 include_directories(${lib.getDev h3}/include/h3)
59 link_directories(${h3}/lib)
60 '';
61 in
62 ''
63 rm -r src/h3lib
64 substituteInPlace CMakeLists.txt --replace "add_subdirectory(src/h3lib)" "${cmakeCommands}"
65 '';
66
67 # Extra check to make sure we can import it from Python
68 pythonImportsCheck = [ "h3" ];
69
70 meta = with lib; {
71 homepage = "https://github.com/uber/h3-py";
72 description = "Hierarchical hexagonal geospatial indexing system";
73 license = licenses.asl20;
74 maintainers = [ maintainers.kalbasit ];
75 };
76}