Direnv 'use flake' gives 'store path ... not allowed to have refences'

Trying to use a flake with direnv to enable a development environment, I get the error

store path '/nix/store/<hash>-<my-flake-name>' is not allowed to have references

However, an explicit nix develop succeeds, and thereafter, direnv also succeeds.

Given that direnv fails with this error every time I check out a different commit, or modify (or even touch) the flake, I have got into the habit of using the hacky workaround

nix develop -c true && cd - && cd -

where

  1. nix develop gets around the error and puts me in a state which direnv can handle
  2. the two cd -s nudge direnv into action to that it loads the new environment.

This works and gets me around the error, but

  • it’s very annoying
  • collaborators will be stupmed when trying to use this environment and are faced with this error

I observe this behaviour on Linux, but (if memory serves me correctly, I don’t have direct access to a Mac) not on Darwin.

Can you suggest what might be causing this, and how to get rid of the problem?

A sample repository that exhibits this behaviour can be found here. To trigger the error:

  1. Ensure you have direnv enabled.
  2. cd into the repository root.
  3. Approve with direnv allow.

The error should appear.

After getting around the error with nix develop -c true && cd - && cd - it can be triggered again by any of these actions:

  • touch flake.nix
  • modifying the flake
  • checking out any other commit

There is a lot going on in that example. Can we trim it down to isolate the cause? Some IFD or other interaction with the various abstractions can be involved.

Note: fundamentally, direnv doesn’t use nix develop, but it uses nix print-dev-env (https://github.com/direnv/direnv/blob/f597abf8b93653f5dd8e6cb3e3b54f8d506c481c/stdlib.sh#L1185)

Note2: i was not able to reproduce. got a nix version?

1 Like

I poked around a bit:

  • My development machine running NixOS 21.11 and nix 2.5.0pre20211206_d1aaa7e: the error appears.

  • Brand new VM with Debian 11 and Nix 2.6.1: NO ERROR.

  • Brand new VM with Debian 11 and Nix 2.5.0: NO ERROR.

  • Brand new VM with Debian 11 and Nix 2.4: THE ERROR APPEARS.

edit: added result for Nix 2.4

The second commit (6ed1d4343e3df69b6d1b860397273d7284d6b8e0) already exhibits this behaviour. I can try to trim it down even further if you’re interested, but given the above information about the Nix versions, perhaps this isn’t worth the effort.

Indeed, this was pointed out to me recently, but it hasn’t quite sunk in, or rather, old habits/beliefs take time to fade. Thanks for reminding me.

Another data point: brand new VM with Nix 2.4: the error does occur.