-
Notifications
You must be signed in to change notification settings - Fork 44
/
flake.nix
221 lines (201 loc) · 9.79 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
{
description = "hevm";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
foundry.url = "github:shazow/foundry.nix/monthly";
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
solidity = {
url = "github:ethereum/solidity/1c8745c54a239d20b6fb0f79a8bd2628d779b27e";
flake = false;
};
ethereum-tests = {
url = "github:ethereum/tests/v12.2";
flake = false;
};
cabal-head = {
url = "github:haskell/cabal";
flake = false;
};
forge-std = {
url = "github:foundry-rs/forge-std";
flake = false;
};
};
outputs = { self, nixpkgs, flake-utils, solidity, forge-std, ethereum-tests, foundry, cabal-head, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = (import nixpkgs { inherit system; config = { allowBroken = true; }; });
testDeps = with pkgs; [
go-ethereum
solc
z3
cvc5
git
] ++ lib.optional (!(pkgs.stdenv.isDarwin && pkgs.stdenv.isAarch64)) [
foundry.defaultPackage.${system}
];
# custom package set capable of building latest (unreleased) `cabal-install`.
# This gives us support for multiple home units in cabal repl
cabal-multi-pkgs = pkgs.haskell.packages.ghc94.override {
overrides = with pkgs.haskell.lib; self: super: rec {
cabal-install = dontCheck (self.callCabal2nix "cabal-install" "${cabal-head}/cabal-install" {});
cabal-install-solver = dontCheck (self.callCabal2nix "cabal-install-solver" "${cabal-head}/cabal-install-solver" {});
Cabal-described = dontCheck (self.callCabal2nix "Cabal-described" "${cabal-head}/Cabal-described" {});
Cabal-QuickCheck = dontCheck (self.callCabal2nix "Cabal-QuickCheck" "${cabal-head}/Cabal-QuickCheck" {});
Cabal-tree-diff = dontCheck (self.callCabal2nix "Cabal-tree-diff" "${cabal-head}/Cabal-tree-diff" {});
Cabal-syntax = dontCheck (self.callCabal2nix "Cabal-syntax" "${cabal-head}/Cabal-syntax" {});
Cabal = dontCheck (self.callCabal2nix "Cabal" "${cabal-head}/Cabal" {});
unix = dontCheck (doJailbreak super.unix_2_8_1_1);
filepath = dontCheck (doJailbreak super.filepath_1_4_100_4);
process = dontCheck (doJailbreak super.process_1_6_17_0);
directory = dontCheck (doJailbreak (super.directory_1_3_7_1));
tasty = dontCheck (doJailbreak super.tasty);
QuickCheck = dontCheck (doJailbreak super.QuickCheck);
hashable = dontCheck (doJailbreak super.hashable);
async = dontCheck (doJailbreak super.async);
hspec-meta = dontCheck (doJailbreak super.hspec-meta);
hpc = dontCheck (doJailbreak super.hpc);
ghci = dontCheck (doJailbreak super.ghci);
ghc-boot = dontCheck (doJailbreak super.ghc-boot);
setenv = dontCheck (doJailbreak super.setenv);
vector = dontCheck (doJailbreak super.vector);
network-uri = dontCheck (doJailbreak super.network-uri);
aeson = dontCheck (doJailbreak super.aeson);
th-compat = dontCheck (doJailbreak super.th-compat);
safe-exceptions = dontCheck (doJailbreak super.safe-exceptions);
bifunctors = dontCheck (doJailbreak super.bifunctors);
base-compat-batteries = dontCheck (doJailbreak super.base-compat-batteries);
distributative = dontCheck (doJailbreak super.distributative);
semialign = dontCheck (doJailbreak super.semialign);
semigroupoids = dontCheck (doJailbreak super.semigroupoids);
hackage-security = dontCheck (doJailbreak super.hackage-security);
};
};
secp256k1-static = stripDylib (pkgs.secp256k1.overrideAttrs (attrs: {
configureFlags = attrs.configureFlags ++ [ "--enable-static" ];
}));
hevmUnwrapped = (with pkgs; lib.pipe (
haskellPackages.callCabal2nix "hevm" ./. {
# Haskell libs with the same names as C libs...
# Depend on the C libs, not the Haskell libs.
# These are system deps, not Cabal deps.
inherit secp256k1;
})
[
(haskell.lib.compose.overrideCabal (old: { testTarget = "test"; }))
(haskell.lib.compose.addTestToolDepends testDeps)
(haskell.lib.compose.appendBuildFlags ["-v3"])
(haskell.lib.compose.appendConfigureFlags (
[ "-fci"
"-O2"
"--extra-lib-dirs=${stripDylib (pkgs.gmp.override { withStatic = true; })}/lib"
"--extra-lib-dirs=${stripDylib secp256k1-static}/lib"
"--extra-lib-dirs=${stripDylib (libff.override { enableStatic = true; })}/lib"
"--extra-lib-dirs=${zlib.static}/lib"
"--extra-lib-dirs=${stripDylib (libffi.overrideAttrs (_: { dontDisableStatic = true; }))}/lib"
"--extra-lib-dirs=${stripDylib (ncurses.override { enableStatic = true; })}/lib"
]
++ lib.optionals stdenv.isLinux [
"--enable-executable-static"
# TODO: replace this with musl: https://stackoverflow.com/a/57478728
"--extra-lib-dirs=${glibc}/lib"
"--extra-lib-dirs=${glibc.static}/lib"
]))
haskell.lib.dontHaddock
]).overrideAttrs(final: prev: {
HEVM_SOLIDITY_REPO = solidity;
HEVM_ETHEREUM_TESTS_REPO = ethereum-tests;
HEVM_FORGE_STD_REPO = forge-std;
DAPP_SOLC = "${pkgs.solc}/bin/solc";
});
# wrapped binary for use on systems with nix available. ensures all
# required runtime deps are available and on path
hevmWrapped = with pkgs; symlinkJoin {
name = "hevm";
paths = [ (haskell.lib.dontCheck hevmUnwrapped) ];
buildInputs = [ makeWrapper ];
postBuild = ''
wrapProgram $out/bin/hevm \
--prefix PATH : "${lib.makeBinPath ([ bash coreutils git solc z3 cvc5 ])}"
'';
};
# "static" binary for distribution
# on linux this is actually a real fully static binary
# on macos this has everything except libcxx, libsystem and libiconv
# statically linked. we can be confident that these three will always
# be provided in a well known location by macos itself.
hevmRedistributable = let
grep = "${pkgs.gnugrep}/bin/grep";
otool = "${pkgs.darwin.binutils.bintools}/bin/otool";
install_name_tool = "${pkgs.darwin.binutils.bintools}/bin/install_name_tool";
codesign_allocate = "${pkgs.darwin.binutils.bintools}/bin/codesign_allocate";
codesign = "${pkgs.darwin.sigtool}/bin/codesign";
in if pkgs.stdenv.isLinux
then pkgs.haskell.lib.dontCheck hevmUnwrapped
else pkgs.runCommand "stripNixRefs" {} ''
mkdir -p $out/bin
cp ${pkgs.haskell.lib.dontCheck hevmUnwrapped}/bin/hevm $out/bin/
# get the list of dynamic libs from otool and tidy the output
libs=$(${otool} -L $out/bin/hevm | tail -n +2 | sed 's/^[[:space:]]*//' | cut -d' ' -f1)
# get the paths for libcxx and libiconv
cxx=$(echo "$libs" | ${grep} '^/nix/store/.*-libcxx')
iconv=$(echo "$libs" | ${grep} '^/nix/store/.*-libiconv')
# rewrite /nix/... library paths to point to /usr/lib
chmod 777 $out/bin/hevm
${install_name_tool} -change "$cxx" /usr/lib/libc++.1.dylib $out/bin/hevm
${install_name_tool} -change "$iconv" /usr/lib/libiconv.dylib $out/bin/hevm
CODESIGN_ALLOCATE=${codesign_allocate} ${codesign} -f -s - $out/bin/hevm
chmod 555 $out/bin/hevm
'';
# if we pass a library folder to ghc via --extra-lib-dirs that contains
# only .a files, then ghc will link that library statically instead of
# dynamically (even if --enable-executable-static is not passed to cabal).
# we use this trick to force static linking of some libraries on macos.
stripDylib = drv : pkgs.runCommand "${drv.name}-strip-dylibs" {} ''
mkdir -p $out
mkdir -p $out/lib
cp -r ${drv}/* $out/
rm -rf $out/**/*.dylib
'';
in rec {
# --- packages ----
packages.ci = with pkgs.haskell.lib; doBenchmark (dontHaddock (disableLibraryProfiling hevmUnwrapped));
packages.noTests = pkgs.haskell.lib.dontCheck hevmUnwrapped;
packages.hevm = hevmWrapped;
packages.redistributable = hevmRedistributable;
packages.default = packages.hevm;
# --- apps ----
apps.hevm = flake-utils.lib.mkApp { drv = packages.hevm; };
apps.default = apps.hevm;
# --- shell ---
devShell = with pkgs;
let libraryPath = "${lib.makeLibraryPath [ libff secp256k1 gmp ]}";
in haskellPackages.shellFor {
packages = _: [ hevmUnwrapped ];
buildInputs = [
# cabal from nixpkgs
# haskellPackages.cabal-install
cabal-multi-pkgs.cabal-install
mdbook
yarn
haskellPackages.eventlog2html
haskellPackages.haskell-language-server
] ++ testDeps;
withHoogle = true;
HEVM_SOLIDITY_REPO = solidity;
DAPP_SOLC = "${pkgs.solc}/bin/solc";
HEVM_ETHEREUM_TESTS_REPO = ethereum-tests;
HEVM_FORGE_STD_REPO = forge-std;
# NOTE: hacks for bugged cabal new-repl
LD_LIBRARY_PATH = libraryPath;
shellHook = lib.optionalString stdenv.isDarwin ''
export DYLD_LIBRARY_PATH="${libraryPath}";
'';
};
}
);
}