forked from matter-labs/zksync-era
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
230 lines (201 loc) · 7.63 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
###################################################################################################
#
# To build the rust components with this flake, run:
# $ nix build .#cargoDeps
# set `cargoHash` below to the result of the build
# then
# $ nix build .#zksync_server
# or
# $ nix build .#zksync_server.contract_verifier
# $ nix build .#zksync_server.external_node
# $ nix build .#zksync_server.server
# $ nix build .#zksync_server.snapshots_creator
# $ nix build .#zksync_server.block_reverter
#
# To enter the development shell, run:
# $ nix develop --impure
#
# To vendor the dependencies manually, run:
# $ nix shell .#cargo-vendor -c cargo vendor --no-merge-sources
#
###################################################################################################
{
description = "zkSync-era";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
};
outputs = { self, nixpkgs, flake-utils, rust-overlay }:
flake-utils.lib.eachDefaultSystem (system:
let
###########################################################################################
# This changes every time `Cargo.lock` changes. Set to `null` to force re-vendoring
cargoHash = null;
# cargoHash = "sha256-LloF3jrvFkOlZ2lQXB+/sFthfJQLLu8BvHBE88gRvFc=";
###########################################################################################
officialRelease = false;
versionSuffix =
if officialRelease
then ""
else "pre${builtins.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}_${self.shortRev or "dirty"}";
pkgs = import nixpkgs { inherit system; overlays = [ rust-overlay.overlays.default ]; };
# patched version of cargo to support `cargo vendor` for vendoring dependencies
# see https://github.com/matter-labs/zksync-era/issues/1086
# used as `cargo vendor --no-merge-sources`
cargo-vendor = pkgs.rustPlatform.buildRustPackage rec {
pname = "cargo-vendor";
version = "0.78.0";
src = pkgs.fetchFromGitHub {
owner = "haraldh";
repo = "cargo";
rev = "3ee1557d2bd95ca9d0224c5dbf1d1e2d67186455";
hash = "sha256-A8xrOG+NmF8dQ7tA9I2vJSNHlYxsH44ZRXdptLblCXk=";
};
doCheck = false;
cargoHash = "sha256-LtuNtdoX+FF/bG5LQc+L2HkFmgCtw5xM/m0/0ShlX2s=";
nativeBuildInputs = [
pkgs.pkg-config
pkgs.rustPlatform.bindgenHook
];
buildInputs = [
pkgs.openssl
];
};
# custom import-cargo-lock to import Cargo.lock file and vendor dependencies
# see https://github.com/matter-labs/zksync-era/issues/1086
import-cargo-lock = { lib, cacert, runCommand }: { src, cargoHash ? null } @ args:
runCommand "import-cargo-lock"
{
inherit src;
nativeBuildInputs = [ cargo-vendor cacert ];
preferLocalBuild = true;
outputHashMode = "recursive";
outputHashAlgo = "sha256";
outputHash = if cargoHash != null then cargoHash else lib.fakeSha256;
}
''
mkdir -p $out/.cargo
mkdir -p $out/cargo-vendor-dir
HOME=$(pwd)
pushd ${src}
HOME=$HOME cargo vendor --no-merge-sources $out/cargo-vendor-dir > $out/.cargo/config
sed -i -e "s#$out#import-cargo-lock#g" $out/.cargo/config
cp $(pwd)/Cargo.lock $out/Cargo.lock
popd
''
;
cargoDeps = pkgs.buildPackages.callPackage import-cargo-lock { } { inherit src; inherit cargoHash; };
rustVersion = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain;
stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.clangStdenv;
rustPlatform = (pkgs.makeRustPlatform {
cargo = rustVersion;
rustc = rustVersion;
inherit stdenv;
});
zksync_server_cargoToml = (builtins.fromTOML (builtins.readFile ./core/bin/zksync_server/Cargo.toml));
hardeningEnable = [ "fortify3" "pie" "relro" ];
src = with pkgs.lib.fileset; toSource {
root = ./.;
fileset = unions [
./Cargo.lock
./Cargo.toml
./core
./prover
./.github/release-please/manifest.json
];
};
zksync_server = with pkgs; stdenv.mkDerivation {
pname = "zksync";
version = zksync_server_cargoToml.package.version + versionSuffix;
updateAutotoolsGnuConfigScriptsPhase = ":";
nativeBuildInputs = [
pkg-config
rustPlatform.bindgenHook
rustPlatform.cargoSetupHook
rustPlatform.cargoBuildHook
rustPlatform.cargoInstallHook
];
buildInputs = [
libclang
openssl
snappy.dev
lz4.dev
bzip2.dev
];
inherit src;
cargoBuildFlags = "--all";
cargoBuildType = "release";
inherit cargoDeps;
inherit hardeningEnable;
outputs = [
"out"
"contract_verifier"
"external_node"
"server"
"snapshots_creator"
"block_reverter"
];
postInstall = ''
mkdir -p $out/nix-support
for i in $outputs; do
[[ $i == "out" ]] && continue
mkdir -p "''${!i}/bin"
echo "''${!i}" >> $out/nix-support/propagated-user-env-packages
if [[ -e "$out/bin/zksync_$i" ]]; then
mv "$out/bin/zksync_$i" "''${!i}/bin"
else
mv "$out/bin/$i" "''${!i}/bin"
fi
done
mkdir -p $external_node/nix-support
echo "block_reverter" >> $external_node/nix-support/propagated-user-env-packages
mv $out/bin/merkle_tree_consistency_checker $server/bin
mkdir -p $server/nix-support
echo "block_reverter" >> $server/nix-support/propagated-user-env-packages
'';
};
in
{
formatter = pkgs.nixpkgs-fmt;
packages = {
inherit zksync_server;
default = zksync_server;
inherit cargo-vendor;
inherit cargoDeps;
};
devShells = with pkgs; {
default = pkgs.mkShell.override { inherit stdenv; } {
inputsFrom = [ zksync_server ];
packages = [
docker-compose
nodejs
yarn
axel
postgresql
python3
solc
sqlx-cli
mold
];
inherit hardeningEnable;
shellHook = ''
export ZKSYNC_HOME=$PWD
export PATH=$ZKSYNC_HOME/bin:$PATH
export RUSTFLAGS='-C link-arg=-fuse-ld=${pkgs.mold}/bin/mold'
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER="clang"
if [ "x$NIX_LD" = "x" ]; then
export NIX_LD="$ZK_NIX_LD"
fi
if [ "x$NIX_LD_LIBRARY_PATH" = "x" ]; then
export NIX_LD_LIBRARY_PATH="$ZK_NIX_LD_LIBRARY_PATH"
else
export NIX_LD_LIBRARY_PATH="$NIX_LD_LIBRARY_PATH:$ZK_NIX_LD_LIBRARY_PATH"
fi
'';
ZK_NIX_LD_LIBRARY_PATH = lib.makeLibraryPath [ ];
ZK_NIX_LD = builtins.readFile "${clangStdenv.cc}/nix-support/dynamic-linker";
};
};
});
}