diff --git a/CHANGELOG.md b/CHANGELOG.md index 555b7b5c4..af3a1aed9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 8ff250db9..c4d8403a5 100644 --- a/README.md +++ b/README.md @@ -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, ) ``` @@ -276,6 +277,18 @@ filegroup( instead of a file name.

+ + fail_not_supported + +

Boolean; optional; default = True

+

+ 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. +

+ + diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index 72df34dc2..72aeeb771 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -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: @@ -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, @@ -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. + """), }, )