Skip to content

Commit 8cb53e3

Browse files
committed
Improve documentation of build_file and add tests
Adds some user documentation on how to generate bazel targets for the repositories generated by `nixpkgs_package`. Also adds two tests to check whether this actually works as documented.
1 parent 54906df commit 8cb53e3

File tree

5 files changed

+130
-22
lines changed

5 files changed

+130
-22
lines changed

README.md

+37-7
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ nixpkgs clone in `nix_file` or `nix_file_content`.
168168
<td>
169169
<p><code>Label-keyed String dict; optional</code></p>
170170
<p>A dictionary mapping repositoriy labels to `NIX_PATH` entries.</p>
171-
<p>Setting it to <pre>
171+
<p>Setting it to
172+
<pre><code>
172173
repositories = { "myrepo" : "//:myrepo" }
173-
</pre>
174+
</code></pre>
174175
for example would replace all instances
175176
of <code>&lt;myrepo&gt;</code> in the called nix code by the
176177
path to the target <code>"//:myrepo"</code>. See the
@@ -182,17 +183,46 @@ repositories = { "myrepo" : "//:myrepo" }
182183
<tr>
183184
<td><code>build_file</code></td>
184185
<td>
185-
<p><code>String; optional</code></p>
186-
<p>The file to use as the BUILD file for this repository. This
187-
attribute is a label relative to the main workspace. The
188-
file does not need to be named BUILD, but can be.</p>
186+
<p><code>Label; optional</code></p>
187+
<p>The file to use as the BUILD file for this repository.
188+
Its contents are copied copied into the file
189+
<code>BUILD</code> in root of the nix output folder.
190+
The Label does not need to be named BUILD, but can be.
191+
</p>
192+
<p>For common use cases we provide filegroups that expose
193+
certain files as targets:
194+
<dl>
195+
<dt><code>:bin</code></dt>
196+
<dd>Everything in the <code>bin/</code> directory.</dd>
197+
<dt><code>:lib</code></dt>
198+
<dd>All <code>.so</code> and <code>.a</code> files
199+
that can be found in subdirectories of
200+
<code>lib/</code>.</dd>
201+
<dt><code>:include</code></dt>
202+
<dd>All <code>.h</code> files
203+
that can be found in subdirectories of
204+
<code>bin/</code>.</dd>
205+
</dl>
206+
</p>
207+
<p>If you need different files from the nix package,
208+
you can reference them like this: <pre><code>package(default_visibility = [ "//visibility:public" ])
209+
filegroup(
210+
name = "our-docs",
211+
srcs = glob(["share/doc/ourpackage/**/*"]),
212+
)</code></pre>
213+
See the bazel documentation of
214+
<a href="https://docs.bazel.build/versions/master/be/general.html#filegroup">filegroup</a>
215+
and
216+
<a href="https://docs.bazel.build/versions/master/be/functions.html#glob">glob</a>.
217+
</p>
189218
</td>
190219
</tr>
191220
<tr>
192221
<td><code>build_file_content</code></td>
193222
<td>
194223
<p><code>String; optional</code></p>
195-
<p>The content for the BUILD file for this repository.</p>
224+
<p>Like <code>build_file</code>, but a string of the contents
225+
instead of a file name.</p>
196226
</td>
197227
</tr>
198228
</tbody>

WORKSPACE

+19
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,22 @@ nixpkgs_package(
5454
nix_file_deps = ["//tests:pkgname.nix"],
5555
repositories = { "nixpkgs": "//:nixpkgs.nix" },
5656
)
57+
58+
nixpkgs_package(
59+
name = "output-filegroup-test",
60+
nix_file = "//tests:output.nix",
61+
repositories = { "nixpkgs": "//:nixpkgs.nix" },
62+
)
63+
64+
nixpkgs_package(
65+
name = "output-filegroup-manual-test",
66+
nix_file = "//tests:output.nix",
67+
repositories = { "nixpkgs": "//:nixpkgs.nix" },
68+
build_file_content = """
69+
package(default_visibility = [ "//visibility:public" ])
70+
filegroup(
71+
name = "manual-filegroup",
72+
srcs = glob(["hi-i-exist", "hi-i-exist-too", "bin/*"]),
73+
)
74+
""",
75+
)

tests/BUILD

+47-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,49 @@
11
package(default_testonly = 1)
22

3-
[sh_test(
4-
name= "run-{0}".format(test),
5-
srcs = ["test_bin.sh"],
6-
args=["$(location @{0}//:bin)".format(test)],
7-
data = ["@{0}//:bin".format(test)],
8-
timeout = "short",
9-
) for test in [
10-
"hello",
11-
"expr-test",
12-
"attribute-test",
13-
"expr-attribute-test",
14-
"nix-file-test",
15-
"nix-file-deps-test",
16-
"nixpkgs-git-repository-test",
17-
]]
3+
[
4+
# All of these tests use the "hello" binary to see
5+
# whether different invocations of `nixpkgs_package`
6+
# produce a valid bazel repository.
7+
sh_test(
8+
name= "run-{0}".format(test),
9+
srcs = ["test_bin.sh"],
10+
args=["$(location @{0}//:bin)".format(test)],
11+
data = ["@{0}//:bin".format(test)],
12+
timeout = "short",
13+
) for test in [
14+
"hello",
15+
"expr-test",
16+
"attribute-test",
17+
"expr-attribute-test",
18+
"nix-file-test",
19+
"nix-file-deps-test",
20+
"nixpkgs-git-repository-test",
21+
]
22+
] \
23+
+ \
24+
[
25+
# These tests use the nix package generated by ./output.nix
26+
27+
# Checks whether the `:include` filegroup of `nixpkgs_package`
28+
# repositories works as intended
29+
# (that the expected number of files are inside the target)
30+
sh_test(
31+
name = "run-test-include",
32+
srcs = ["test_output.sh"],
33+
data = ["@output-filegroup-test//:include"],
34+
args = ["2", "$(locations @output-filegroup-test//:include)"],
35+
timeout = "short",
36+
),
37+
38+
# Checks whether specifying a manual filegroup in the
39+
# `nixpkgs_package` BUILD file works as well.
40+
sh_test(
41+
name = "run-test-manual-filegroup",
42+
srcs = ["test_output.sh"],
43+
data = ["@output-filegroup-manual-test//:manual-filegroup"],
44+
args = [
45+
"3",
46+
"$(locations @output-filegroup-manual-test//:manual-filegroup)"],
47+
timeout = "short",
48+
)
49+
]

tests/output.nix

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
with import <nixpkgs> {};
2+
3+
runCommand "some-output" {
4+
preferLocalBuild = true;
5+
allowSubstitutes = false;
6+
} ''
7+
mkdir -p $out/{bin,include/mylib}
8+
touch $out/hi-i-exist
9+
touch $out/hi-i-exist-too
10+
touch $out/bin/im-a-binary
11+
touch $out/include/mylib/im-a-header.h
12+
touch $out/include/mylib/im-also-a-header.h
13+
''

tests/test_output.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
# first param is the expected number of files given by `locations`
3+
expected_length="$1"
4+
5+
# rest of the arguments are files
6+
shift
7+
8+
if [[ ${#@} -ne "${expected_length}" ]]; then
9+
echo "Should have received $expected_length files, but got ${#@}:"
10+
for f in "$@"; do
11+
echo "$f"
12+
done
13+
exit 1
14+
fi

0 commit comments

Comments
 (0)