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 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 # replace invalid symlinks when executable suffix is .exe
115 postInstall = lib.optionalString stdenv.hostPlatform.isCygwin ''
116 for link in unzstd zstdcat zstdmt; do
117 ln -sf zstd.exe $bin/bin/$link
118 done
119 '';
120
121 outputs = [
122 "bin"
123 "dev"
124 ]
125 ++ lib.optional stdenv.hostPlatform.isUnix "man"
126 ++ [ "out" ];
127
128 passthru = {
129 updateScript = nix-update-script { };
130 tests = {
131
132 # Reverse dependencies
133
134 inherit libarchive rocksdb arrow-cpp;
135 libzip = libzip.override { withZstd = true; };
136 curl = curl.override { zstdSupport = true; };
137 python-zstd = python3Packages.zstd;
138 haskell-zstd = haskellPackages.zstd;
139 haskell-hs-zstd = haskellPackages.hs-zstd;
140
141 # Package tests (coherent with overrides)
142
143 pkg-config = testers.hasPkgConfigModules { package = finalAttrs.finalPackage; };
144 };
145 };
146
147 meta = {
148 description = "Zstandard real-time compression algorithm";
149 longDescription = ''
150 Zstd, short for Zstandard, is a fast lossless compression algorithm,
151 targeting real-time compression scenarios at zlib-level compression
152 ratio. Zstd can also offer stronger compression ratio at the cost of
153 compression speed. Speed/ratio trade-off is configurable by small
154 increment, to fit different situations. Note however that decompression
155 speed is preserved and remain roughly the same at all settings, a
156 property shared by most LZ compression algorithms, such as zlib.
157 '';
158 homepage = "https://facebook.github.io/zstd/";
159 changelog = "https://github.com/facebook/zstd/blob/v${finalAttrs.version}/CHANGELOG";
160 license = with lib.licenses; [ bsd3 ]; # Or, at your opinion, GPL-2.0-only.
161 mainProgram = "zstd";
162 platforms = lib.platforms.all;
163 maintainers = [ ];
164 pkgConfigModules = [ "libzstd" ];
165 };
166})