1{ lib, stdenv, fetchFromGitHub, cmake, bash, gnugrep
2, fixDarwinDylibNames
3, file
4, fetchpatch
5, legacySupport ? false
6, static ? stdenv.hostPlatform.isStatic
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.5";
25
26 src = fetchFromGitHub {
27 owner = "facebook";
28 repo = "zstd";
29 rev = "v${version}";
30 sha256 = "sha256-tHHHIsQU7vJySrVhJuMKUSq11MzkmC+Pcsj00uFJdnQ=";
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 LDFLAGS = lib.optionalString stdenv.hostPlatform.isRiscV "-latomic";
54
55 cmakeFlags = lib.attrsets.mapAttrsToList
56 (name: value: "-DZSTD_${name}:BOOL=${if value then "ON" else "OFF"}") {
57 BUILD_SHARED = !static;
58 BUILD_STATIC = static;
59 BUILD_CONTRIB = buildContrib;
60 PROGRAMS_LINK_SHARED = !static;
61 LEGACY_SUPPORT = legacySupport;
62 BUILD_TESTS = doCheck;
63 };
64
65 cmakeDir = "../build/cmake";
66 dontUseCmakeBuildDir = true;
67 preConfigure = ''
68 mkdir -p build_ && cd $_
69 '';
70
71 nativeCheckInputs = [ file ];
72 inherit doCheck;
73 checkPhase = ''
74 runHook preCheck
75 # Patch shebangs for playTests
76 patchShebangs ../programs/zstdgrep
77 ctest -R playTests # The only relatively fast test.
78 runHook postCheck
79 '';
80
81 preInstall = ''
82 mkdir -p $bin/bin
83 substituteInPlace ../programs/zstdgrep \
84 --replace ":-grep" ":-${gnugrep}/bin/grep" \
85 --replace ":-zstdcat" ":-$bin/bin/zstdcat"
86
87 substituteInPlace ../programs/zstdless \
88 --replace "zstdcat" "$bin/bin/zstdcat"
89 '' + lib.optionalString buildContrib (
90 ''
91 cp contrib/pzstd/pzstd $bin/bin/pzstd
92 '' + lib.optionalString stdenv.isDarwin ''
93 install_name_tool -change @rpath/libzstd.1.dylib $out/lib/libzstd.1.dylib $bin/bin/pzstd
94 ''
95 );
96
97 outputs = [ "bin" "dev" ]
98 ++ lib.optional stdenv.hostPlatform.isUnix "man"
99 ++ [ "out" ];
100
101 passthru = {
102 updateScript = nix-update-script { };
103 tests = {
104 inherit libarchive rocksdb arrow-cpp;
105 libzip = libzip.override { withZstd = true; };
106 curl = curl.override { zstdSupport = true; };
107 python-zstd = python3Packages.zstd;
108 haskell-zstd = haskellPackages.zstd;
109 haskell-hs-zstd = haskellPackages.hs-zstd;
110 };
111 };
112
113 meta = with lib; {
114 description = "Zstandard real-time compression algorithm";
115 longDescription = ''
116 Zstd, short for Zstandard, is a fast lossless compression algorithm,
117 targeting real-time compression scenarios at zlib-level compression
118 ratio. Zstd can also offer stronger compression ratio at the cost of
119 compression speed. Speed/ratio trade-off is configurable by small
120 increment, to fit different situations. Note however that decompression
121 speed is preserved and remain roughly the same at all settings, a
122 property shared by most LZ compression algorithms, such as zlib.
123 '';
124 homepage = "https://facebook.github.io/zstd/";
125 changelog = "https://github.com/facebook/zstd/blob/v${version}/CHANGELOG";
126 license = with licenses; [ bsd3 ]; # Or, at your opinion, GPL-2.0-only.
127
128 platforms = platforms.all;
129 maintainers = with maintainers; [ orivej ];
130 };
131}