Error to run zig build

Even in a simple flake demo, unable to run zig build, due to the blocking error: ReadOnlyFileSystem.

[flake.nix]

{
  description = "A very basic flake";

  inputs = {
    nixpkgs.url = "flake:nixpkgs";
  };

  outputs = { self, nixpkgs }: let
      system = "aarch64-darwin";
      pkgs = import nixpkgs { inherit system; };
    in {
    packages.${system}.default = pkgs.stdenv.mkDerivation {
      pname = "zig-hello";
      version = "0.1.0";

      src = ./src;
      buildInputs = [ pkgs.zig ];

      buildPhase = ''
        mkdir -p $TMPDIR/bin
        # touch $TMPDIR/bin/zig-hello
        zig build-exe main.zig -femit-bin=$TMPDIR/bin/zig-hello
      '';

      installPhase = ''
        mkdir -p $out/bin
        cp $TMPDIR/bin/zig-hello $out/bin
      '';
    };

  };
}

mkdir and touch run regularly, but zig build always complains that error.

Is there somebody understand why?

I am afraid that I don’t know why zig build fails but I think what you are trying to achieve has already been implemented the the zig hook in nixpkgs.

The documentation for the hook can be found here: nixpkgs/doc/hooks/zig.section.md at ae88a0775335e6b66016565543392a7575923644 · NixOS/nixpkgs · GitHub

And the source code so you can see what its actually doing here: nixpkgs/pkgs/development/compilers/zig/hook.nix at ae88a0775335e6b66016565543392a7575923644 · NixOS/nixpkgs · GitHub

Hope this helps

@delliott Thanks for your informative reply!
zig hook works fine.

And for working without zig hook, the core snippet useful to overcome error ROFS is

        ZIG_GLOBAL_CACHE_DIR=$(mktemp -d)
        export ZIG_GLOBAL_CACHE_DIR

Although I’ve not gotten why ZIG_GLOBAL_CACHE_DIR and --global-cache-dir is valid,but --cache-dir not work.