1{ lib, stdenv, fetchurl, zlib, readline, ncurses
2
3# for tests
4, python3Packages, sqldiff, sqlite-analyzer, tracker
5
6# uses readline & ncurses for a better interactive experience if set to true
7, interactive ? false
8# TODO: can be removed since 3.36 since it is the default now.
9, enableDeserialize ? false
10}:
11
12let
13 archiveVersion = import ./archive-version.nix lib;
14in
15
16stdenv.mkDerivation rec {
17 pname = "sqlite${lib.optionalString interactive "-interactive"}";
18 version = "3.41.2";
19
20 # nixpkgs-update: no auto update
21 # NB! Make sure to update ./tools.nix src (in the same directory).
22 src = fetchurl {
23 url = "https://sqlite.org/2023/sqlite-autoconf-${archiveVersion version}.tar.gz";
24 hash = "sha256-6YwQDdHaTjD6Rgdh2rfAuRpQt4XhZ/jFesxGUU+ulJk=";
25 };
26
27 outputs = [ "bin" "dev" "out" ];
28 separateDebugInfo = stdenv.isLinux;
29
30 buildInputs = [ zlib ] ++ lib.optionals interactive [ readline ncurses ];
31
32 # required for aarch64 but applied for all arches for simplicity
33 preConfigure = ''
34 patchShebangs configure
35 '';
36
37 configureFlags = [ "--enable-threadsafe" ] ++ lib.optional interactive "--enable-readline";
38
39 env.NIX_CFLAGS_COMPILE = toString ([
40 "-DSQLITE_ENABLE_COLUMN_METADATA"
41 "-DSQLITE_ENABLE_DBSTAT_VTAB"
42 "-DSQLITE_ENABLE_JSON1"
43 "-DSQLITE_ENABLE_FTS3"
44 "-DSQLITE_ENABLE_FTS3_PARENTHESIS"
45 "-DSQLITE_ENABLE_FTS3_TOKENIZER"
46 "-DSQLITE_ENABLE_FTS4"
47 "-DSQLITE_ENABLE_FTS5"
48 "-DSQLITE_ENABLE_RTREE"
49 "-DSQLITE_ENABLE_STMT_SCANSTATUS"
50 "-DSQLITE_ENABLE_UNLOCK_NOTIFY"
51 "-DSQLITE_SOUNDEX"
52 "-DSQLITE_SECURE_DELETE"
53 "-DSQLITE_MAX_VARIABLE_NUMBER=250000"
54 "-DSQLITE_MAX_EXPR_DEPTH=10000"
55 ] ++ lib.optionals enableDeserialize [
56 # Can be removed in v3.36+, as this will become the default
57 "-DSQLITE_ENABLE_DESERIALIZE"
58 ]);
59
60 # Test for features which may not be available at compile time
61 preBuild = ''
62 # Use pread(), pread64(), pwrite(), pwrite64() functions for better performance if they are available.
63 if cc -Werror=implicit-function-declaration -x c - -o "$TMPDIR/pread_pwrite_test" <<< \
64 ''$'#include <unistd.h>\nint main()\n{\n pread(0, NULL, 0, 0);\n pwrite(0, NULL, 0, 0);\n return 0;\n}'; then
65 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -DUSE_PREAD"
66 fi
67 if cc -Werror=implicit-function-declaration -x c - -o "$TMPDIR/pread64_pwrite64_test" <<< \
68 ''$'#include <unistd.h>\nint main()\n{\n pread64(0, NULL, 0, 0);\n pwrite64(0, NULL, 0, 0);\n return 0;\n}'; then
69 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -DUSE_PREAD64"
70 elif cc -D_LARGEFILE64_SOURCE -Werror=implicit-function-declaration -x c - -o "$TMPDIR/pread64_pwrite64_test" <<< \
71 ''$'#include <unistd.h>\nint main()\n{\n pread64(0, NULL, 0, 0);\n pwrite64(0, NULL, 0, 0);\n return 0;\n}'; then
72 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -DUSE_PREAD64 -D_LARGEFILE64_SOURCE"
73 fi
74
75 # Necessary for FTS5 on Linux
76 export NIX_LDFLAGS="$NIX_LDFLAGS -lm"
77
78 echo ""
79 echo "NIX_CFLAGS_COMPILE = $NIX_CFLAGS_COMPILE"
80 echo ""
81 '';
82
83 postInstall = ''
84 # Do not contaminate dependent libtool-based projects with sqlite dependencies.
85 sed -i $out/lib/libsqlite3.la -e "s/dependency_libs=.*/dependency_libs='''/"
86 '';
87
88 doCheck = false; # fails to link against tcl
89
90 passthru.tests = {
91 inherit (python3Packages) sqlalchemy;
92 inherit sqldiff sqlite-analyzer tracker;
93 };
94
95 meta = with lib; {
96 changelog = "https://www.sqlite.org/releaselog/${lib.replaceStrings [ "." ] [ "_" ] version}.html";
97 description = "A self-contained, serverless, zero-configuration, transactional SQL database engine";
98 downloadPage = "https://sqlite.org/download.html";
99 homepage = "https://www.sqlite.org/";
100 license = licenses.publicDomain;
101 mainProgram = "sqlite3";
102 maintainers = with maintainers; [ eelco np ];
103 platforms = platforms.unix ++ platforms.windows;
104 };
105}