Skip to content

Commit 53700e4

Browse files
committed
Rename expression to nix_file_content. Add nix_file attribute.
Fixes #6.
1 parent 57945f4 commit 53700e4

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ nixpkgs_git_repository(name, revision)
8181
Make the content of a Nixpkgs package available in the Bazel workspace.
8282

8383
```bzl
84-
nixpkgs_package(name, attribute_path, repository, build_file, build_file_content)
84+
nixpkgs_package(name, attribute_path, nix_file, nix_file_content,
85+
path, repository, build_file, build_file_content)
8586
```
8687

8788
If neither `repository` or `path` are specified, `<nixpkgs>` is
@@ -115,6 +116,20 @@ recommended. The two are mutually exclusive.
115116
names separated by dots.</p>
116117
</td>
117118
</tr>
119+
<tr>
120+
<td><code>nix_file</code></td>
121+
<td>
122+
<p><code>String; optional</code></p>
123+
<p>A file containing an expression for a Nix derivation.</p>
124+
</td>
125+
</tr>
126+
<tr>
127+
<td><code>nix_file_content</code></td>
128+
<td>
129+
<p><code>String; optional</code></p>
130+
<p>An expression for a Nix derivation.</p>
131+
</td>
132+
</tr>
118133
<tr>
119134
<td><code>repository</code></td>
120135
<td>

WORKSPACE

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ nixpkgs_package(name = "hello", repository = "@nixpkgs")
1313

1414
nixpkgs_package(
1515
name = "expr-test",
16-
expression = "let pkgs = import <nixpkgs> {}; in pkgs.hello",
16+
nix_file_content = "let pkgs = import <nixpkgs> {}; in pkgs.hello",
1717
repository = "@nixpkgs"
1818
)
1919

@@ -25,7 +25,7 @@ nixpkgs_package(
2525

2626
nixpkgs_package(
2727
name = "expr-attribute-test",
28-
expression = "import <nixpkgs> {}",
28+
nix_file_content = "import <nixpkgs> {}",
2929
attribute_path = "hello",
3030
repository = "@nixpkgs",
3131
)

nixpkgs/nixpkgs.bzl

+18-12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ def _nixpkgs_package_impl(ctx):
2727
else:
2828
ctx.template("BUILD", Label("@io_tweag_rules_nixpkgs//nixpkgs:BUILD.pkg"))
2929

30+
if ctx.attr.nix_file and ctx.attr.nix_file_content:
31+
fail("Specify one of 'nix_file' or 'nix_file_content', but not both.")
32+
elif ctx.attr.nix_file:
33+
ctx.symlink(ctx.attr.nix_file, "default.nix")
34+
elif ctx.attr.nix_file_content:
35+
expr_args = ["-E", ctx.attr.nix_file_content]
36+
else:
37+
expr_args = ["-E", "import <nixpkgs> {}"]
38+
39+
expr_args.extend([
40+
"-A", ctx.attr.attribute_path
41+
if ctx.attr.nix_file or ctx.attr.nix_file_content
42+
else ctx.attr.attribute_path or ctx.attr.name,
43+
])
44+
3045
# If neither repository or path are set, leave empty which will use
3146
# default value from NIX_PATH.
3247
path = []
@@ -40,12 +55,6 @@ def _nixpkgs_package_impl(ctx):
4055
if ctx.attr.path:
4156
path = ["-I", "nixpkgs={0}".format(ctx.attr.path)]
4257

43-
expr_args = [
44-
"-E", ctx.attr.expression or "import <nixpkgs> {}",
45-
"-A", ctx.attr.attribute_path
46-
if ctx.attr.expression
47-
else ctx.attr.attribute_path or ctx.attr.name,
48-
]
4958
nix_build = ["nix-build"] + path + ["--no-out-link"] + expr_args
5059

5160
res = ctx.execute(nix_build, quiet = False)
@@ -58,12 +67,9 @@ def _nixpkgs_package_impl(ctx):
5867
nixpkgs_package = repository_rule(
5968
implementation = _nixpkgs_package_impl,
6069
attrs = {
61-
"attribute_path": attr.string(
62-
doc="Nix attribute to build. Exclusive to expression."
63-
),
64-
"expression": attr.string(
65-
doc="Nix expression to build. Rule name used as attribute if not present.",
66-
),
70+
"attribute_path": attr.string(),
71+
"nix_file": attr.label(allow_single_file = [".nix"]),
72+
"nix_file_content": attr.string(),
6773
"path": attr.string(),
6874
"repository": attr.label(),
6975
"build_file": attr.label(),

0 commit comments

Comments
 (0)