Skip to content

Commit

Permalink
start: add ability to create a nixpkgs-based workspace
Browse files Browse the repository at this point in the history
When we switched the default to bindists, we lost the ability to
generate a nixpkgs-based workspace with the `start` script.

`start` has two options now, `--use-nix` and `--use-bindists`.

Closes: #784
  • Loading branch information
Profpatsch committed Jul 30, 2019
1 parent 39d8963 commit 791d7bc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 13 deletions.
112 changes: 100 additions & 12 deletions start
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@ MAX_BAZEL_MINOR=28

set -e

usage () {
cat >&2 <<"EOF"
start [--use-bindists|--use-nix]
Set up a minimal rules_haskell bazel configuration.
--use-bindists: The project is set up to provision GHC from binary distributions. This does not require nix to build.
--use-nix: The project is set up to provision GHC from nixpkgs. This requires nix to build.
For more information visit https://haskell.build/
EOF
exit "$1"
}

# either bindists or nix
mode=

parse_args () {
[ $# -lt 1 ] && usage 1
[ "$1" = "--help" ] && usage 0
case "$1" in
"--help") usage 0 ;;
"--use-bindists") mode="bindists" ;;
"--use-nix") mode="nix" ;;
*) usage 1 ;;
esac
}

check_files_dont_exist () {
if [ -e WORKSPACE ] || [ -e BUILD ] || [ -e BazelExample.hs ]
then
Expand Down Expand Up @@ -44,10 +72,63 @@ EOF
fi
}

insert_if_equal () {
[ "$1" = "$2" ] && printf '%s' "$3"
}

parse_args "$@"
check_files_dont_exist
check_bazel_version

cat > WORKSPACE <<"EOF"
bindist_toolchain=$(cat <<EOF
# Download a GHC binary distribution from haskell.org and register it as a toolchain.
rules_haskell_toolchains()
EOF
)

nix_toolchain=$(cat <<EOF
# Load nixpkgs_git_repository from rules_nixpkgs,
# which was already initialized by rules_haskell_dependencies above.
load(
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
"nixpkgs_git_repository",
)
# Fetch a version of nixpkgs from GitHub.
# For more information see the documentation of rules_nixpkgs at
# https://github.com/tweag/rules_nixpkgs/blob/master/README.md
nixpkgs_git_repository(
name = "nixpkgs",
# TODO: fix this to a git commit hash and insert sha256
revision = "19.03",
# sha256 = …
)
load(
"@rules_haskell//haskell:nixpkgs.bzl",
"haskell_register_ghc_nixpkgs",
)
# Fetch a GHC binary distribution from nixpkgs and register it as a toolchain.
# For more information:
# https://api.haskell.build/haskell/nixpkgs.html#haskell_register_ghc_nixpkgs
haskell_register_ghc_nixpkgs(
repository = "@nixpkgs",
attribute_path = "ghc",
version = "8.6.4",
)
EOF
)

get_toolchain () {
case $mode in
bindists) printf '%s' "$bindist_toolchain" ;;
nix) printf '%s' "$nix_toolchain" ;;
esac
}

echo "Creating WORKSPACE" >&2
cat > WORKSPACE <<EOF
# Give your project a name. :)
workspace(name = "YOUR_PROJECT_NAME_HERE")
Expand All @@ -57,8 +138,7 @@ load(
"http_archive"
)
# Download `rules_haskell`.
# and make it accessible `@rules_haskell`.
# Download rules_haskell and make it accessible as "@rules_haskell".
http_archive(
name = "rules_haskell",
strip_prefix = "rules_haskell-0.9.1",
Expand All @@ -72,21 +152,27 @@ load(
"rules_haskell_toolchains",
)
# Setup all Bazel dependencies required by `rules_haskell`.
# Setup all Bazel dependencies required by rules_haskell.
rules_haskell_dependencies()
# Download a GHC binary distribution from haskell.org
# and register it as a toolchain.
rules_haskell_toolchains()
$(get_toolchain)
EOF

cat > .bazelrc <<"EOF"
echo "Creating .bazelrc" >&2
cat > .bazelrc <<EOF
build:ci --loading_phase_threads=1
build:ci --jobs=2
build:ci --verbose_failures
common:ci --color=no
test:ci --test_output=errors
$(insert_if_equal $mode "nix" '
# This project uses a GHC provisioned via nix.
# We need to use the rules_haskell nix toolchain accordingly:
build --host_platform=@rules_haskell//haskell/platforms:linux_x86_64_nixpkgs
run --host_platform=@rules_haskell//haskell/platforms:linux_x86_64_nixpkgs'
)
build --incompatible_use_python_toolchains=false
# test environment does not propagate locales by default
Expand All @@ -98,24 +184,25 @@ test --test_env=LANG=en_US.utf8 --test_env=LOCALE_ARCHIVE
try-import .bazelrc.local
EOF

echo "Creating BUILD.bazel" >&2
cat > BUILD.bazel <<"EOF"
# Set all target’s visibility in this package to "public".
package(default_visibility = ["//visibility:public"])
# Load `rules_haskell` rules.
# Load rules_haskell rules.
load(
"@rules_haskell//haskell:defs.bzl",
"haskell_toolchain_library",
"haskell_library",
"haskell_binary",
)
# `haskell_toolchain_library` can access builtin GHC packages
# haskell_toolchain_library can access builtin GHC packages
# and assign them a bazel target name, so that they
# can be referenced as dependencies.
haskell_toolchain_library(name = "base")
# You can add your own libraries with `haskell_library`.
# You can add your own libraries with haskell_library.
# haskell_library(
# name = "MY_LIBRARY_NAME",
# src_strip_prefix = "src",
Expand All @@ -134,6 +221,7 @@ haskell_binary(
)
EOF

echo "Creating Example.hs" >&2
cat > Example.hs <<"EOF"
module Main where
Expand All @@ -142,7 +230,7 @@ import Prelude (putStrLn)
main = putStrLn "Hello from rules_haskell!"
EOF

cat <<"EOF"
cat >&2 <<"EOF"
WORKSPACE and initial BUILD files created. To run Bazel and build the example:
$ bazel run //:example
Expand Down
2 changes: 1 addition & 1 deletion tests/run-start-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set -e

pwd=$(pwd)
cd $(mktemp -d)
$pwd/start
$pwd/start --use-bindists

# Copy the bazel configuration, this is only useful for CI
mkdir tools
Expand Down

0 comments on commit 791d7bc

Please sign in to comment.