Skip to content

Commit

Permalink
Nixify code from ocaml/ directory
Browse files Browse the repository at this point in the history
Add a flake.nix that is capable of building ocaml components of
bindings.
  • Loading branch information
georgeee committed Aug 14, 2024
1 parent f6022c7 commit 9767adf
Show file tree
Hide file tree
Showing 12 changed files with 713 additions and 51 deletions.
534 changes: 534 additions & 0 deletions flake.lock

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11-small";
mina.url =
"git+https://github.com/minaprotocol/mina?submodules=1&ref=georgeee/granular-nix&shallow=1";
mina.inputs.nixpkgs.follows = "nixpkgs";
describe-dune.url = "github:o1-labs/describe-dune";
describe-dune.inputs.nixpkgs.follows = "nixpkgs";
describe-dune.inputs.flake-utils.follows = "flake-utils";
dune-nix.url = "github:o1-labs/dune-nix";
dune-nix.inputs.nixpkgs.follows = "nixpkgs";
dune-nix.inputs.flake-utils.follows = "flake-utils";
mina.inputs.dune-nix.follows = "dune-nix";
mina.inputs.describe-dune.follows = "describe-dune";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }@inputs:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
dune-nix = inputs.dune-nix.lib.${system};
describe-dune = inputs.describe-dune.defaultPackage.${system};
dune-description = pkgs.stdenv.mkDerivation {
pname = "dune-description";
version = "dev";
src = with pkgs.lib.fileset;
(toSource {
root = ./.;
fileset = unions [
./ocaml/dune
./ocaml/lib/dune
./ocaml/dune-project
./ocaml/js/dune
];
});
phases = [ "unpackPhase" "buildPhase" ];
buildPhase = ''
${describe-dune}/bin/describe-dune > $out
'';
};
desc = builtins.fromJSON (builtins.readFile dune-description);
allOcamlDeps_ = pkgs.lib.concatMap (duneSpec:
pkgs.lib.concatMap (unitSpec: unitSpec.deps or [ ])
(duneSpec.units or [ ])) desc;
allOcamlDeps =
builtins.map (d: builtins.head (pkgs.lib.splitString "." d))
allOcamlDeps_;
mina = inputs.mina.packages."${system}";
minaDepsDrv = mina.info.deps;
minaDeps_ =
builtins.intersectAttrs (pkgs.lib.genAttrs allOcamlDeps (_: { }))
mina.info.raw.deps.units;
minaDeps = builtins.attrNames (builtins.foldl'
(acc: pkg: acc // dune-nix.deps.packageDeps minaDeps_ "pkgs" pkg)
minaDeps_ (builtins.attrNames minaDeps_));
commonOverrides = {
DUNE_PROFILE = "dev";
buildInputs = [ mina.base-libs ] ++ mina.external-libs
++ pkgs.lib.attrVals minaDeps mina.pkgs;
};
info = dune-nix.info desc;
allDeps = dune-nix.allDeps info;
noTestSkipping = _: false;
prj_ = dune-nix.outputs' commonOverrides (pkgs.lib.fileset.toSource {
root = ./.;
fileset = ./ocaml;
}) allDeps info noTestSkipping prj;
prj = prj_ // {
pkgs = prj_.pkgs // {
__ocaml-js__ = prj_.pkgs.__ocaml-js__.overrideAttrs {
PREBUILT_KIMCHI_BINDINGS_JS_WEB =
"${mina.files.src-lib-crypto-kimchi_bindings-js-web}/src/lib/crypto/kimchi_bindings/js/web";
PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS =
"${mina.files.src-lib-crypto-kimchi_bindings-js-node_js}/src/lib/crypto/kimchi_bindings/js/node_js";
};
};
};
in {
formatter = pkgs.nixfmt;
# TODO build from ./ocaml root, not ./. (after fixing a bug in dune-nix)
packages = {
inherit dune-description;
default = prj.pkgs.o1js_bindings;
js = prj.pkgs.__ocaml-js__;
};
});
}
52 changes: 4 additions & 48 deletions ocaml/dune
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
(env
(_
(js_of_ocaml
(compilation_mode whole_program))))

(data_only_dirs test_module)

(vendored_dirs o1js)

(executable
(name o1js_node)
(modules o1js_node)
(modes js)
(flags (-g))
(link_flags
(-noautolink -g))
(js_of_ocaml
(flags +toplevel.js +dynlink.js --pretty --source-map)
(link_flags --source-map)
(javascript_files overrides.js))
(libraries o1js_bindings_lib bindings_js.node_backend)
(link_deps
../../mina/src/lib/crypto/kimchi_bindings/js/node_js/plonk_wasm.js
../../mina/src/lib/crypto/kimchi_bindings/js/node_js/plonk_wasm_bg.wasm)
(instrumentation
(backend bisect_ppx))
(forbidden_libraries async core re2 ctypes)
(preprocess
(pps ppx_version js_of_ocaml-ppx)))

(executable
(name o1js_web)
(modules o1js_web)
(modes js)
(link_flags (-noautolink))
(js_of_ocaml
(flags +toplevel.js +dynlink.js --pretty)
(javascript_files overrides.js))
(libraries o1js_bindings_lib bindings_js.web_backend)
(link_deps
../../mina/src/lib/crypto/kimchi_bindings/js/web/plonk_wasm.js
../../mina/src/lib/crypto/kimchi_bindings/js/web/plonk_wasm_bg.wasm)
(instrumentation
(backend bisect_ppx))
(forbidden_libraries async core re2 ctypes)
(preprocess
(pps ppx_version js_of_ocaml-ppx)))

(executable
(name o1js_types)
(public_name o1js-types)
(package o1js_bindings)
(modules o1js_types)
(link_flags (-linkall))
(modes native)
Expand All @@ -66,6 +20,8 @@

(executable
(name o1js_constants)
(public_name o1js-constants)
(package o1js_bindings)
(modules o1js_constants)
(link_flags (-linkall))
(modes native)
Expand Down
3 changes: 3 additions & 0 deletions ocaml/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lang dune 3.3)

(package (name o1js_bindings))
81 changes: 81 additions & 0 deletions ocaml/js/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
(env
(_
(js_of_ocaml
(compilation_mode whole_program))))

(rule
(enabled_if
(= %{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n} n))
(targets node_js_plonk_wasm.js node_js_plonk_wasm_bg.wasm)
(deps (:d1 ../../../mina/src/lib/crypto/kimchi_bindings/js/node_js/plonk_wasm.js) (:d2 ../../../mina/src/lib/crypto/kimchi_bindings/js/node_js/plonk_wasm_bg.wasm) )
(action
(progn
(run cp %{d1} node_js_plonk_wasm.js)
(run cp %{d2} node_js_plonk_wasm_bg.wasm)
)))

(rule
(enabled_if
(= %{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n} n))
(targets web_plonk_wasm.js web_plonk_wasm_bg.wasm)
(deps (:d1 ../../../mina/src/lib/crypto/kimchi_bindings/js/web/plonk_wasm.js) (:d2 ../../../mina/src/lib/crypto/kimchi_bindings/js/web/plonk_wasm_bg.wasm) )
(action
(progn
(run cp %{d1} web_plonk_wasm.js)
(run cp %{d2} web_plonk_wasm_bg.wasm)
)))

(rule
(enabled_if
(<> %{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n} n))
(targets node_js_plonk_wasm.js node_js_plonk_wasm_bg.wasm)
(action
(progn
(run cp %{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n}/plonk_wasm.js node_js_plonk_wasm.js)
(run cp %{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n}/plonk_wasm_bg.wasm node_js_plonk_wasm_bg.wasm)
)))

(rule
(enabled_if
(<> %{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n} n))
(targets web_plonk_wasm.js web_plonk_wasm_bg.wasm)
(action
(progn
(run cp %{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n}/plonk_wasm.js web_plonk_wasm.js)
(run cp %{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n}/plonk_wasm_bg.wasm web_plonk_wasm_bg.wasm)
)))

(executable
(name o1js_node)
(modules o1js_node)
(modes js)
(flags (-g))
(link_flags
(-noautolink -g))
(js_of_ocaml
(flags +toplevel.js +dynlink.js --pretty --source-map)
(link_flags --source-map)
(javascript_files overrides.js))
(libraries o1js_bindings.lib bindings_js.node_backend)
(link_deps node_js_plonk_wasm.js node_js_plonk_wasm_bg.wasm)
(instrumentation
(backend bisect_ppx))
(forbidden_libraries async core re2 ctypes)
(preprocess
(pps ppx_version js_of_ocaml-ppx)))

(executable
(name o1js_web)
(modules o1js_web)
(modes js)
(link_flags (-noautolink))
(js_of_ocaml
(flags +toplevel.js +dynlink.js --pretty)
(javascript_files overrides.js))
(libraries o1js_bindings.lib bindings_js.web_backend)
(link_deps web_plonk_wasm.js web_plonk_wasm_bg.wasm)
(instrumentation
(backend bisect_ppx))
(forbidden_libraries async core re2 ctypes)
(preprocess
(pps ppx_version js_of_ocaml-ppx)))
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions ocaml/lib/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(library
(public_name o1js_bindings.lib)
(name o1js_bindings_lib)
(libraries
;; opam libraries ;;
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-o1js-node-artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e

MINA_PATH="src/mina"
DUNE_PATH="src/bindings/ocaml"
DUNE_PATH="src/bindings/ocaml/js"
BUILD_PATH="_build/default/$DUNE_PATH"
KIMCHI_BINDINGS="$MINA_PATH/src/lib/crypto/kimchi_bindings"

Expand Down
2 changes: 1 addition & 1 deletion scripts/build-o1js-web.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e

MINA_PATH="src/mina"
DUNE_PATH="src/bindings/ocaml"
DUNE_PATH="src/bindings/ocaml/js"
BUILD_PATH="_build/default/$DUNE_PATH"
KIMCHI_BINDINGS="$MINA_PATH/src/lib/crypto/kimchi_bindings"
WEB_BINDINGS="src/bindings/compiled/web_bindings"
Expand Down
2 changes: 1 addition & 1 deletion scripts/update-o1js-bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e

MINA_PATH="src/mina"
DUNE_PATH="src/bindings/ocaml"
DUNE_PATH="src/bindings/ocaml/js"
BUILD_PATH="_build/default/$DUNE_PATH"
DIR_PATH=$(dirname "$0")
KIMCHI_BINDINGS="$MINA_PATH/src/lib/crypto/kimchi_bindings"
Expand Down

0 comments on commit 9767adf

Please sign in to comment.