treefmt-nix icon indicating copy to clipboard operation
treefmt-nix copied to clipboard

`nix flake check` failed, couldn't open cache

Open fuzzko opened this issue 6 months ago • 1 comments

Describe the bug

error: builder for '/nix/store/ybvrra9ha5b3bb714csld0hwm2j2r9lz-check-formatting.drv' failed with exit code 1;
       last 1 log lines:
       > Error: failed to open cache: could not resolve local path for the cache: could not create any of the following paths: [/homeless-shelter/.cache/treefmt/eval-cache]
       For full logs, run 'nix log /nix/store/ybvrra9ha5b3bb714csld0hwm2j2r9lz-check-formatting.drv'.

To Reproduce

Steps to reproduce the behavior:

  1. Add formatting = (inputs.treefmt-nix.lib.evalModule pkgs ./treefmt.nix).config.build.check to checks output.
  2. Run nix flake check.

Expected behavior

nix flake check runs successfully.

System information

NixOS Unstable (25.11)

Additional context

-

fuzzko avatar Jun 19 '25 19:06 fuzzko

@0komo, could you share a full repo with a repro? This isn't enough for me to know how to reproduce the issue.

jfly avatar Jun 24 '25 20:06 jfly

Full repo with repro:

git clone [email protected]:engmark/root.git
cd root
nix develop --ignore-env --command pre-commit run --all-files nixfmt

Log:

nixfmt...................................................................Failed
- hook id: nixfmt
- exit code: 1

Error: failed to open cache: could not resolve local path for the cache: could not create any of the following paths: [/.cache/treefmt/eval-cache]

Or closer to the metal:

❯ nix develop --ignore-env --command treefmt
Error: failed to open cache: could not resolve local path for the cache: could not create any of the following paths: [/.cache/treefmt/eval-cache]

l0b0 avatar Nov 28 '25 21:11 l0b0

@l0b0, your repro looks different than @0komo's. In the OP, the failure occurred during a build in the build sandbox running our check derivation, which sets $HOME to a path that exists.

By running nix develop with --ignore-env, you're invoking treefmt with no $HOME environment variable, and we end up trying to put the cache in /.cache. We should probably error out more clearly there (or print out a warning that we're running without a cache).

jfly avatar Dec 02 '25 05:12 jfly

By running nix develop with --ignore-env, you're invoking treefmt with no $HOME environment variable, […]

~~That seems like a bug in the Nix CLI. --ignore-env is to avoid propagating variables from the environment into the sandbox, but nix develop doesn't do that in any case – it passes HOME=/homeless-shelter. I'll file a bug upstream when I have time.~~

Update: Wait, I'm thinking of nix build.

l0b0 avatar Dec 02 '25 06:12 l0b0