final: prev:

{
  jetbrains = prev.jetbrains // {
    clion = prev.jetbrains.clion.overrideAttrs (old: {
      patches = (old.patches or [ ]) ++ [
        ./remote.patch
      ];

      installPhase =
        (old.installPhase or "")
        + ''
          makeWrapper "$out/$pname/bin/remote-dev-server.sh" "$out/bin/$pname-remote-dev-server" \
            --prefix PATH : "$out/libexec/$pname:${
              final.lib.makeBinPath [
                final.jdk
                final.coreutils
                final.gnugrep
                final.which
                final.git
              ]
            }" \
            --prefix LD_LIBRARY_PATH : "${
              final.lib.makeLibraryPath ([
                # Some internals want libstdc++.so.6
                final.stdenv.cc.cc.lib
                final.libsecret
                final.e2fsprogs
                final.libnotify
              ])
            }" \
            --set-default JDK_HOME "$jdk" \
            --set-default ANDROID_JAVA_HOME "$jdk" \
            --set-default JAVA_HOME "$jdk" \
            --set CLION_JDK "$jdk" \
            --set CLION_VM_OPTIONS ${old.vmoptsFile}
        '';
    });
  };
}
