Core Haskell rules

Rules

ghc_plugin

Declare a GHC plugin.

Example:

haskell_library(
    name = "plugin-lib",
    srcs = ["Plugin.hs"],
)

ghc_plugin(
    name = "plugin",
    module = "Plugin",
    deps = [":plugin-lib"],
)

haskell_binary(
    name = "some-binary",
    srcs = ["Main.hs"],
    plugins = [":plugin"],

Plugins to use during compilation by GHC are given by the plugins attribute to Haskell rules. Plugins are haskell libraries with some extra metadata, like the name of the module that acts as the entrypoint for the plugin and plugin options.

See https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/extending_ghc.html#writing-compiler-plugins for more information.

name

A unique name for this target.

args

Plugin options.

deps

Plugin dependencies. These are compile-time dependencies only.

module

Plugin entrypoint.

tools

Tools needed by the plugin when it used.

haskell_doc

Create API documentation.

Builds API documentation (using Haddock) for the given Haskell libraries. It will automatically build documentation for any transitive dependencies to allow for cross-package documentation linking.

Examples

haskell_library(
  name = "my-lib",
  ...
)

haskell_doc(
  name = "my-lib-doc",
  deps = [":my-lib"],
)

name

A unique name for this target.

deps

List of Haskell libraries to generate documentation for.

index_transitive_deps

Whether to include documentation of transitive dependencies in index.

haskell_import

name

A unique name for this target.

deps

haddock_html

haddock_interfaces

hdrs

id

includes

linkopts

shared_libraries

static_libraries

static_profiling_libraries

version

haskell_repl

Build a REPL for multiple targets.

Examples

haskell_repl(
    name = "repl",
    deps = [
        "//lib:some_lib",
        "//exe:some_exe",
    ],
    experimental_from_source = [
        "//lib/...",
        "//exe/...",
        "//common/...",
    ],
    experimental_from_binary = [
        "//lib/vendored/...",
    ],
)

Collects all transitive Haskell dependencies from deps. Those that match experimental_from_binary or are defined in an external workspace will be loaded as binary packages. Those that match experimental_from_source and are defined in the local workspace will be loaded by source.

You can call the REPL like this:

$ bazel run //:repl

IDE Support (Experimental)

haskell_repl targets provide the hie_bios output group to optionally generate GHCi flags for hie-bios's bios cradle. You can use this for IDE support with ghcide.

Given a haskell_repl target //:repl an example .hie-bios script could look as follows. Please refer to the hie-bios documentation for further information.

#!/usr/bin/env bash
set -euo pipefail
bazel build //:repl --output_groups=hie_bios
cat bazel-bin/repl@hie-bios >"$HIE_BIOS_OUTPUT"

name

A unique name for this target.

collect_data

Whether to collect the data runfiles from the dependencies in srcs, data and deps attributes.

data

See Bazel documentation. Only available when collect_data = True.

deps

List of Haskell targets to load into the REPL

experimental_from_binary

Black-list of targets to not load by source but as packages.

Wild-card targets such as //... or //:all are allowed.

The black-list takes precedence over the white-list.

Note, this attribute will change depending on the outcome of https://github.com/bazelbuild/bazel/issues/7763.

experimental_from_source

White-list of targets to load by source.

Wild-card targets such as //... or //:all are allowed.

The black-list takes precedence over the white-list.

Note, this attribute will change depending on the outcome of https://github.com/bazelbuild/bazel/issues/7763.

repl_ghci_args

Arbitrary extra arguments to pass to GHCi. This extends compiler_flags and repl_ghci_args from the toolchain. Subject to Make variable substitution.

repl_ghci_commands

Arbitrary extra commands to execute in GHCi.

haskell_toolchain_library

Import packages that are prebuilt outside of Bazel.

Examples

haskell_toolchain_library(
    name = "base_pkg",
    package = "base",
)

haskell_library(
    name = "hello-lib",
    srcs = ["Lib.hs"],
    deps = [
        ":base_pkg",
        "//hello-sublib:lib",
    ],
)

Use this rule to make dependencies that are prebuilt (supplied as part of the compiler toolchain) available as targets.

name

A unique name for this target.

package

The name of a GHC package not built by Bazel. Defaults to the name of the rule.

Macros and Functions

haskell_binary

Build an executable from Haskell source.

Every haskell_binary target also defines an optional REPL target that is not built by default, but can be built on request. The name of the REPL target is the same as the name of binary with "@repl" added at the end. For example, the target above also defines main@repl.

You can call the REPL like this (requires Bazel 0.15 or later):

$ bazel run //:hello@repl

Examples

haskell_binary(
    name = "hello",
    srcs = ["Main.hs", "Other.hs"],
    deps = ["//lib:some_lib"]
)

name

A unique name for this rule.

src_strip_prefix

DEPRECATED. Attribute has no effect.

srcs

Haskell source files.

extra_srcs

Extra (non-Haskell) source files that will be needed at compile time (e.g. by Template Haskell).

deps

List of other Haskell libraries to be linked to this target.

data

See Bazel documentation.,

compiler_flags

Flags to pass to Haskell compiler. Subject to Make variable substitution.

repl_ghci_args

Arbitrary extra arguments to pass to GHCi. This extends compiler_flags and repl_ghci_args from the toolchain. Subject to Make variable substitution.,

runcompile_flags

Arbitrary extra arguments to pass to runghc. This extends compiler_flags and repl_ghci_args from the toolchain. Subject to Make variable substitution.

plugins

Compiler plugins to use during compilation.

tools

Extra tools needed at compile-time, like preprocessors.

worker

Experimental. Worker binary employed by Bazel's persistent worker mode. See use-cases documentation.

linkstatic

Link dependencies statically wherever possible. Some system libraries may still be linked dynamically, as are libraries for which there is no static library. So the resulting executable will still be dynamically linked, hence only mostly static.

main_function

A function with type IO _, either the qualified name of a function from any module or the bare name of a function from a Main module. It is also possible to give the qualified name of any module exposing a main function.

version

Executable version. If this is specified, CPP version macros will be generated for this build.

kwargs

Common rule attributes. See Bazel documentation.

haskell_library

Build a library from Haskell source.

Every haskell_library target also defines an optional REPL target that is not built by default, but can be built on request. It works the same way as for haskell_binary.

Examples

haskell_library(
    name = "hello-lib",
    srcs = glob(["src/**/*.hs"]),
    src_strip_prefix = "src",
    deps = [
        "//hello-sublib:lib",
    ],
    reexported_modules = {
        "//hello-sublib:lib": "Lib1 as HelloLib1, Lib2",
    },
)

name

A unique name for this rule.

src_strip_prefix

DEPRECATED. Attribute has no effect.

srcs

Haskell source files.

extra_srcs

Extra (non-Haskell) source files that will be needed at compile time (e.g. by Template Haskell).

deps

List of other Haskell libraries to be linked to this target.

data

See Bazel documentation.,

compiler_flags

Flags to pass to Haskell compiler. Subject to Make variable substitution.

repl_ghci_args

Arbitrary extra arguments to pass to GHCi. This extends compiler_flags and repl_ghci_args from the toolchain. Subject to Make variable substitution.,

runcompile_flags

Arbitrary extra arguments to pass to runghc. This extends compiler_flags and repl_ghci_args from the toolchain. Subject to Make variable substitution.

plugins

Compiler plugins to use during compilation.

tools

Extra tools needed at compile-time, like preprocessors.

worker

Experimental. Worker binary employed by Bazel's persistent worker mode. See use-cases documentation.

hidden_modules

Modules that should be unavailable for import by dependencies.

reexported_modules

A dictionary mapping dependencies to module reexports that should be available for import by dependencies.

exports

A list of other haskell libraries that will be transparently added as a dependency to every downstream rule

linkstatic

Create a static library, not both a static and a shared library.

package_name

Library name used in version macro generation. Only used if the version attribute is defined, see version attribute documentation. Optional, defaults to target name.

version

Library version. Not normally necessary unless to build a library originally defined as a Cabal package. If this is specified, CPP version macro will be generated.

kwargs

Common rule attributes. See Bazel documentation.

haskell_register_toolchains

Register GHC binary distributions for all platforms as toolchains.

kwargs

haskell_test

Build a test suite.

Additionally, it accepts all common bazel test rule fields. This allows you to influence things like timeout and resource allocation for the test.

name

A unique name for this rule.

src_strip_prefix

DEPRECATED. Attribute has no effect.

srcs

Haskell source files.

extra_srcs

Extra (non-Haskell) source files that will be needed at compile time (e.g. by Template Haskell).

deps

List of other Haskell libraries to be linked to this target.

data

See Bazel documentation.,

compiler_flags

Flags to pass to Haskell compiler. Subject to Make variable substitution.

repl_ghci_args

Arbitrary extra arguments to pass to GHCi. This extends compiler_flags and repl_ghci_args from the toolchain. Subject to Make variable substitution.,

runcompile_flags

Arbitrary extra arguments to pass to runghc. This extends compiler_flags and repl_ghci_args from the toolchain. Subject to Make variable substitution.

plugins

Compiler plugins to use during compilation.

tools

Extra tools needed at compile-time, like preprocessors.

worker

Experimental. Worker binary employed by Bazel's persistent worker mode. See use-cases documentation.

linkstatic

Link dependencies statically wherever possible. Some system libraries may still be linked dynamically, as are libraries for which there is no static library. So the resulting executable will still be dynamically linked, hence only mostly static.

main_function

A function with type IO _, either the qualified name of a function from any module or the bare name of a function from a Main module. It is also possible to give the qualified name of any module exposing a main function.

version

Executable version. If this is specified, CPP version macros will be generated for this build.

expected_covered_expressions_percentage

The expected percentage of expressions covered by testing.

expected_uncovered_expression_count

The expected number of expressions which are not covered by testing.

strict_coverage_analysis

Requires that the coverage metric is matched exactly, even doing better than expected is not allowed.

coverage_report_format

The format to output the coverage report in.

Supported values: "text", "html". Default: "text".

Report can be seen in the testlog XML file, or by setting --test_output=all when running bazel coverage.

experimental_coverage_source_patterns

The path patterns specifying which targets to analyze for test coverage metrics.

Wild-card targets such as //... or //:all are allowed. The paths must be relative to the workspace, which means they must start with "//".

Note, this attribute may leave experimental status depending on the outcome of https://github.com/bazelbuild/bazel/issues/7763.

kwargs

Common rule attributes. See Bazel documentation.

haskell_toolchain

Declare a compiler toolchain.

You need at least one of these declared somewhere in your BUILD files for the other rules to work. Once declared, you then need to register the toolchain using register_toolchains in your WORKSPACE file (see example below).

Examples

In a BUILD file:

haskell_toolchain(
    name = "ghc",
    version = "1.2.3",
    static_runtime = static_runtime,
    fully_static_link = fully_static_link,
    tools = ["@sys_ghc//:bin"],
    compiler_flags = ["-Wall"],
)

where @sys_ghc is an external repository defined in the WORKSPACE, e.g. using:

nixpkgs_package(
    name = 'sys_ghc',
    attribute_path = 'haskell.compiler.ghc822',
)

register_toolchains("//:ghc")

name

version

static_runtime

tools

libraries

compiler_flags

repl_ghci_args

haddock_flags

cabalopts

locale_archive

kwargs

Aspects

haskell_doc_aspect

Propagates along attributes named: deps,exports

name

A unique name for this target.

haskell_repl_aspect

Haskell REPL aspect.

Used to implement the haskell_repl rule. Does not generate an executable REPL by itself.

Propagates along attributes named: deps

name

A unique name for this target.