"not allowed to refer to a store path" error

This happens for every builtin that calls EvalState::forceStringNoCtx. For example builtins.fromJSON, builtins.fromTOML, builtins.fetchGit, import, builtins.getEnv, builtins.placeholder. builtins.hashFile, … Some attributes on the derivation such as system, outputHash*, outputs. Basically anything that doesn’t output either a derivation or a string again.

In that specific case, it seems like attrset keys are also not allowed to hold a context:

nix-repl> :l <nixpkgs>
Added 11298 variables.

nix-repl> { "${pkgs.coreutils}" = 3; }
error: the string '/nix/store/d7hlkykjjqs3f200jnmjm1y2hzgvbqa8-coreutils-8.31' is not allowed to refer to a store path (such as '!out!/nix/store/pvhv8cm3g8hn8prjjiw3agqbycr6jbr6-coreutils-8.31.drv')

You can cheat it by using builtins.unsafeDiscardStringContext:

nix-repl> { "${builtins.unsafeDiscardStringContext pkgs.coreutils}" = 3; }
{ "/nix/store/d7hlkykjjqs3f200jnmjm1y2hzgvbqa8-coreutils-8.31" = 3; }

But since the context is not propagated, the store path might not exist on your system though. So make sure it’s referred to it in another place as well.

6 Likes