diff --git a/WORKSPACE b/WORKSPACE index a2d575006..87bf2058e 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -136,7 +136,16 @@ nixpkgs_package( nixpkgs_cc_configure(repository = "@remote_nixpkgs") -nixpkgs_python_configure(repository = "@remote_nixpkgs") +nixpkgs_python_configure( + python2_attribute_path = "python2", + repository = "@remote_nixpkgs", +) + +nixpkgs_package( + name = "nixpkgs_python_configure_test", + nix_file = "//tests:python-test.nix", + repository = "@remote_nixpkgs", +) load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index 263bb37c1..96d9b950c 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -162,10 +162,12 @@ def _nixpkgs_package_impl(repository_ctx): # ensure that the output is a directory test_path = repository_ctx.which("test") - _execute_or_fail(repository_ctx, [test_path, "-d", output_path], - failure_message = "nixpkgs_package '@{}' outputs a single file which is not supported by rules_nixpkgs. Please only use directories.".format( - repository_ctx.name - ), + _execute_or_fail( + repository_ctx, + [test_path, "-d", output_path], + failure_message = "nixpkgs_package '@{}' outputs a single file which is not supported by rules_nixpkgs. Please only use directories.".format( + repository_ctx.name, + ), ) # Build a forest of symlinks (like new_local_package() does) to the @@ -357,14 +359,27 @@ _nixpkgs_python_toolchain = repository_rule( }, ) -_python_build_file_content = """ -py_runtime( - name = "runtime", - files = glob(["**"]), - interpreter = "{bin_path}", - python_version = "{version}", - visibility = ["//visibility:public"], -) +_python_nix_file_content = """ +with import {{ config = {{}}; overlays = []; }}; +runCommand "bazel-nixpkgs-python-toolchain" + {{ executable = false; + # Pointless to do this on a remote machine. + preferLocalBuild = true; + allowSubstitutes = false; + }} + '' + n=$out/BUILD.bazel + mkdir -p "$(dirname "$n")" + + cat >>$n < { config = {}; overlays = []; }; +runCommand "test-nixpkgs-python-toolchain" + { executable = false; } + '' + mkdir -p $out + + cat >$out/BUILD.bazel <<'EOF_BUILD' + py_test( + name = "python2-test", + main = "python-test.py", + srcs = ["python-test.py"], + python_version = "PY2", + srcs_version = "PY2", + visibility = ["//visibility:public"], + ) + py_test( + name = "python3-test", + main = "python-test.py", + srcs = ["python-test.py"], + python_version = "PY3", + srcs_version = "PY3", + visibility = ["//visibility:public"], + ) + EOF_BUILD + + cat >$out/python-test.py <<'EOF_PYTHON' + import os + import sys + + _failure_message = """\ + Python interpreter is not provided by the toolchain. + Expected: '{expected}' + Actual: '{actual}'. + """ + + if __name__ == "__main__": + if sys.version_info.major == 2: + python_bin = "${python2}/bin/python" + else: + python_bin = "${python3}/bin/python" + if not sys.executable == python_bin: + sys.stderr.write(_failure_message.format( + expected = python_bin, + actual = sys.executable, + )) + sys.exit(1) + EOF_PYTHON + '' diff --git a/tests/python-test.py b/tests/python-test.py deleted file mode 100644 index 5cb28d610..000000000 --- a/tests/python-test.py +++ /dev/null @@ -1,19 +0,0 @@ -import os -import sys - -_failure_message = """\ -Python interpreter is not provided by the toolchain. -Expected: '{expected}' -Actual: '{actual}'. -""" - -if __name__ == "__main__": - runfiles_dir = os.environ["RUNFILES_DIR"] - python_bin = os.path.join( - runfiles_dir, "nixpkgs_python_toolchain_python3", "bin", "python") - if not sys.executable == python_bin: - print(_failure_message.format( - expected = python_bin, - actual = sys.executable, - ), file=sys.stderr) - sys.exit(1)