nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 stdenv,
3 lib,
4 callPackage,
5
6 abseil-cpp_202508,
7 bzip2,
8 cbc,
9 cmake,
10 eigen,
11 ensureNewerSourcesForZipFilesHook,
12 fetchFromGitHub,
13 fetchpatch,
14 gtest,
15 gbenchmark,
16 glpk,
17 highs,
18 pkg-config,
19 protobuf_32,
20 protobuf-matchers,
21 python3,
22 re2,
23 swig,
24 unzip,
25 zlib,
26
27 scipopt-scip,
28 withScip ? true,
29}:
30
31let
32 # OR-Tools strictly requires specific versions of abseil-cpp and
33 # protobuf. Do not un-pin these, even if you're upgrading them to
34 # what might happen to be the latest version at the current moment;
35 # future upgrades *will* break the build.
36 abseil-cpp' = abseil-cpp_202508;
37 gtest' = gtest.override {
38 withAbseil = true;
39 abseil-cpp = abseil-cpp';
40 };
41 protobuf' = protobuf_32.override { abseil-cpp = abseil-cpp'; };
42 protobuf-matchers' = protobuf-matchers.override { protobuf = protobuf'; };
43 python-protobuf' = python3.pkgs.protobuf6.override { protobuf = protobuf'; };
44
45 pybind11' = callPackage ./pybind11-2.13.6.nix {
46 inherit (python3.pkgs)
47 buildPythonPackage
48 cmake
49 ninja
50 numpy
51 pytestCheckHook
52 setuptools
53 ;
54 python = python3;
55 };
56 pybind11-abseil' = python3.pkgs.pybind11-abseil.override {
57 pybind11 = pybind11';
58 abseil-cpp = abseil-cpp';
59 };
60 pybind11-protobuf' = callPackage ./pybind11-protobuf.nix {
61 inherit (python3.pkgs) buildPythonPackage;
62 pybind11 = pybind11';
63 };
64 # re2 must also use the same abseil version, else these two versions will conflict during linking
65 re2' = re2.override { abseil-cpp = abseil-cpp'; };
66
67 # 77a28070b9c4c83995ac6bbfa9544722ff3342ce renamed the scip cmake target(s) differently
68 # to what upstream still calls it. Apply this patch to scipopt-scip.
69 scipopt-scip' = scipopt-scip.overrideAttrs (old: {
70 patches = old.patches or [ ] ++ [
71 # from https://github.com/google/or-tools/commit/77a28070b9c4c83995ac6bbfa9544722ff3342ce#diff-c95174a817e73db366d414af1e329c1856f70e5158ed3994d43da88765ccc98f
72 # and updated with https://github.com/google/or-tools/pull/4932/files#diff-e6b0a69b2e4b97ec922abc459d909483d440a1e0d2868bed263927b106b6efe6
73 ./scip.patch
74 ];
75 # Their patch forgets to find_package() soplex, bring it back.
76 postPatch = (old.postPatch or "") + ''
77 substituteInPlace CMakeLists.txt \
78 --replace-fail 'message(STATUS "Finding Soplex")' 'find_package(SOPLEX CONFIG HINTS ''${SOPLEX_DIR})'
79 '';
80 });
81
82in
83stdenv.mkDerivation (finalAttrs: {
84 pname = "or-tools";
85 version = "9.15";
86
87 src = fetchFromGitHub {
88 owner = "google";
89 repo = "or-tools";
90 tag = "v${finalAttrs.version}";
91 hash = "sha256-9+tvgP/+/VY6wu7lzTdP4xfiJIgPSLVR9lEdZjQCZkE=";
92 };
93
94 patches = [
95 # Rebased from https://build.opensuse.org/public/source/science/google-or-tools/0001-Do-not-try-to-copy-pybind11_abseil-status-extension-.patch?rev=19
96 ./0001-Do-not-try-to-copy-pybind11_abseil-status-extension-.patch
97 (fetchpatch {
98 name = "0001-Revert-python-Fix-python-install-on-windows-breaks-L.patch";
99 url = "https://build.opensuse.org/public/source/science/google-or-tools/0001-Revert-python-Fix-python-install-on-windows-breaks-L.patch?rev=19";
100 hash = "sha256-BNB3KlgjpWcZtb9e68Jkc/4xC4K0c+Iisw0eS6ltYXE=";
101 })
102 ./0001-Fix-up-broken-CMake-rules-for-bundled-pybind-stuff.patch
103 ];
104
105 # or-tools normally attempts to build Protobuf for the build platform when
106 # cross-compiling. Instead, just tell it where to find protoc.
107 postPatch = ''
108 echo "set(PROTOC_PRG $(type -p protoc))" > cmake/host.cmake
109 substituteInPlace CMakeLists.txt \
110 --replace-fail 'set(BUILD_protobuf_matchers ON)' 'set(BUILD_protobuf_matchers OFF)'
111 ''
112 # Patches from OpenSUSE:
113 # https://build.opensuse.org/projects/science/packages/google-or-tools/files/google-or-tools.spec?expand=1
114 + ''
115 sed -i -e '/CMAKE_DEPENDENT_OPTION(INSTALL_DOC/ s/BUILD_CXX AND BUILD_DOC/BUILD_CXX/' CMakeLists.txt
116 find . -iname \*CMakeLists.txt -exec sed -i -e 's/pybind11_native_proto_caster/pybind11_protobuf::pybind11_native_proto_caster/' '{}' \;
117 sed -i -e 's/TARGET pybind11_native_proto_caster/TARGET pybind11_protobuf::pybind11_native_proto_caster/' cmake/check_deps.cmake
118 sed -i -e "/protobuf/ { s/.*,/'protobuf >= 5.26',/ }" ortools/python/setup.py.in
119 '';
120
121 cmakeFlags = [
122 (lib.cmakeBool "BUILD_DEPS" false)
123 (lib.cmakeBool "BUILD_PYTHON" true)
124 (lib.cmakeBool "BUILD_pybind11" false)
125 (lib.cmakeFeature "CMAKE_INSTALL_BINDIR" "bin")
126 (lib.cmakeFeature "CMAKE_INSTALL_INCLUDEDIR" "include")
127 (lib.cmakeFeature "CMAKE_INSTALL_LIBDIR" "lib")
128 (lib.cmakeBool "FETCH_PYTHON_DEPS" false)
129 # not packaged in nixpkgs
130 (lib.cmakeBool "USE_GLPK" true)
131 (lib.cmakeBool "USE_SCIP" withScip)
132 (lib.cmakeFeature "Python3_EXECUTABLE" "${python3.pythonOnBuildForHost.interpreter}")
133 ]
134 ++ lib.optionals withScip [
135 # scip code parts require setting this unfortunately…
136 (lib.cmakeFeature "CMAKE_CXX_FLAGS" "-Wno-error=format-security")
137 ]
138 ++ lib.optionals stdenv.hostPlatform.isDarwin [
139 (lib.cmakeBool "CMAKE_MACOSX_RPATH" false)
140 ];
141
142 strictDeps = true;
143
144 nativeBuildInputs = [
145 cmake
146 ensureNewerSourcesForZipFilesHook
147 pkg-config
148 python3.pythonOnBuildForHost
149 swig
150 unzip
151 ]
152 ++ (with python3.pythonOnBuildForHost.pkgs; [
153 pip
154 mypy-protobuf
155 mypy
156 ]);
157 buildInputs = [
158 abseil-cpp'
159 bzip2
160 cbc
161 eigen
162 glpk
163 gbenchmark
164 gtest'
165 highs
166 protobuf-matchers'
167 python3.pkgs.absl-py
168 pybind11'
169 pybind11-abseil'
170 pybind11-protobuf'
171 python3.pkgs.pytest
172 python3.pkgs.scipy
173 python3.pkgs.setuptools
174 python3.pkgs.wheel
175 re2'
176 zlib
177 ];
178 propagatedBuildInputs = [
179 abseil-cpp'
180 highs
181 protobuf'
182 python-protobuf'
183 python3.pkgs.immutabledict
184 python3.pkgs.numpy
185 python3.pkgs.pandas
186 ]
187 ++ lib.optionals withScip [
188 # Needed for downstream cmake consumers to not need to set SCIP_ROOT explicitly
189 scipopt-scip'
190 ];
191
192 nativeCheckInputs = [
193 python3.pkgs.matplotlib
194 python3.pkgs.pandas
195 python3.pkgs.pytest
196 python3.pkgs.scipy
197 python3.pkgs.svgwrite
198 python3.pkgs.virtualenv
199 ];
200
201 # some tests hang on darwin
202 doCheck = stdenv.hostPlatform.isLinux;
203
204 preCheck = ''
205 patchShebangs examples/python
206 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH''${LD_LIBRARY_PATH:+:}$PWD/lib
207 '';
208
209 # This extra configure step prevents the installer from littering
210 # $out/bin with sample programs that only really function as tests,
211 # and disables the upstream installation of a zipped Python egg that
212 # can’t be imported with our Python setup.
213 installPhase = ''
214 cmake . -DBUILD_EXAMPLES=OFF -DBUILD_PYTHON=OFF -DBUILD_SAMPLES=OFF
215 cmake --install .
216
217 # Install the Python bindings.
218 # --no-build-isolation: Required because Nix provides build tools (setuptools/wheel)
219 # locally; without this, pip tries to download them from the internet.
220 # --no-index: Prevents pip from searching PyPI for packages.
221 pip install --no-index --no-build-isolation --prefix="$python" python/
222 '';
223
224 outputs = [
225 "out"
226 "python"
227 ];
228
229 meta = {
230 homepage = "https://github.com/google/or-tools";
231 license = lib.licenses.asl20;
232 description = ''
233 Google's software suite for combinatorial optimization.
234 '';
235 mainProgram = "fzn-cp-sat";
236 maintainers = with lib.maintainers; [ andersk ];
237 platforms = with lib.platforms; linux ++ darwin;
238
239 # Only version 9.15 adds support for Python 3.14: https://github.com/google/or-tools/releases/tag/v9.15
240 # Also this package is tied to pybind 2.13.6, and only 3.0.0 supports Python 3.14: https://github.com/pybind/pybind11/releases/tag/v3.0.0
241 # Also, nix review fails to build python314Packages.ortools
242 broken = python3.pythonAtLeast "3.14";
243 };
244})