Cargo emits "Too many open files" error when compiling Bevy apps

When I try to compile Bevy apps, like the breakout example, no matter what setup I use, cargo emits errors like this:

error: proc-macro derive panicked
  --> crates/bevy_scene/src/bundle.rs:19:10
   |
19 | #[derive(Component, Deref, DerefMut)]
   |          ^^^^^^^^^
   |
   = help: message: Unable to read cargo manifest: /home/nilirad/devel/bevy/crates/bevy_scene/Cargo.toml

and this:

error: could not write output to /home/nilirad/devel/bevy/target/debug/deps/bevy_text-e06677f4f5a63e80.0rypmf54r8tq7fws97r2dg99x.rcgu.o: Too many open files

After running cargo run --example breakout at least three times, the app finally gets compiled.

Given the nature of the error messages, I increased the maximum number of open files by putting the following to my configuration:

  security.pam.loginLimits = [
    {
      domain = "*";
      type = "soft";
      item = "nofile";
      value = "268435456";
    }
    {
      domain = "*";
      type = "hard";
      item = "nofile";
      value = "536870912";
    }
  ];

However, this barely made a difference (I only had the feeling that I have to run cargo run less times).

As mentioned in the beginning I’ve tried multiple methods, all with the same result.

The first method consisted in a devenv shell with the following configuration:

# devenv.nix
{ pkgs, lib, config, inputs, ... }:

rec {
  packages = with pkgs; [
    pkg-config
    udev
    alsa-lib
    vulkan-loader
    xorg.libX11
    xorg.libXcursor
    xorg.libXi
    xorg.libXrandr
    libxkbcommon
    wayland
  ];

  env.LD_LIBRARY_PATH = lib.makeLibraryPath packages;

  cachix.enable = false;

  languages.rust = {
    enable = true;
    channel = "stable";
    mold.enable = true;
  };
}

The other method consisted in the shell.nix setup example in the Bevy repository, with cargo and rustc packages included in the nativeBuildInputs.

I’m using NixOS unstable, however the issue has always been present since I’ve installed NixOS many months ago.

At this point I don’t know how to solve this issue. So far, it only presents with Bevy and Bevy-based projects, not with other Rust projects. Given the fact that nobody else seems to have the same issue is probably due to my configuration, but I don’t know what precisely.

After some more testing, looks like the problem lies in my impermanence or btrfs setup, where it can’t manage a great deal of I/O. For the moment I’m moving the folder to a different filesystem, where the problem doesn’t appear.

I ran into exactly the same issue, following the advice from this github comment has solved this issue for me.: Compiling Rust projects in a bind mount causes a "Too many open files" error · Issue #206 · nix-community/impermanence · GitHub

Reposting it here because this appeared at the top of my search results for this error.

1 Like

I was having trouble building llvm with an impermanence setup. As it turns out, bindfs process runs out of file descriptors. It should be possible to modify the systemd service to extend FILENO limits here impermanence/home-manager.nix at 4b3e914cdf97a5b536a889e939fb2fd2b043a170 · nix-community/impermanence · GitHub

As suggested in the cargo issue, switching to nixos module for impermanence greatly improves performance, because bind mounts are used instead of FUSE.