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

build(nix): Use crane to cache cargo dependencies #121

Merged
merged 7 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions crates/arrowstore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "arrowstore"
version = "0.1.0"
edition = "2021"
build = "build_proto.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions crates/raft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "raft"
version = "0.1.0"
edition = "2021"
build = "build_proto.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
File renamed without changes.
7 changes: 5 additions & 2 deletions flake-parts/devshell/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
config,
pkgs,
system,
inputs',
...
}: let
rust-stable = self.lib.rust-stable system;
rust-nightly = self.lib.rust-nightly system;
rust-stable = inputs'.fenix.packages.stable.toolchain;
rust-nightly = inputs'.fenix.packages.latest.toolchain;
Comment on lines +13 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, what does the fenix version get us that the other one doesn't?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were using rust-overlay before, but weren't using it as an overlay. In nix an overlay basically adds/overrides packages from nixpkgs. Instead of applying that I had looked at the flake outputs from it and saw the overlay flake exported a package with the toolchain, but they don't make any guarantee's on the stability of any outputs but the overlay as that is the purpose of the flake.

Fenix is designed specifically for getting the toolchain without mucking around with overlays. According to their readme the goal is to be a replacement for rustup. It also works particularly well with crane as we can pass a fenix toolchain directly to crane


devTools = with pkgs; [
rustfmt
bacon
Expand All @@ -37,6 +39,7 @@
nativeBuildInputs = otherNativeBuildInputs;
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath buildInputs;
inherit LIBCLANG_PATH;
inherit PROTOC PROTOC_INCLUDE;
};
nightly = pkgs.mkShell rec {
buildInputs = [rust-nightly] ++ devTools ++ otherBuildInputs;
Expand Down
23 changes: 18 additions & 5 deletions flake-parts/lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
{
inputs,
self,
lib,
...
}: let
inherit (inputs.gitignore.lib) gitignoreSource;
in {
flake.lib = {
flake_source = gitignoreSource ../..;
cargo_lock = ../../Cargo.lock;
rust-stable = system: inputs.rust-overlay.packages.${system}.rust;
rust-nightly = system: inputs.rust-overlay.packages.${system}.rust-nightly;

otherNativeBuildInputs = pkgs: with pkgs; [pkgconfig openssl openssl.dev llvmPackages.bintools llvmPackages.libclang];
otherBuildInputs = pkgs:
with pkgs; [
clang
openssl
protobuf
];

# Filters for files to include in nix store
filterProto = orig_path: type:
let
path = (toString orig_path);
base = baseNameOf path;

matchesSuffix = lib.any (suffix: lib.hasSuffix suffix base) [
# Include all proto files
".proto"
];
in
type == "directory" || matchesSuffix;

filterSrc = craneLib: orig_path: type:
(self.lib.filterProto orig_path type) || (craneLib.filterCargoSources orig_path type);

};
}
60 changes: 38 additions & 22 deletions flake-parts/packages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,53 @@
config,
pkgs,
system,
inputs',
...
}: let
rust-stable = self.lib.rust-stable system;
rust-nightly = self.lib.rust-nightly system;

otherNativeBuildInputs = self.lib.otherNativeBuildInputs pkgs;
otherBuildInputs = self.lib.otherBuildInputs pkgs;

craneLib = inputs.crane.lib.${system}.overrideToolchain
inputs'.fenix.packages.stable.toolchain;

common-build-args = rec {
src = lib.cleanSourceWith {
src = ../..;
filter = self.lib.filterSrc craneLib;
};

buildInputs = otherBuildInputs;
nativeBuildInputs = otherNativeBuildInputs;
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath buildInputs;
BINDGEN_EXTRA_CLANG_ARGS = "-isystem ${pkgs.llvmPackages.libclang.lib}/lib/clang/${lib.getVersion pkgs.clang}/include";
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
PROTOC = "${pkgs.protobuf}/bin/protoc";
PROTOC_INCLUDE = "${pkgs.protobuf}/include";
};
cargoArtifacts = craneLib.buildDepsOnly ({
pname = "glaredb";
} // common-build-args);

clippy-check = craneLib.cargoClippy ({
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-features -- --deny warnings";
} // common-build-args);

in rec {
checks = {
inherit clippy-check;
build-crate = packages.default;
};
packages = {
default = packages.cli;
cli = pkgs.rustPlatform.buildRustPackage rec {
pname = "glaredb-cli";
version = "0.1.0";

buildAndTestSubdir = "crates/glaredb";
src = self.lib.flake_source;
cargoLock = {
lockFile = self.lib.cargo_lock;
outputHashes = {
"openraft-0.6.4" = "sha256-JXeEzUYeJdxDTYoJvniRt/WfdCT6FHsfauTI5KyZYzA=";
};
};
buildInputs = [rust-stable] ++ otherBuildInputs;
nativeBuildInputs = otherNativeBuildInputs;
# cli = craneLib.cargoBuild ({
cli = craneLib.buildPackage ({
pname = "glaredb-cli";
inherit cargoArtifacts;
cargoExtraArgs = "--bin glaredb";
} // common-build-args);

LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath buildInputs;
BINDGEN_EXTRA_CLANG_ARGS = "-isystem ${pkgs.llvmPackages.libclang.lib}/lib/clang/${lib.getVersion pkgs.clang}/include";
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
PROTOC = "${pkgs.protobuf}/bin/protoc";
PROTOC_INCLUDE = "${pkgs.protobuf}/include";
};
server_image = pkgs.dockerTools.buildLayeredImage {
name = "glaredb";
contents = [packages.cli];
Expand Down
96 changes: 76 additions & 20 deletions flake.lock

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

8 changes: 6 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
url = "github:hercules-ci/flake-parts";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-overlay = {
url = "github:oxalica/rust-overlay";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
gitignore = {
url = "github:hercules-ci/gitignore.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = {
Expand Down