nix-community / crate2nix

rebuild only changed crates in CI with crate2nix and nix
https://nix-community.github.io/crate2nix/
Apache License 2.0
335 stars 82 forks source link

Set custom environment variables in `packages` and `devShells` #312

Open vbrandl opened 8 months ago

vbrandl commented 8 months ago

I want to convert a Rust project into a flake, that uses the vergen crate to create version metadata from the git repository at build time. I use the git ref and git describe output. vergen is somehow aware of nix and will return VERGEN_IDEMPOTENT_OUTPUT if .git is not present and the metadata cannot be obtained (e.g. in a nix build).

To fully convert the project into a flake, I'd like to set both ${self.rev or self.dirtyRev or "dirty"} and ${self.version} as environment in the nix build and fallback on something like env!("NIX_FLAKE_REF") if vergen cannot calculate metadata from the git repo.

How can I set custom environment variables, that are available when building the project (and possibly also in devShells) when using the template flake?

samuela commented 2 months ago

I'm facing the same issue trying to load my sqlite schema at build time for sqlx...