Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable using a custom Nickel binary #24

Open
vkleen opened this issue Oct 4, 2023 · 1 comment
Open

Enable using a custom Nickel binary #24

vkleen opened this issue Oct 4, 2023 · 1 comment
Assignees

Comments

@vkleen
Copy link
Collaborator

vkleen commented Oct 4, 2023

Currently, the Nickel toolchain in rules_nickel insists on downloading one of the static binaries built as part of Nickel's GitHub release process. It would be useful to have the option of using a custom build of Nickel for various purposes:

  • Trying unreleased versions
  • Running Nickel on architectures other than x86_64-linux and arm64-linux

In particular, this would make it possible to use rules_nixpkgs to provision a custom Nickel executable on whichever architectures supported by nixpkgs. For example, this would include darwin which is supported by Nickel but not built as part of the GitHub release.

@vkleen vkleen moved this to Todo in rules_nickel Roadmap Oct 4, 2023
@vkleen vkleen moved this from Todo to In Progress in rules_nickel Roadmap Oct 4, 2023
@vkleen vkleen self-assigned this Oct 4, 2023
@gkze
Copy link

gkze commented Oct 6, 2023

Thanks @vkleen - for now I'm wrapping the Nixpkgs Nickel with a minimal toolchain & export rule

nickel_nix.bzl
"""Nixpkgs-based Nickel toolchain and Nickel export rule."""

NickelInfo = provider(
    "Information about how to invoke Nickel.",
    fields = {
        "nixpkgs_package": "Nix(pkgs) package for Nickel",
        "nickel_binary": "Nickel binary",
    },
)

def _nickel_nix_toolchain_impl(ctx):
    return [platform_common.ToolchainInfo(nickel_info = NickelInfo(
        nixpkgs_package = ctx.attr.nixpkgs_package,
        nickel_binary = ctx.file.nickel_binary,
    ))]

nickel_nix_toolchain = rule(
    implementation = _nickel_nix_toolchain_impl,
    attrs = {
        "nixpkgs_package": attr.string(mandatory = True),
        "nickel_binary": attr.label(mandatory = True, allow_single_file = True),
    },
)

def _nickel_export_impl(ctx):
    nickel = ctx.toolchains[":toolchain_type"].nickel_info

    args = ctx.actions.args()
    args.add_all([
        "export",
        "--file",
        ctx.file.src.path,
        "--format",
        ctx.attr.format,
    ])

    output = ctx.actions.declare_file(ctx.label.name)
    args.add_all(["--output", output.path])

    ctx.actions.run(
        inputs = [ctx.file.src] + ctx.files.deps,
        outputs = [output],
        arguments = [args],
        progress_message = "Exporting %s" % output.short_path,
        executable = nickel.nickel_binary,
        toolchain = ":toolchain_type",
    )

    return [DefaultInfo(files = depset([output]))]

nickel_export = rule(
    implementation = _nickel_export_impl,
    attrs = {
        "src": attr.label(
            doc = "Top-level Nickel file to export from",
            mandatory = True,
            allow_single_file = [".ncl"],
        ),
        "deps": attr.label_list(
            doc = "Nickel files required by the top-level file",
            allow_files = [".ncl"],
        ),
        "format": attr.string(
            doc = "Output format",
            default = "json",
            values = ["json", "yaml", "toml", "raw"],
        ),
    },
    toolchains = [":toolchain_type"],
)
BUILD.bazel
load(":nickel_nix.bzl", "nickel_nix_toolchain")

toolchain_type(name = "toolchain_type")

COMPAT_PLATFORMS = [
    "@platforms//cpu:arm64",
    "@platforms//os:macos",
]

toolchain(
    name = "nickel_nix_toolchain",
    exec_compatible_with = COMPAT_PLATFORMS,
    target_compatible_with = COMPAT_PLATFORMS,
    toolchain = "nickel_nix",
    toolchain_type = ":toolchain_type",
)

nickel_nix_toolchain(
    name = "nickel_nix",
    nickel_binary = "@nickel//:bin/nickel",
    nixpkgs_package = "nickel",
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

No branches or pull requests

2 participants