forked from ComposableFi/composable-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
522 lines (471 loc) · 17.2 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
{
description = "CVM and MANTIS";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
nix2container = {
url = "github:nlewo/nix2container";
inputs.nixpkgs.follows = "nixpkgs";
};
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
cosmos = {
url = "github:informalsystems/cosmos.nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.rust-overlay.follows = "rust-overlay";
};
datamodel-code-generator-src = {
url = "github:koxudaxi/datamodel-code-generator";
flake = false;
};
poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
networks = {
url = "github:ComposableFi/networks";
};
cosmpy-src = {
url = "github:fetchai/cosmpy";
flake = false;
};
fastapi-cache-src = {
url = "github:long2ice/fastapi-cache";
flake = false;
};
scip = {
url = github:dzmitry-lahoda-forks/scip/169747d9a7d5b01a44722ea7db2ed389443e7a57;
};
devenv.url = "github:cachix/devenv";
strictly-typed-pandas-src = {
url = "github:nanne-aben/strictly_typed_pandas";
flake = false;
};
maturin-src = {
url = "github:PyO3/maturin";
flake = false;
};
pydantic-src = {
url = "github:pydantic/pydantic/v2.5.3";
flake = false;
};
scipy-src = {
url = "github:scipy/scipy/v1.9.3";
flake = false;
};
cvxpy-src = {
url = "github:cvxpy/cvxpy/v1.3.2";
flake = false;
};
devour-flake = {
url = "github:srid/devour-flake";
flake = false;
};
pyscipopt-src = {
url = "github:scipopt/PySCIPOpt/v4.3.0";
flake = false;
};
};
outputs = inputs @ {
flake-parts,
self,
crane,
devour-flake,
datamodel-code-generator-src,
poetry2nix,
cosmpy-src,
nixpkgs,
scip,
pyscipopt-src,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
imports = [
inputs.devenv.flakeModule
];
systems = nixpkgs.lib.systems.flakeExposed;
perSystem = {
config,
self',
inputs',
pkgs,
system,
...
}: let
craneLib = crane.lib.${system};
rust-src = pkgs.lib.cleanSourceWith {
filter =
pkgs.nix-gitignore.gitignoreFilterPure
(
name: type:
!(pkgs.lib.strings.hasSuffix ".nix" name)
|| builtins.match ".*proto$" name != null
|| builtins.match ".*txt$" name != null
|| craneLib.filterCargoSources name type
) [./.gitignore]
./.;
src = craneLib.path ./.;
};
devour-flake = pkgs.callPackage inputs.devour-flake {};
rust-toolchain =
pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
rust =
(self.inputs.crane.mkLib pkgs).overrideToolchain
rust-toolchain;
makeCosmwasmContract = name: rust: std-config: let
binaryName = "${builtins.replaceStrings ["-"] ["_"] name}.wasm";
maxWasmSizeBytes = 819200 * 2;
profile = "deployment";
in
rust.buildPackage (rust-attrs
// {
src = rust-src;
pnameSuffix = "-${name}";
nativeBuildInputs = [
pkgs.binaryen
self.inputs.cosmos.packages.${system}.cosmwasm-check
];
pname = name;
cargoBuildCommand = "cargo build --target wasm32-unknown-unknown --profile ${profile} --package ${name} ${std-config}";
RUSTFLAGS = "-C link-arg=-s";
installPhaseCommand = ''
mkdir --parents $out/lib
# from CosmWasm/rust-optimizer
# --signext-lowering is needed to support blockchains runnning CosmWasm < 1.3. It can be removed eventually
wasm-opt target/wasm32-unknown-unknown/${profile}/${binaryName} -o $out/lib/${binaryName} -Os --signext-lowering
cosmwasm-check $out/lib/${binaryName}
SIZE=$(stat --format=%s "$out/lib/${binaryName}")
if [[ "$SIZE" -gt ${builtins.toString maxWasmSizeBytes} ]]; then
echo "Wasm file size is $SIZE, which is larger than the maximum allowed size of ${builtins.toString maxWasmSizeBytes} bytes."
echo "Either reduce size or increase maxWasmSizeBytes if you know what you are doing."
exit 1
fi
'';
});
rust-attrs = {
doCheck = false;
checkPhase = "true";
cargoCheckCommand = "true";
NIX_BUILD_FLAKE = "true";
RUST_BACKTRACE = "full";
CARGO_PROFILE_RELEASE_BUILD_OVERRIDE_DEBUG = true;
buildInputs = [pkgs.protobuf];
};
cw-cvm-outpost = makeCosmwasmContract "cw-cvm-outpost" rust "--no-default-features --features=std,json-schema,cosmos";
cw-cvm-executor = makeCosmwasmContract "cw-cvm-executor" rust "--no-default-features --features=std,json-schema,cosmos";
cw-mantis-order = makeCosmwasmContract "cw-mantis-order" rust "--no-default-features --features=std,json-schema";
cosmwasm-contracts = pkgs.symlinkJoin {
name = "cosmwasm-contracts";
paths = [
cw-cvm-executor
cw-cvm-outpost
cw-mantis-order
];
};
cosmwasm-json-schema-ts = pkgs.writeShellApplication {
name = "cosmwasm-json-schema-ts";
runtimeInputs = with pkgs; [
rust.rustc
rust.cargo
nodejs
nodePackages.npm
];
text = ''
echo "generating TypeScript types and client definitions from JSON schema of CosmWasm contracts"
cd contracts/cosmwasm/
npm install
rm --recursive --force dist
rm --recursive --force schema
cargo run --bin order --package cw-mantis-order --features=std,json-schema
npm run build-cw-mantis-order
rm --recursive --force schema
cargo run --bin outpost --package cvm-runtime --features=std,json-schema,cosmos,cosmwasm
npm run build-cvm-runtime
npm publish
'';
};
in let
datamodel-code-generator = mkPoetryApplication {
projectDir = datamodel-code-generator-src;
checkGroups = [];
};
cosmpy = pkgs.python3Packages.buildPythonPackage {
name = "cosmpy";
version = "0.9.1";
format = "pyproject";
src = cosmpy-src;
nativeBuildInputs = [
pkgs.python3Packages.poetry-core
];
};
# https://github.com/nanne-aben/strictly_typed_pandas/issues/140
strictly-typed-pandas-latest = pkgs.python3Packages.buildPythonPackage {
name = "strictly-typed-pandas";
version = "0.0.1";
format = "pyproject";
src = inputs.strictly-typed-pandas-src;
nativeBuildInputs = with pkgs.python3Packages; [
poetry-core
setuptools
setuptools-git-versioning
];
};
scipy-latest = pkgs.python3Packages.buildPythonPackage {
name = "scipy";
version = "0.0.1";
format = "pyproject";
src = inputs.scipy-src;
nativeBuildInputs = with pkgs.python3Packages; [
poetry-core
meson
meson-python
setuptools
setuptools-git-versioning
pkgs.pkg-config
];
};
pyscipopt-latest = pkgs.python3Packages.buildPythonPackage {
name = "pyscipopt";
version = "v4.3.0";
format = "pyproject";
src = inputs.pyscipopt-src;
nativeBuildInputs = with pkgs.python3Packages; [
setuptools
pkgs.pkg-config
inputs'.scip.packages.scip
pkgs.python311Packages.cython
];
buildInputs = with pkgs.python3Packages; [
inputs'.scip.packages.scip
cython
];
};
dep = name:
builtins.head (pkgs.lib.lists.filter
(x: pkgs.lib.strings.hasInfix name x.name)
deps.poetryPackages);
cvxpy-latest = pkgs.python3Packages.buildPythonPackage {
name = "cvxpy";
version = "1.3.2";
format = "pyproject";
src = inputs.cvxpy-src;
nativeBuildInputs = with pkgs.python3Packages; [
(dep "numpy")
(dep "scipy")
poetry-core
setuptools
setuptools-git-versioning
pkgs.pkg-config
];
};
maturin-latest = pkgs.python3Packages.buildPythonPackage {
name = "maturin";
version = "0.0.1";
format = "pyproject";
src = inputs.maturin-src;
cargoDeps = pkgs.rustPlatform.fetchCargoTarball {
src = inputs.maturin-src;
name = "maturin";
version = "0.0.1";
hash = "sha256-3zPG/6EQiXaDaxgNYIAsJ5A7MbbK2Gxj8NDpEukUit4=";
};
nativeBuildInputs = with pkgs.python3Packages; [
poetry-core
setuptools
setuptools-rust
setuptools-git-versioning
pkgs.rustPlatform.cargoSetupHook
pkgs.rustPlatform.maturinBuildHook
];
};
deps = mkPoetryPackages {
projectDir = ./mantis;
};
override = overrides:
overrides.withDefaults (self: super: {
editables = super.editables.overridePythonAttrs (old: {
buildInputs = old.buildInputs or [] ++ [self.python.pkgs.flit-core];
});
pydantic-extra-types = super.pydantic-extra-types.overridePythonAttrs (old: {
buildInputs = old.buildInputs or [] ++ [self.python.pkgs.hatchling];
});
pyscipopt = pyscipopt-latest;
google = super.google.overridePythonAttrs (old: {
buildInputs = old.buildInputs or [] ++ [self.python.pkgs.setuptools];
});
cylp = super.cylp.overridePythonAttrs (old: {
buildInputs = old.buildInputs or [] ++ [self.python.pkgs.setuptools self.python.pkgs.wheel pkgs.cbc pkgs.pkg-config];
nativeBuildInputs = old.nativeBuildInputs or [] ++ [self.python.pkgs.setuptools self.python.pkgs.wheel pkgs.cbc pkgs.pkg-config];
});
google-cloud = super.google-cloud.overridePythonAttrs (old: {
buildInputs = old.buildInputs or [] ++ [self.python.pkgs.setuptools];
});
cvxpy = cvxpy-latest;
cosmpy = cosmpy;
maturin = maturin-latest;
strictly-typed-pandas = strictly-typed-pandas-latest;
});
envShell = mkPoetryEnv {
projectDir = ./mantis;
overrides = override overrides;
};
mantis-blackbox-package = mkPoetryApplication {
projectDir = ./mantis;
overrides = override overrides;
};
mantis-blackbox = pkgs.writeShellApplication {
name = "mantis-blackbox";
runtimeInputs = [mantis-blackbox-package];
text = ''
# shellcheck disable=SC2068
mantis-blackbox $@
'';
};
inherit (poetry2nix.lib.mkPoetry2Nix {inherit pkgs;}) mkPoetryApplication mkPoetryPackages mkPoetryEnv overrides;
env = {
OSMOSIS_POOLS = "https://app.osmosis.zone/api/pools?page=1&limit=1000&min_liquidity=500000";
ASTROPORT_POOLS = "https://app.astroport.fi/api/trpc/pools.getAll?input=%7B%22json%22%3A%7B%22chainId%22%3A%5B%22neutron-1%22%5D%7D%7D";
SKIP_MONEY_SWAGGER = "https://api-swagger.skip.money/";
SKIP_MONEY = "https://api.skip.money/";
};
cosmwasm-json-schema-py = let
in
pkgs.writeShellApplication {
name = "cosmwasm-json-schema-py";
runtimeInputs = with pkgs; [
rust.cargo
datamodel-code-generator
];
text = ''
RUST_BACKTRACE=1 cargo run --package cvm-runtime --bin outpost --features=cosmwasm,json-schema,cosmos,std
datamodel-codegen --input schema/raw/ --input-file-type jsonschema --output mantis/blackbox/cvm_runtime/ --disable-timestamp --target-python-version "3.10" --use-schema-description --output-model-type "pydantic_v2.BaseModel"
curl "${env.OSMOSIS_POOLS}" | jq .pools > schema/osmosis_pools.json
datamodel-codegen --input schema/osmosis_pools.json --input-file-type json --output mantis/blackbox/osmosis_pools.py --disable-timestamp --target-python-version "3.10" --use-schema-description --output-model-type "pydantic_v2.BaseModel"
curl "${env.ASTROPORT_POOLS}" | jq .result.data > schema/neutron_pools.json
datamodel-codegen --input schema/neutron_pools.json --input-file-type json --output mantis/blackbox/neutron_pools.py --disable-timestamp --target-python-version "3.10" --use-schema-description --output-model-type "pydantic_v2.BaseModel"
curl "${env.SKIP_MONEY_SWAGGER}swagger.yml" > schema/skip_money_swagger.yml
datamodel-codegen --input schema/skip_money_swagger.yml --input-file-type openapi --output mantis/blackbox/skip_money.py --disable-timestamp --target-python-version "3.10" --use-schema-description --output-model-type "pydantic_v2.BaseModel"
curl --request GET --url https://api.skip.money/v1/info/chains --header 'accept: application/json' | jq . > schema/skip_money_chain.json
curl --request GET --url https://api.skip.money/v1/fungible/assets --header 'accept: application/json' | jq . > schema/skip_money_assets.json
'';
};
native-deps = [
pkgs.cbc
inputs'.scip.packages.scip
pkgs.CoinMP
pkgs.ipopt
pkgs.or-tools
];
in {
_module.args.pkgs = import self.inputs.nixpkgs {
inherit system;
overlays = with self.inputs; [
rust-overlay.overlays.default
];
};
devenv.shells.default = {
env = {
OSMOSIS_POOLS = env.OSMOSIS_POOLS;
ASTROPORT_POOLS = env.ASTROPORT_POOLS;
SKIP_MONEY = env.SKIP_MONEY;
COMPOSABLE_COSMOS_GRPC = inputs.networks.lib.pica.mainnet.GRPC;
CVM_ADDRESS = inputs.networks.lib.pica.mainnet.CVM_OUTPOST_CONTRACT_ADDRESS;
LD_LIBRARY_PATH = pkgs.lib.strings.makeLibraryPath [
pkgs.stdenv.cc.cc.lib
pkgs.zlib
pkgs.zlib.dev
pkgs.zlib.out
"${inputs'.scip.packages.scip}/lib"
];
};
packages =
[
pkgs.nix
pkgs.zlib
pkgs.zlib.dev
pkgs.zlib.out
devour-flake
pkgs.conda
pkgs.nodejs
pkgs.nodePackages.npm
pkgs.poetry
pkgs.pyo3-pack
pkgs.python3Packages.flit
pkgs.python3Packages.flit-core
pkgs.python3Packages.uvicorn
pkgs.virtualenv
pkgs.zlib
pkgs.zlib.dev
pkgs.zlib.out
rust.cargo
rust.rustc
envShell
devour-flake
]
++ native-deps;
devcontainer.enable = true;
enterShell = ''
if [[ -f ./.env ]]; then
source ./.env
fi
(
cd mantis
poetry install
)
'';
};
formatter = pkgs.alejandra;
packages = rec {
inherit
cw-mantis-order
cw-cvm-executor
cw-cvm-outpost
cosmwasm-contracts
cosmwasm-json-schema-py
datamodel-code-generator
cosmwasm-json-schema-ts
mantis-blackbox
pyscipopt-latest
;
all =
pkgs.linkFarmFromDrvs "all"
(with self'.packages; [
cw-mantis-order
cw-cvm-executor
cw-cvm-outpost
cosmwasm-contracts
cosmwasm-json-schema-py
datamodel-code-generator
cosmwasm-json-schema-ts
mantis-blackbox
]);
mantis = rust.buildPackage (rust-attrs
// {
src = rust-src;
pname = "mantis";
name = "mantis";
cargoBuildCommand = "cargo build --release --bin mantis";
nativeBuildInputs = native-deps;
});
default = mantis-blackbox;
ci = pkgs.writeShellApplication {
name = "nix-build-all";
runtimeInputs = [
pkgs.nix
devour-flake
];
text = ''
nix flake lock --no-update-lock-file
# devour-flake . "$@" --impure
nix build .#all
'';
};
};
};
};
}