diff --git a/.bazelrc b/.bazelrc index d0f79ad..d4012ac 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 diff --git a/.github/workflows/workflow.yaml b/.github/workflows/workflow.yaml index 9441d4e..fcd3aa1 100644 --- a/.github/workflows/workflow.yaml +++ b/.github/workflows/workflow.yaml @@ -17,6 +17,11 @@ jobs: matrix: os: [ubuntu-latest] ghc-version: [ghc_8_10_7, ghc_9_0_2, ghc_9_2_7, ghc_9_4_5] + enable-bzlmod: [false] + include: + - os: ubuntu-latest + ghc-version: ghc_9_4_5 + enable-bzlmod: true runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 @@ -38,8 +43,13 @@ jobs: cache_setting="--remote_header=x-buildbuddy-api-key=$BUILDBUDDY_API_KEY" fi + bzlmod_setting= + if [[ ${{ matrix.enable-bzlmod }} = true ]]; then + bzlmod_setting="common --config=bzlmod" + fi + cat >.bazelrc.local < 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 diff --git a/WORKSPACE b/WORKSPACE index 980b832..fea4c94 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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") @@ -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() @@ -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") diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod new file mode 100644 index 0000000..e69de29 diff --git a/example/MODULE.bazel b/example/MODULE.bazel new file mode 100644 index 0000000..4cf7b96 --- /dev/null +++ b/example/MODULE.bazel @@ -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") diff --git a/example/README.md b/example/README.md index 5ea2537..4aeefd6 100644 --- a/example/README.md +++ b/example/README.md @@ -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" ``` diff --git a/example/WORKSPACE b/example/WORKSPACE index 0c53a56..a189110 100644 --- a/example/WORKSPACE +++ b/example/WORKSPACE @@ -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") @@ -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") @@ -136,6 +130,8 @@ gazelle_dependencies() # zlib #################### +load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package") + nixpkgs_package( name = "nixpkgs_zlib", attribute_path = "zlib", diff --git a/example/WORKSPACE.bzlmod b/example/WORKSPACE.bzlmod new file mode 100644 index 0000000..e69de29 diff --git a/example/non_module_deps.bzl b/example/non_module_deps.bzl new file mode 100644 index 0000000..3a5efd0 --- /dev/null +++ b/example/non_module_deps.bzl @@ -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, +) diff --git a/gazelle_cabal/BUILD.bazel b/gazelle_cabal/BUILD.bazel index 5a17a4a..d468417 100644 --- a/gazelle_cabal/BUILD.bazel +++ b/gazelle_cabal/BUILD.bazel @@ -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 = [ diff --git a/non_module_deps.bzl b/non_module_deps.bzl new file mode 100644 index 0000000..0a248f2 --- /dev/null +++ b/non_module_deps.bzl @@ -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, +) diff --git a/tests/alternative-deps/README.md b/tests/alternative-deps/README.md index 5e8cfe1..454af5d 100644 --- a/tests/alternative-deps/README.md +++ b/tests/alternative-deps/README.md @@ -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" ``` diff --git a/tests/alternative-deps/WORKSPACE b/tests/alternative-deps/WORKSPACE index ee8c157..72ad84e 100644 --- a/tests/alternative-deps/WORKSPACE +++ b/tests/alternative-deps/WORKSPACE @@ -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") @@ -107,10 +104,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") @@ -147,6 +141,8 @@ gazelle_dependencies() # zlib #################### +load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package") + nixpkgs_package( name = "nixpkgs_zlib", attribute_path = "zlib",