1{ lib, stdenv, fetchFromGitHub, cmake, bash, gnugrep
2, fixDarwinDylibNames
3, file
4, legacySupport ? false
5, static ? stdenv.hostPlatform.isStatic # generates static libraries *only*
6, enableStatic ? static
7# these need to be ran on the host, thus disable when cross-compiling
8, buildContrib ? stdenv.hostPlatform == stdenv.buildPlatform
9, doCheck ? stdenv.hostPlatform == stdenv.buildPlatform
10, nix-update-script
11
12# for passthru.tests
13, libarchive
14, rocksdb
15, arrow-cpp
16, libzip
17, curl
18, python3Packages
19, haskellPackages
20}:
21
22stdenv.mkDerivation rec {
23 pname = "zstd";
24 version = "1.5.6";
25
26 src = fetchFromGitHub {
27 owner = "facebook";
28 repo = "zstd";
29 rev = "v${version}";
30 hash = "sha256-qcd92hQqVBjMT3hyntjcgk29o9wGQsg5Hg7HE5C0UNc=";
31 };
32
33 nativeBuildInputs = [ cmake ]
34 ++ lib.optional stdenv.isDarwin fixDarwinDylibNames;
35 buildInputs = lib.optional stdenv.hostPlatform.isUnix bash;
36
37 patches = [
38 # This patches makes sure we do not attempt to use the MD5 implementation
39 # of the host platform when running the tests
40 ./playtests-darwin.patch
41 ];
42
43 postPatch = lib.optionalString (!static) ''
44 substituteInPlace build/cmake/CMakeLists.txt \
45 --replace 'message(SEND_ERROR "You need to build static library to build tests")' ""
46 substituteInPlace build/cmake/tests/CMakeLists.txt \
47 --replace 'libzstd_static' 'libzstd_shared'
48 sed -i \
49 "1aexport ${lib.optionalString stdenv.isDarwin "DY"}LD_LIBRARY_PATH=$PWD/build_/lib" \
50 tests/playTests.sh
51 '';
52
53 cmakeFlags = lib.attrsets.mapAttrsToList
54 (name: value: "-DZSTD_${name}:BOOL=${if value then "ON" else "OFF"}") {
55 BUILD_SHARED = !static;
56 BUILD_STATIC = enableStatic;
57 BUILD_CONTRIB = buildContrib;
58 PROGRAMS_LINK_SHARED = !static;
59 LEGACY_SUPPORT = legacySupport;
60 BUILD_TESTS = doCheck;
61 };
62
63 cmakeDir = "../build/cmake";
64 dontUseCmakeBuildDir = true;
65 preConfigure = ''
66 mkdir -p build_ && cd $_
67 '';
68
69 nativeCheckInputs = [ file ];
70 inherit doCheck;
71 checkPhase = ''
72 runHook preCheck
73 # Patch shebangs for playTests
74 patchShebangs ../programs/zstdgrep
75 ctest -R playTests # The only relatively fast test.
76 runHook postCheck
77 '';
78
79 preInstall = ''
80 mkdir -p $bin/bin
81 substituteInPlace ../programs/zstdgrep \
82 --replace ":-grep" ":-${gnugrep}/bin/grep" \
83 --replace ":-zstdcat" ":-$bin/bin/zstdcat"
84
85 substituteInPlace ../programs/zstdless \
86 --replace "zstdcat" "$bin/bin/zstdcat"
87 '' + lib.optionalString buildContrib (
88 ''
89 cp contrib/pzstd/pzstd $bin/bin/pzstd
90 '' + lib.optionalString stdenv.isDarwin ''
91 install_name_tool -change @rpath/libzstd.1.dylib $out/lib/libzstd.1.dylib $bin/bin/pzstd
92 ''
93 );
94
95 outputs = [ "bin" "dev" ]
96 ++ lib.optional stdenv.hostPlatform.isUnix "man"
97 ++ [ "out" ];
98
99 passthru = {
100 updateScript = nix-update-script { };
101 tests = {
102 inherit libarchive rocksdb arrow-cpp;
103 libzip = libzip.override { withZstd = true; };
104 curl = curl.override { zstdSupport = true; };
105 python-zstd = python3Packages.zstd;
106 haskell-zstd = haskellPackages.zstd;
107 haskell-hs-zstd = haskellPackages.hs-zstd;
108 };
109 };
110
111 meta = with lib; {
112 description = "Zstandard real-time compression algorithm";
113 longDescription = ''
114 Zstd, short for Zstandard, is a fast lossless compression algorithm,
115 targeting real-time compression scenarios at zlib-level compression
116 ratio. Zstd can also offer stronger compression ratio at the cost of
117 compression speed. Speed/ratio trade-off is configurable by small
118 increment, to fit different situations. Note however that decompression
119 speed is preserved and remain roughly the same at all settings, a
120 property shared by most LZ compression algorithms, such as zlib.
121 '';
122 homepage = "https://facebook.github.io/zstd/";
123 changelog = "https://github.com/facebook/zstd/blob/v${version}/CHANGELOG";
124 license = with licenses; [ bsd3 ]; # Or, at your opinion, GPL-2.0-only.
125
126 platforms = platforms.all;
127 maintainers = with maintainers; [ orivej ];
128 };
129}