@@ -23,13 +23,17 @@ usage () {
2323 exit_code=" $1 "
2424
2525 cat >&2 << -"EOF "
26- start [--use-bindists|--use-nix|--help]
26+ start [--use-bindists|--use-nix|--help|--with-bzlmod=true|false ]
2727
2828 Set up a minimal rules_haskell bazel configuration.
2929
3030 --use-bindists: The project is set up to provision GHC from binary distributions. This does not require nix to build.
3131 --use-nix: The project is set up to provision GHC from nixpkgs. This requires nix to build.
3232
33+ --with-bzlmod=true|false:
34+ If enabled, use a MODULE.bazel file and enable bzlmod to fetch rules_haskell.
35+ (only supported in bindist mode)
36+
3337 If no argument is given, `--use-bindists` is assumed
3438 and a helpful message is printed that `--use-nix` also exists.
3539
@@ -40,23 +44,32 @@ usage () {
4044}
4145
4246# either bindists or nix
43- MODE=
47+ MODE=" bindists "
4448PRINT_NIX_USAGE=
49+ BZLMOD=false
4550
4651parse_args () {
4752 # Defaults, if no arguments provided
4853 if [ " $# " -eq 0 ]; then
49- MODE=" bindists"
5054 PRINT_NIX_USAGE=1
5155 return
5256 fi
5357
54- case " $1 " in
55- " --help" ) usage 0 ;;
56- " --use-bindists" ) MODE=" bindists" ;;
57- " --use-nix" ) MODE=" nix" ;;
58- * ) usage 1 ;;
59- esac
58+ for arg; do
59+ case " $arg " in
60+ " --help" ) usage 0 ;;
61+ " --use-bindists" ) MODE=" bindists" ;;
62+ " --use-nix" ) MODE=" nix" ;;
63+ " --with-bzlmod=true" ) BZLMOD=true ;;
64+ " --with-bzlmod=false" ) BZLMOD=false ;;
65+ * ) usage 1 ;;
66+ esac
67+ done
68+
69+ if $BZLMOD && [ $MODE != bindists ]; then
70+ stderr " error: --with-bzlmod is only supported with --use-bindists"
71+ exit 1
72+ fi
6073}
6174
6275check_dir () {
@@ -99,7 +112,7 @@ check_files_dont_exist () {
99112 check_alt WORKSPACE.bazel WORKSPACE
100113 check_alt BUILD BUILD.bazel
101114
102- for clash in .bazelrc WORKSPACE BUILD.bazel zlib.BUILD.bazel Example.hs; do
115+ for clash in .bazelrc WORKSPACE BUILD.bazel MODULE.bazel zlib.BUILD.bazel Example.hs non_module_deps.bzl ; do
103116 check_clash " ${clash} "
104117 done
105118}
@@ -212,7 +225,10 @@ esac
212225
213226stderr " Creating WORKSPACE"
214227
215- cat > WORKSPACE << EOF
228+ if $BZLMOD ; then
229+ touch WORKSPACE
230+ else
231+ cat > WORKSPACE << EOF
216232# Give your project a name. :)
217233workspace(name = "YOUR_PROJECT_NAME_HERE")
218234
@@ -264,9 +280,9 @@ stack_snapshot(
264280
265281EOF
266282
267- # Append toolchain and zlib rules
268- case " ${MODE} " in
269- " bindists" ) cat << -EOF
283+ # Append toolchain and zlib rules
284+ case " ${MODE} " in
285+ " bindists" ) cat << -EOF
270286 # Download a GHC binary distribution from haskell.org and register it as a toolchain.
271287 rules_haskell_toolchains(
272288 version = "${GHC_VERSION} ",
@@ -282,7 +298,7 @@ case "${MODE}" in
282298 EOF
283299 ;;
284300
285- " nix" ) cat << -EOF
301+ " nix" ) cat << -EOF
286302 # Load nixpkgs_git_repository from rules_nixpkgs,
287303 # which was already initialized by rules_haskell_dependencies above.
288304 load(
@@ -337,7 +353,79 @@ case "${MODE}" in
337353 )
338354 EOF
339355 ;;
340- esac >> WORKSPACE
356+ esac >> WORKSPACE
357+ fi
358+
359+ if $BZLMOD ; then
360+ stderr " Creating MODULE.bazel"
361+
362+ cat > MODULE.bazel << EOF
363+ module(name = "your_project_name_here", version = "0.1")
364+
365+ bazel_dep(name = "rules_haskell", version = "0.17")
366+ bazel_dep(name = "rules_cc", version = "0.0.9")
367+
368+ haskell_toolchains = use_extension(
369+ "@rules_haskell//extensions:haskell_toolchains.bzl",
370+ "haskell_toolchains",
371+ )
372+
373+ haskell_toolchains.bindists(version = "$GHC_VERSION ")
374+
375+ non_module_deps = use_extension(
376+ "//:non_module_deps.bzl",
377+ "non_module_deps",
378+ )
379+
380+ use_repo(
381+ non_module_deps,
382+ "zlib.dev",
383+ )
384+
385+ stack = use_extension(
386+ "@rules_haskell//extensions:stack_snapshot.bzl",
387+ "stack_snapshot",
388+ )
389+
390+ use_repo(
391+ stack,
392+ "stackage",
393+ "stackage-exe",
394+ "stackage-unpinned",
395+ )
396+
397+ stack.package(
398+ name = "zlib",
399+ extra_deps = ["@zlib.dev//:zlib"],
400+ )
401+
402+ # LTS snapshot published for ghc-${GHC_VERSION} (default version used by rules_haskell)
403+ stack.snapshot(name = "$SNAPSHOT ")
404+
405+ # This uses an unpinned version of stack_snapshot, meaning that stack is invoked on every build.
406+ # To switch to pinned stackage dependencies, run \` bazel run @stackage-unpinned//:pin\` and
407+ # uncomment the following line.
408+ #stack.stack_snapshot_json(label = "//:stackage_snapshot.json")
409+
410+ EOF
411+
412+ stderr " Creating non_module_deps.bzl"
413+
414+ cat > non_module_deps.bzl << -EOF
415+ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
416+
417+ def _non_module_deps_impl(_mctx):
418+ http_archive(
419+ name = "zlib.dev",
420+ build_file = "//:${ZLIB_BUILD_FILE} ",
421+ sha256 = "b5b06d60ce49c8ba700e0ba517fa07de80b5d4628a037f4be8ad16955be7a7c0",
422+ strip_prefix = "zlib-1.3",
423+ urls = ["https://github.com/madler/zlib/archive/v1.3.tar.gz"],
424+ )
425+
426+ non_module_deps = module_extension(implementation = _non_module_deps_impl)
427+ EOF
428+ fi
341429
342430# # Write .bazelrc File #################################################
343431
@@ -350,6 +438,8 @@ build:ci --verbose_failures
350438common:ci --color=no
351439test:ci --test_output=errors
352440
441+ common --enable_bzlmod=$BZLMOD
442+
353443# Should become the default in bazel 7
354444build --incompatible_enable_cc_toolchain_resolution
355445
0 commit comments