nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
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}:
28
29stdenv.mkDerivation rec {
30 pname = "zstd";
31 version = "1.5.7";
32
33 src = fetchFromGitHub {
34 owner = "facebook";
35 repo = "zstd";
36 rev = "v${version}";
37 hash = "sha256-tNFWIT9ydfozB8dWcmTMuZLCQmQudTFJIkSr0aG7S44=";
38 };
39
40 nativeBuildInputs = [ cmake ] ++ lib.optional stdenv.hostPlatform.isDarwin fixDarwinDylibNames;
41 buildInputs = lib.optional stdenv.hostPlatform.isUnix bashNonInteractive;
42
43 patches = [
44 # This patches makes sure we do not attempt to use the MD5 implementation
45 # of the host platform when running the tests
46 ./playtests-darwin.patch
47
48 # Pull missing manpages update:
49 # https://github.com/facebook/zstd/pull/4302
50 # TODO: remove with 1.5.8 release
51 (fetchpatch {
52 name = "man-fix.patch";
53 url = "https://github.com/facebook/zstd/commit/6af3842118ea5325480b403213b2a9fbed3d3d74.patch";
54 hash = "sha256-i+iv+owUXbKU3UtZBsjfj86kFB3TDlpcVDNsDX8dyZE=";
55 })
56 ];
57
58 postPatch = lib.optionalString (!static) ''
59 substituteInPlace build/cmake/CMakeLists.txt \
60 --replace 'message(SEND_ERROR "You need to build static library to build tests")' ""
61 substituteInPlace build/cmake/tests/CMakeLists.txt \
62 --replace 'libzstd_static' 'libzstd_shared'
63 sed -i \
64 "1aexport ${lib.optionalString stdenv.hostPlatform.isDarwin "DY"}LD_LIBRARY_PATH=$PWD/build_/lib" \
65 tests/playTests.sh
66 '';
67
68 cmakeFlags =
69 lib.attrsets.mapAttrsToList (name: value: "-DZSTD_${name}:BOOL=${if value then "ON" else "OFF"}")
70 {
71 BUILD_SHARED = !static;
72 BUILD_STATIC = enableStatic;
73 BUILD_CONTRIB = buildContrib;
74 PROGRAMS_LINK_SHARED = !static;
75 LEGACY_SUPPORT = legacySupport;
76 BUILD_TESTS = doCheck;
77 };
78
79 cmakeDir = "../build/cmake";
80 dontUseCmakeBuildDir = true;
81 preConfigure = ''
82 mkdir -p build_ && cd $_
83 '';
84
85 nativeCheckInputs = [ file ];
86 inherit doCheck;
87 checkPhase = ''
88 runHook preCheck
89 # Patch shebangs for playTests
90 patchShebangs ../programs/zstdgrep
91 ctest -R playTests # The only relatively fast test.
92 runHook postCheck
93 '';
94
95 preInstall =
96 ''
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 [
116 "bin"
117 "dev"
118 ]
119 ++ lib.optional stdenv.hostPlatform.isUnix "man"
120 ++ [ "out" ];
121
122 passthru = {
123 updateScript = nix-update-script { };
124 tests = {
125 inherit libarchive rocksdb arrow-cpp;
126 libzip = libzip.override { withZstd = true; };
127 curl = curl.override { zstdSupport = true; };
128 python-zstd = python3Packages.zstd;
129 haskell-zstd = haskellPackages.zstd;
130 haskell-hs-zstd = haskellPackages.hs-zstd;
131 };
132 };
133
134 meta = with lib; {
135 description = "Zstandard real-time compression algorithm";
136 longDescription = ''
137 Zstd, short for Zstandard, is a fast lossless compression algorithm,
138 targeting real-time compression scenarios at zlib-level compression
139 ratio. Zstd can also offer stronger compression ratio at the cost of
140 compression speed. Speed/ratio trade-off is configurable by small
141 increment, to fit different situations. Note however that decompression
142 speed is preserved and remain roughly the same at all settings, a
143 property shared by most LZ compression algorithms, such as zlib.
144 '';
145 homepage = "https://facebook.github.io/zstd/";
146 changelog = "https://github.com/facebook/zstd/blob/v${version}/CHANGELOG";
147 license = with licenses; [ bsd3 ]; # Or, at your opinion, GPL-2.0-only.
148 mainProgram = "zstd";
149 platforms = platforms.all;
150 maintainers = with maintainers; [ orivej ];
151 };
152}