Skip to content
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
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples
44 changes: 41 additions & 3 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-10.15]
os: [ubuntu-latest, macos-10.15] # TODO: this job does not yet work on more recent macOS (macos-11)
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v14.1
- uses: actions/checkout@v2.4.0
- uses: cachix/install-nix-action@v16
with:
nix_path: nixpkgs=./nixpkgs.nix
- name: Configure
Expand All @@ -27,3 +27,41 @@ jobs:
set -euo pipefail
bazel test //...
'
test-examples:
name: Build & Test - Examples
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest] # TODO: this job does not yet work on any version of macOS
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2.4.0
- uses: cachix/install-nix-action@v16
with:
nix_path: nixpkgs=./nixpkgs.nix
- name: Configure
env:
BUILDBUDDY_API_KEY: ${{ secrets.BUILDBUDDY_API_KEY }}
run: |
cat >.bazelrc.local <<EOF
common --config=ci
build --remote_header=x-buildbuddy-api-key="$BUILDBUDDY_API_KEY"
EOF
- name: Build & test
run: |
cd examples/toolchains
for dir in *; do
cd "$dir"
echo
echo Running $(head -n1 README.md) with Nix
nix-shell --command 'bazel run --config=nix :hello'
# TODO: all toolchains should run without Nixpkgs
cd ..
done
for dir in cc go; do
cd "$dir"
echo
echo Running $(head -n1 README.md) without Nix
bazel run :hello
cd ..
done
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
1 change: 1 addition & 0 deletions examples/toolchains/cc/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build:nix --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
6 changes: 6 additions & 0 deletions examples/toolchains/cc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "hello",
srcs = ["hello.cc"],
)
19 changes: 19 additions & 0 deletions examples/toolchains/cc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
C++ Toolchain Example
=====================

This is an example C++ project that uses `rules_cc`.

If the Nix package manager is present in the build environment, this example will use Nix to provide the C++ toolchain. Otherwise, it will use the toolchain provided by `rules_cc` and not rely on Nix at all.

# Usage

To run the example with Nix, issue the following command:
```
nix-shell --command 'bazel run --config=nix :hello'
```

To run the example without Nix, make sure you have Bazel installed, and issue the following command:
```
bazel run :hello
```
This non-Nix example will look for the C++ toolchain on the PATH.
43 changes: 43 additions & 0 deletions examples/toolchains/cc/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Replace with http_archive: https://github.com/tweag/rules_nixpkgs/#setup
local_repository(
name = "io_tweag_rules_nixpkgs",
path = "../../../",
)

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

http_archive(
name = "rules_cc",
sha256 = "4dccbfd22c0def164c8f47458bd50e0c7148f3d92002cdb459c2a96a68498241",
urls = [
"https://github.com/bazelbuild/rules_cc/releases/download/0.0.1/rules_cc-0.0.1.tar.gz",
],
)

load("@io_tweag_rules_nixpkgs//nixpkgs:repositories.bzl", "rules_nixpkgs_dependencies")

rules_nixpkgs_dependencies()

load(
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
"nixpkgs_cc_configure",
"nixpkgs_git_repository",
)

nixpkgs_git_repository(
name = "nixpkgs",
revision = "21.11",
Comment thread
itsadrake marked this conversation as resolved.
sha256 = "c77bb41cf5dd82f4718fa789d49363f512bb6fa6bc25f8d60902fe2d698ed7cc",
)

nixpkgs_cc_configure(
name = "nixpkgs_config_cc",
fail_not_supported = False,
repository = "@nixpkgs",
)

load("@rules_cc//cc:repositories.bzl", "rules_cc_dependencies", "rules_cc_toolchains")

rules_cc_dependencies()

rules_cc_toolchains()
6 changes: 6 additions & 0 deletions examples/toolchains/cc/hello.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>

int main()
{
std::cout << "Hello world!\n";
Comment thread
itsadrake marked this conversation as resolved.
}
6 changes: 6 additions & 0 deletions examples/toolchains/cc/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ pkgs ? import (builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/21.11.tar.gz";
sha256 = "162dywda2dvfj1248afxc45kcrg83appjd0nmdb541hl7rnncf02";
}) { } }:

pkgs.mkShell { nativeBuildInputs = [ pkgs.bazel_4 ]; }
1 change: 1 addition & 0 deletions examples/toolchains/cc_with_deps/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build:nix --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
10 changes: 10 additions & 0 deletions examples/toolchains/cc_with_deps/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "hello",
srcs = ["hello.cc"],
deps = [
"@boost.dev//:boost",
"@zlib.dev//:zlib",
],
)
13 changes: 13 additions & 0 deletions examples/toolchains/cc_with_deps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
C++ With Dependencies Toolchain Example
=======================================

This is an example C++ project with dependencies that uses `rules_cc`.

This example uses the Nix package manager to provide C++ dependencies, and as such only works with Nix installed. Demonstrating other methods of providing C++ dependencies is out of scope of this example.

# Usage

To run the example with Nix, issue the following command:
```
nix-shell --command 'bazel run --config=nix :hello'
```
81 changes: 81 additions & 0 deletions examples/toolchains/cc_with_deps/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Replace with http_archive: https://github.com/tweag/rules_nixpkgs/#setup
local_repository(
name = "io_tweag_rules_nixpkgs",
path = "../../../",
)

load("@io_tweag_rules_nixpkgs//nixpkgs:repositories.bzl", "rules_nixpkgs_dependencies")

rules_nixpkgs_dependencies()

load(
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
"nixpkgs_cc_configure",
"nixpkgs_git_repository",
)

nixpkgs_git_repository(
name = "nixpkgs",
revision = "21.11",
sha256 = "c77bb41cf5dd82f4718fa789d49363f512bb6fa6bc25f8d60902fe2d698ed7cc",
)

nixpkgs_cc_configure(
name = "nixpkgs_config_cc",
fail_not_supported = False,
repository = "@nixpkgs",
)

load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_package")

nixpkgs_package(
name = "boost",
attribute_path = "boost175",
repository = "@nixpkgs",
)

nixpkgs_package(
name = "boost.dev",
attribute_path = "boost175.dev",
build_file_content = """\
load("@rules_cc//cc:defs.bzl", "cc_library")
filegroup(
name = "include",
srcs = glob(["include/**/*.h", "include/**/*.hpp"]),
visibility = ["//visibility:public"],
)
cc_library(
name = "boost",
srcs = ["@boost//:lib"],
hdrs = [":include"],
strip_include_prefix = "include",
visibility = ["//visibility:public"],
)
""",
repository = "@nixpkgs",
)

nixpkgs_package(
name = "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 = ["@zlib//:lib"],
hdrs = [":include"],
strip_include_prefix = "include",
visibility = ["//visibility:public"],
)
""",
repository = "@nixpkgs",
)
8 changes: 8 additions & 0 deletions examples/toolchains/cc_with_deps/hello.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>
#include <boost/filesystem/operations.hpp>
#include <zlib.h>

int main()
{
std::cout << "Hello world!\n";
}
6 changes: 6 additions & 0 deletions examples/toolchains/cc_with_deps/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ pkgs ? import (builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/21.11.tar.gz";
sha256 = "162dywda2dvfj1248afxc45kcrg83appjd0nmdb541hl7rnncf02";
}) { } }:

pkgs.mkShell { nativeBuildInputs = [ pkgs.bazel_4 ]; }
1 change: 1 addition & 0 deletions examples/toolchains/go/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build:nix --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
6 changes: 6 additions & 0 deletions examples/toolchains/go/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary")

go_binary(
name = "hello",
srcs = ["hello.go"],
)
18 changes: 18 additions & 0 deletions examples/toolchains/go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Go Toolchain Example
====================

This is an example Go project that uses `rules_go`.

If the Nix package manager is present in the build environment, this example will use Nix to provide the Go toolchain. Otherwise, it will use the toolchain provided by `rules_go` and not rely on Nix at all.

# Usage

To run the example with Nix, issue the following command:
```
nix-shell --command 'bazel run --config=nix :hello'
```

To run the example without Nix, make sure you have Bazel installed, and issue the following command:
```
bazel run :hello
Comment thread
itsadrake marked this conversation as resolved.
```
46 changes: 46 additions & 0 deletions examples/toolchains/go/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Replace with http_archive: https://github.com/tweag/rules_nixpkgs/#setup
local_repository(
name = "io_tweag_rules_nixpkgs",
path = "../../../",
)

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

http_archive(
name = "io_bazel_rules_go",
sha256 = "2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies")

go_rules_dependencies()

load("@io_tweag_rules_nixpkgs//nixpkgs:repositories.bzl", "rules_nixpkgs_dependencies")

rules_nixpkgs_dependencies()

load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_git_repository")

nixpkgs_git_repository(
name = "nixpkgs",
revision = "21.11",
sha256 = "c77bb41cf5dd82f4718fa789d49363f512bb6fa6bc25f8d60902fe2d698ed7cc",
)

load("@io_tweag_rules_nixpkgs//nixpkgs:toolchains/go.bzl", "nixpkgs_go_configure")

nixpkgs_go_configure(
fail_not_supported = False,
repository = "@nixpkgs",
sdk_name = "nix_go",
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.17.1")
7 changes: 7 additions & 0 deletions examples/toolchains/go/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("Hello world!")
}
6 changes: 6 additions & 0 deletions examples/toolchains/go/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ pkgs ? import (builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/21.11.tar.gz";
sha256 = "162dywda2dvfj1248afxc45kcrg83appjd0nmdb541hl7rnncf02";
}) { } }:

pkgs.mkShell { nativeBuildInputs = [ pkgs.bazel_4 ]; }
1 change: 1 addition & 0 deletions examples/toolchains/python/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build:nix --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
5 changes: 5 additions & 0 deletions examples/toolchains/python/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
py_binary(
name = "hello",
srcs = ["hello.py"],
main = "hello.py",
)
15 changes: 15 additions & 0 deletions examples/toolchains/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Python Toolchain Example
========================

This is an example Python project with modules that uses `rules_python`.

This example uses the Nix package manager to provide Python packages, and as such only works with Nix installed. Demonstrating other methods of providing Python packages is out of scope of this example.

# Usage

To run the example with Nix, issue the following command:
```
nix-shell --command 'bazel run --config=nix :hello'
```

To specify Python version or modules, change the `python3_attribute_path` parameter in the `nixpkgs_python_configure` call in the `WORKSPACE` file.
Loading