1{
2 fetchurl,
3 stdenv,
4 unixODBC,
5 cmake,
6 mariadb,
7 sqlite,
8 zlib,
9 libxml2,
10 dpkg,
11 lib,
12 openssl,
13 libkrb5,
14 libuuid,
15 patchelf,
16 libiconv,
17 fixDarwinDylibNames,
18 fetchFromGitHub,
19 psqlodbc,
20}:
21
22# Each of these ODBC drivers can be configured in your odbcinst.ini file using
23# the various passthru and meta values. Of note are:
24#
25# * `passthru.fancyName`, the typical name used to reference the driver
26# * `passthru.driver`, the path to the driver within the built package
27# * `meta.description`, a short description of the ODBC driver
28#
29# For example, you might generate it as follows:
30#
31# ''
32# [${package.fancyName}]
33# Description = ${package.meta.description}
34# Driver = ${package}/${package.driver}
35# ''
36
37{
38 psql = psqlodbc.override {
39 withUnixODBC = true;
40 withLibiodbc = false;
41 };
42
43 mariadb = stdenv.mkDerivation rec {
44 pname = "mariadb-connector-odbc";
45 version = "3.1.20";
46
47 src = fetchFromGitHub {
48 owner = "mariadb-corporation";
49 repo = "mariadb-connector-odbc";
50 rev = version;
51 hash = "sha256-l+HlS7/A0shwsEXYKDhi+QCmwHaMTeKrtcvo9yYpYws=";
52 # this driver only seems to build correctly when built against the mariadb-connect-c subrepo
53 # (see https://github.com/NixOS/nixpkgs/issues/73258)
54 fetchSubmodules = true;
55 };
56
57 patches = [
58 # Fix `call to undeclared function 'sleep'` with clang 16
59 ./mariadb-connector-odbc-unistd.patch
60 ];
61
62 nativeBuildInputs = [ cmake ];
63 buildInputs = [
64 unixODBC
65 openssl
66 libiconv
67 zlib
68 ]
69 ++ lib.optionals stdenv.hostPlatform.isDarwin [ libkrb5 ];
70
71 cmakeFlags = [
72 "-DWITH_EXTERNAL_ZLIB=ON"
73 "-DODBC_LIB_DIR=${lib.getLib unixODBC}/lib"
74 "-DODBC_INCLUDE_DIR=${lib.getDev unixODBC}/include"
75 "-DWITH_OPENSSL=ON"
76 # on darwin this defaults to ON but we want to build against unixODBC
77 "-DWITH_IODBC=OFF"
78 ];
79
80 buildFlags = if stdenv.hostPlatform.isDarwin then [ "maodbc" ] else null;
81
82 env = lib.optionalAttrs stdenv.cc.isGNU {
83 NIX_CFLAGS_COMPILE = "-Wno-error=incompatible-pointer-types";
84 };
85
86 installTargets = if stdenv.hostPlatform.isDarwin then [ "install/fast" ] else null;
87
88 # see the top of the file for an explanation
89 passthru = {
90 fancyName = "MariaDB";
91 driver = "lib/libmaodbc${stdenv.hostPlatform.extensions.sharedLibrary}";
92 };
93
94 meta = with lib; {
95 description = "MariaDB ODBC database driver";
96 homepage = "https://downloads.mariadb.org/connector-odbc/";
97 license = licenses.gpl2;
98 platforms = platforms.linux ++ platforms.darwin;
99 };
100 };
101
102 mysql = stdenv.mkDerivation rec {
103 pname = "mysql-connector-odbc";
104 majorVersion = "5.3";
105 version = "${majorVersion}.6";
106
107 src = fetchurl {
108 url = "https://dev.mysql.com/get/Downloads/Connector-ODBC/${majorVersion}/${pname}-${version}-src.tar.gz";
109 sha256 = "1smi4z49i4zm7cmykjkwlxxzqvn7myngsw5bc35z6gqxmi8c55xr";
110 };
111
112 nativeBuildInputs = [ cmake ];
113 buildInputs = [
114 unixODBC
115 mariadb
116 ];
117
118 cmakeFlags = [ "-DWITH_UNIXODBC=1" ];
119
120 # see the top of the file for an explanation
121 passthru = {
122 fancyName = "MySQL";
123 driver = "lib/libmyodbc3-3.51.12.so";
124 };
125
126 meta = with lib; {
127 description = "MySQL ODBC database driver";
128 homepage = "https://dev.mysql.com/downloads/connector/odbc/";
129 license = licenses.gpl2;
130 platforms = platforms.linux;
131 broken = true;
132 };
133 };
134
135 sqlite = stdenv.mkDerivation rec {
136 pname = "sqlite-connector-odbc";
137 version = "0.9993";
138
139 src = fetchurl {
140 url = "http://www.ch-werner.de/sqliteodbc/sqliteodbc-${version}.tar.gz";
141 sha256 = "0dgsj28sc7f7aprmdd0n5a1rmcx6pv7170c8dfjl0x1qsjxim6hs";
142 };
143
144 buildInputs = [
145 unixODBC
146 sqlite
147 zlib
148 libxml2
149 ];
150
151 configureFlags = [
152 "--with-odbc=${unixODBC}"
153 "--with-sqlite3=${sqlite.dev}"
154 ];
155
156 installTargets = [ "install-3" ];
157
158 # move libraries to $out/lib where they're expected to be
159 postInstall = ''
160 mkdir -p "$out/lib"
161 mv "$out"/*.* "$out/lib"
162 '';
163
164 # see the top of the file for an explanation
165 passthru = {
166 fancyName = "SQLite";
167 driver = "lib/libsqlite3odbc.so";
168 };
169
170 meta = with lib; {
171 description = "ODBC driver for SQLite";
172 homepage = "http://www.ch-werner.de/sqliteodbc";
173 license = licenses.bsd2;
174 platforms = platforms.unix;
175 maintainers = with maintainers; [ vlstill ];
176 };
177 };
178
179 msodbcsql17 = stdenv.mkDerivation rec {
180 pname = "msodbcsql17";
181 version = "${versionMajor}.${versionMinor}.${versionAdditional}-1";
182
183 versionMajor = "17";
184 versionMinor = "7";
185 versionAdditional = "1.1";
186
187 src = fetchurl {
188 url = "https://packages.microsoft.com/debian/10/prod/pool/main/m/msodbcsql17/msodbcsql${versionMajor}_${version}_amd64.deb";
189 sha256 = "0vwirnp56jibm3qf0kmi4jnz1w7xfhnsfr8imr0c9hg6av4sk3a6";
190 };
191
192 nativeBuildInputs = [
193 dpkg
194 patchelf
195 ];
196
197 unpackPhase = "dpkg -x $src ./";
198 buildPhase = "";
199
200 installPhase = ''
201 mkdir -p $out
202 mkdir -p $out/lib
203 cp -r opt/microsoft/msodbcsql${versionMajor}/lib64 opt/microsoft/msodbcsql${versionMajor}/share $out/
204 '';
205
206 postFixup = ''
207 patchelf --set-rpath ${
208 lib.makeLibraryPath [
209 unixODBC
210 openssl
211 libkrb5
212 libuuid
213 stdenv.cc.cc
214 ]
215 } \
216 $out/lib/libmsodbcsql-${versionMajor}.${versionMinor}.so.${versionAdditional}
217 '';
218
219 # see the top of the file for an explanation
220 passthru = {
221 fancyName = "ODBC Driver ${versionMajor} for SQL Server";
222 driver = "lib/libmsodbcsql-${versionMajor}.${versionMinor}.so.${versionAdditional}";
223 };
224
225 meta = with lib; {
226 broken = stdenv.hostPlatform.isDarwin;
227 description = "ODBC Driver ${versionMajor} for SQL Server";
228 homepage = "https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017";
229 sourceProvenance = with sourceTypes; [ binaryNativeCode ];
230 license = licenses.unfree;
231 platforms = platforms.linux;
232 maintainers = with maintainers; [ spencerjanssen ];
233 };
234 };
235
236 msodbcsql18 = stdenv.mkDerivation (finalAttrs: {
237 pname = "msodbcsql${finalAttrs.versionMajor}";
238 version = "${finalAttrs.versionMajor}.${finalAttrs.versionMinor}.${finalAttrs.versionAdditional}${finalAttrs.versionSuffix}";
239
240 versionMajor = "18";
241 versionMinor = "1";
242 versionAdditional = "1.1";
243 versionSuffix = lib.optionalString stdenv.hostPlatform.isLinux "-1";
244
245 src = fetchurl {
246 url =
247 {
248 x86_64-linux = "https://packages.microsoft.com/debian/11/prod/pool/main/m/msodbcsql${finalAttrs.versionMajor}/msodbcsql${finalAttrs.versionMajor}_${finalAttrs.version}_amd64.deb";
249 aarch64-linux = "https://packages.microsoft.com/debian/11/prod/pool/main/m/msodbcsql${finalAttrs.versionMajor}/msodbcsql${finalAttrs.versionMajor}_${finalAttrs.version}_arm64.deb";
250 x86_64-darwin = "https://download.microsoft.com/download/6/4/0/64006503-51e3-44f0-a6cd-a9b757d0d61b/msodbcsql${finalAttrs.versionMajor}-${finalAttrs.version}-amd64.tar.gz";
251 aarch64-darwin = "https://download.microsoft.com/download/6/4/0/64006503-51e3-44f0-a6cd-a9b757d0d61b/msodbcsql${finalAttrs.versionMajor}-${finalAttrs.version}-arm64.tar.gz";
252 }
253 .${stdenv.system} or (throw "Unsupported system: ${stdenv.system}");
254 hash =
255 {
256 x86_64-linux = "sha256:1f0rmh1aynf1sqmjclbsyh2wz5jby0fixrwz71zp6impxpwvil52";
257 aarch64-linux = "sha256:0zphnbvkqdbkcv6lvv63p7pyl68h5bs2dy6vv44wm6bi89svms4a";
258 x86_64-darwin = "sha256:1fn80byn1yihflznxcm9cpj42mpllnz54apnk9n46vzm2ng2lj6d";
259 aarch64-darwin = "sha256:116xl8r2apr5b48jnq6myj9fwqs88yccw5176yfyzh4534fznj5x";
260 }
261 .${stdenv.system} or (throw "Unsupported system: ${stdenv.system}");
262 };
263
264 nativeBuildInputs =
265 if stdenv.hostPlatform.isDarwin then
266 [
267 # Fix up the names encoded into the dylib, and make them absolute.
268 fixDarwinDylibNames
269 ]
270 else
271 [
272 dpkg
273 patchelf
274 ];
275
276 unpackPhase = lib.optionalString stdenv.hostPlatform.isLinux ''
277 dpkg -x $src ./
278 '';
279
280 installPhase =
281 if stdenv.hostPlatform.isDarwin then
282 ''
283 mkdir -p $out
284 tar xf $src --strip-components=1 -C $out
285 ''
286 else
287 ''
288 mkdir -p $out
289 mkdir -p $out/lib
290 cp -r opt/microsoft/msodbcsql${finalAttrs.versionMajor}/lib64 opt/microsoft/msodbcsql${finalAttrs.versionMajor}/share $out/
291 '';
292
293 # Replace the hard-coded paths in the dylib with nixpkgs equivalents.
294 fixupPhase = lib.optionalString stdenv.hostPlatform.isDarwin ''
295 ${stdenv.cc.bintools.targetPrefix}install_name_tool \
296 -change /usr/lib/libiconv.2.dylib ${libiconv}/lib/libiconv.2.dylib \
297 -change /opt/homebrew/lib/libodbcinst.2.dylib ${unixODBC}/lib/libodbcinst.2.dylib \
298 $out/${finalAttrs.passthru.driver}
299 '';
300
301 postFixup = lib.optionalString stdenv.hostPlatform.isLinux ''
302 patchelf --set-rpath ${
303 lib.makeLibraryPath [
304 unixODBC
305 openssl
306 libkrb5
307 libuuid
308 stdenv.cc.cc
309 ]
310 } \
311 $out/${finalAttrs.passthru.driver}
312 '';
313
314 # see the top of the file for an explanation
315 passthru = {
316 fancyName = "ODBC Driver ${finalAttrs.versionMajor} for SQL Server";
317 driver = "lib/libmsodbcsql${
318 if stdenv.hostPlatform.isDarwin then
319 ".${finalAttrs.versionMajor}.dylib"
320 else
321 "-${finalAttrs.versionMajor}.${finalAttrs.versionMinor}.so.${finalAttrs.versionAdditional}"
322 }";
323 };
324
325 meta = with lib; {
326 description = finalAttrs.passthru.fancyName;
327 homepage = "https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16";
328 sourceProvenance = with sourceTypes; [ binaryNativeCode ];
329 platforms = platforms.unix;
330 license = licenses.unfree;
331 maintainers = with maintainers; [ SamirTalwar ];
332 };
333 });
334
335 redshift = stdenv.mkDerivation rec {
336 pname = "redshift-odbc";
337 version = "1.4.49.1000";
338
339 src = fetchurl {
340 url = "https://s3.amazonaws.com/redshift-downloads/drivers/odbc/${version}/AmazonRedshiftODBC-64-bit-${version}-1.x86_64.deb";
341 sha256 = "sha256-r5HvsZjB7+x+ClxtWoONkE1/NAbz90NbHfzxC6tf7jA=";
342 };
343
344 nativeBuildInputs = [ dpkg ];
345
346 unpackPhase = ''
347 dpkg -x $src src
348 cd src
349 '';
350
351 # `unixODBC` is loaded with `dlopen`, so `autoPatchElfHook` cannot see it, and `patchELF` phase would strip the manual patchelf. Thus:
352 # - Manually patchelf with `unixODCB` libraries
353 # - Disable automatic `patchELF` phase
354 installPhase = ''
355 mkdir -p $out/lib
356 cp opt/amazon/redshiftodbc/lib/64/* $out/lib
357 patchelf --set-rpath ${unixODBC}/lib/ $out/lib/libamazonredshiftodbc64.so
358 '';
359
360 dontPatchELF = true;
361
362 buildInputs = [ unixODBC ];
363
364 # see the top of the file for an explanation
365 passthru = {
366 fancyName = "Amazon Redshift (x64)";
367 driver = "lib/libamazonredshiftodbc64.so";
368 };
369
370 meta = with lib; {
371 broken = stdenv.hostPlatform.isDarwin;
372 description = "Amazon Redshift ODBC driver";
373 homepage = "https://docs.aws.amazon.com/redshift/latest/mgmt/configure-odbc-connection.html";
374 sourceProvenance = with sourceTypes; [ binaryNativeCode ];
375 license = licenses.unfree;
376 platforms = platforms.linux;
377 maintainers = with maintainers; [ sir4ur0n ];
378 };
379 };
380}