Error: creating directory '/nix/var': Permission denied

I’ve had success building several libraries using the method outlined in Skyfold’s post.

However, today I’m trying to build an executable for a simple Haskell hello world program, but both nix-shell and nix-build give me errors. I can’t figure out why.

$ nix-shell
building '/nix/store/hjsvi026mfmwabpgxhhpkyzzqgrsprma-cabal2nix-wombat.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/rwbrabf73sgn8isgv7y2lybaqv4w44zw-wombat.cabal", sourceRevision = "", sourceHash = Guess "", sourceCabalDir = ""})
builder for '/nix/store/hjsvi026mfmwabpgxhhpkyzzqgrsprma-cabal2nix-wombat.drv' failed with exit code 1
error: build of '/nix/store/hjsvi026mfmwabpgxhhpkyzzqgrsprma-cabal2nix-wombat.drv' failed

I did find this post on SO with the same error message. Their diagnosis was that cabal2nix is invoking a Nix command inside the sandbox, probably caused by a mistake in the version of Nixpkgs in NIX_PATH . However, as you’ll see below, I’ve pinned nixpkgs. (And anyway, their workaround just gives me the same error.)

$ cat default.nix
{ nixpkgs ? import ./nix/nixos-20-03.nix }:
let
  overlay = self: super: {
    myHaskellPackages =
      super.haskell.packages.ghc865.override (old: {
        overrides = self.lib.composeExtensions (old.overrides or (_: _: {}))
          (hself: hsuper: {
            ghc = hsuper.ghc // { withPackages = hsuper.ghc.withHoogle; };
            ghcWithPackages = hself.ghc.withPackages;
          });
      });
  };

  pkgs = import nixpkgs {
    overlays = [overlay];
  };

  drv = pkgs.myHaskellPackages.callCabal2nix "wombat" ./wombat.cabal {};

  drvWithTools = drv.env.overrideAttrs (
    old: with pkgs.myHaskellPackages; {
      nativeBuildInputs = old.nativeBuildInputs ++ [
        ghcid
        # Add other development tools like ormolu here
      ];
#      shellHook = ''
#         source .config/secrets     '';
      }
  );
in
  drvWithTools
$ cat wombat.cabal
cabal-version:       >=1.10
name:                wombat
version:             0.1.0.0
license-file:        LICENSE
author:              Amy de Buitléir
maintainer:          amy@nualeargais.ie
build-type:          Simple
extra-source-files:  CHANGELOG.md

executable wombat
  main-is:             Main.hs
  build-depends:       base >=4.11 && <4.12,
                       directory ==1.3.* -- workaround NixOS bug #82245
  default-language:    Haskell2010
$ cat Main.hs
module Main where

main :: IO ()
main = putStrLn "Hello, Wombat!"
$ cat nix/nixos-20-03.json
{
  "url": "https://github.com/nixos/nixpkgs-channels.git",
  "rev": "ab3adfe1c769c22b6629e59ea0ef88ec8ee4563f",
  "date": "2020-04-28T17:56:19-04:00",
  "sha256": "1m4wvrrcvif198ssqbdw897c8h84l0cy7q75lyfzdsz9khm1y2n1",
  "fetchSubmodules": false
}
$ cat nix/nixos-20-03.nix
let
  hostNix = import <nixpkgs> {};
  nixpkgsPin = hostNix.pkgs.lib.importJSON ./nixos-20-03.json;

  pinnedPkgs = hostNix.pkgs.fetchFromGitHub {
    owner = "NixOS";
    repo  = "nixpkgs-channels";
    inherit (nixpkgsPin) rev sha256;
  };
in
  pinnedPkgs
$ echo $NIX_PATH
/home/amy/.nix-defexpr/channels:nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels