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

Preliminary bzlmod support #60

Merged
merged 15 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ common:ghc_9_2_7 --repo_env=GHC_VERSION=9.2.7

common:ghc_9_4_5 --repo_env=GHC_VERSION=9.4.5

common:bzlmod --enable_bzlmod

try-import .bazelrc.local
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
fi

cat >.bazelrc.local <<EOF
build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
build --host_platform=@rules_nixpkgs_core//platforms:host
build --bes_results_url=https://app.buildbuddy.io/invocation/
build --bes_backend=grpcs://cloud.buildbuddy.io
build --remote_cache=grpcs://cloud.buildbuddy.io
Expand Down
21 changes: 21 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module(
name = "gazelle_cabal",
version = "0.0.0",
)

bazel_dep(name = "rules_haskell")

stack_snapshot = use_extension(
"@rules_haskell//extensions:stack_snapshot.bzl",
"stack_snapshot",
)
use_repo(stack_snapshot, "stackage")
stack_snapshot.package(name = "directory")
stack_snapshot.package(name = "filepath")
stack_snapshot.package(name = "json")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nicer 👍


non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps")
use_repo(non_module_deps, "io_tweag_gazelle_cabal_deps")

bazel_dep(name = "rules_go", version = "0.41.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "gazelle", version = "0.33.0", repo_name = "bazel_gazelle")
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,13 @@ configuration with default flag values.
## Hacking on gazelle_cabal with nix

If you are running NixOS or if you want to provision `ghc` and other dependencies using `nix`,
you'll need to set the `host_platform` config option to `@io_tweag_rules_nixpkgs//nixpkgs/platforms:host`:
you'll need to set the `host_platform` config option to `@rules_nixpkgs_core//platforms:host`:
```sh
> bazel build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host ...
> bazel build --host_platform=@rules_nixpkgs_core//platforms:host ...
```
For ease of use, we recommend setting this in your `.bazelrc.local` file:
```sh
echo "build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host" >> .bazelrc.local
echo "run --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host" >> .bazelrc.local
echo "build --host_platform=@rules_nixpkgs_core//platforms:host" >> .bazelrc.local
```

## Sponsors
Expand Down
15 changes: 5 additions & 10 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ load("@rules_haskell//haskell:repositories.bzl", "rules_haskell_dependencies")

rules_haskell_dependencies()

load(
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
"nixpkgs_local_repository",
"nixpkgs_python_configure",
)
load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_local_repository")

nixpkgs_local_repository(
name = "nixpkgs",
nix_file = "//:nixpkgs.nix",
)

load("@rules_nixpkgs_python//:python.bzl", "nixpkgs_python_configure")

nixpkgs_python_configure(repository = "@nixpkgs")

load("@rules_haskell//haskell:cabal.bzl", "stack_snapshot")
Expand All @@ -41,7 +39,7 @@ load("//:config_settings/setup.bzl", "config_settings")
config_settings(name = "config_settings")

load("@config_settings//:info.bzl", "ghc_version")
load("@io_tweag_gazelle_cabal//:defs.bzl", "gazelle_cabal_dependencies")
load("//:defs.bzl", "gazelle_cabal_dependencies")

gazelle_cabal_dependencies()

Expand Down Expand Up @@ -115,10 +113,7 @@ http_archive(
],
)

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

nixpkgs_go_configure(repository = "@nixpkgs")

Expand Down
Empty file added WORKSPACE.bzlmod
Empty file.
118 changes: 118 additions & 0 deletions example/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
module(name = "gazelle_cabal_example")

bazel_dep(name = "gazelle_cabal", repo_name = "io_tweag_gazelle_cabal")
local_path_override(module_name = "gazelle_cabal", path = "..")

bazel_dep(name = "rules_haskell")
bazel_dep(name = "rules_haskell_nix")
bazel_dep(name = "rules_go", version = "0.41.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "gazelle", version = "0.33.0", repo_name = "bazel_gazelle")

stack_snapshot = use_extension(
"@rules_haskell//extensions:stack_snapshot.bzl",
"stack_snapshot",
)
use_repo(stack_snapshot, "stackage", "stackage-exe")
# TODO these should be generated by Gazelle
stack_snapshot.package(name = "base")
stack_snapshot.package(name = "inspection-testing")
stack_snapshot.package(name = "mtl")
stack_snapshot.package(name = "tasty")
stack_snapshot.package(
name = "tasty-discover",
components = [
"lib",
"exe:tasty-discover",
],
)
stack_snapshot.package(name = "tasty-hunit")
stack_snapshot.package(name = "text")
stack_snapshot.package(name = "void")

# TODO replace this once a proper public interface for Nix provided Haskell toolchains exists.
nix_haskell_toolchains = use_extension(
"@rules_haskell_nix//extensions:nix_haskell_toolchains.bzl",
"nix_haskell_toolchains",
)

GHC_VERSION = "9.4.5"
nix_haskell_toolchains.new(
attribute_path = "haskell.compiler.ghc" + GHC_VERSION.replace(".", ""),
repository = "@nixpkgs",
version = GHC_VERSION,
)
use_repo(
nix_haskell_toolchains,
"nix_haskell_toolchains_configurations",
# TODO register via rules_nixpkgs_posix once supported.
"all_posix_toolchains",
)

# TODO remove this once a proper public interface for Nix provided Haskell toolchain exists.
declare_nix_toolchains = use_extension(
"@rules_haskell_nix//private:declare_toolchains.bzl",
"declare_nix_toolchains",
)
use_repo(
declare_nix_toolchains,
"all_nix_toolchains",
)

register_toolchains(
"@all_nix_toolchains//:all",
"@all_posix_toolchains//:all",
)

# TODO Remove once rules_haskell is in the BCR.
RULES_HASKELL_REV = "155ba21c7d315cc421ee3f17a1c09b758cc7279d"
RULES_HASKELL_INTEGRITY = "sha384-dIsFufYml0KGSUiC6jhY9ClMv/yEW1kGof7fLahSwfbWd28jLiwMGBGUuoOsQwWf"
archive_override(
module_name = "rules_haskell",
urls = ["https://github.com/tweag/rules_haskell/archive/%s.tar.gz" % RULES_HASKELL_REV],
strip_prefix = "rules_haskell-%s" % RULES_HASKELL_REV,
integrity = RULES_HASKELL_INTEGRITY,
)
archive_override(
module_name = "rules_haskell_nix",
urls = ["https://github.com/tweag/rules_haskell/archive/%s.tar.gz" % RULES_HASKELL_REV],
strip_prefix = "rules_haskell-%s/rules_haskell_nix" % RULES_HASKELL_REV,
integrity = RULES_HASKELL_INTEGRITY,
)

# Needed for custom toolchain definitions in non_module_deps.
# TODO update once a bzlmod API for Nix provided toolchains exists.
bazel_dep(name = "rules_nixpkgs_core")
bazel_dep(name = "rules_nixpkgs_cc")
bazel_dep(name = "rules_nixpkgs_posix")
bazel_dep(name = "platforms", version = "0.0.7")
bazel_dep(name = "rules_cc", version = "0.0.9")

# TODO Remove once rules_nixpkgs is in the BCR.
RULES_NIXPKGS_REV = "7e627d76ba65d6c42586fc265e46bd370672b4eb"
RULES_NIXPKGS_INTEGRITY = "sha384-N2bS5BvSk8QMjhmuxdeeve9PBJUTwFOHufMV4sDgaviwXNdCGc7JaJPqwqZV1TbC"
archive_override(
module_name = "rules_nixpkgs_core",
urls = ["https://github.com/tweag/rules_nixpkgs/archive/%s.tar.gz" % RULES_NIXPKGS_REV],
strip_prefix = "rules_nixpkgs-%s/core" % RULES_NIXPKGS_REV,
integrity = RULES_NIXPKGS_INTEGRITY,
)
archive_override(
module_name = "rules_nixpkgs_cc",
urls = ["https://github.com/tweag/rules_nixpkgs/archive/%s.tar.gz" % RULES_NIXPKGS_REV],
strip_prefix = "rules_nixpkgs-%s/toolchains/cc" % RULES_NIXPKGS_REV,
integrity = RULES_NIXPKGS_INTEGRITY,
)
archive_override(
module_name = "rules_nixpkgs_posix",
urls = ["https://github.com/tweag/rules_nixpkgs/archive/%s.tar.gz" % RULES_NIXPKGS_REV],
strip_prefix = "rules_nixpkgs-%s/toolchains/posix" % RULES_NIXPKGS_REV,
integrity = RULES_NIXPKGS_INTEGRITY,
)

non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps")
use_repo(non_module_deps, "nixpkgs")
use_repo(non_module_deps, "nixpkgs_config_cc")
use_repo(non_module_deps, "nixpkgs_config_cc_info")
use_repo(non_module_deps, "nixpkgs_config_cc_toolchains")
use_repo(non_module_deps, "zlib.dev")
register_toolchains("@nixpkgs_config_cc_toolchains//:all")
3 changes: 2 additions & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ This is an example project using the [gazelle_cabal][gazelle_cabal] extension.

Run the following to generate the build configuration from the Cabal files.
```bazel
echo "build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host" > .bazelrc.local

echo "build --host_platform=@rules_nixpkgs_core//platforms:host" > .bazelrc.local
nix-shell --pure --run "bazel run //:gazelle"
nix-shell --pure --run "bazel run //:gazelle-update-repos"
```
Expand Down
16 changes: 6 additions & 10 deletions example/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ load("@rules_haskell//haskell:repositories.bzl", "rules_haskell_dependencies")

rules_haskell_dependencies()

load(
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
"nixpkgs_local_repository",
"nixpkgs_package",
"nixpkgs_python_configure",
)
load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_local_repository")

nixpkgs_local_repository(
name = "nixpkgs",
nix_file = "//:nixpkgs.nix",
)

load("@rules_nixpkgs_python//:python.bzl", "nixpkgs_python_configure")

nixpkgs_python_configure(repository = "@nixpkgs")

load("@rules_haskell//haskell:cabal.bzl", "stack_snapshot")
Expand Down Expand Up @@ -96,10 +93,7 @@ http_archive(
],
)

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

nixpkgs_go_configure(repository = "@nixpkgs")

Expand Down Expand Up @@ -136,6 +130,8 @@ gazelle_dependencies()
# zlib
####################

load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package")

nixpkgs_package(
name = "nixpkgs_zlib",
attribute_path = "zlib",
Expand Down
Empty file added example/WORKSPACE.bzlmod
Empty file.
57 changes: 57 additions & 0 deletions example/non_module_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
load(
"@rules_nixpkgs_core//:nixpkgs.bzl",
"nixpkgs_local_repository",
"nixpkgs_package",
)
load("@rules_nixpkgs_cc//:cc.bzl", "nixpkgs_cc_configure")

def _non_module_deps_impl(_ctx):
nixpkgs_local_repository(
name = "nixpkgs",
nix_file = "//:nixpkgs.nix",
)

nixpkgs_cc_configure(
name = "nixpkgs_config_cc",
repository = "@nixpkgs",
register = False,
)

nixpkgs_package(
name = "nixpkgs_zlib",
attribute_path = "zlib",
repository = "@nixpkgs",
)

nixpkgs_package(
name = "zlib.dev",
build_file_content = """
load("@rules_cc//cc:defs.bzl", "cc_library")

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

cc_library(
name = "zlib",
srcs = ["@nixpkgs_zlib//:lib"],
hdrs = [":include"],
strip_include_prefix = "include",
visibility = ["//visibility:public"],
# This rule only bundles headers and a library and doesn't compile or link by itself.
# We set linkstatic = 1 to quiet to quiet the following warning:
#
# in linkstatic attribute of cc_library rule @zlib.dev//:zlib:
# setting 'linkstatic=1' is recommended if there are no object files.
#
linkstatic = 1,
)
""",
repository = "@nixpkgs",
)

non_module_deps = module_extension(
implementation = _non_module_deps_impl,
)
2 changes: 1 addition & 1 deletion gazelle_cabal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go_library(
"dependency_resolution.go",
"lang.go",
],
data = ["@io_tweag_gazelle_cabal//cabalscan"],
data = ["//cabalscan"],
importpath = "github.com/tweag/gazelle_cabal/gazelle_cabal",
visibility = ["//visibility:public"],
deps = [
Expand Down
11 changes: 11 additions & 0 deletions non_module_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("//:defs.bzl", "gazelle_cabal_dependencies")

def _non_module_deps_impl(_ctx):
# TODO[AH]: This is only required for backward compatibility to WORKSPACE mode.
# Once WORKSPACE mode support is removed we can directly reference the
# `@stackage//...` labels for the dependencies introduced by this macro.
gazelle_cabal_dependencies()

non_module_deps = module_extension(
implementation = _non_module_deps_impl,
)
2 changes: 1 addition & 1 deletion tests/alternative-deps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ It tests using other repository names than `stackage` from which to get dependen

Run the following to generate the build configuration from the Cabal files.
```bazel
echo "build --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host" > .bazelrc.local
echo "build --host_platform=@rules_nixpkgs_core//platforms:host" > .bazelrc.local
nix-shell --pure --run "bazel run //:gazelle"
nix-shell --pure --run "bazel run //:gazelle-update-repos"
```
Expand Down
Loading