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

feat: Support nix package manager #234

Merged
merged 7 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/nix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Nix builds

on: [push]
kobyhallx marked this conversation as resolved.
Show resolved Hide resolved

# This will cancel previous runs when a branch or PR is updated
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref || github.run_id }}
cancel-in-progress: true

jobs:
nix-build:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
target: [llvm12, wasm32, cross-aarch64]

steps:
- name: Checkout
uses: actions/checkout@v3

- uses: cachix/install-nix-action@v20
with:
nix_path: nixpkgs=channel:nixos-22.11
github_access_token: ${{ secrets.GITHUB_TOKEN }}

- name: Build barretenberg as ${{ matrix.target }}
run: |
nix build -L .#${{ matrix.target }}

- name: Prepare artifact
run: |
mkdir artifacts
cp -a ./result/. artifacts
7z a -ttar -so -an ./artifacts/* | 7z a -si ./libbarretenberg.tar.gz

- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: libbarretenberg-${{ runner.os }}-${{ matrix.target }}.tar.gz
path: ./libbarretenberg.tar.gz
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
.vscode
result
41 changes: 41 additions & 0 deletions barretenberg.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{ overrideCC, stdenv, llvmPackages, cmake, ninja, lib, callPackage, binaryen, gcc11 }:
let
targetPlatform = stdenv.targetPlatform;
buildEnv =
if (stdenv.targetPlatform.isGnu && stdenv.targetPlatform.isAarch64) then
# As per https://discourse.nixos.org/t/gcc11stdenv-and-clang/17734/7 since it seems that aarch64-linux uses
# gcc9 instead of gcc11 for the C++ stdlib, while all other targets we support provide the correct libstdc++
overrideCC llvmPackages.stdenv (llvmPackages.clang.override { gccForLibs = gcc11.cc; })
else
llvmPackages.stdenv;
optionals = lib.lists.optionals;
toolchain_file = ./cpp/cmake/toolchains/${targetPlatform.system}.cmake;
in
buildEnv.mkDerivation
{
pname = "libbarretenberg";
version = "0.1.0";

src = ./cpp;

nativeBuildInputs = [ cmake ninja ]
kobyhallx marked this conversation as resolved.
Show resolved Hide resolved
++ optionals targetPlatform.isWasm [ binaryen ];
kobyhallx marked this conversation as resolved.
Show resolved Hide resolved

buildInputs = [ ]
++ optionals (targetPlatform.isDarwin || targetPlatform.isLinux) [
llvmPackages.openmp
];

cmakeFlags = [
"-DTESTING=OFF"
"-DBENCHMARKS=OFF"
"-DCMAKE_TOOLCHAIN_FILE=${toolchain_file}"
]
++ optionals (targetPlatform.isDarwin || targetPlatform.isLinux)
[ "-DCMAKE_BUILD_TYPE=RelWithAssert" ];

NIX_CFLAGS_COMPILE =
optionals targetPlatform.isDarwin [ " -fno-aligned-allocation" ];

enableParallelBuilding = true;
}
43 changes: 43 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
description =
"Barretenberg: C++ cryptographic library, BN254 elliptic curve library, and PLONK SNARK prover";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils }:
let
barretenbergOverlay = self: super: {
# It seems that llvmPackages_11 can't build WASI, so default to llvmPackages_12
barretenberg = super.callPackage ./barretenberg.nix {
llvmPackages = self.llvmPackages_12;
};
};
in
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ barretenbergOverlay ];
};

optional = pkgs.lib.lists.optional;

crossTargets = builtins.listToAttrs
(
[ ] ++ optional (pkgs.hostPlatform.isx86_64 && pkgs.hostPlatform.isLinux) {
name = "cross-aarch64";
value = pkgs.pkgsCross.aarch64-multiplatform-musl.pkgsLLVM.barretenberg;
} ++ optional (pkgs.hostPlatform.isx86_64 && pkgs.hostPlatform.isDarwin) {
name = "cross-aarch64";
value = pkgs.pkgsCross.aarch64-darwin.barretenberg.override {
# llvmPackages_12 seems to fail when we try to cross-compile but llvmPackages_11 works
llvmPackages = pkgs.llvmPackages_11;
};
}
);

shellDefaults = {
nativeBuildInputs = [
pkgs.starship
pkgs.llvmPackages_12.llvm
];

shellHook = ''
eval "$(starship init bash)"
'';
};
in
rec {
packages = {
llvm11 = pkgs.barretenberg.override {
llvmPackages = pkgs.llvmPackages_11;
};
llvm12 = pkgs.barretenberg;
llvm13 = pkgs.barretenberg.override {
llvmPackages = pkgs.llvmPackages_13;
};
llvm14 = pkgs.barretenberg.override {
llvmPackages = pkgs.llvmPackages_14;
};
wasm32 = pkgs.pkgsCross.wasi32.barretenberg;

# Defaulting to llvm12 so we can ensure consistent shells
default = packages.llvm12;
} // crossTargets;

# Provide legacyPackages with our overlay so we can run
# > `nix build .#pkgsCross.aarch64-multiplatform.barretenberg`
# Ref https://discourse.nixos.org/t/how-do-i-cross-compile-a-flake/12062/12
legacyPackages = import nixpkgs {
inherit system;
overlays = [ barretenbergOverlay ];
crossOverlays = [ barretenbergOverlay ];
};

devShells = {
default = pkgs.mkShell.override { stdenv = packages.default.stdenv; }
({
inputsFrom =
[ packages.default ];
} // shellDefaults);

wasm32 = pkgs.mkShell.override
{
stdenv = packages.wasm32.stdenv;
}
({
inputsFrom = [ packages.wasm32 ];
} // shellDefaults);
};
}) // {
overlays.default = barretenbergOverlay;
};
}