anchor.diff:    PYTORCH_BUILD_VERSION = version;
     PYTORCH_BUILD_NUMBER = 0;
   
  +  BUILD_NAMEDTENSOR = buildNamedTensor;  # experimental feature
  +  USE_SYSTEM_NCCL=true;                  # don't build pytorch's third_party NCCL
  +
     # Suppress a weird warning in mkl-dnn, part of ideep in pytorch
     # (upstream seems to have fixed this in the wrong place?)
     # https://github.com/intel/mkl-dnn/commit/8134d346cdb7fe1695a2aa55771071d455fae0bc
  -  NIX_CFLAGS_COMPILE = lib.optionals (numpy.blasImplementation == "mkl") [ "-Wno-error=array-bounds" ];
  +  # https://github.com/pytorch/pytorch/issues/22346
  +  #
  +  # Also of interest: pytorch ignores CXXFLAGS uses CFLAGS for both C and C++:
  +  # https://github.com/pytorch/pytorch/blob/v1.2.0/setup.py#L17
  +  NIX_CFLAGS_COMPILE = lib.optionals (numpy.blas == mkl) [ "-Wno-error=array-bounds" ];
   
     nativeBuildInputs = [
  -     cmake
  -     utillinux
  -     which
  +    cmake
  +    utillinux
  +    which
  +    ninja
     ] ++ lib.optionals cudaSupport [ cudatoolkit_joined ];
   
     buildInputs = [
  -     numpy.blas
  -  ] ++ lib.optionals cudaSupport [ cudnn ]
  +    numpy.blas
  +  ] ++ lib.optionals cudaSupport [ cudnn magma nccl ]
       ++ lib.optionals stdenv.isLinux [ numactl ];
   
     propagatedBuildInputs = [
       cffi
  +    click
       numpy
       pyyaml
  -  ] ++ lib.optional (pythonOlder "3.5") typing;
  -
  -  checkInputs = [ hypothesis ];
  -  checkPhase = ''
  -    ${cudaStubEnv}python test/run_test.py --exclude dataloader sparse torch utils thd_distributed distributed cpp_extensions
  -  '';
  +  ] ++ lib.optionals openMPISupport [ openmpi ]
  +    ++ lib.optional (pythonOlder "3.5") typing
  +    ++ lib.optionals tensorboardSupport [pillow six future tensorflow-tensorboard];
  +
  +  checkInputs = [ hypothesis ninja ];
  +
  +  doCheck = false; # tests take a long time for channel release, so doCheck should be overridden only when developing
  +  checkPhase = "${cudaStubEnv}python test/run_test.py"
  +    + " --exclude utils" # utils requires git, which is not allowed in the check phase
  +
  +    # Other tests which have been disabled in previous nix derivations of pytorch.
  +    # --exclude dataloader sparse torch utils thd_distributed distributed cpp_extensions
  +    ;
  +  postInstall = ''
  +    mkdir $dev
  +    cp -r $out/${python.sitePackages}/torch/lib     $dev/lib
  +    cp -r $out/${python.sitePackages}/torch/include $dev/include
  +  '';
  +
  +  postFixup = stdenv.lib.optionalString stdenv.isDarwin ''
  +    for f in $(ls $dev/lib/*.dylib); do
  +        install_name_tool -id $dev/lib/$(basename $f) $f || true
  +    done
  +
  +    install_name_tool -change @rpath/libshm.dylib $dev/lib/libshm.dylib $dev/lib/libtorch_python.dylib
  +    install_name_tool -change @rpath/libtorch.dylib $dev/lib/libtorch.dylib $dev/lib/libtorch_python.dylib
  +    install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libtorch_python.dylib
  +
  +    install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libtorch.dylib
  +
  +    install_name_tool -change @rpath/libtorch.dylib $dev/lib/libtorch.dylib $dev/lib/libcaffe2_observers.dylib
  +    install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libcaffe2_observers.dylib
  +
  +    install_name_tool -change @rpath/libtorch.dylib $dev/lib/libtorch.dylib $dev/lib/libcaffe2_module_test_dynamic.dylib
  +    install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libcaffe2_module_test_dynamic.dylib
  +
  +    install_name_tool -change @rpath/libtorch.dylib $dev/lib/libtorch.dylib $dev/lib/libcaffe2_detectron_ops.dylib
  +    install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libcaffe2_detectron_ops.dylib
  +
  +    install_name_tool -change @rpath/libtorch.dylib $dev/lib/libtorch.dylib $dev/lib/libshm.dylib
  +    install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libshm.dylib
  +  '';
  +
   
     meta = {
       description = "Open source, prototype-to-production deep learning platform";
       homepage    = https://pytorch.org/;
       license     = lib.licenses.bsd3;
  -    platforms   = lib.platforms.linux;
  -    maintainers = with lib.maintainers; [ teh thoughtpolice ];
  +    platforms   = with lib.platforms; linux ++ lib.optionals (!cudaSupport) darwin;
  +    maintainers = with lib.maintainers; [ teh thoughtpolice stites tscholak ]; # tscholak esp. for darwin-related builds
     };
   }
  
positive.code:     "LD_LIBRARY_PATH=${cudaStub}\${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} ";
  
  in buildPythonPackage rec {
    version = "1.0.0";
    pname = "pytorch";
    disabled = !isPy3k;
  
    src = fetchFromGitHub {
      owner  = "pytorch";
      repo   = "pytorch";
      rev    = "v${version}";
      fetchSubmodules = true;
      sha256 = "076cpbig4sywn9vv674c0xdg832sdrd5pk1d0725pjkm436kpvlm";
    };
  
    patches =
      [ # Skips two tests that are only meant to run on multi GPUs
        (fetchurl {
          url = "https://github.com/pytorch/pytorch/commit/bfa666eb0deebac21b03486e26642fd70d66e478.patch";
          sha256 = "1fgblcj02gjc0y62svwc5gnml879q3x2z7m69c9gax79dpr37s9i";
        })
      ];
  
    dontUseCmakeConfigure = true;
    preConfigure = lib.optionalString cudaSupport ''
      export CC=${cudatoolkit.cc}/bin/gcc CXX=${cudatoolkit.cc}/bin/g++
    '' + lib.optionalString (cudaSupport && cudnn != null) ''
      export CUDNN_INCLUDE_DIR=${cudnn}/include
  
negative.code:       RP_NEW=$(join_by : ''${RP[@]:2})
        patchelf --set-rpath \$ORIGIN:''${RP_NEW} "$1"
      }
      for f in $(find ''${out} -name 'libcaffe2*.so')
      do
        strip2 $f
  