From 2502e5eafa6c58a3abfdd8d69fbb49250fff6d4a Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Thu, 31 Oct 2019 10:17:28 +0100 Subject: [PATCH 1/4] Add nixpkgs_posix_configure This uses `rules_sh`. However, users of `rules_nixpkgs` will only need to depend on `rules_sh` if they use this macro. Otherwise, `rules_nixpkgs` does not depend on `rules_sh`. --- nixpkgs/nixpkgs.bzl | 124 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index 1d54c6bd6..215827246 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -431,6 +431,130 @@ def nixpkgs_python_configure( ) native.register_toolchains("@%s//:toolchain" % name) +def nixpkgs_posix_config(name, packages, **kwargs): + nixpkgs_package( + name = name, + nix_file_content = """ +with import {{ config = {{}}; overlays = []; }}; + +let + # `packages` might include lists, e.g. `stdenv.initialPath` is a list itself, + # so we need to flatten `packages`. + flatten = builtins.concatMap (x: if builtins.isList x then x else [x]); + env = buildEnv {{ + name = "posix-toolchain"; + paths = flatten [ {} ]; + }}; + cmd_glob = "${{env}}/bin/*"; + os = if stdenv.isDarwin then "osx" else "linux"; +in + +runCommand "bazel-nixpkgs-posix-toolchain" + {{ executable = false; + # Pointless to do this on a remote machine. + preferLocalBuild = true; + allowSubstitutes = false; + }} + '' + n=$out/nixpkgs_posix.bzl + mkdir -p "$(dirname "$n")" + + cat >>$n <>$n + fi + done + cat >>$n < Date: Thu, 31 Oct 2019 10:41:53 +0100 Subject: [PATCH 2/4] Test nixpkgs_unix_configure This adds a dependency on `rules_sh` for the test-suite. --- WORKSPACE | 20 ++++++++++++++++++++ tests/BUILD.bazel | 14 ++++++++++++++ tests/test_posix_toolchain.sh | 10 ++++++++++ 3 files changed, 44 insertions(+) create mode 100755 tests/test_posix_toolchain.sh diff --git a/WORKSPACE b/WORKSPACE index c6a787724..b25011133 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -6,6 +6,7 @@ load( "nixpkgs_git_repository", "nixpkgs_local_repository", "nixpkgs_package", + "nixpkgs_posix_configure", "nixpkgs_python_configure", ) @@ -118,3 +119,22 @@ nixpkgs_package( nixpkgs_cc_configure(repository = "@remote_nixpkgs") nixpkgs_python_configure(repository = "@remote_nixpkgs") + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +http_archive( + name = "rules_sh", + sha256 = "ad72710293db59986e1400d70944b3a13a52a11ce411969634669fc545e06c2f", + strip_prefix = "rules_sh-5aaea6d44c1ff1ccefc9ec4a22acef301af8f90a", + urls = ["https://github.com/tweag/rules_sh/archive/5aaea6d44c1ff1ccefc9ec4a22acef301af8f90a.tar.gz"], +) + +load("@rules_sh//sh:repositories.bzl", "rules_sh_dependencies") + +rules_sh_dependencies() + +nixpkgs_posix_configure(repository = "@nixpkgs") + +load("@rules_sh//sh:posix.bzl", "sh_posix_configure") + +sh_posix_configure() diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 44d474c28..1f9e9695e 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -65,3 +65,17 @@ py_test( python_version = "PY3", srcs_version = "PY3", ) + +# Test nixpkgs_posix_configure() checking that Unix commands are in Nix store. +sh_test( + name = "run-test-posix-toolchain", + timeout = "short", + srcs = ["test_posix_toolchain.sh"], + args = [ + "$(POSIX_AWK)", + "$(POSIX_CAT)", + "$(POSIX_GREP)", + "$(POSIX_MAKE)", + ], + toolchains = ["@rules_sh//sh/posix:make_variables"], +) diff --git a/tests/test_posix_toolchain.sh b/tests/test_posix_toolchain.sh new file mode 100755 index 000000000..df813a75a --- /dev/null +++ b/tests/test_posix_toolchain.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +fail=0 +for cmd in "$@"; do + if [[ ! "$cmd" = /nix/store/* ]]; then + echo "ERROR: '$cmd' is not in the Nix store." >&2 + fail=1 + fi +done +exit $fail From 1c62918e0d9f095e9cbef93eb9fb0398bf14e70d Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Wed, 13 Nov 2019 13:50:47 +0100 Subject: [PATCH 3/4] Use rules_sh v0.1.0 --- WORKSPACE | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index b25011133..0e1aeae12 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -124,9 +124,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "rules_sh", - sha256 = "ad72710293db59986e1400d70944b3a13a52a11ce411969634669fc545e06c2f", - strip_prefix = "rules_sh-5aaea6d44c1ff1ccefc9ec4a22acef301af8f90a", - urls = ["https://github.com/tweag/rules_sh/archive/5aaea6d44c1ff1ccefc9ec4a22acef301af8f90a.tar.gz"], + sha256 = "2613156e96b41fe0f91ac86a65edaea7da910b7130f2392ca02e8270f674a734", + strip_prefix = "rules_sh-0.1.0", + urls = ["https://github.com/tweag/rules_sh/archive/v0.1.0.tar.gz"], ) load("@rules_sh//sh:repositories.bzl", "rules_sh_dependencies") From 498006b974b2032369f6f439c20db1e31d5bd4f1 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Wed, 13 Nov 2019 16:28:04 +0100 Subject: [PATCH 4/4] posix --> sh_posix https://github.com/tweag/rules_nixpkgs/pull/95#pullrequestreview-316319488 --- WORKSPACE | 4 ++-- nixpkgs/nixpkgs.bzl | 30 +++++++++++++++--------------- tests/BUILD.bazel | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 0e1aeae12..61b441cc1 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -6,8 +6,8 @@ load( "nixpkgs_git_repository", "nixpkgs_local_repository", "nixpkgs_package", - "nixpkgs_posix_configure", "nixpkgs_python_configure", + "nixpkgs_sh_posix_configure", ) # For tests @@ -133,7 +133,7 @@ load("@rules_sh//sh:repositories.bzl", "rules_sh_dependencies") rules_sh_dependencies() -nixpkgs_posix_configure(repository = "@nixpkgs") +nixpkgs_sh_posix_configure(repository = "@nixpkgs") load("@rules_sh//sh:posix.bzl", "sh_posix_configure") diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index 215827246..92d9fe7c9 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -431,7 +431,7 @@ def nixpkgs_python_configure( ) native.register_toolchains("@%s//:toolchain" % name) -def nixpkgs_posix_config(name, packages, **kwargs): +def nixpkgs_sh_posix_config(name, packages, **kwargs): nixpkgs_package( name = name, nix_file_content = """ @@ -456,7 +456,7 @@ runCommand "bazel-nixpkgs-posix-toolchain" allowSubstitutes = false; }} '' - n=$out/nixpkgs_posix.bzl + n=$out/nixpkgs_sh_posix.bzl mkdir -p "$(dirname "$n")" cat >>$n <