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

Migrate to bzlmod #107

Merged
merged 7 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .bazel-nix.rc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ run --host_platform=@rules_nixpkgs_core//platforms:host

# disable auto config, use cc toolchain from nixpkgs
build --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1

common --registry=https://bcr.bazel.build

4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
common --enable_bzlmod

common --registry=file:///%workspace%/registry

build:ci --loading_phase_threads=1
build:ci --jobs=2
build:ci --verbose_failures
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ repeat.cabal
*~
/bazel-*
/.pre-commit-config.yaml
/.direnv/
87 changes: 87 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module(
name = "replay",
version = "0.2",
)

bazel_dep(name = "rules_nixpkgs_core", version = "0.11.1")
bazel_dep(
name = "rules_haskell_nix",
version = "0.19",
)
bazel_dep(
name = "rules_haskell",
version = "0.19",
)
bazel_dep(
name = "rules_nixpkgs_cc",
version = "0.11.1",
)
bazel_dep(
name = "rules_nixpkgs_python",
version = "0.11.1",
)
bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "protobuf", version = "21.7")
bazel_dep(name = "rules_proto", version = "5.3.0-21.7") #, repo_name = "com_google_protobuf")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "rules_sh", version = "0.4.0")
bazel_dep(name = "rules_python", version = "0.25.0")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_nixpkgs_go", version = "0.11.1")

GHC_OPTS = [
"-Wall",
"-Wcompat",
"-Widentities",
"-Wincomplete-record-updates",
"-Wincomplete-uni-patterns",
"-Wpartial-fields",
"-Wredundant-constraints",
"-Werror",
"-XNamedFieldPuns",
]

GHC_VERSION = "9.4.8"

nix_repo = use_extension("@rules_nixpkgs_core//extensions:repository.bzl", "nix_repo")
nix_repo.file(
name = "nixpkgs",
file = "//:nixpkgs.nix",
file_deps = [
"//:flake.lock",
]
)
use_repo(nix_repo, "nixpkgs")

nix_haskell_toolchains = use_extension(
"@rules_haskell_nix//extensions:nix_haskell_toolchains.bzl",
"nix_haskell_toolchains",
)
nix_haskell_toolchains.new(
attribute_path = "",
ghcopts = GHC_OPTS,
nix_file = "//nix:ghc.nix",
nix_file_deps = [
"//:nixpkgs.nix",
"//:flake.lock",
],
nixopts = [
"--argstr",
"ghcVersion",
GHC_VERSION,
],
repository = "@nixpkgs",
version = GHC_VERSION,
)

sh_configure = use_extension("@rules_sh//bzlmod:extensions.bzl", "sh_configure")

use_repo(sh_configure, "local_posix_config")

stack = use_extension("@rules_haskell//extensions:stack_snapshot.bzl", "stack_snapshot")

stack.snapshot(name = "lts-18.28")

stack.package(name = "json")

use_repo(stack, "stackage")
139 changes: 6 additions & 133 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,115 +2,14 @@
workspace(name = "replay")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@rules_nixpkgs_cc//:cc.bzl", "nixpkgs_cc_configure")

http_archive(
name = "bazel_skylib",
sha256 = "bc283cdfcd526a52c3201279cda4bc298652efa898b10b4db0837dc51652756f",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

http_archive(
name = "io_tweag_rules_nixpkgs",
sha256 = "2a555348d7f8593fca2bf3fc6ce53c5d62929de81b6c292e23f16c557c0ae45a",
strip_prefix = "rules_nixpkgs-0.11.1",
urls = ["https://github.com/tweag/rules_nixpkgs/releases/download/v0.11.1/rules_nixpkgs-0.11.1.tar.gz"],
)

# Download rules_haskell and make it accessible as "@rules_haskell".
http_archive(
name = "rules_haskell",
sha256 = "34742848a8882d94a0437b3b1917dea6f58c82fe5762afe8d249d3a36e51935d",
strip_prefix = "rules_haskell-0.19",
url = "https://github.com/tweag/rules_haskell/releases/download/v0.19/rules_haskell-0.19.tar.gz",
)

http_archive(
name = "rules_sh",
sha256 = "23a4571ec8afc83db1eae99c0efd0b8b4fa1644069f0340a10f8859e086ba02c",
strip_prefix = "rules_sh-0.4.0",
urls = ["https://github.com/tweag/rules_sh/archive/v0.4.0.tar.gz"],
)

load("@rules_sh//sh:repositories.bzl", "rules_sh_dependencies")

rules_sh_dependencies()

load("@rules_sh//sh:posix.bzl", "sh_posix_configure")

# Bring in a "local" toolchain - (only hermetic if in a pure nix-shell!)
sh_posix_configure()

# Add rules_nixpkgs toolchains
load(
"@io_tweag_rules_nixpkgs//nixpkgs:repositories.bzl",
"rules_nixpkgs_dependencies",
)

rules_nixpkgs_dependencies(
toolchains = [
"cc",
"go",
"python",
"posix",
],
)

load(
"@rules_haskell//haskell:repositories.bzl",
"rules_haskell_dependencies",
)

# Setup all Bazel dependencies required by rules_haskell.
rules_haskell_dependencies()

# Load nixpkgs_git_repository from rules_nixpkgs,
# which was already initialized by rules_haskell_dependencies above.
load(
"@rules_nixpkgs_cc//:cc.bzl",
"nixpkgs_cc_configure",
)
load(
"@rules_nixpkgs_core//:nixpkgs.bzl",
"nixpkgs_local_repository",
)
load(
"@rules_nixpkgs_python//:python.bzl",
"nixpkgs_python_configure",
)

# TODO Use module extension once available.
nixpkgs_cc_configure(
name = "nixpkgs_cc",
repository = "@nixpkgs",
)

nixpkgs_python_configure(
repository = "@nixpkgs",
)

nixpkgs_local_repository(
name = "nixpkgs",
nix_file = "//:nixpkgs.nix",
nix_file_deps = [
"//:flake.lock",
],
)

load(
"@rules_haskell//haskell:nixpkgs.bzl",
"haskell_register_ghc_nixpkgs",
)
load(
"//:ghc.bzl",
"GHC_VERSION",
)

GHC_OPTS = [
"-Wall",
"-Wcompat",
Expand All @@ -123,26 +22,10 @@ GHC_OPTS = [
"-XNamedFieldPuns",
]

# 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(
attribute_path = None,
ghcopts = GHC_OPTS,
nix_file = "//nix:ghc.nix",
nix_file_deps = [
"//:nixpkgs.nix",
"//:flake.lock",
],
nixopts = [
"--argstr",
"ghcVersion",
GHC_VERSION,
],
repository = "@nixpkgs",
version = GHC_VERSION,
load(
"//:ghc.bzl",
"GHC_VERSION",
)

load("//:ghc_inside_nix.bzl", "register_ghc_in_nix")

register_ghc_in_nix(
Expand Down Expand Up @@ -201,6 +84,7 @@ load(

nixpkgs_go_configure(
repository = "@nixpkgs",
rules_go_repo_name = "io_bazel_rules_go",
)

gazelle_dependencies(go_repository_default_config = "//:WORKSPACE.bazel")
Expand All @@ -217,14 +101,3 @@ load("@io_tweag_gazelle_haskell_modules//:defs.bzl", "gazelle_haskell_modules_de
gazelle_haskell_modules_dependencies(
# json = "@someother//:some-other-json",
)

load("@rules_haskell//haskell:cabal.bzl", "stack_snapshot")

stack_snapshot(
name = "stackage",
packages = [
"json",
],
# Most snapshots of your choice might do
snapshot = "lts-18.28",
)
17 changes: 14 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
];
devTools = let inherit (pkgs) bazel-watcher buildifier haskell-language-server lib; in
[ bazel buildifier ghcWithHoogle haskell-language-server ] ++ lib.optional (!bazel-watcher.meta.broken) bazel-watcher;

registry = pkgs.fetchFromGitHub {
owner = "bazelbuild";
repo = "bazel-central-registry";
rev = "1c729c2775715fd98f0f948a512eb173213250da";
hash = "sha256-1iaDDM8/v8KCOUjPgLUtZVta7rMzwlIK//cCoLUrb/s=";
};
in
rec {
packages.replay = pkgs.buildBazelPackage {
Expand All @@ -60,7 +67,11 @@

removeRulesCC = false;

bazelFlags = [ "--extra_toolchains=@rules_haskell_nix_ghc_in_nix_toolchain//:toolchain" ];
bazelFlags = [
"--extra_toolchains=@rules_haskell_nix_ghc_in_nix_toolchain//:toolchain"
"--registry"
"file://${registry}"
];

bazelBuildFlags = [
"--compilation_mode=opt" # optimize
Expand All @@ -78,12 +89,12 @@
nativeBuildInputs = nativeBuildInputs ++ [ ghc ];

fetchAttrs = {
sha256 = "sha256-TfkXA+QRx2/EMzP/8LgIu9G2MUGwWkvX2nHhRyqaYhY=";
sha256 = "sha256-WFjCjZ/9PaYaF1c9xyb/t0JD0J20ooOWghrKrIc/6vQ=";
};

buildAttrs = {
preBuild = ''
patchShebangs $bazelOut/external/rules_haskell/haskell/private/ghc_wrapper.sh
patchShebangs $bazelOut/external/rules_haskell~0.19/haskell/private/ghc_wrapper.sh
'';
installPhase = ''
install -D -t $out/bin bazel-bin/replay
Expand Down
4 changes: 4 additions & 0 deletions registry/bazel_registry.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mirrors": [],
"module_base_path": ".."
}
69 changes: 69 additions & 0 deletions registry/modules/rules_haskell_nix/0.19/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module(
name = "rules_haskell_nix",
version = "0.19",
)

bazel_dep(
name = "rules_haskell",
version = "0.19",
)
bazel_dep(
name = "rules_sh",
version = "0.3.0",
)
bazel_dep(
name = "rules_nixpkgs_core",
version = "0.11.1",
)
bazel_dep(
name = "rules_nixpkgs_posix",
version = "0.11.1",
)
bazel_dep(
name = "platforms",
version = "0.0.8",
)
bazel_dep(name = "bazel_skylib", version = "1.5.0")

bazel_dep(name = "rules_go", version = "0.41.0", dev_dependency = True, repo_name = "io_bazel_rules_go")

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

nix_haskell_toolchains = use_extension(
"//extensions:nix_haskell_toolchains.bzl",
"nix_haskell_toolchains",
)

# Declare a default nix-based toolchain
nix_haskell_toolchains.new(
attribute_path = "",
nix_file_content = """with import <nixpkgs> {}; haskell.packages.ghc946.ghc""",
repository = "@nixpkgs_default",
version = "9.4.6",
)
use_repo(
nix_haskell_toolchains,
"nix_haskell_toolchains_configurations",
# Once toochains can be registered directly via the rules_nixpkgs_posix module,
# This repository could be removed from the nix_haskell_toolchains extension.
"all_posix_toolchains",
)

declare_nix_toolchains = use_extension(
"//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",
)
Loading