Skip to content

Commit

Permalink
Test nixpkgs_package is invalidated on a change in Nix files
Browse files Browse the repository at this point in the history
This test runs Bazel in the Bazel sandbox to ensure some dependencies
are well tracked at the workspace level. So, we create a workspace,
build it, then change the content of some workspace files and check if
a rebuild of this workspace take into account these changes.

This is a regression test for the issue #113.
  • Loading branch information
nlewo committed Apr 23, 2020
1 parent d3c7bc9 commit c9d3c71
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 1 deletion.
2 changes: 2 additions & 0 deletions nixpkgs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ exports_files([
"nixpkgs.bzl",
])

filegroup(name = "srcs", srcs = glob(["**"]), visibility = ["//visibility:public"])

# @bazel_tools//tools does not define a bzl_library itself, instead we are
# supposed to define our own using the @bazel_tools//tools:bzl_srcs filegroup.
# See https://github.com/bazelbuild/skydoc/issues/166
Expand Down
4 changes: 3 additions & 1 deletion nixpkgs/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ load("@bazel_tools//tools/cpp:cc_configure.bzl", "cc_autoconf_impl")
load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value")

def _nixpkgs_git_repository_impl(repository_ctx):
repository_ctx.file("BUILD")
repository_ctx.file(
"BUILD",
content = 'filegroup(name = "srcs", srcs = glob(["**"]), visibility = ["//visibility:public"])')

# Make "@nixpkgs" (syntactic sugar for "@nixpkgs//:nixpkgs") a valid
# label for default.nix.
Expand Down
12 changes: 12 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,15 @@ go_binary(
name = "go-test",
srcs = ["go-test.go"]
)

sh_test(
name = "run-test-invalid-nixpkgs-package",
timeout = "short",
srcs = ["test_invalid_nixpkgs_package.sh"],
deps = ["@bazel_tools//tools/bash/runfiles"],
data = [
"//nixpkgs:srcs",
"//tests/invalid_nixpkgs_package:srcs",
"@remote_nixpkgs//:srcs"
],
)
1 change: 1 addition & 0 deletions tests/invalid_nixpkgs_package/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
filegroup(name = "srcs", srcs = glob(["**"]), visibility = ["//visibility:public"])
6 changes: 6 additions & 0 deletions tests/invalid_nixpkgs_package/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ ... }:
let
# To not predict from where the nix command is executed, we replace
# with sed the import path with an absolute path.
pkgs = import REPLACE-WITH-ABSPATH { };
in import ./hello.nix { inherit pkgs; }
5 changes: 5 additions & 0 deletions tests/invalid_nixpkgs_package/hello-1.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{ pkgs }:

{
hello = pkgs.writeShellScriptBin "hello" "echo hello-1";
}
5 changes: 5 additions & 0 deletions tests/invalid_nixpkgs_package/hello-2.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{ pkgs }:

{
hello = pkgs.writeShellScriptBin "hello" "echo hello-2";
}
6 changes: 6 additions & 0 deletions tests/invalid_nixpkgs_package/nested-build.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
genrule(
name = "hello-output",
outs = ["hello-output.txt"],
cmd = "$(execpath @hello//:bin) > \"$@\"",
tools = [ "@hello//:bin" ]
)
19 changes: 19 additions & 0 deletions tests/invalid_nixpkgs_package/workspace.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
workspace(name = "io_tweag_rules_nixpkgs")

load(
"//nixpkgs:nixpkgs.bzl",
"nixpkgs_local_repository",
"nixpkgs_package",
)

nixpkgs_local_repository(
name = "nixpkgs",
nix_file = "//:default.nix",
nix_file_deps = [ "//:hello.nix" ],
)

nixpkgs_package(
name = "hello",
attribute_path = "hello",
repository = "@nixpkgs",
)
36 changes: 36 additions & 0 deletions tests/test_invalid_nixpkgs_package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh
set -e

ln -s tests/invalid_nixpkgs_package/workspace.bazel WORKSPACE
ln -s tests/invalid_nixpkgs_package/nested-build.bazel BUILD
ln -s tests/invalid_nixpkgs_package/default.nix default.nix

# We need to provide a nixpkgs to create an output store path which is
# a folder (because nixpkgs_package requires the output store path to
# be a directory).
#
# It would be much more simple to use the mkdir bash builtin because
# we could create the output store path without having to import
# nixpkgs. However, the mkdir bash builtin is segfaulting:/
sed "s;REPLACE-WITH-ABSPATH;$(pwd)/external/remote_nixpkgs;" -i default.nix

# First we build the :hello-output target with the content of
# hello-1.nix in hello.nix. Building this deriviation creates a file
# containing "hello-1".
cp tests/invalid_nixpkgs_package/hello-1.nix hello.nix
bazel build //:hello-output
if [[ $(cat bazel-bin/hello-output.txt) != "hello-1" ]]; then
exit 1
fi

# Then, we override the content of the hello.nix file to ensure Bazel
# rebuilds the :hello-output target when a Nix files is modified. The
# hello.nix file now builds a derivation creating a file with content
# "hello-2".
cp tests/invalid_nixpkgs_package/hello-2.nix hello.nix
bazel build //:hello-output
content=$(cat bazel-bin/hello-output.txt)
if [[ $content != "hello-2" ]]; then
echo 'error: the content of bazel-bin/hello-output.txt must be hello-2 instead of' "$content"
exit 1
fi

0 comments on commit c9d3c71

Please sign in to comment.