Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions pkgs/development/tools/build-managers/bazel/bash-tools-test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{ stdenv, writeText, runCommandCC, bazel }:

# Tests that certain executables are available in bazel-executed bash shells.

let
WORKSPACE = writeText "WORKSPACE" ''
workspace(name = "our_workspace")
'';

fileIn = writeText "input.txt" ''
one
two
three
'';

fileBUILD = writeText "BUILD" ''
genrule(
name = "tool_usage",
srcs = [ ":input.txt" ],
outs = [ "output.txt" ],
cmd = "cat $(location :input.txt) | gzip - | gunzip - | awk '/t/' > $@",
)
'';

runLocal = name: script: runCommandCC name { preferLocalBuild = true; } script;

workspaceDir = runLocal "our_workspace" ''
mkdir $out
cp ${WORKSPACE} $out/WORKSPACE
cp ${fileIn} $out/input.txt
cp ${fileBUILD} $out/BUILD
'';

testBazel = runLocal "bazel-test-bash-tools" ''
export HOME=$(mktemp -d)
cp -r ${workspaceDir} wd && chmod +w wd && cd wd
${bazel}/bin/bazel build :tool_usage
cp bazel-genfiles/output.txt $out
echo "Testing content" && [ "$(cat $out | wc -l)" == "2" ] && echo "OK"
'';

in testBazel
39 changes: 36 additions & 3 deletions pkgs/development/tools/build-managers/bazel/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ stdenv, callPackage, lib, fetchurl, fetchpatch, runCommand, makeWrapper
, jdk, zip, unzip, bash, writeCBin, coreutils
, which, python, perl, gawk, gnused, gnugrep, findutils
, which, python, perl, gawk, gnused, gnutar, gnugrep, gzip, findutils
# Apple dependencies
, cctools, clang, libcxx, CoreFoundation, CoreServices, Foundation
# Allow to independently override the jdks used to build and run respectively
Expand All @@ -23,7 +23,35 @@ let
for i in ${builtins.toString srcDeps}; do cp $i $out/$(stripHash $i); done
'';

defaultShellPath = lib.makeBinPath [ bash coreutils findutils gawk gnugrep gnused which unzip ];
defaultShellPath = lib.makeBinPath
# Keep this list conservative. For more exotic tools, prefer to use
# @rules_nixpkgs to pull in tools from the nix repository. Example:
#
# WORKSPACE:
#
# nixpkgs_git_repository(
# name = "nixpkgs",
# revision = "def5124ec8367efdba95a99523dd06d918cb0ae8",
# )
#
# # This defines an external Bazel workspace.
# nixpkgs_package(
# name = "bison",
# repositories = { "nixpkgs": "@nixpkgs//:default.nix" },
# )
#
# some/BUILD.bazel:
#
# genrule(
# ...
# cmd = "$(location @bison//:bin/bison) -other -args",
# tools = [
# ...
# "@bison//:bin/bison",
# ],
# )
#
[ bash coreutils findutils gawk gnugrep gnutar gnused gzip which unzip ];

in
stdenv.mkDerivation rec {
Expand All @@ -38,9 +66,14 @@ stdenv.mkDerivation rec {
platforms = platforms.linux ++ platforms.darwin;
};

# additional tests that check bazel’s functionality
# Additional tests that check bazel’s functionality. Execute
#
# nix-build . -A bazel.tests
#
# in the nixpkgs checkout root to exercise them locally.
passthru.tests = {
pythonBinPath = callPackage ./python-bin-path-test.nix {};
bashTools = callPackage ./bash-tools-test.nix {};
};

name = "bazel-${version}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ let

testBazel = runLocal "bazel-test-builtin-rules" ''
export HOME=$(mktemp -d)
cp -r ${workspaceDir}/* .
${bazel}/bin/bazel --output_base=/tmp/bazel-tests/wd\
# Note https://github.com/bazelbuild/bazel/issues/5763#issuecomment-456374609
# about why to create a subdir for the workspace.
cp -r ${workspaceDir} wd && chmod u+w wd && cd wd
${bazel}/bin/bazel \
test \
--test_output=errors \
--host_javabase='@local_jdk//:jdk' \
Expand Down