-
-
Save greglearns/a8f63f33040248411f5ef281dde51e33 to your computer and use it in GitHub Desktop.
Nix Flake for building a rust project that has a build.rs that references a file that is "../another_directory/file"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/ipetkov/crane/blob/master/examples/build-std/flake.nix | |
{ | |
inputs = { | |
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; | |
crane = { | |
url = "github:ipetkov/crane"; | |
inputs.nixpkgs.follows = "nixpkgs"; | |
}; | |
flake-utils.url = "github:numtide/flake-utils"; | |
rust-overlay = { | |
url = "github:oxalica/rust-overlay"; | |
inputs = { | |
nixpkgs.follows = "nixpkgs"; | |
flake-utils.follows = "flake-utils"; | |
}; | |
}; | |
}; | |
outputs = { self, nixpkgs, crane, flake-utils, rust-overlay }: | |
# let | |
# optionalList = cond: list: if cond then list else [ ]; | |
# in | |
flake-utils.lib.eachDefaultSystem | |
(system: | |
let | |
overlays = [ (import rust-overlay) ]; | |
pkgs = import nixpkgs { | |
inherit system overlays; | |
}; | |
rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; | |
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; | |
filterProto = path: _type: builtins.match ".*proto$" path != null; | |
protoOrCargo = path: type: | |
(filterProto path type) | |
|| (craneLib.filterCargoSources path type); | |
# src = craneLib.cleanCargoSource (craneLib.path ./.); | |
##### PROBLEM: this flake lives in the "rust" directory. | |
##### PROBLEM: this does not find the file "../spec/service.proto", which is needed by ./build.rs | |
##### PROBLEM: | |
src = pkgs.lib.debug.traceVal (nixpkgs.lib.cleanSourceWith { | |
src = craneLib.path ./.; # The original, unfiltered source | |
filter = protoOrCargo; | |
}); | |
my-crate = craneLib.buildPackage { | |
inherit src; | |
cargoVendorDir = craneLib.vendorMultipleCargoDeps { | |
inherit (craneLib.findCargoFiles src) cargoConfigs; | |
cargoLockList = [ | |
./Cargo.lock | |
# Unfortunately this approach requires IFD (import-from-derivation) | |
# otherwise Nix will refuse to read the Cargo.lock from our toolchain | |
# (unless we build with `--impure`). | |
# | |
# Another way around this is to manually copy the rustlib `Cargo.lock` | |
# to the repo and import it with `./path/to/rustlib/Cargo.lock` which | |
# will avoid IFD entirely but will require manually keeping the file | |
# up to date! | |
"${rustToolchain.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/Cargo.lock" | |
###### BY THE WAY: I'm not sure what this comment above means or how to remove the IFD. I already keep the Cargo.lock up to date, so... ? | |
]; | |
}; | |
#### I don't remember why this is here... | |
# cargoExtraArgs = "-Z build-std --target x86_64-unknown-linux-gnu"; | |
buildInputs = with pkgs; [ | |
protobuf | |
rustToolchain | |
gcc11 ###### For some reason, gcc11 does not seem to get used, and instead gcc12 is used... | |
# protobufc | |
# protoc-gen-rust | |
# Add additional build inputs here | |
]; | |
}; | |
nativeBuildInputs = with pkgs; [ | |
rustToolchain | |
gcc11 ###### For some reason, gcc11 does not seem to get used, and instead gcc12 is used... | |
# pkg-config | |
# protobuf | |
# protobufc | |
# protoc-gen-rust | |
# protoc-gen-go | |
# protoc-gen-go-grpc | |
]; | |
buildInputs = with pkgs; [ | |
gcc11 ###### For some reason, gcc11 does not seem to get used, and instead gcc12 is used... | |
# protobuf | |
# protobufc | |
# protoc-gen-rust | |
# openssl | |
]; | |
in | |
{ | |
checks = { | |
inherit my-crate; | |
}; | |
packages.default = my-crate; | |
devShells.default = pkgs.mkShell { | |
inherit buildInputs nativeBuildInputs; | |
inputsFrom = builtins.attrValues self.checks.${system}; | |
PROTOC = "${pkgs.protobuf}/bin/protoc"; | |
}; | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment