Skip to content

Commit 1a1f545

Browse files
committed
Add build via Nix
1 parent 5f9218b commit 1a1f545

File tree

5 files changed

+110
-32
lines changed

5 files changed

+110
-32
lines changed

apps/build.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::process::Command;
33
use std::{env, str};
44

55
/// Path to the .proto source files, relative to `apps` directory
6-
const PROTO_SRC: &str = "../proto";
6+
const PROTO_SRC: &str = "./proto";
77

88
/// The version should match the one we use in the `Makefile`
99
const RUSTFMT_TOOLCHAIN_SRC: &str = "../rust-nightly-version";
@@ -13,21 +13,20 @@ fn main() {
1313
println!("cargo:rerun-if-changed={}", PROTO_SRC);
1414

1515
// The version should match the one we use in the `Makefile`
16-
let rustfmt_toolchain = read_to_string(RUSTFMT_TOOLCHAIN_SRC)
17-
.expect("File with Rust nightly version should be present");
18-
19-
// Try to find the path to rustfmt.
20-
if let Ok(output) = Command::new("rustup")
21-
.args(&["which", "rustfmt", "--toolchain", rustfmt_toolchain.trim()])
22-
.output()
23-
{
24-
if let Ok(rustfmt) = str::from_utf8(&output.stdout) {
25-
// Set the command to be used by tonic_build below to format the
26-
// generated files
27-
let rustfmt = rustfmt.trim();
28-
if !rustfmt.is_empty() {
29-
println!("using rustfmt from path \"{}\"", rustfmt);
30-
env::set_var("RUSTFMT", rustfmt);
16+
if let Ok(rustfmt_toolchain) = read_to_string(RUSTFMT_TOOLCHAIN_SRC) {
17+
// Try to find the path to rustfmt.
18+
if let Ok(output) = Command::new("rustup")
19+
.args(&["which", "rustfmt", "--toolchain", rustfmt_toolchain.trim()])
20+
.output()
21+
{
22+
if let Ok(rustfmt) = str::from_utf8(&output.stdout) {
23+
// Set the command to be used by tonic_build below to format the
24+
// generated files
25+
let rustfmt = rustfmt.trim();
26+
if !rustfmt.is_empty() {
27+
println!("using rustfmt from path \"{}\"", rustfmt);
28+
env::set_var("RUSTFMT", rustfmt);
29+
}
3130
}
3231
}
3332
}

apps/proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../proto

default.nix

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{ rust_overlay ? import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz")
2+
, pkgs ? import <nixpkgs> {
3+
overlays = [
4+
rust_overlay
5+
6+
(self: super: {
7+
rustc = pkgs.rust-bin.stable.latest.minimal.override {
8+
targets = [ "x86_64-unknown-linux-gnu" "wasm32-unknown-unknown" ];
9+
};
10+
inherit (super.rust-bin.stable.latest) rustfmt;
11+
})
12+
];
13+
}
14+
}:
15+
let
16+
crateOverrides = pkgs: with pkgs; {
17+
prost-build = attrs: { buildInputs = [ protobuf ]; };
18+
libp2p-core = attrs: { buildInputs = [ protobuf ]; PROTOC = "${protobuf}/bin/protoc"; };
19+
libp2p-plaintext = attrs: { buildInputs = [ protobuf ]; PROTOC = "${protobuf}/bin/protoc"; };
20+
libp2p-floodsub = attrs: { buildInputs = [ protobuf ]; PROTOC = "${protobuf}/bin/protoc"; };
21+
libp2p-gossipsub = attrs: { buildInputs = [ protobuf ]; PROTOC = "${protobuf}/bin/protoc"; };
22+
libp2p-identify = attrs: { buildInputs = [ protobuf ]; PROTOC = "${protobuf}/bin/protoc"; };
23+
libp2p-kad = attrs: { buildInputs = [ protobuf ]; PROTOC = "${protobuf}/bin/protoc"; };
24+
libp2p-relay = attrs: { buildInputs = [ protobuf ]; PROTOC = "${protobuf}/bin/protoc"; };
25+
libp2p-noise = attrs: { buildInputs = [ protobuf ]; PROTOC = "${protobuf}/bin/protoc"; };
26+
27+
librocksdb-sys = attrs: {
28+
buildInputs = [ clang rustfmt snappy lz4 zstd zlib bzip2 ];
29+
LIBCLANG_PATH = "${llvmPackages.libclang.lib}/lib";
30+
# rust-rocksdb uses {}_LIB_DIR to determine whether it embeds compression libs in itself.
31+
# We need to tell it to use libs from the nix store so that we don't get linker errors later on.
32+
SNAPPY_LIB_DIR = "${snappy}/lib";
33+
LZ4_LIB_DIR = "${lz4}/lib";
34+
ZSTD_LIB_DIR = "${zstd}/lib";
35+
Z_LIB_DIR = "${zlib}/lib";
36+
BZ2_LIB_DIR = "${bzip2}/lib";
37+
# rust-rocksdb relies on CARGO_CFG_TARGET_FEATURE env variable
38+
# which is set by cargo. We are using rustc directly here, so
39+
# we need to set that variable.
40+
#
41+
# See: https://github.com/rust-rocksdb/rust-rocksdb/commit/81a9edea83473012378e808606cdd92c1212c076#diff-7377ccc9f5cb3386318bd8afcd84814e7dd6d9b40efa909fb2dc218ecb779499
42+
#
43+
# XXX empty string is the most portable, but it can't take
44+
# advantage of fast vectorization instructions on newer CPUs.
45+
CARGO_CFG_TARGET_FEATURE = "";
46+
};
47+
48+
anoma = attrs: {
49+
buildInputs = [ rustfmt ];
50+
patchPhase = ''
51+
substituteInPlace build.rs --replace ./proto ${./proto}
52+
'';
53+
};
54+
55+
anoma_apps = attrs: {
56+
buildInputs = [ rustfmt lz4 zstd zlib bzip2 ];
57+
patchPhase = ''
58+
substituteInPlace build.rs --replace ./proto ${./proto}
59+
'';
60+
};
61+
};
62+
63+
# Generate Cargo.nix with: crate2nix generate
64+
cargo_nix = import ./Cargo.nix {
65+
inherit pkgs;
66+
buildRustCrateForPkgs = pkgs: pkgs.buildRustCrate.override {
67+
defaultCrateOverrides = pkgs.defaultCrateOverrides // crateOverrides pkgs;
68+
};
69+
};
70+
71+
in {
72+
apps = cargo_nix.workspaceMembers.anoma_apps;
73+
}

shared/build.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::process::Command;
33
use std::{env, str};
44

55
/// Path to the .proto source files, relative to `shared` directory
6-
const PROTO_SRC: &str = "../proto";
6+
const PROTO_SRC: &str = "./proto";
77

88
/// The version should match the one we use in the `Makefile`
99
const RUSTFMT_TOOLCHAIN_SRC: &str = "../rust-nightly-version";
@@ -20,21 +20,25 @@ fn main() {
2020
println!("cargo:rerun-if-changed={}", PROTO_SRC);
2121

2222
// The version should match the one we use in the `Makefile`
23-
let rustfmt_toolchain = read_to_string(RUSTFMT_TOOLCHAIN_SRC)
24-
.expect("File with Rust nightly version should be present");
25-
26-
// Try to find the path to rustfmt.
27-
if let Ok(output) = Command::new("rustup")
28-
.args(&["which", "rustfmt", "--toolchain", rustfmt_toolchain.trim()])
29-
.output()
30-
{
31-
if let Ok(rustfmt) = str::from_utf8(&output.stdout) {
32-
// Set the command to be used by tonic_build below to format the
33-
// generated files
34-
let rustfmt = rustfmt.trim();
35-
if !rustfmt.is_empty() {
36-
println!("using rustfmt from path \"{}\"", rustfmt);
37-
env::set_var("RUSTFMT", rustfmt);
23+
if let Ok(rustfmt_toolchain) = read_to_string(RUSTFMT_TOOLCHAIN_SRC) {
24+
// Try to find the path to rustfmt.
25+
if let Ok(output) = Command::new("rustup")
26+
.args(&[
27+
"which",
28+
"rustfmt",
29+
"--toolchain",
30+
rustfmt_toolchain.trim(),
31+
])
32+
.output()
33+
{
34+
if let Ok(rustfmt) = str::from_utf8(&output.stdout) {
35+
// Set the command to be used by tonic_build below to format the
36+
// generated files
37+
let rustfmt = rustfmt.trim();
38+
if !rustfmt.is_empty() {
39+
println!("using rustfmt from path \"{}\"", rustfmt);
40+
env::set_var("RUSTFMT", rustfmt);
41+
}
3842
}
3943
}
4044
}

shared/proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../proto

0 commit comments

Comments
 (0)