All Projects → tweag → Rules_nixpkgs

tweag / Rules_nixpkgs

Licence: apache-2.0
Rules for importing Nixpkgs packages into Bazel.

Projects that are alternatives of or similar to Rules nixpkgs

svelte-ts
[WIP] Tools for building Svelte apps with TS
Stars: ✭ 89 (+1.14%)
Mutual labels:  bazel, bazel-rules
Rules nodejs
JavaScript and NodeJS rules for Bazel
Stars: ✭ 488 (+454.55%)
Mutual labels:  bazel, bazel-rules
rules clojure
Clojure rules for Bazel
Stars: ✭ 27 (-69.32%)
Mutual labels:  bazel, bazel-rules
rules gwt
Bazel rules for GWT
Stars: ✭ 20 (-77.27%)
Mutual labels:  bazel, bazel-rules
Rules go
Go rules for Bazel
Stars: ✭ 852 (+868.18%)
Mutual labels:  bazel, bazel-rules
ios-bazel-users
Resources for using bazel for iOS development
Stars: ✭ 80 (-9.09%)
Mutual labels:  bazel, bazel-rules
Rules typescript
MOVED to https://github.com/bazelbuild/rules_nodejs/tree/3.x/third_party/github.com/bazelbuild/rules_typescript
Stars: ✭ 280 (+218.18%)
Mutual labels:  bazel, bazel-rules
rules scala
Robust and featureful Bazel rules for Scala
Stars: ✭ 62 (-29.55%)
Mutual labels:  bazel, bazel-rules
Rules terraform
Bazel rules for using Hashicorp's Terraform in your Bazel builds.
Stars: ✭ 26 (-70.45%)
Mutual labels:  bazel, bazel-rules
Rules docker
Rules for building and handling Docker images with Bazel
Stars: ✭ 744 (+745.45%)
Mutual labels:  bazel, bazel-rules
Bazel Linting System
🌿💚 Experimental system for registering, configuring, and invoking source-code linters in Bazel.
Stars: ✭ 63 (-28.41%)
Mutual labels:  bazel, bazel-rules
Platforms
Constraint values for specifying platforms and toolchains
Stars: ✭ 34 (-61.36%)
Mutual labels:  bazel, bazel-rules
grab-bazel-common
Common rules and macros for Grab's Android projects built with Bazel.
Stars: ✭ 20 (-77.27%)
Mutual labels:  bazel, bazel-rules
rules poetry
Bazel rules that use Poetry for Python package management
Stars: ✭ 40 (-54.55%)
Mutual labels:  bazel, bazel-rules
rules verilator
Bazel build rules for Verilator
Stars: ✭ 14 (-84.09%)
Mutual labels:  bazel, bazel-rules
Rules scala
Scala rules for Bazel
Stars: ✭ 269 (+205.68%)
Mutual labels:  bazel, bazel-rules
rules helm
rules_helm: Bazel rules for managing helm charts
Stars: ✭ 46 (-47.73%)
Mutual labels:  bazel, bazel-rules
rules elm
Bazel rules for building web applications written in Elm
Stars: ✭ 22 (-75%)
Mutual labels:  bazel, bazel-rules
Awesome Bazel
A curated list of Bazel rules, tooling and resources.
Stars: ✭ 640 (+627.27%)
Mutual labels:  bazel, bazel-rules
Rules codeowners
Bazel rules for generating CODEOWNERS from a workspace.
Stars: ✭ 31 (-64.77%)
Mutual labels:  bazel, bazel-rules

Nixpkgs rules for Bazel

Build status

Use Nix and the Nixpkgs package set to import external dependencies (like system packages) into Bazel hermetically. If the version of any dependency changes, Bazel will correctly rebuild targets, and only those targets that use the external dependencies that changed.

Links:

Rules

Setup

Add the following to your WORKSPACE file, and select a $COMMIT accordingly.

http_archive(
    name = "io_tweag_rules_nixpkgs",
    strip_prefix = "rules_nixpkgs-$COMMIT",
    urls = ["https://github.com/tweag/rules_nixpkgs/archive/$COMMIT.tar.gz"],
)

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

load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_git_repository", "nixpkgs_package", "nixpkgs_cc_toolchain")

load("@io_tweag_rules_nixpkgs//nixpkgs:toolchains/go.bzl", "nixpkgs_go_configure") # optional

If you use rules_nixpkgs to configure a toolchain then you will also need to configure the build platform to include the @io_tweag_rules_nixpkgs//nixpkgs/constraints:support_nix constraint. For example by adding the following to .bazelrc:

build [email protected]_tweag_rules_nixpkgs//nixpkgs/platforms:host

Example

nixpkgs_git_repository(
    name = "nixpkgs",
    revision = "17.09", # Any tag or commit hash
    sha256 = "" # optional sha to verify package integrity!
)

nixpkgs_package(
    name = "hello",
    repositories = { "nixpkgs": "@nixpkgs//:default.nix" }
)

Rules

Rules for importing Nixpkgs packages.

nixpkgs_git_repository

nixpkgs_git_repository(name, remote, revision, sha256)

Name a specific revision of Nixpkgs on GitHub or a local checkout.

Attributes

name

Name; required

A unique name for this repository.

remote

String; optional

The URI of the remote Git repository. This must be a HTTP URL. There is currently no support for authentication. Defaults to upstream nixpkgs.

revision

String; required

Git commit hash or tag identifying the version of Nixpkgs to use.

sha256

String; optional

The SHA256 used to verify the integrity of the repository.

nixpkgs_local_repository

nixpkgs_local_repository(name, nix_file, nix_file_content, nix_file_deps)

Create an external repository representing the content of Nixpkgs, based on a Nix expression stored locally or provided inline. One of nix_file or nix_file_content must be provided.

Attributes

name

Name; required

A unique name for this repository.

nix_file

Label; optional

A file containing an expression for a Nix derivation.

nix_file_content

String; optional

An expression for a Nix derivation.

nix_file_deps

List of labels; optional

Dependencies of nix_file if any.

nixpkgs_cc_configure

nixpkgs_cc_configure(name, attribute_path, nix_file, nix_file_content, nix_file_deps, repositories,
                     repository, nixopts, quiet, fail_not_supported)

Use a CC toolchain from Nixpkgs. No-op if not a nix-based platform.

By default, Bazel auto-configures a CC toolchain from commands (e.g. gcc) available in the environment. To make builds more hermetic, use this rule to specify explicitly which commands the toolchain should use.

Specifically, it builds a Nix derivation that provides the CC toolchain tools in the bin/ path and constructs a CC toolchain that uses those tools. Tools that aren't found are replaced by ${coreutils}/bin/false. You can inspect the resulting @<name>_info//:CC_TOOLCHAIN_INFO to see which tools were discovered.

This rule depends on rules_cc.

Note: You need to configure [email protected]<name>//:toolchain to activate this toolchain.

Parameters

name

optional. default is "local_config_cc"

attribute_path

optional. default is ""

optional, string, Obtain the toolchain from the Nix expression under this attribute path. Requires nix_file or nix_file_content.

nix_file

optional. default is None

optional, Label, Obtain the toolchain from the Nix expression defined in this file. Specify only one of nix_file or nix_file_content.

nix_file_content

optional. default is ""

optional, string, Obtain the toolchain from the given Nix expression. Specify only one of nix_file or nix_file_content.

nix_file_deps

optional. default is []

optional, list of Label, Additional files that the Nix expression depends on.

repositories

optional. default is {}

dict of Label to string, Provides <nixpkgs> and other repositories. Specify one of repositories or repository.

repository

optional. default is None

Label, Provides <nixpkgs>. Specify one of repositories or repository.

nixopts

optional. default is []

optional, list of string, Extra flags to pass when calling Nix. Subject to location expansion, any instance of $(location LABEL) will be replaced by the path to the file ferenced by LABEL relative to the workspace root.

quiet

optional. default is False

bool, Whether to hide nix-build output.

fail_not_supported

optional. default is True

bool, Whether to fail if nix-build is not available.

nixpkgs_cc_configure_deprecated

nixpkgs_cc_configure_deprecated(repository, repositories, nix_file, nix_file_deps, nix_file_content,
                                nixopts)

Use a CC toolchain from Nixpkgs. No-op if not a nix-based platform.

Tells Bazel to use compilers and linkers from Nixpkgs for the CC toolchain. By default, Bazel auto-configures a CC toolchain from commands available in the environment (e.g. gcc). Overriding this autodetection makes builds more hermetic and is considered a best practice.

Example

nixpkgs_cc_configure(repository = "@nixpkgs//:default.nix")

Parameters

repository

optional. default is None

A repository label identifying which Nixpkgs to use. Equivalent to repositories = { "nixpkgs": ...}.

repositories

optional. default is {}

A dictionary mapping NIX_PATH entries to repository labels.

Setting it to

repositories = { "myrepo" : "//:myrepo" }

for example would replace all instances of <myrepo> in the called nix code by the path to the target "//:myrepo". See the relevant section in the nix manual for more information.

Specify one of repository or repositories.

nix_file

optional. default is None

An expression for a Nix environment derivation. The environment should expose all the commands that make up a CC toolchain (cc, ld etc). Exposes all commands in stdenv.cc and binutils by default.

nix_file_deps

optional. default is None

Dependencies of nix_file if any.

nix_file_content

optional. default is None

An expression for a Nix environment derivation.

nixopts

optional. default is []

Options to forward to the nix command.

nixpkgs_package

nixpkgs_package(name, attribute_path, nix_file, nix_file_deps, nix_file_content, repository,
                repositories, build_file, build_file_content, nixopts, quiet, fail_not_supported,
                kwargs)

Make the content of a Nixpkgs package available in the Bazel workspace.

If repositories is not specified, you must provide a nixpkgs clone in nix_file or nix_file_content.

Parameters

name

required.

A unique name for this repository.

attribute_path

optional. default is ""

Select an attribute from the top-level Nix expression being evaluated. The attribute path is a sequence of attribute names separated by dots.

nix_file

optional. default is None

A file containing an expression for a Nix derivation.

nix_file_deps

optional. default is []

Dependencies of nix_file if any.

nix_file_content

optional. default is ""

An expression for a Nix derivation.

repository

optional. default is None

A repository label identifying which Nixpkgs to use. Equivalent to repositories = { "nixpkgs": ...}

repositories

optional. default is {}

A dictionary mapping NIX_PATH entries to repository labels.

Setting it to

repositories = { "myrepo" : "//:myrepo" }

for example would replace all instances of <myrepo> in the called nix code by the path to the target "//:myrepo". See the relevant section in the nix manual for more information.

Specify one of repository or repositories.

build_file

optional. default is None

The file to use as the BUILD file for this repository.

Its contents are copied copied into the file BUILD in root of the nix output folder. The Label does not need to be named BUILD, but can be.

For common use cases we provide filegroups that expose certain files as targets:

:bin
Everything in the bin/ directory.
:lib
All .so and .a files that can be found in subdirectories of lib/.
:include
All .h files that can be found in subdirectories of bin/.

If you need different files from the nix package, you can reference them like this:

package(default_visibility = [ "//visibility:public" ])
filegroup(
    name = "our-docs",
    srcs = glob(["share/doc/ourpackage/**/*"]),
)

See the bazel documentation of filegroup and glob.

build_file_content

optional. default is ""

Like build_file, but a string of the contents instead of a file name.

nixopts

optional. default is []

Extra flags to pass when calling Nix.

quiet

optional. default is False

Whether to hide the output of the Nix command.

fail_not_supported

optional. default is 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.

kwargs

optional.

nixpkgs_python_configure

nixpkgs_python_configure(name, python2_attribute_path, python2_bin_path, python3_attribute_path,
                         python3_bin_path, repository, repositories, nix_file_deps, nixopts,
                         fail_not_supported, quiet)

Define and register a Python toolchain provided by nixpkgs.

Creates nixpkgs_packages for Python 2 or 3 py_runtime instances and a corresponding py_runtime_pair and toolchain. The toolchain is automatically registered and uses the constraint:

"@io_tweag_rules_nixpkgs//nixpkgs/constraints:support_nix"

Parameters

name

optional. default is "nixpkgs_python_toolchain"

The name-prefix for the created external repositories.

python2_attribute_path

optional. default is None

The nixpkgs attribute path for python2.

python2_bin_path

optional. default is "bin/python"

The path to the interpreter within the package.

python3_attribute_path

optional. default is "python3"

The nixpkgs attribute path for python3.

python3_bin_path

optional. default is "bin/python"

The path to the interpreter within the package.

repository

optional. default is None

See nixpkgs_package.

repositories

optional. default is {}

See nixpkgs_package.

nix_file_deps

optional. default is None

See nixpkgs_package.

nixopts

optional. default is []

See nixpkgs_package.

fail_not_supported

optional. default is True

See nixpkgs_package.

quiet

optional. default is False

See nixpkgs_package.

nixpkgs_sh_posix_configure

nixpkgs_sh_posix_configure(name, packages, kwargs)

Create a POSIX toolchain from nixpkgs.

Loads the given Nix packages, scans them for standard Unix tools, and generates a corresponding sh_posix_toolchain.

Make sure to call nixpkgs_sh_posix_configure before sh_posix_configure, if you use both. Otherwise, the local toolchain will always be chosen in favor of the nixpkgs one.

Parameters

name

optional. default is "nixpkgs_sh_posix_config"

Name prefix for the generated repositories.

packages

optional. default is ["stdenv.initialPath"]

List of Nix attribute paths to draw Unix tools from.

kwargs

optional.

Rules for importing a Go toolchain from Nixpkgs.

NOTE: The following rules must be loaded from @io_tweag_rules_nixpkgs//nixpkgs:toolchains/go.bzl to avoid unnecessary dependencies on rules_go for those who don't need go toolchain. io_bazel_rules_go must be available for loading before loading of @io_tweag_rules_nixpkgs//nixpkgs:toolchains/go.bzl.

nixpkgs_go_configure

nixpkgs_go_configure(sdk_name, repository, repositories, nix_file, nix_file_deps, nix_file_content,
                     nixopts)

Use go toolchain from Nixpkgs. Will fail if not a nix-based platform.

By default rules_go configures the go toolchain to be downloaded as binaries (which doesn't work on NixOS), there is a way to tell rules_go to look into environment and find local go binary which is not hermetic. This command allows to setup hermetic go sdk from Nixpkgs, which should be considerate as best practice.

Note that the nix package must provide a full go sdk at the root of the package instead of in $out/share/go, and also provide an empty normal file named PACKAGE_ROOT at the root of package.

Example

nixpkgs_go_configure(repository = "@nixpkgs//:default.nix")

Example (optional nix support when go is a transitive dependency):

# .bazel-lib/nixos-support.bzl
def _has_nix(ctx):
    return ctx.which("nix-build") != None

def _gen_imports_impl(ctx):
    ctx.file("BUILD", "")

    imports_for_nix = """
        load("@io_tweag_rules_nixpkgs//nixpkgs:toolchains/go.bzl", "nixpkgs_go_configure")

        def fix_go():
            nixpkgs_go_configure(repository = "@nixpkgs")
    """
    imports_for_non_nix = """
        def fix_go():
            # if go isn't transitive you'll need to add call to go_register_toolchains here
            pass
    """

    if _has_nix(ctx):
        ctx.file("imports.bzl", imports_for_nix)
    else:
        ctx.file("imports.bzl", imports_for_non_nix)

_gen_imports = repository_rule(
    implementation = _gen_imports_impl,
    attrs = dict(),
)

def gen_imports():
    _gen_imports(
        name = "nixos_support",
    )

# WORKSPACE

// ...
http_archive(name = "io_tweag_rules_nixpkgs", ...)
// ...
local_repository(
    name = "bazel_lib",
    path = ".bazel-lib",
)
load("@bazel_lib//:nixos-support.bzl", "gen_imports")
gen_imports()
load("@nixos_support//:imports.bzl", "fix_go")
fix_go()

Parameters

sdk_name

optional. default is "go_sdk"

Go sdk name to pass to rules_go

repository

optional. default is None

A repository label identifying which Nixpkgs to use. Equivalent to repositories = { "nixpkgs": ...}.

repositories

optional. default is {}

A dictionary mapping NIX_PATH entries to repository labels.

Setting it to

repositories = { "myrepo" : "//:myrepo" }

for example would replace all instances of <myrepo> in the called nix code by the path to the target "//:myrepo". See the relevant section in the nix manual in the nix manual for more information.

Specify one of path or repositories.

nix_file

optional. default is None

An expression for a Nix environment derivation. The environment should expose the whole go SDK (bin, src, ...) at the root of package. It also must contain a PACKAGE_ROOT file in the root of pacakge.

nix_file_deps

optional. default is None

Dependencies of nix_file if any.

nix_file_content

optional. default is None

An expression for a Nix environment derivation.

nixopts

optional. default is []

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].