How can I fix the nix-shell error "error: creating directory '/nix/var': Permission denied"?

How can I fix the nix-shell error "error: creating directory '/nix/var': Permission denied"?

我突然尝试 运行 nix-shell 时遇到奇怪的错误。它曾经工作得很好,但后来我开始收到这些错误,这不是我自己的错:

jon@jon-laptop ~/D/C/D/0/color-word-analyzer (master)> nix-shell
building '/nix/store/02szzrd1w7wqmnkh9is5qlx64m7r6f1z-cabal2nix-color-word-analyzer.drv'...
installing
error: creating directory '/nix/var': Permission denied
error: creating directory '/nix/var': Permission denied
** need a revision for VCS when the hash is given. skipping.
** need a revision for VCS when the hash is given. skipping.
** need a revision for VCS when the hash is given. skipping.
** need a revision for VCS when the hash is given. skipping.
cabal2nix: user error (Failed to fetch source. Does this source exist? Source {sourceUrl = "/nix/store/fvka3nd7slnqgjvfkjg649v77y10lc89-color-word-analyzer", sourceRevision = "", sourceHash = Guess "", sourceCabalDir = ""})
builder for '/nix/store/02szzrd1w7wqmnkh9is5qlx64m7r6f1z-cabal2nix-color-word-analyzer.drv' failed with exit code 1
error: build of '/nix/store/02szzrd1w7wqmnkh9is5qlx64m7r6f1z-cabal2nix-color-word-analyzer.drv' failed

Color-word analyzer 是我正在开发的 Haskell 项目的名称。它的 default.nix is here 非常简单,所以我不明白为什么会导致这些错误。

看起来 cabal2nix 正在沙盒中调用 Nix 命令。在您 NIX_PATH 中的 Nixpkgs 版本中,这是不允许的,并且可能是错误发生的。我能够使用

构建项目
nix-build -I nixpkgs=channel:nixos-unstable

题外话:递归 nix 调用 will 得到支持,但对结果的纯度和密封性有一些限制。