"Too many open files" when the Gradle cache is persisted via impermanence

Hi, using NixOS I’m unable to build a fairly complex Android Studio project due to the following error:

  /home/user/.gradle/caches/transforms-3/5733630caa7698f873e3b6cb13dedb97/transformed/exifinterface-1.3.3-api.jar: Too many open files

This repeats for lots of different dependencies.

I found a few posts here already, but they are either specific to nixos-rebuild (which works fine for me) and/or the solutions don’t work for me.

What I’ve tried:

boot.kernel.sysctl = { "fs.file-max" = 524288; };

security.pam.loginLimits = [
  {
    domain = "*";
    type = "-";
    item = "nofile";
    value = "524288";
  }
];

fileSystems."/" = {
  device = "none";
  fsType = "tmpfs";
  options = ["defaults" "size=12G" "mode=755" "nr_inodes=1M"];
};
 
fileSystems."/home/user" = {
  device = "none";
  fsType = "tmpfs";
  options = ["defaults" "size=4G" "mode=777" "nr_inodes=1M"];
};

Also, ulimit -n outputs 524288.

What’s worth mentioning is that I’m using impermanence, and the Gradle cache directory is persisted:

persistence."/nix/persist/home/user".directories = [
  ".gradle"
]

Always tried rebooting inbetween testing the configurations, just to be sure.

Any idea on what the issue could be?

There are no issues when I remove ~/.gradle from the list of persisted directories, so it must be connected to that.
But I cannot figure out what exactly causes it to break.