Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix: add flake and derivation #275

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

markoburcul
Copy link

Referenced issue: waku-org/nwaku#3232

Referenced issue: waku-org/nwaku#3232

Signed-off-by: markoburcul <[email protected]>
@markoburcul
Copy link
Author

What needs to be checked is what to do with the rest of these architectures and systems from the list in Cross.toml file. From what I know nix supports following list.

Copy link

Benchmark for ef87b27

Click to view benchmark
Test Base PR %
FullMerkleTree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
FullMerkleTree::delete 855.5±10.31ns 856.2±10.35ns +0.08%
FullMerkleTree::get 3.4±0.09ns 3.5±0.08ns +2.94%
FullMerkleTree::get_empty_leaves_indices 7.2±0.10ns 7.2±0.41ns 0.00%
FullMerkleTree::get_subtree_root 3.7±0.10ns 3.7±0.12ns 0.00%
FullMerkleTree::override_range 1379.4±12.03ns 1346.4±10.19ns -2.39%
FullMerkleTree::set 870.2±9.79ns 864.3±13.21ns -0.68%
OptimalMerkleTree::compute_root 999.4±11.60ns 999.5±7.30ns +0.01%
OptimalMerkleTree::delete 998.1±14.20ns 1008.5±17.80ns +1.04%
OptimalMerkleTree::get 22.8±0.30ns 22.5±0.34ns -1.32%
OptimalMerkleTree::get_empty_leaves_indices 7.2±0.36ns 7.2±0.11ns 0.00%
OptimalMerkleTree::get_subtree_root 24.0±0.31ns 24.0±0.20ns 0.00%
OptimalMerkleTree::override_range 4.1±0.05µs 4.1±0.10µs 0.00%
OptimalMerkleTree::set 989.4±7.19ns 1010.7±35.77ns +2.15%

Copy link

Benchmark for ef87b27

Click to view benchmark
Test Base PR %
FullMerkleTree::::full_height_gen 14.1±0.17ms 14.2±0.62ms +0.71%
OptimalMerkleTree::::full_height_gen 582.4±4.22µs 583.3±7.41µs +0.15%
Pmtree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
Pmtree::get 369.3±5.38ns 370.5±4.51ns +0.32%
Pmtree::get_empty_leaves_indices 10.3±0.14ns 8.7±0.22ns -15.53%
Pmtree::get_subtree_root 529.0±6.51ns 530.0±4.90ns +0.19%
Pmtree::override_range 234.4±8.90µs 236.2±3.85µs +0.77%
Pmtree::set 54.6±0.50µs 54.7±0.99µs +0.18%
Pmtree:delete 54.9±0.52µs 54.7±0.53µs -0.36%
Set/FullMerkleTree::::set/1 561.7±2.68µs 558.9±4.61µs -0.50%
Set/FullMerkleTree::::set/10 5.6±0.00ms 5.6±0.02ms 0.00%
Set/FullMerkleTree::::set/100 56.0±0.16ms 55.9±0.15ms -0.18%
Set/FullMerkleTree::::set_range/1 560.9±3.46µs 558.9±3.85µs -0.36%
Set/FullMerkleTree::::set_range/10 763.6±3.09µs 765.1±9.06µs +0.20%
Set/FullMerkleTree::::set_range/100 3.3±0.02ms 3.3±0.04ms 0.00%
Set/OptimalMerkleTree::::set/1 567.6±18.07µs 562.6±11.45µs -0.88%
Set/OptimalMerkleTree::::set/10 5.6±0.01ms 5.6±0.02ms 0.00%
Set/OptimalMerkleTree::::set/100 56.7±0.28ms 56.3±0.12ms -0.71%
Set/OptimalMerkleTree::::set_range/1 564.8±4.08µs 562.2±3.06µs -0.46%
Set/OptimalMerkleTree::::set_range/10 5.6±0.01ms 5.6±0.01ms 0.00%
Set/OptimalMerkleTree::::set_range/100 56.6±0.24ms 56.3±0.13ms -0.53%
vk::vk_from_ark_serialized 1597.6±7.46ns 1594.2±16.25ns -0.21%
zkey::zkey_from_raw 3.4±0.00s 3.4±0.01s 0.00%

Copy link

Benchmark for ef87b27

Click to view benchmark
Test Base PR %
FullMerkleTree::::full_height_gen 13.8±0.05ms 14.0±0.06ms +1.45%
OptimalMerkleTree::::full_height_gen 579.7±4.31µs 580.4±4.66µs +0.12%
Pmtree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
Pmtree::get 365.9±4.36ns 363.1±2.83ns -0.77%
Pmtree::get_empty_leaves_indices 8.4±0.12ns 8.4±0.10ns 0.00%
Pmtree::get_subtree_root 515.9±6.56ns 508.8±9.50ns -1.38%
Pmtree::override_range 233.8±3.16µs 233.3±5.11µs -0.21%
Pmtree::set 54.6±1.50µs 54.2±0.40µs -0.73%
Pmtree:delete 54.5±1.13µs 54.6±0.45µs +0.18%
Set/FullMerkleTree::::set/1 560.5±5.69µs 559.4±3.88µs -0.20%
Set/FullMerkleTree::::set/10 5.6±0.04ms 5.6±0.02ms 0.00%
Set/FullMerkleTree::::set/100 56.1±0.15ms 56.0±0.14ms -0.18%
Set/FullMerkleTree::::set_range/1 560.2±8.44µs 560.5±5.35µs +0.05%
Set/FullMerkleTree::::set_range/10 762.2±4.48µs 759.9±4.15µs -0.30%
Set/FullMerkleTree::::set_range/100 3.3±0.12ms 3.3±0.04ms 0.00%
Set/OptimalMerkleTree::::set/1 578.5±5.61µs 559.3±2.65µs -3.32%
Set/OptimalMerkleTree::::set/10 5.7±0.02ms 5.6±0.01ms -1.75%
Set/OptimalMerkleTree::::set/100 56.8±0.11ms 56.0±0.35ms -1.41%
Set/OptimalMerkleTree::::set_range/1 576.1±4.33µs 561.1±3.34µs -2.60%
Set/OptimalMerkleTree::::set_range/10 5.6±0.01ms 5.6±0.03ms 0.00%
Set/OptimalMerkleTree::::set_range/100 56.2±0.21ms 56.3±0.14ms +0.18%
arkzkey::arkzkey_from_raw_compressed 340.0±0.40ms 340.0±0.44ms 0.00%
arkzkey::arkzkey_from_raw_uncompressed 6.0±0.01ms 6.0±0.02ms 0.00%
vk::vk_from_ark_serialized 1625.2±4.76ns 1620.3±7.01ns -0.30%
zkey::zkey_from_raw 3.4±0.00s 3.4±0.01s 0.00%

@jakubgs
Copy link

jakubgs commented Jan 22, 2025

I don't think we should worry about cross compiling. I don't see any scenario in which we have to cross-compile nim-waku when building it for use with status-go. Right @richard-ramos ?

@richard-ramos
Copy link
Member

richard-ramos commented Jan 22, 2025

@jakubgs: I don't see any scenario in which we have to cross-compile nim-waku when building it for use with status-go

For android I currently cross compile zerokit and status-go to the following architectures

  • aarch64-linux-android
  • x86_64-linux-android
  • i686-linux-android
  • armv7a-linux-androideabi

This is being done thru the targets libwaku-android-* which call this target: https://github.com/waku-org/nwaku/blob/505ec84ce858d159456a9de86705e30dc27e6d78/Makefile#L407-L410 which rebuilds the nat libraries for the target architectures, builds zerokit's librln via cross-rs, and cross compiles nwaku using:

@richard-ramos
Copy link
Member

There's another possibility, which is to cross compile zerokit without using cross-rs, like it's described in https://rust-lang.github.io/rustup/cross-compilation.html . In theory we should add a .cargo/config.toml file describing the toolchains to use depending on the arch, for example:

[target.aarch64-linux-android]
ar = "aarch64-linux-android-ar"
linker = "aarch64-linux-android21-clang"

And assuming you have the android NDK toolchain in the $PATH, it should work
(I have not tested this tho)

@jakubgs
Copy link

jakubgs commented Jan 22, 2025

Oh, right, I'm dumb. For android we would need to cross compile. But with Nix there's two ways to do this:

@markoburcul
Copy link
Author

markoburcul commented Jan 22, 2025

Use native Nix cross-compilation abilities with pkgsCross.aarch64-android.

I've added this in my newest commit for cross compiling but only for android-arm64 and android-arm for now

Copy link

Benchmark for 2fdda1b

Click to view benchmark
Test Base PR %
FullMerkleTree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
FullMerkleTree::delete 881.5±10.97ns 861.4±6.17ns -2.28%
FullMerkleTree::get 3.5±0.11ns 3.4±0.03ns -2.86%
FullMerkleTree::get_empty_leaves_indices 7.2±0.20ns 7.2±0.11ns 0.00%
FullMerkleTree::get_subtree_root 3.6±0.13ns 3.6±0.15ns 0.00%
FullMerkleTree::override_range 1334.5±21.50ns 1332.8±8.26ns -0.13%
FullMerkleTree::set 855.7±7.21ns 855.4±14.16ns -0.04%
OptimalMerkleTree::compute_root 998.3±14.24ns 1018.0±15.81ns +1.97%
OptimalMerkleTree::delete 1019.7±11.44ns 1006.9±26.54ns -1.26%
OptimalMerkleTree::get 22.6±0.28ns 22.5±0.24ns -0.44%
OptimalMerkleTree::get_empty_leaves_indices 7.2±0.10ns 7.2±0.12ns 0.00%
OptimalMerkleTree::get_subtree_root 24.5±0.35ns 24.4±0.26ns -0.41%
OptimalMerkleTree::override_range 4.1±0.04µs 4.2±0.05µs +2.44%
OptimalMerkleTree::set 1016.3±9.80ns 1004.6±22.17ns -1.15%

Copy link

Benchmark for 2fdda1b

Click to view benchmark
Test Base PR %
FullMerkleTree::::full_height_gen 14.5±0.08ms 14.6±0.07ms +0.69%
OptimalMerkleTree::::full_height_gen 586.4±6.82µs 586.6±4.53µs +0.03%
Pmtree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
Pmtree::get 360.7±6.77ns 365.7±4.80ns +1.39%
Pmtree::get_empty_leaves_indices 8.7±0.16ns 10.2±0.13ns +17.24%
Pmtree::get_subtree_root 507.1±6.39ns 514.1±6.38ns +1.38%
Pmtree::override_range 241.6±5.29µs 241.0±6.46µs -0.25%
Pmtree::set 55.0±0.93µs 55.6±0.51µs +1.09%
Pmtree:delete 55.2±0.56µs 55.8±0.99µs +1.09%
Set/FullMerkleTree::::set/1 563.9±10.54µs 559.0±10.41µs -0.87%
Set/FullMerkleTree::::set/10 5.6±0.01ms 5.6±0.01ms 0.00%
Set/FullMerkleTree::::set/100 56.4±0.15ms 56.4±0.18ms 0.00%
Set/FullMerkleTree::::set_range/1 562.8±4.40µs 563.7±10.35µs +0.16%
Set/FullMerkleTree::::set_range/10 764.9±5.40µs 764.7±5.03µs -0.03%
Set/FullMerkleTree::::set_range/100 3.3±0.01ms 3.3±0.01ms 0.00%
Set/OptimalMerkleTree::::set/1 561.8±8.62µs 564.6±4.87µs +0.50%
Set/OptimalMerkleTree::::set/10 5.6±0.01ms 5.6±0.06ms 0.00%
Set/OptimalMerkleTree::::set/100 56.6±0.16ms 56.5±0.67ms -0.18%
Set/OptimalMerkleTree::::set_range/1 564.0±3.79µs 566.4±5.53µs +0.43%
Set/OptimalMerkleTree::::set_range/10 5.6±0.01ms 5.7±0.04ms +1.79%
Set/OptimalMerkleTree::::set_range/100 56.6±0.90ms 56.8±0.20ms +0.35%
vk::vk_from_ark_serialized 1586.4±11.34ns 1587.7±7.93ns +0.08%
zkey::zkey_from_raw 3.4±0.00s 3.4±0.00s 0.00%

Copy link

Benchmark for 2fdda1b

Click to view benchmark
Test Base PR %
FullMerkleTree::::full_height_gen 14.3±0.13ms 14.8±0.18ms +3.50%
OptimalMerkleTree::::full_height_gen 586.3±9.47µs 585.7±11.26µs -0.10%
Pmtree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
Pmtree::get 363.1±5.10ns 363.5±4.57ns +0.11%
Pmtree::get_empty_leaves_indices 8.4±0.11ns 9.1±0.15ns +8.33%
Pmtree::get_subtree_root 508.5±4.90ns 502.4±6.54ns -1.20%
Pmtree::override_range 239.0±3.64µs 236.6±4.36µs -1.00%
Pmtree::set 54.3±0.49µs 54.5±0.67µs +0.37%
Pmtree:delete 54.6±0.64µs 54.3±0.40µs -0.55%
Set/FullMerkleTree::::set/1 558.6±3.39µs 560.3±4.70µs +0.30%
Set/FullMerkleTree::::set/10 5.6±0.01ms 5.6±0.01ms 0.00%
Set/FullMerkleTree::::set/100 56.1±0.43ms 56.3±0.09ms +0.36%
Set/FullMerkleTree::::set_range/1 559.0±2.62µs 560.0±5.20µs +0.18%
Set/FullMerkleTree::::set_range/10 762.0±3.10µs 761.3±4.66µs -0.09%
Set/FullMerkleTree::::set_range/100 3.3±0.01ms 3.3±0.01ms 0.00%
Set/OptimalMerkleTree::::set/1 562.6±2.78µs 560.2±4.81µs -0.43%
Set/OptimalMerkleTree::::set/10 5.6±0.01ms 5.6±0.02ms 0.00%
Set/OptimalMerkleTree::::set/100 56.3±0.12ms 56.1±0.21ms -0.36%
Set/OptimalMerkleTree::::set_range/1 561.0±4.16µs 560.5±4.44µs -0.09%
Set/OptimalMerkleTree::::set_range/10 5.6±0.01ms 5.6±0.01ms 0.00%
Set/OptimalMerkleTree::::set_range/100 56.1±0.30ms 57.1±0.23ms +1.78%
arkzkey::arkzkey_from_raw_compressed 340.2±0.31ms 340.2±0.43ms 0.00%
arkzkey::arkzkey_from_raw_uncompressed 6.0±0.01ms 6.0±0.02ms 0.00%
vk::vk_from_ark_serialized 1609.6±20.81ns 1607.8±12.80ns -0.11%
zkey::zkey_from_raw 3.4±0.00s 3.4±0.00s 0.00%

@markoburcul markoburcul force-pushed the add-nix-flake-and-derivation branch from 2dd815f to 0474219 Compare January 23, 2025 09:56
Copy link

Benchmark for 3ee9c5f

Click to view benchmark
Test Base PR %
FullMerkleTree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
FullMerkleTree::delete 859.0±12.34ns 883.1±6.75ns +2.81%
FullMerkleTree::get 3.5±0.19ns 3.4±0.03ns -2.86%
FullMerkleTree::get_empty_leaves_indices 7.2±0.05ns 7.2±0.06ns 0.00%
FullMerkleTree::get_subtree_root 3.7±0.13ns 3.6±0.13ns -2.70%
FullMerkleTree::override_range 1348.0±12.12ns 1348.5±81.46ns +0.04%
FullMerkleTree::set 864.9±7.71ns 876.0±13.04ns +1.28%
OptimalMerkleTree::compute_root 999.8±47.85ns 996.4±11.16ns -0.34%
OptimalMerkleTree::delete 993.6±20.61ns 1031.6±43.47ns +3.82%
OptimalMerkleTree::get 22.4±0.10ns 22.2±0.16ns -0.89%
OptimalMerkleTree::get_empty_leaves_indices 7.2±0.09ns 7.5±0.12ns +4.17%
OptimalMerkleTree::get_subtree_root 24.0±0.19ns 23.9±0.15ns -0.42%
OptimalMerkleTree::override_range 4.1±0.05µs 4.2±0.06µs +2.44%
OptimalMerkleTree::set 991.9±33.36ns 1002.1±22.22ns +1.03%

Copy link

Benchmark for 3ee9c5f

Click to view benchmark
Test Base PR %
FullMerkleTree::::full_height_gen 14.3±0.13ms 14.3±0.11ms 0.00%
OptimalMerkleTree::::full_height_gen 587.8±5.00µs 585.8±14.08µs -0.34%
Pmtree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
Pmtree::get 363.7±3.36ns 358.1±3.37ns -1.54%
Pmtree::get_empty_leaves_indices 9.3±0.13ns 8.7±0.12ns -6.45%
Pmtree::get_subtree_root 506.3±10.05ns 522.3±7.14ns +3.16%
Pmtree::override_range 233.0±4.04µs 242.4±8.86µs +4.03%
Pmtree::set 54.7±0.48µs 54.9±0.59µs +0.37%
Pmtree:delete 54.9±0.52µs 54.9±0.57µs 0.00%
Set/FullMerkleTree::::set/1 557.9±4.66µs 560.3±14.32µs +0.43%
Set/FullMerkleTree::::set/10 5.6±0.02ms 5.6±0.02ms 0.00%
Set/FullMerkleTree::::set/100 56.0±0.27ms 56.1±1.64ms +0.18%
Set/FullMerkleTree::::set_range/1 560.9±6.44µs 562.4±5.85µs +0.27%
Set/FullMerkleTree::::set_range/10 763.3±6.47µs 763.9±5.07µs +0.08%
Set/FullMerkleTree::::set_range/100 3.3±0.01ms 3.3±0.01ms 0.00%
Set/OptimalMerkleTree::::set/1 564.7±5.91µs 576.9±8.20µs +2.16%
Set/OptimalMerkleTree::::set/10 5.7±0.01ms 5.6±0.04ms -1.75%
Set/OptimalMerkleTree::::set/100 56.4±0.05ms 56.3±0.14ms -0.18%
Set/OptimalMerkleTree::::set_range/1 564.6±6.73µs 563.8±6.43µs -0.14%
Set/OptimalMerkleTree::::set_range/10 5.7±0.02ms 5.6±0.01ms -1.75%
Set/OptimalMerkleTree::::set_range/100 56.7±0.23ms 56.3±0.12ms -0.71%
vk::vk_from_ark_serialized 1583.5±8.06ns 1594.9±21.18ns +0.72%
zkey::zkey_from_raw 3.5±0.01s 3.5±0.01s 0.00%

Copy link

Benchmark for 3ee9c5f

Click to view benchmark
Test Base PR %
FullMerkleTree::::full_height_gen 13.9±0.14ms 13.9±0.07ms 0.00%
OptimalMerkleTree::::full_height_gen 581.4±3.71µs 580.5±4.72µs -0.15%
Pmtree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
Pmtree::get 360.0±3.45ns 374.3±3.96ns +3.97%
Pmtree::get_empty_leaves_indices 8.4±0.07ns 8.4±0.09ns 0.00%
Pmtree::get_subtree_root 502.3±3.02ns 514.4±5.69ns +2.41%
Pmtree::override_range 230.7±3.94µs 232.1±5.21µs +0.61%
Pmtree::set 54.2±0.49µs 54.4±0.39µs +0.37%
Pmtree:delete 54.4±0.84µs 54.5±0.52µs +0.18%
Set/FullMerkleTree::::set/1 558.9±3.78µs 560.3±4.03µs +0.25%
Set/FullMerkleTree::::set/10 5.6±0.13ms 5.6±0.02ms 0.00%
Set/FullMerkleTree::::set/100 55.9±0.12ms 56.0±0.09ms +0.18%
Set/FullMerkleTree::::set_range/1 563.7±17.91µs 559.4±8.27µs -0.76%
Set/FullMerkleTree::::set_range/10 763.9±6.01µs 760.0±9.18µs -0.51%
Set/FullMerkleTree::::set_range/100 3.3±0.03ms 3.3±0.01ms 0.00%
Set/OptimalMerkleTree::::set/1 562.6±2.54µs 560.8±5.74µs -0.32%
Set/OptimalMerkleTree::::set/10 5.7±0.03ms 5.6±0.03ms -1.75%
Set/OptimalMerkleTree::::set/100 56.4±0.47ms 56.0±0.12ms -0.71%
Set/OptimalMerkleTree::::set_range/1 565.4±5.43µs 558.0±4.00µs -1.31%
Set/OptimalMerkleTree::::set_range/10 5.6±0.02ms 5.6±0.02ms 0.00%
Set/OptimalMerkleTree::::set_range/100 56.4±0.10ms 56.3±2.45ms -0.18%
arkzkey::arkzkey_from_raw_compressed 340.3±0.94ms 340.2±0.56ms -0.03%
arkzkey::arkzkey_from_raw_uncompressed 6.0±0.02ms 6.1±0.03ms +1.67%
vk::vk_from_ark_serialized 1620.2±38.25ns 1603.4±6.42ns -1.04%
zkey::zkey_from_raw 3.4±0.01s 3.4±0.01s 0.00%

@markoburcul
Copy link
Author

The problem I am encountering is that we need to get the shared object file as the result but unfortunately with this initial version of the flake that is not possible.The only thing I get is:

[nix-shell:~/work/zerokit]$ tree result
result
├── bin
│   └── rln-cli
└── lib
    └── librln.a

3 directories, 2 files

Within the scripts from nwaku that build this with cross it is using some combination of rustc and cargo flags which is not possible with just cargo or just rustc.

Issuing this command within the rln-wasm subdirectory using a Docker container with rust 1.84 version:
cargo rustc --crate-type=cdylib --release --lib --target=armv7-linux-androideabi
which resulted in multiple compiling errors.
compiler-errors.txt

I expected that when building with cargo build --all-targets there would be also an .so file as result, but I lack the knowledge of cargo build system and internalities of how things are managed within Cargo.toml file.

@markoburcul
Copy link
Author

After discussion with @richard-ramos I've adjusted the build commands and managed to build shared object library with some minor tweaks to the derivation:

  buildPhase = ''
    pushd rln
    cargo rustc --crate-type=cdylib --release --lib
    popd
  '';

  installPhase = ''
    mkdir -p $out/build/
    cp -r ./target/release/librln.so $out/build/
  '';

The next step is to test cross compilation.

@markoburcul
Copy link
Author

I've tried on my laptop to cross compile using:

pkgs.pkgsCross.${target-platform}.rustPlatform.buildRustPackage

but it blocked after it finished build phase for the platform binaries(?) in 30min which is tragic. I'll try it on one of our CI hosts which are more beefy, but if this doesn't work I'll have to think of providing all the packages depending on the target os/arch within the derivation.

@jakubgs
Copy link

jakubgs commented Feb 5, 2025

What do you mean it blocked? It got stuck? Was it doing anything(strace?) or just hanged?

@markoburcul
Copy link
Author

What do you mean it blocked? It got stuck? Was it doing anything(strace?) or just hanged?

I didn't look with strace, it just hanged with no output and then I killed it

@jakubgs
Copy link

jakubgs commented Feb 5, 2025

Well, trying on a bigger CI host is a good idea, especially since this is something that probably will rarely change and we can just cache in our Nix binary cache. But it would be interesting to know if this was a resource problem or some bug if it happens again.

@markoburcul markoburcul force-pushed the add-nix-flake-and-derivation branch from 8d4f36d to d6675af Compare February 6, 2025 09:19
@markoburcul
Copy link
Author

Well, trying on a bigger CI host is a good idea, especially since this is something that probably will rarely change and we can just cache in our Nix binary cache. But it would be interesting to know if this was a resource problem or some bug if it happens again.

I went with path to create a minimal Dockerfile and compile it in container to understand all of the components necessary for compiling the zerokit shared object library:

FROM debian:buster

# Install Rust
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    ca-certificates \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN curl https://sh.rustup.rs -sSf | sh -s -- -y

ENV PATH="/root/.cargo/bin:${PATH}"

# Install required dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates-java \
    openjdk-11-jdk \
    wget \
    unzip \
    build-essential \
    clang \
    make \
    gcc \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Set environment variables for Android NDK
ENV ANDROID_NDK_HOME=/android-ndk
ENV PATH="$ANDROID_NDK_HOME:$PATH"

# Download and install the Android NDK
ARG ANDROID_NDK_VERSION=r27c
RUN wget https://dl.google.com/android/repository/android-ndk-${ANDROID_NDK_VERSION}-linux.zip \
    && unzip android-ndk-${ANDROID_NDK_VERSION}-linux.zip -d / \
    && mv /android-ndk-${ANDROID_NDK_VERSION} $ANDROID_NDK_HOME \
    && rm android-ndk-${ANDROID_NDK_VERSION}-linux.zip

# Install Rust targets for Android
RUN rustup target add armv7-linux-androideabi

ENV CC=/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi25-clang
ENV CXX=/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi25-clang++
ENV CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER=/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi25-clang

# Set working directory
WORKDIR /workspace

# Default command
CMD ["/bin/bash"]

run it inside zerokit repo:

docker run --rm -it -v ./:/workspace cross_rust:latest bash

compile inside container:

cargo rustc --crate-type=cdylib --release --lib --target=armv7-linux-androideabi

I think I need to figure out how to point to the right linker from android ndk using the env variable CARGO_TARGET_<TRIPLE>_LINKER.

@markoburcul markoburcul force-pushed the add-nix-flake-and-derivation branch from 3d4aeae to 1ad8b55 Compare February 6, 2025 17:09
@vacp2p vacp2p deleted a comment from github-actions bot Feb 6, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 6, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 6, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 6, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 6, 2025
Copy link

github-actions bot commented Feb 6, 2025

Benchmark for 5c008eb

Click to view benchmark
Test Base PR %
FullMerkleTree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
FullMerkleTree::delete 864.4±12.99ns 865.6±10.91ns +0.14%
FullMerkleTree::get 3.4±0.03ns 3.4±0.03ns 0.00%
FullMerkleTree::get_empty_leaves_indices 7.5±0.06ns 7.5±0.07ns 0.00%
FullMerkleTree::get_subtree_root 3.7±0.09ns 3.6±0.10ns -2.70%
FullMerkleTree::override_range 1348.9±14.82ns 1325.0±9.48ns -1.77%
FullMerkleTree::set 856.3±6.68ns 858.3±11.55ns +0.23%
OptimalMerkleTree::compute_root 1035.9±11.21ns 1005.5±14.24ns -2.93%
OptimalMerkleTree::delete 1014.5±8.14ns 1012.6±81.37ns -0.19%
OptimalMerkleTree::get 25.9±0.23ns 22.4±1.13ns -13.51%
OptimalMerkleTree::get_empty_leaves_indices 7.5±0.06ns 7.5±0.12ns 0.00%
OptimalMerkleTree::get_subtree_root 24.1±0.28ns 24.2±0.27ns +0.41%
OptimalMerkleTree::override_range 4.1±0.03µs 4.1±0.14µs 0.00%
OptimalMerkleTree::set 1009.8±14.06ns 1000.2±19.33ns -0.95%

Copy link

github-actions bot commented Feb 6, 2025

Benchmark for 114376b

Click to view benchmark
Test Base PR %
FullMerkleTree::compute_root 0.0±0.00ns 0.0±0.00ns NaN%
FullMerkleTree::delete 856.0±5.44ns 858.2±6.05ns +0.26%
FullMerkleTree::get 3.5±0.06ns 3.4±0.12ns -2.86%
FullMerkleTree::get_empty_leaves_indices 7.5±0.42ns 7.5±0.07ns 0.00%
FullMerkleTree::get_subtree_root 3.7±0.08ns 3.7±0.10ns 0.00%
FullMerkleTree::override_range 1313.2±9.83ns 1325.9±8.38ns +0.97%
FullMerkleTree::set 862.7±11.67ns 872.9±14.06ns +1.18%
OptimalMerkleTree::compute_root 1004.0±12.17ns 1005.9±16.84ns +0.19%
OptimalMerkleTree::delete 997.0±11.46ns 1013.9±15.89ns +1.70%
OptimalMerkleTree::get 22.4±0.27ns 22.4±0.35ns 0.00%
OptimalMerkleTree::get_empty_leaves_indices 7.5±0.10ns 7.5±0.31ns 0.00%
OptimalMerkleTree::get_subtree_root 24.0±0.22ns 24.0±0.35ns 0.00%
OptimalMerkleTree::override_range 4.1±0.05µs 4.1±0.05µs 0.00%
OptimalMerkleTree::set 998.0±11.66ns 1033.5±56.99ns +3.56%

@markoburcul
Copy link
Author

I've managed to cross compile for aarch64-linux-android so I commented out stuff that is not needed. Apparently Android NDK is included when using pkgsCross with aarch64-android-prebuilt as target.
When trying to compile for armv7-linux-androideabi with cross target being armv7a-android-prebuilt it fails with linker error for one if its crates.
build.log

Another thing I tried is to cross compile the code, in my docker container from above, without using rustup to install targets and the error is basically related to std not being there which is expected:

error[E0463]: can't find crate for `core`
  |
  = note: the `aarch64-linux-android` target may not be installed
  = help: consider downloading the target with `rustup target add aarch64-linux-android`

For more information about this error, try `rustc --explain E0463`.
error: could not compile `libc` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error[E0463]: can't find crate for `std`
  |
  = note: the `aarch64-linux-android` target may not be installed
  = help: consider downloading the target with `rustup target add aarch64-linux-android`

error: could not compile `memoffset` (lib) due to 1 previous error
error: could not compile `cfg-if` (lib) due to 1 previous error
error: could not compile `scopeguard` (lib) due to 1 previous error
error: could not compile `either` (lib) due to 1 previous error
error: could not compile `once_cell` (lib) due to 1 previous error
error: could not compile `num-traits` (lib) due to 1 previous error
error: could not compile `memchr` (lib) due to 1 previous error
error: could not compile `ppv-lite86` (lib) due to 1 previous error
error: could not compile `smallvec` (lib) due to 1 previous error
error: could not compile `serde` (lib) due to 1 previous error

What I don't like is that I still haven't found a way to mitigate this issue and usage of rustup.

@markoburcul markoburcul force-pushed the add-nix-flake-and-derivation branch from 1ad8b55 to b17d9a2 Compare February 7, 2025 09:48
@markoburcul markoburcul force-pushed the add-nix-flake-and-derivation branch from b17d9a2 to 4479810 Compare February 7, 2025 10:48
@vacp2p vacp2p deleted a comment from github-actions bot Feb 11, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 11, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 11, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 11, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 11, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 11, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 11, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 11, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 11, 2025
@vacp2p vacp2p deleted a comment from github-actions bot Feb 11, 2025
@markoburcul
Copy link
Author

I've managed to eliminate rustup from the equation, as an example this is the Dockerfile:

# Use a base image with a minimal installation of Ubuntu
FROM ubuntu:20.04

# Set the working directory
WORKDIR /tmp

# Update and install required dependencies
RUN apt-get update && apt-get install -y \
    curl \
    wget \
    unzip \
    xz-utils \
    build-essential \
    clang \
    make \
    gcc \
    file

# Download necessary files
RUN wget https://dl.google.com/android/repository/android-ndk-r28-linux.zip && \
    wget https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init && \
    wget https://static.rust-lang.org/dist/2025-01-30/rust-std-1.84.1-x86_64-linux-android.tar.gz && \
    wget https://static.rust-lang.org/dist/rust-1.84.1-x86_64-unknown-linux-gnu.tar.xz && \
    wget https://static.rust-lang.org/dist/2025-01-30/rust-std-1.84.1-aarch64-linux-android.tar.gz

# Extract the Android NDK
RUN unzip android-ndk-r28-linux.zip -d /android-ndk/ && \
    mv /android-ndk/android-ndk-r28/* /android-ndk/ && \
    rm -rf /android-ndk/android-ndk-r28

# Extract and install the Rust toolchain
RUN tar -xvzf rust-std-1.84.1-x86_64-linux-android.tar.gz && \
    tar -xvzf rust-std-1.84.1-aarch64-linux-android.tar.gz && \
    tar -xvf rust-1.84.1-x86_64-unknown-linux-gnu.tar.xz && \
    ./rust-1.84.1-x86_64-unknown-linux-gnu/install.sh --prefix=$HOME/rust-install/rust-1.84.1-x86_64-unknown-linux-gnu && \
    ./rust-std-1.84.1-x86_64-linux-android/install.sh --prefix=$HOME/rust-install/rust-1.84.1-x86_64-unknown-linux-gnu && \
    ./rust-std-1.84.1-aarch64-linux-android/install.sh --prefix=$HOME/rust-install/rust-1.84.1-x86_64-unknown-linux-gnu

    

# Set up Rust toolchain
RUN chmod +x rustup-init && \
    ./rustup-init --default-toolchain none -y && \
    . "$HOME/.cargo/env" && \
    rustup toolchain link rust-toolchain-1.84.1 $HOME/rust-install/rust-1.84.1-x86_64-unknown-linux-gnu && \
    rustup default rust-toolchain-1.84.1

# Export environment variables for Android NDK
RUN export CC=/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android25-clang && \
    export CXX=/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android25-clang++ && \
    export CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER=/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android25-clang

# Set up working directory for your project
WORKDIR /workspace

CMD ["bash"]

Inside of the container you can easily compile the RLN shared object library. The only thing that needs to be changed when compiling are CC, CXX and CARGO_TARGET__LINKER variable and their values.

The issue is that in Nix derivation you can't run bash scripts since env is not available so removing rustup from the equation is not a valid approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants