Skip to content

Commit 4a75b2d

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 c653798 commit 4a75b2d

File tree

5 files changed

+130
-23
lines changed

5 files changed

+130
-23
lines changed

README.md

+36-8
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ 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>
172-
repositories = { "myrepo" : "//:myrepo" }
173-
</pre>
171+
<p>Setting it to
172+
<pre><code>repositories = { "myrepo" : "//:myrepo" }</code></pre>
174173
for example would replace all instances
175174
of <code>&lt;myrepo&gt;</code> in the called nix code by the
176175
path to the target <code>"//:myrepo"</code>. See the
@@ -182,17 +181,46 @@ repositories = { "myrepo" : "//:myrepo" }
182181
<tr>
183182
<td><code>build_file</code></td>
184183
<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>
184+
<p><code>Label; optional</code></p>
185+
<p>The file to use as the BUILD file for this repository.
186+
Its contents are copied copied into the file
187+
<code>BUILD</code> in root of the nix output folder.
188+
The Label does not need to be named BUILD, but can be.
189+
</p>
190+
<p>For common use cases we provide filegroups that expose
191+
certain files as targets:
192+
<dl>
193+
<dt><code>:bin</code></dt>
194+
<dd>Everything in the <code>bin/</code> directory.</dd>
195+
<dt><code>:lib</code></dt>
196+
<dd>All <code>.so</code> and <code>.a</code> files
197+
that can be found in subdirectories of
198+
<code>lib/</code>.</dd>
199+
<dt><code>:include</code></dt>
200+
<dd>All <code>.h</code> files
201+
that can be found in subdirectories of
202+
<code>bin/</code>.</dd>
203+
</dl>
204+
</p>
205+
<p>If you need different files from the nix package,
206+
you can reference them like this: <pre><code>package(default_visibility = [ "//visibility:public" ])
207+
filegroup(
208+
name = "our-docs",
209+
srcs = glob(["share/doc/ourpackage/**/*"]),
210+
)</code></pre>
211+
See the bazel documentation of
212+
<a href="https://docs.bazel.build/versions/master/be/general.html#filegroup">filegroup</a>
213+
and
214+
<a href="https://docs.bazel.build/versions/master/be/functions.html#glob">glob</a>.
215+
</p>
189216
</td>
190217
</tr>
191218
<tr>
192219
<td><code>build_file_content</code></td>
193220
<td>
194221
<p><code>String; optional</code></p>
195-
<p>The content for the BUILD file for this repository.</p>
222+
<p>Like <code>build_file</code>, but a string of the contents
223+
instead of a file name.</p>
196224
</td>
197225
</tr>
198226
</tbody>

WORKSPACE

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

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

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
no_of_files=$#
8+
9+
if [ "$no_of_files" -ne "$expected_length" ]; then
10+
echo "Should have received $expected_length files, but got $no_of_files:"
11+
for f in "$@"; do
12+
echo "$f"
13+
done
14+
exit 1
15+
fi

0 commit comments

Comments
 (0)