diff --git a/nixpkgs/BUILD.bazel b/nixpkgs/BUILD.bazel index fd17f0f17..09fa6aa52 100644 --- a/nixpkgs/BUILD.bazel +++ b/nixpkgs/BUILD.bazel @@ -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 diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index fb87da494..5b77c8718 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -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. diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 450810bd8..f288f7c48 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -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" + ], +) diff --git a/tests/invalid_nixpkgs_package/BUILD.bazel b/tests/invalid_nixpkgs_package/BUILD.bazel new file mode 100644 index 000000000..00cbfeaa5 --- /dev/null +++ b/tests/invalid_nixpkgs_package/BUILD.bazel @@ -0,0 +1 @@ +filegroup(name = "srcs", srcs = glob(["**"]), visibility = ["//visibility:public"]) diff --git a/tests/invalid_nixpkgs_package/default.nix b/tests/invalid_nixpkgs_package/default.nix new file mode 100644 index 000000000..7c1343d1e --- /dev/null +++ b/tests/invalid_nixpkgs_package/default.nix @@ -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; } diff --git a/tests/invalid_nixpkgs_package/hello-1.nix b/tests/invalid_nixpkgs_package/hello-1.nix new file mode 100644 index 000000000..92854745d --- /dev/null +++ b/tests/invalid_nixpkgs_package/hello-1.nix @@ -0,0 +1,5 @@ +{ pkgs }: + +{ + hello = pkgs.writeShellScriptBin "hello" "echo hello-1"; +} diff --git a/tests/invalid_nixpkgs_package/hello-2.nix b/tests/invalid_nixpkgs_package/hello-2.nix new file mode 100644 index 000000000..4f35fcf7d --- /dev/null +++ b/tests/invalid_nixpkgs_package/hello-2.nix @@ -0,0 +1,5 @@ +{ pkgs }: + +{ + hello = pkgs.writeShellScriptBin "hello" "echo hello-2"; +} diff --git a/tests/invalid_nixpkgs_package/nested-build.bazel b/tests/invalid_nixpkgs_package/nested-build.bazel new file mode 100644 index 000000000..6228e1f2c --- /dev/null +++ b/tests/invalid_nixpkgs_package/nested-build.bazel @@ -0,0 +1,6 @@ +genrule( + name = "hello-output", + outs = ["hello-output.txt"], + cmd = "$(execpath @hello//:bin) > \"$@\"", + tools = [ "@hello//:bin" ] +) diff --git a/tests/invalid_nixpkgs_package/workspace.bazel b/tests/invalid_nixpkgs_package/workspace.bazel new file mode 100644 index 000000000..5ea0505cb --- /dev/null +++ b/tests/invalid_nixpkgs_package/workspace.bazel @@ -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", +) diff --git a/tests/test_invalid_nixpkgs_package.sh b/tests/test_invalid_nixpkgs_package.sh new file mode 100755 index 000000000..0ba74a533 --- /dev/null +++ b/tests/test_invalid_nixpkgs_package.sh @@ -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