Skip to content

Commit

Permalink
Add nodejs toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
betaboon committed Apr 23, 2022
1 parent be7af4c commit 022d217
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
toolchains/python
toolchains/posix
toolchains/rust
toolchains/nodejs
)
for dir in "${dirs[@]}"
do
Expand Down
6 changes: 6 additions & 0 deletions nixpkgs/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ See [examples](/examples/toolchains) for how to use `rules_nixpkgs` with differe
* [nixpkgs_go_configure](toolchains/go/README.md#nixpkgs_go_configure)
* [nixpkgs_rust_configure](#nixpkgs_rust_configure)
* [nixpkgs_sh_posix_configure](#nixpkgs_sh_posix_configure)
* [nixpkgs_nodejs_configure](#nixpkgs_nodejs_configure)
## Setup
Expand Down Expand Up @@ -144,6 +145,10 @@ load(
"@rules_nixpkgs_posix//:posix.bzl",
_nixpkgs_sh_posix_configure = "nixpkgs_sh_posix_configure",
)
load(
"@rules_nixpkgs_nodejs//:nodejs.bzl",
_nixpkgs_nodejs_configure = "nixpkgs_nodejs_configure",
)

# aliases for backwards compatibility prior to `bzlmod`
nixpkgs_git_repository = _nixpkgs_git_repository
Expand All @@ -154,6 +159,7 @@ nixpkgs_java_configure = _nixpkgs_java_configure
nixpkgs_cc_configure = _nixpkgs_cc_configure
nixpkgs_rust_configure = _nixpkgs_rust_configure
nixpkgs_sh_posix_configure = _nixpkgs_sh_posix_configure
nixpkgs_nodejs_configure = _nixpkgs_nodejs_configure

def nixpkgs_cc_autoconf_impl(repository_ctx):
cpu_value = get_cpu_value(repository_ctx)
Expand Down
1 change: 1 addition & 0 deletions nixpkgs/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def rules_nixpkgs_dependencies(rules_nixpkgs_name = "io_tweag_rules_nixpkgs"):
("rules_nixpkgs_go", "toolchains/go"),
("rules_nixpkgs_rust", "toolchains/rust"),
("rules_nixpkgs_posix", "toolchains/posix"),
("rules_nixpkgs_nodejs", "toolchains/nodejs"),
]:
# case analysis in inner loop to reduce code duplication
if kind == "local_repository":
Expand Down
1 change: 1 addition & 0 deletions toolchains/nodejs/.bazelrc
17 changes: 17 additions & 0 deletions toolchains/nodejs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

package(default_visibility = ["//visibility:public"])

filegroup(
name = "srcs",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)

bzl_library(
name = "nodejs",
srcs = ["//:nodejs.bzl"],
deps = [
"@rules_nixpkgs_core//:nixpkgs",
],
)
8 changes: 8 additions & 0 deletions toolchains/nodejs/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module(
name = "rules_nixpkgs_nodejs",
version = "0.8.1",
)

bazel_dep(name = "rules_nixpkgs_core", version = "0.8.1")
# TODO: the version of `rules_nodejs` on BCR is quite old
# bazel_dep(name = "rules_nodejs", version = "5.4.0")
Empty file added toolchains/nodejs/README.md
Empty file.
39 changes: 39 additions & 0 deletions toolchains/nodejs/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# NOTE: temporary boilerplate for compatibility with `WORKSPACE` setup!
# TODO: remove when migration to `bzlmod` is complete

### generic dependencies for rendering documentation

local_repository(
name = "io_tweag_rules_nixpkgs",
path = "../..",
)

load("@io_tweag_rules_nixpkgs//nixpkgs:repositories.bzl", "rules_nixpkgs_dependencies")

rules_nixpkgs_dependencies()

load("@rules_nixpkgs_core//docs:dependencies_1.bzl", "docs_dependencies_1")

docs_dependencies_1()

load("@rules_nixpkgs_core//docs:dependencies_2.bzl", "docs_dependencies_2")

docs_dependencies_2()

### end generic dependencies for rendering documentation

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "2b2004784358655f334925e7eadc7ba80f701144363df949b3293e1ae7a2fb7b",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.4.0/rules_nodejs-5.4.0.tar.gz"],
)

load("//:nodejs.bzl", "nixpkgs_nodejs_configure")

nixpkgs_nodejs_configure(repository = "@nixpkgs")

load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_dependencies")

build_bazel_rules_nodejs_dependencies()
8 changes: 8 additions & 0 deletions toolchains/nodejs/docs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# load("@rules_nixpkgs_core//docs:stardoc.bzl", "generate_documentation")
#
# generate_documentation(
# name = "README.md",
# input = "//:nodejs.bzl",
# symbol_names = ["nixpkgs_nodejs_configure"],
# deps = ["//:nodejs"],
# )
107 changes: 107 additions & 0 deletions toolchains/nodejs/nodejs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package")
load("@rules_nixpkgs_core//:util.bzl", "ensure_constraints")

_nodejs_nix_content = """\
let
pkgs = import <nixpkgs> {{ config = {{}}; overlays = []; }};
nodejs = pkgs.{attribute_path};
in
pkgs.buildEnv {{
extraOutputsToInstall = ["out" "bin" "lib"];
name = "bazel-nodejs-toolchain";
paths = [ nodejs ];
postBuild = ''
touch $out/ROOT
cat <<EOF > $out/BUILD
filegroup(
name = "nodejs",
srcs = ["bin/node"],
visibility = ["//visibility:public"],
)
load("@rules_nodejs//nodejs:toolchain.bzl", "node_toolchain")
node_toolchain(
name = "nodejs_nix_impl",
target_tool = ":nodejs",
visibility = ["//visibility:public"],
)
EOF
'';
}}
"""

_nodejs_nix_toolchain = """
toolchain(
name = "nodejs_nix",
toolchain = "@{toolchain_repo}//:nodejs_nix_impl",
toolchain_type = "@rules_nodejs//nodejs:toolchain_type",
exec_compatible_with = {exec_constraints},
target_compatible_with = {target_constraints},
)
"""

def _nixpkgs_nodejs_toolchain_impl(repository_ctx):
exec_constraints, target_constraints = ensure_constraints(repository_ctx)
repository_ctx.file(
"BUILD.bazel",
executable = False,
content = _nodejs_nix_toolchain.format(
toolchain_repo = repository_ctx.attr.toolchain_repo,
exec_constraints = exec_constraints,
target_constraints = target_constraints,
),
)

_nixpkgs_nodejs_toolchain = repository_rule(
_nixpkgs_nodejs_toolchain_impl,
attrs = {
"toolchain_repo": attr.string(),
"exec_constraints": attr.string_list(),
"target_constraints": attr.string_list(),
},
)

def nixpkgs_nodejs_configure(
name = "nixpkgs_nodejs",
attribute_path = "nodejs",
repository = None,
repositories = {},
nix_file = None,
nix_file_content = None,
nix_file_deps = None,
nixopts = [],
fail_not_supported = True,
quiet = False,
exec_constraints = None,
target_constraints = None,
):
if attribute_path == None:
fail("'attribute_path' is required.", "attribute_path")

if not nix_file and not nix_file_content:
nix_file_content = _nodejs_nix_content.format(
attribute_path = attribute_path,
)

nixpkgs_package(
name = name,
repository = repository,
repositories = repositories,
nix_file = nix_file,
nix_file_deps = nix_file_deps,
nix_file_content = nix_file_content,
nixopts = nixopts,
fail_not_supported = fail_not_supported,
quiet = quiet,
)

_nixpkgs_nodejs_toolchain(
name = "{}_toolchain".format(name),
toolchain_repo = name,
exec_constraints = exec_constraints,
target_constraints = target_constraints,
)

native.register_toolchains("@{}_toolchain//:nodejs_nix".format(name))
8 changes: 8 additions & 0 deletions toolchains/nodejs/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")

package(default_testonly = 1)

nodejs_binary(
name = "nodejs-test",
entry_point = ":nodejs-test.js",
)
5 changes: 5 additions & 0 deletions toolchains/nodejs/tests/nodejs-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function main() {
console.log("Hello world");
}

main()

0 comments on commit 022d217

Please sign in to comment.