Skip to content

Commit 4494c55

Browse files
authored
Merge pull request #2046 from tweag/start-script-with-bzlmod
Add --with-bzlmod option to start script
2 parents 32f8028 + 61d519c commit 4494c55

File tree

3 files changed

+123
-25
lines changed

3 files changed

+123
-25
lines changed

.github/workflows/workflow.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ jobs:
104104
nix-shell --arg docTools false --argstr ghcVersion ${{ matrix.ghc }} --pure --run '
105105
set -euo pipefail
106106
cd rules_haskell_tests
107-
./tests/run-start-script.sh --use-nix
107+
# XXX run start script `--with-bzlmod=true` when supported
108+
if ! ${{ matrix.bzlmod }}; then
109+
./tests/run-start-script.sh --use-nix --with-bzlmod=${{ matrix.bzlmod }}
110+
fi
108111
bazel build //tests:run-tests
109112
./bazel-ci-bin/tests/run-tests
110113
bazel coverage //...
@@ -130,6 +133,10 @@ jobs:
130133
- ghc: 9.4.6
131134
bzlmod: true
132135
env:
136+
# prevent auto-detection of system compilers on Windows
137+
BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN: ${{ matrix.os == 'windows-latest' && 1 || 0 }}
138+
# do not use Xcode on macOS
139+
BAZEL_USE_CPP_ONLY_TOOLCHAIN: ${{ matrix.os == 'macos-11' && 1 || 0 }}
133140
GHC_VERSION: ${{ matrix.ghc }}
134141
runs-on: ${{ matrix.os }}
135142
steps:
@@ -202,10 +209,7 @@ jobs:
202209
if: matrix.module == 'rules_haskell'
203210
shell: bash
204211
run: |
205-
[[ ${{ runner.os }} == macOS ]] && export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
206212
if [[ ${{ runner.os }} == Windows ]]; then
207-
# prevent auto-detection of system compilers
208-
export BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
209213
# On Windows `//...` expands to `/...`.
210214
bazel test ///...
211215
else
@@ -217,11 +221,8 @@ jobs:
217221
shell: bash
218222
run: |
219223
cd rules_haskell_tests
220-
[[ ${{ runner.os }} == macOS ]] && export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
221-
./tests/run-start-script.sh --use-bindists
224+
./tests/run-start-script.sh --use-bindists --with-bzlmod=${{ matrix.bzlmod }}
222225
if [[ ${{ runner.os }} == Windows ]]; then
223-
# prevent auto-detection of system compilers
224-
export BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
225226
# On Windows `//...` expands to `/...`.
226227
bazel test ///...
227228
else

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ In a fresh directory, run:
3939
$ curl https://haskell.build/start | sh
4040
```
4141

42+
Alternatively, if you want to start a project with bzlmod, run:
43+
44+
```console
45+
$ sh <(curl https://haskell.build/start) --with-bzlmod=true
46+
```
47+
4248
This will generate initial `WORKSPACE` and `BUILD` files for you. See the
4349
[examples](./examples) and the [API reference](#Rules) below to adapt these for
44-
you project. Then,
50+
your project. Then,
4551

4652
```console
4753
$ bazel build //... # Build all targets
@@ -52,6 +58,7 @@ You can learn more about Bazel's command line
5258
syntax [here][bazel-cli]. Common [commands][bazel-cli-commands] are
5359
`build`, `test`, `run` and `coverage`.
5460

61+
5562
### Nixpkgs
5663

5764
This rule set supports using [Nixpkgs][nixpkgs] to provision your GHC

start

Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
4448
PRINT_NIX_USAGE=
49+
BZLMOD=false
4550

4651
parse_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

6275
check_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

213226
stderr "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. :)
217233
workspace(name = "YOUR_PROJECT_NAME_HERE")
218234
@@ -264,9 +280,9 @@ stack_snapshot(
264280
265281
EOF
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
350438
common:ci --color=no
351439
test:ci --test_output=errors
352440
441+
common --enable_bzlmod=$BZLMOD
442+
353443
# Should become the default in bazel 7
354444
build --incompatible_enable_cc_toolchain_resolution
355445

0 commit comments

Comments
 (0)