Allow the `--isolated` pip flag to be optionally unset (#512)

* Allow the `--isolated` flag to be optionally unset

* Update python/pip_install/pip_repository.bzl

Co-authored-by: Henry Fuller <hrofuller@gmail.com>

Co-authored-by: Henry Fuller <hrofuller@gmail.com>
diff --git a/python/pip_install/extract_wheels/__init__.py b/python/pip_install/extract_wheels/__init__.py
index 228346a..214be9a 100644
--- a/python/pip_install/extract_wheels/__init__.py
+++ b/python/pip_install/extract_wheels/__init__.py
@@ -64,7 +64,9 @@
     arguments.deserialize_structured_args(deserialized_args)
 
     pip_args = (
-        [sys.executable, "-m", "pip", "--isolated", "wheel", "-r", args.requirements] +
+        [sys.executable, "-m", "pip"] + 
+        (["--isolated"] if args.isolated else []) + 
+        ["wheel", "-r", args.requirements] +
         deserialized_args["extra_pip_args"]
     )
 
diff --git a/python/pip_install/extract_wheels/lib/arguments.py b/python/pip_install/extract_wheels/lib/arguments.py
index 46d08a8..0090137 100644
--- a/python/pip_install/extract_wheels/lib/arguments.py
+++ b/python/pip_install/extract_wheels/lib/arguments.py
@@ -10,6 +10,9 @@
         help="The external repo name to install dependencies. In the format '@{REPO_NAME}'",
     )
     parser.add_argument(
+        "--isolated", action="store_true", help="Whether or not to include the `--isolated` pip flag.",
+    )
+    parser.add_argument(
         "--extra_pip_args", action="store", help="Extra arguments to pass down to pip.",
     )
     parser.add_argument(
diff --git a/python/pip_install/parse_requirements_to_bzl/extract_single_wheel/__init__.py b/python/pip_install/parse_requirements_to_bzl/extract_single_wheel/__init__.py
index a46ea2e..2c1a124 100644
--- a/python/pip_install/parse_requirements_to_bzl/extract_single_wheel/__init__.py
+++ b/python/pip_install/parse_requirements_to_bzl/extract_single_wheel/__init__.py
@@ -29,7 +29,9 @@
     configure_reproducible_wheels()
 
     pip_args = (
-        [sys.executable, "-m", "pip", "--isolated", "wheel", "--no-deps"] +
+        [sys.executable, "-m", "pip"] +
+        (["--isolated"] if args.isolated else []) + 
+        ["wheel", "--no-deps"] +
         deserialized_args["extra_pip_args"]
     )
 
diff --git a/python/pip_install/pip_repository.bzl b/python/pip_install/pip_repository.bzl
index d7d1113..71d5f66 100644
--- a/python/pip_install/pip_repository.bzl
+++ b/python/pip_install/pip_repository.bzl
@@ -36,6 +36,20 @@
     Returns: Augmented args list.
     """
 
+    # Determine whether or not to pass the pip `--isloated` flag to the pip invocation
+    use_isolated = rctx.attr.isolated
+
+    # The environment variable will take precedence over the attribute
+    isolated_env = rctx.os.environ.get("RULES_PYTHON_PIP_ISOLATED", None)
+    if isolated_env != None:
+        if isolated_env.lower() in ("0", "false"):
+            use_isolated = False
+        else:
+            use_isolated = True
+
+    if use_isolated:
+        args.append("--isolated")
+
     # Check for None so we use empty default types from our attrs.
     # Some args want to be list, and some want to be dict.
     if rctx.attr.extra_pip_args != None:
@@ -125,6 +139,10 @@
 
     return
 
+common_env = [
+    "RULES_PYTHON_PIP_ISOLATED",
+]
+
 common_attrs = {
     "enable_implicit_namespace_pkgs": attr.bool(
         default = False,
@@ -149,6 +167,14 @@
     "extra_pip_args": attr.string_list(
         doc = "Extra arguments to pass on to pip. Must not contain spaces.",
     ),
+    "isolated": attr.bool(
+        doc = """\
+Whether or not to pass the [--isolated](https://pip.pypa.io/en/stable/cli/pip/#cmdoption-isolated) flag to
+the underlying pip command. Alternatively, the `RULES_PYTHON_PIP_ISOLATED` enviornment varaible can be used
+to control this flag.
+""",
+        default = True,
+    ),
     "pip_data_exclude": attr.string_list(
         doc = "Additional data exclusion parameters to add to the pip packages BUILD file.",
     ),
@@ -236,6 +262,7 @@
 ```
 """,
     implementation = _pip_repository_impl,
+    environ = common_env,
 )
 
 def _impl_whl_library(rctx):
@@ -284,4 +311,5 @@
 Download and extracts a single wheel based into a bazel repo based on the requirement string passed in.
 Instantiated from pip_repository and inherits config options from there.""",
     implementation = _impl_whl_library,
+    environ = common_env,
 )