Skip to content

Commit

Permalink
build(nix): Use crane to cache cargo dependencies (#121)
Browse files Browse the repository at this point in the history
* Add crane

* switch rust toolchain to come from fenix

* touch buildscript before executing cargo build

* add clippy and build checks

* rename arrowstore build script

* rename raft build script
  • Loading branch information
justinrubek authored Sep 23, 2022
1 parent 9994be0 commit 9fa25a8
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 51 deletions.
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;

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

0 comments on commit 9fa25a8

Please sign in to comment.