refactor(tvix): build Rust projects using crate2nix

Introduces granular dependency builds using crate2nix, bootstrapped
off the generated configuration from the newly introduced
workspace (see cl/7533).

This commit checks in the generated Cargo.nix file which can be
regenerated with a parameterless invocation of `crate2nix generate` in
`//tvix`. I tried generating this in IFD, but it turned out to be
harder than what seemed worthwhile for now.

In this setup, the various build targets for Rust projects end up
being attributes of the imported `Cargo.nix` file at the `tvix.crates`
attribute. These still lack configuration, however, which has been
fixed in the various `default.nix` files of individual projects.

Note that we (temporarily) lose the ability to build tvix-eval's
benchmarks in CI. I haven't figured out what magic incantation summons
them from the void again ...

The `eval-okay-readDir` tests from both test suites have been disabled
because they fail for unknown reasons when run in this new derivation.
Somebody will have to debug it!

Change-Id: I2014614ccb9c8951aedbd71df7966ca191a13695
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7538
Autosubmit: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2022-12-07 14:05:02 +03:00 committed by tazjin
parent d9d627cdf0
commit 3aca3d3bba
12 changed files with 6346 additions and 57 deletions

View file

@ -46,7 +46,7 @@ let
[dependencies.tvix-eval] [dependencies.tvix-eval]
-path = "../../tvix/eval" -path = "../../tvix/eval"
+path = "${depot.tvix.eval.src}" +path = "${depot.tvix.crates.workspaceMembers.tvix-eval.build.src}"
default-features = false default-features = false
[dependencies.serde] [dependencies.serde]

6296
tvix/Cargo.nix Normal file

File diff suppressed because it is too large Load diff

View file

@ -6,6 +6,23 @@ are interested in people who would like to help us review designs,
brainstorm and describe requirements that we may not yet have brainstorm and describe requirements that we may not yet have
considered. considered.
## Rust projects
Some parts of Tvix are written in Rust. To simplify the dependency
management on the Nix side of these builds, we use `crate2nix` in a
single Rust workspace in `//tvix` to maintain the Nix build
configuration.
When making changes to Cargo dependency configuration in any of the
Rust projects under `//tvix`, be sure to run `crate2nix generate` in
`//tvix` itself and commit the changes to the generated `Cargo.nix`
file.
`crate2nix` is available via `direnv` inside of depot, or can be built
from the `third_party.nixpkgs.crate2nix` attribute of depot. Make sure
to build it from depot to avoid generating files with a different
version that might have different output.
## License structure ## License structure
All code implemented for Tvix is licensed under the GPL-3.0, with the All code implemented for Tvix is licensed under the GPL-3.0, with the

View file

@ -1,14 +1,10 @@
# Nix helpers for projects under //tvix # Nix helpers for projects under //tvix
{ pkgs, ... }: { pkgs, ... }:
{ {
# Construct a sparse tree for naersk's `root` field, for # Load the crate2nix crate tree.
# compatibility with the workspace-workaround (see top-level comment crates = import ./Cargo.nix {
# in //tvix/Cargo.toml) inherit pkgs;
naerskRootFor = cargoToml: pkgs.runCommand "sparse-tvix-root" { } '' nixpkgs = pkgs.path;
mkdir $out };
cp -aT ${./Cargo.lock} $out/Cargo.lock
cp -aT ${cargoToml} $out/Cargo.toml
'';
} }

View file

@ -1,34 +1,9 @@
{ depot, pkgs, lib, ... }: # TODO: find a way to build the benchmarks via crate2nix
{ depot, pkgs, ... }:
lib.fix (self: depot.third_party.naersk.buildPackage (lib.fix (naerskArgs: { depot.tvix.crates.workspaceMembers.tvix-eval.build.override {
src = depot.third_party.gitignoreSource ./.; runTests = true;
# see https://github.com/nix-community/naersk/issues/169
root = depot.tvix.naerskRootFor ./Cargo.toml;
doCheck = true; # Make C++ Nix available, to compare eval results against.
testInputs = [ pkgs.nix ];
# Tell the test suite where to find upstream nix, to compare eval results }
# against
NIX_INSTANTIATE_BINARY_PATH = "${pkgs.nix}/bin/nix-instantiate";
meta.ci.targets = builtins.attrNames self.passthru;
copySources = [
"builtin-macros"
];
passthru.benchmarks = depot.third_party.naersk.buildPackage (naerskArgs // {
name = "tvix-eval-benchmarks";
doCheck = false;
cargoBuildOptions = opts: opts ++ [ "--benches" ];
copyBinsFilter = ''
select(.reason == "compiler-artifact" and any(.target.kind[] == "bench"; .))
'';
passthru = { };
});
}))
)

View file

@ -1,8 +1,5 @@
{ depot, pkgs, ... }: { depot, ... }:
depot.third_party.naersk.buildPackage { depot.tvix.crates.workspaceMembers.nix-cli.build.override {
src = ./.; runTests = true;
root = depot.tvix.naerskRootFor ./Cargo.toml;
doDoc = false;
doCheck = true;
} }

View file

@ -1,19 +1,27 @@
{ depot, pkgs, lib, ... }: { depot, pkgs, ... }:
let let
protoRoot = depot.nix.sparseTree depot.path.origSrc [ protoRoot = depot.nix.sparseTree depot.path.origSrc [
./protos/castore.proto ./protos/castore.proto
./protos/pathinfo.proto ./protos/pathinfo.proto
]; ];
protobufDep = prev: (prev.nativeBuildInputs or [ ]) ++ [ pkgs.protobuf ];
in in
depot.third_party.naersk.buildPackage { depot.tvix.crates.workspaceMembers.tvix-store.build.override {
src = depot.third_party.gitignoreSource ./.; # Ensure protobuf dependencies are available.
# see https://github.com/nix-community/naersk/issues/169 # TODO: figure out a way to embed this directly in the //tvix
root = depot.tvix.naerskRootFor ./Cargo.toml; # crate2nix config.
crateOverrides = {
nativeBuildInputs = [ pkgs.protobuf ]; prost-build = prev: {
nativeBuildInputs = protobufDep prev;
};
tvix-store = prev: {
PROTO_ROOT = protoRoot; PROTO_ROOT = protoRoot;
nativeBuildInputs = protobufDep prev;
};
};
doCheck = true; runTests = true;
} }