Skip to content

Commit

Permalink
Merge pull request tweag#58 from tweag/nm-windows-noop
Browse files Browse the repository at this point in the history
Allow nixpkgs_package rules to not fail on Windows
  • Loading branch information
nmattia authored Jan 16, 2019
2 parents 6747660 + b187de9 commit f23f481
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/).

## [Unreleased]
### Added
- `nixpkgs_package` now has a new optional argument `fail_not_supported`
allowing the rule to _not_ fail on Windows (when set to `False`)

## [0.5.1] - 2018-12-18

### Changed
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ Make the content of a Nixpkgs package available in the Bazel workspace.
nixpkgs_package(
name, attribute_path, nix_file, nix_file_deps, nix_file_content,
repository, repositories, build_file, build_file_content,
fail_not_supported,
)
```

Expand Down Expand Up @@ -276,6 +277,18 @@ filegroup(
instead of a file name.</p>
</td>
</tr>
<tr>
<td><code>fail_not_supported</code></td>
<td>
<p><code>Boolean; optional; default = True</code></p>
<p>
If set to <code>True</code> (default) this rule will fail on
platforms which do not support Nix (e.g. Windows). If set to
<code>False</code> calling this rule will succeed but no output
will be generated.
</p>
</td>
</tr>
</tbody>
</table>

Expand Down
67 changes: 40 additions & 27 deletions nixpkgs/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ def _nixpkgs_package_impl(repository_ctx):
repository = repository_ctx.attr.repository
repositories = repository_ctx.attr.repositories

# Is nix supported on this platform?
not_supported = repository_ctx.os.name.startswith("windows")
# Should we fail if Nix is not supported?
fail_not_supported = repository_ctx.attr.fail_not_supported

if repository and repositories or not repository and not repositories:
fail("Specify one of 'repository' or 'repositories' (but not both).")
elif repository:
Expand Down Expand Up @@ -121,35 +126,40 @@ def _nixpkgs_package_impl(repository_ctx):
elif not (repository_ctx.attr.nix_file or repository_ctx.attr.nix_file_content):
fail(strFailureImplicitNixpkgs)

nix_build_path = _executable_path(
repository_ctx,
"nix-build",
extra_msg = "See: https://nixos.org/nix/",
)
nix_build = [nix_build_path] + expr_args

# Large enough integer that Bazel can still parse. We don't have
# access to MAX_INT and 0 is not a valid timeout so this is as good
# as we can do.
timeout = 1073741824

exec_result = _execute_or_fail(
repository_ctx,
nix_build,
failure_message = "Cannot build Nix attribute '{}'.".format(
repository_ctx.attr.attribute_path,
),
quiet = False,
timeout = timeout,
environment = dict(NIX_PATH = nix_path),
)
output_path = exec_result.stdout.splitlines()[-1]
if not_supported and fail_not_supported:
fail("Platform is not supported (see 'fail_not_supported')")
elif not_supported:
return
else:
nix_build_path = _executable_path(
repository_ctx,
"nix-build",
extra_msg = "See: https://nixos.org/nix/",
)
nix_build = [nix_build_path] + expr_args

# Large enough integer that Bazel can still parse. We don't have
# access to MAX_INT and 0 is not a valid timeout so this is as good
# as we can do.
timeout = 1073741824
exec_result = _execute_or_fail(
repository_ctx,
nix_build,
failure_message = "Cannot build Nix attribute '{}'.".format(
repository_ctx.attr.attribute_path,
),
quiet = False,
timeout = timeout,
environment = dict(NIX_PATH = nix_path),
)
output_path = exec_result.stdout.splitlines()[-1]

# Build a forest of symlinks (like new_local_package() does) to the
# Nix store.
for target in _find_children(repository_ctx, output_path):
basename = target.rpartition("/")[-1]
repository_ctx.symlink(target, basename)
# Build a forest of symlinks (like new_local_package() does) to the
# Nix store.
for target in _find_children(repository_ctx, output_path):
basename = target.rpartition("/")[-1]
repository_ctx.symlink(target, basename)

_nixpkgs_package = repository_rule(
implementation = _nixpkgs_package_impl,
Expand All @@ -163,6 +173,9 @@ _nixpkgs_package = repository_rule(
"build_file": attr.label(),
"build_file_content": attr.string(),
"nixopts": attr.string_list(),
"fail_not_supported": attr.bool(default = True, doc = """
If set to True (default) this rule will fail on platforms which do not support Nix (e.g. Windows). If set to False calling this rule will succeed but no output will be generated.
"""),
},
)

Expand Down

0 comments on commit f23f481

Please sign in to comment.