Creating a Nix shell for Rust development is easy if you’re prepared to either use only stable Rust or use Rustup to manage your Rust toolchain. But the latest version of Bevy was not compatible with stable Rust at the time of writing, and using Rustup would sacrifice one of the biggest advantages of Nix: having reproducibility managed by a single component. I’m pretty happy with what I ended up with, so here’s the gist of what you need.

Devenv setup

devenv.yaml needs nixpkgs and an overlay to install nightly Rust:

---
inputs:
  nixpkgs:
    url: github:NixOS/nixpkgs/nixpkgs-unstable
  rust-overlay:
    url: github:oxalica/rust-overlay
    inputs:
      nixpkgs:
        follows: nixpkgs

devenv.nix needs to install and make libraries available for consumption during the project build:

{
  lib,
  pkgs,
  options,
  ...
}:
let
  libraries = [
    # Omitted project specific packages
  ];
in
{
  packages = [
    # Omitted project specific packages
  ]
  ++ libraries;

  env.LD_LIBRARY_PATH = lib.makeLibraryPath libraries;

  languages = {
    nix.enable = true;
    rust = {
      enable = true;
      channel = "nightly";
      components = options.languages.rust.components.default ++ [
        "rust-src" # For IDE integration; see below
      ];
    };
  };

  enterTest = ''
    cargo test
  '';
}

IDE setup

(These instructions are for RustRover, but should be generally applicable.)

To ensure that RustRover uses the exact source and executables installed in the devenv, you need to configure the following paths:

  • Toolchain location: PROJECT_DIR/.devenv/profile/bin
  • Standard library: PROJECT_DIR/.devenv/profile/lib/rustlib/src/rust/library