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