How to manually build a derivation with xcodebuild?

I am trying to install iterm2 on OSX Mojave through the nix package manager. Unfortunately it fails with the following message:

xcodebuild[47469:692696] [MT] IDELogStore: Failed to open log store at /var/nixbld-home/Library/Developer/Xcode/DerivedData/iTerm2-enoogsncbmggzkbphnzntzezacco/Logs/Build

In order to fix this problem I need to understand the following puzzle pieces:

  1. How does nix know how to build iTerm. xcodebuild is never directly invoked here:

item2 default.nix

{ stdenv, fetchFromGitHub }:

stdenv.mkDerivation rec {
  pname = "iterm2";
  version = "3.0.14";

  src = fetchFromGitHub {
    owner = "gnachman";
    repo = "iTerm2";
    rev = "v${version}";
    sha256 = "03m0ja11w9910z96yi8fzq3436y8xl14q031rdb2w3sapjd54qrj";
  };

  patches = [ ./disable_updates.patch ];
  postPatch = ''
    sed -i -e 's/CODE_SIGN_IDENTITY = "Developer ID Application"/CODE_SIGN_IDENTITY = ""/g' ./iTerm2.xcodeproj/project.pbxproj
  '';
  preConfigure = "LD=$CC";
  makeFlagsArray = ["Deployment"];
  installPhase = ''
    mkdir -p "$out/Applications"
    mv "build/Deployment/iTerm2.app" "$out/Applications/iTerm.app"
  '';

  meta = {
    description = "A replacement for Terminal and the successor to iTerm";
    homepage = https://www.iterm2.com/;
    license = stdenv.lib.licenses.gpl2;
    platforms = stdenv.lib.platforms.darwin;
  };
}

I know that stdenv works with different phases and hooks but in which one does it invoke xcodebuild?

  1. How could I manually invoke xcodebuild? I see that nix has a build-app.nix where xcodebuild is executed.

  2. Somehow Nix must find the path to my Xcode installation. How does it do that? I guess somehow with xcodeenv and composeXcodeWrapper that are documented here: xcodeenv-doc

I am grateful for any hints as I am currently quite lost in the deep sea of nix :).