This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
flake.nix
303 lines (273 loc) · 11.5 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# Copyright (c) 2022 Espresso Systems (espressosys.com)
# This file is part of the Configurable Asset Privacy for Ethereum (CAPE) library.
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
{
description = "A devShell example";
inputs.nixpkgs.url = "github:nixos/nixpkgs";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.flake-compat.url = "github:edolstra/flake-compat";
inputs.flake-compat.flake = false;
inputs.rust-overlay.url = "github:oxalica/rust-overlay";
inputs.pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
# See https://github.com/cachix/pre-commit-hooks.nix/pull/122
inputs.pre-commit-hooks.inputs.flake-utils.follows = "flake-utils";
inputs.pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
outputs =
{ self
, nixpkgs
, flake-utils
, flake-compat
, rust-overlay
, pre-commit-hooks
, ...
}:
flake-utils.lib.eachDefaultSystem (system:
let
# MUSL pkgs
muslPkgs = import nixpkgs {
localSystem = "x86_64-linux";
crossSystem = { config = "x86_64-unknown-linux-musl"; };
};
opensslMusl = muslPkgs.openssl.override { static = true; };
muslPythonEnv = muslPkgs.poetry2nix.mkPoetryEnv { projectDir = ./.; };
muslRustDeps = with muslPkgs; [
pkgconfig
curl
opensslMusl.dev
opensslMusl.out
];
stableMuslRustToolchain = pkgs.rust-bin.stable.latest.minimal.override {
extensions = [ "rustfmt" "clippy" "llvm-tools-preview" "rust-src" ];
targets = [ "x86_64-unknown-linux-musl" ];
};
# glibc pkgs
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
mySolc = pkgs.solc;
pythonEnv = pkgs.poetry2nix.mkPoetryEnv { projectDir = ./.; };
myPython = with pkgs; [ poetry pythonEnv ];
stableRustToolchain = pkgs.rust-bin.stable.latest.minimal.override {
extensions = [ "rustfmt" "clippy" "llvm-tools-preview" "rust-src" ];
};
rustDeps = with pkgs;
[
pkgconfig
openssl
curl
plantuml
stableRustToolchain
cargo-edit
cargo-sort
] ++ lib.optionals stdenv.isDarwin [
# required to compile ethers-rs
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.CoreFoundation
# https://github.com/NixOS/nixpkgs/issues/126182
libiconv
] ++ lib.optionals (stdenv.system != "aarch64-darwin") [
cargo-watch # broken: https://github.com/NixOS/nixpkgs/issues/146349
];
# nixWithFlakes allows pre v2.4 nix installations to use flake commands (like `nix flake update`)
nixWithFlakes = pkgs.writeShellScriptBin "nix" ''
exec ${pkgs.nixFlakes}/bin/nix --experimental-features "nix-command flakes" "$@"
'';
muslNixWithFlakes = pkgs.writeShellScriptBin "nix" ''
exec ${muslPkgs.nixFlakes}/bin/nix --experimental-features "nix-command flakes" "$@"
'';
in
{
checks = {
pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
lint-solidity = {
enable = true;
files = "^contracts/contracts/";
entry = "lint-solidity";
types = [ "solidity" ];
};
check-format = {
enable = true;
entry = "treefmt --fail-on-change";
};
# The hook "clippy" that ships with nix-precommit-hooks is outdated.
cargo-clippy = {
enable = true;
description = "Lint Rust code.";
entry = "cargo-clippy --workspace -- -D warnings" + pkgs.lib.strings.concatMapStrings (lint: " -A clippy::${lint} ") [
# These lints are new from 1.63 to 1.66.1, we should fix the code and remove the allowed lints.
"bool-to-int-with-if"
"explicit-auto-deref"
"needless_borrow"
"partialeq-to-none"
"result_large_err"
"unnecessary_cast"
];
files = "\\.rs$";
pass_filenames = false;
};
cargo-sort = {
enable = true;
description = "Ensure Cargo.toml are sorted";
entry = "cargo sort -w";
pass_filenames = false;
};
license-header-c-style = {
enable = true;
description =
"Ensure files with c-style comments have license header";
entry = ''
insert_license --license-filepath .license-header --comment-style "//"'';
types_or = [ "rust" "ts" "javascript" ];
excludes = [
"bindings/mod\\.rs" # generated file
];
pass_filenames = true;
};
license-header-solidity = {
enable = true;
description = "Ensure solidity files have license header";
entry = ''
insert_license --license-filepath .license-header-solidity --comment-style "//"'';
types = [ "solidity" ];
pass_filenames = true;
};
license-header-hash = {
enable = true;
description =
"Ensure files with hash style comments have license header";
entry = ''
insert_license --license-filepath .license-header --comment-style "#"'';
types_or = [ "bash" "python" "toml" "nix" ];
excludes = [ "poetry.lock" ];
pass_filenames = true;
};
license-header-html = {
enable = true;
description = "Ensure markdown files have license header";
entry = ''
insert_license --license-filepath .license-header --comment-style "<!--| ~| -->"'';
types_or = [ "markdown" ];
pass_filenames = true;
};
make-doc = {
enable = true;
description = "Ensure documentation builds";
entry = "make-doc";
pass_filenames = false;
types = [ "solidity" ];
};
};
};
};
# A nix base image
packages.docker = pkgs.dockerTools.buildImage {
name = "nix-base-docker";
tag = "latest";
contents = with pkgs; [ curl coreutils bashInteractive cacert ];
config = { Env = [ "SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt" ]; };
};
devShell = pkgs.mkShell {
buildInputs = with pkgs;
[
nixWithFlakes
(go-ethereum.overrideAttrs (old: {
# Set deployed bytecode size limit to 32kB (which is also the tx size limit)
# in order to deploy the TestCAPE contracts that currently exceeds the
# 24kB size limit.
patchPhase = old.patchPhase or "" + ''
substituteInPlace params/protocol_params.go --replace "MaxCodeSize = 24576" "MaxCodeSize = 32768"
'';
}))
nodePackages.pnpm
mySolc
hivemind # process runner
nodejs-16_x # nodejs
jq
entr # watch files for changes, for example: ls contracts/*.sol | entr -c hardhat compile
treefmt # multi language formatter
nixpkgs-fmt
git # required for pre-commit hook installation
netcat-gnu # only used to check for open ports
cacert
mdbook # make-doc, documentation generation
moreutils # includes `ts`, used to add timestamps on CI
] ++ myPython ++ rustDeps;
RUST_SRC_PATH = "${stableRustToolchain}/lib/rustlib/src/rust/library";
RUST_BACKTRACE = 1;
RUST_LOG = "info";
SOLCX_BINARY_PATH = "${mySolc}/bin";
SOLC_VERSION = mySolc.version;
SOLC_PATH = "${mySolc}/bin/solc";
SOLC_OPTIMIZER_RUNS = "1000";
shellHook = ''
echo "Ensuring node dependencies are installed"
pnpm --recursive install
if [ ! -f .env ]; then
echo "Copying .env.sample to .env"
cp .env.sample .env
fi
echo "Exporting all vars in .env file"
set -a; source .env; set +a;
# on mac os `bin/pwd -P` returns the canonical path on case insensitive file-systems
my_pwd=$(/bin/pwd -P 2> /dev/null || pwd)
export CONTRACTS_DIR=''${my_pwd}/contracts
export HARDHAT_CONFIG=$CONTRACTS_DIR/hardhat.config.ts
export PATH=''${my_pwd}/node_modules/.bin:$PATH
export PATH=$CONTRACTS_DIR/node_modules/.bin:$PATH
export PATH=''${my_pwd}/bin:$PATH
export WALLET=''${my_pwd}/wallet
git config --local blame.ignoreRevsFile .git-blame-ignore-revs
''
# install pre-commit hooks
+ self.checks.${system}.pre-commit-check.shellHook;
};
devShells = {
# shell with all needed tools to build
# for a statically linked x86_64-unknown-linux-musl target.
# when running cargo build
staticShell = muslPkgs.mkShell {
# build as much as possible with musl
nativeBuildInputs = with muslPkgs;
[
muslNixWithFlakes
go-ethereum
mySolc
hivemind # process runner
] ++ muslRustDeps;
SOLCX_BINARY_PATH = "${mySolc}/bin";
SOLC_VERSION = mySolc.version;
SOLC_PATH = "${mySolc}/bin/solc";
SOLC_OPTIMIZER_RUNS = "1000";
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = "${muslPkgs.llvmPackages_latest.lld}/bin/lld";
RUSTFLAGS = "-C target-feature=+crt-static -L${opensslMusl.out}/lib/";
OPENSSL_STATIC = "true";
OPENSSL_INCLUDE_DIR = "${opensslMusl.dev}/include/";
OPENSSL_LIB_DIR = "${opensslMusl.dev}/lib/";
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
shellHook = ''
echo "Ensuring node dependencies are installed"
${pkgs.nodePackages.pnpm}/bin/pnpm --recursive install
if [ ! -f .env ]; then
echo "Copying .env.sample to .env"
cp .env.sample .env
fi
echo "Exporting all vars in .env file"
set -a; source .env; set +a;
# on mac os `bin/pwd -P` returns the canonical path on case insensitive file-systems
my_pwd=$(/bin/pwd -P 2> /dev/null || pwd)
export CONTRACTS_DIR=''${my_pwd}/contracts
export HARDHAT_CONFIG=$CONTRACTS_DIR/hardhat.config.ts
export PATH=''${my_pwd}/node_modules/.bin:$PATH
export PATH=$CONTRACTS_DIR/node_modules/.bin:$PATH
export PATH=''${my_pwd}/bin:$PATH
export WALLET=''${my_pwd}/wallet
export PATH="${pkgs.nodePackages.pnpm}/bin:${pkgs.nodejs-16_x}/bin:${stableMuslRustToolchain}/bin:${pkgs.gcc}/bin/:$PATH"
'';
};
};
});
}