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