Skip to content

Commit

Permalink
Merge pull request #301 from tweag/python-container-example
Browse files Browse the repository at this point in the history
Fix Python container example
  • Loading branch information
mergify[bot] authored Jan 10, 2023
2 parents 9447dac + 96513da commit 25e7da5
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 15 deletions.
20 changes: 15 additions & 5 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,32 @@ jobs:
read -a ${1} <<< $(echo ${!p//$2})
}
cd examples/toolchains
pushd examples/toolchains
with_nix=( $(ls) )
without_nix=( cc go java rust )
for dir in "${with_nix[@]}"; do
cd "$dir"
pushd "$dir"
echo
echo "::group::Running $(head -n1 README.md) with Nix"
nix-shell --command 'bazel run --config=nix :hello'
# TODO: all toolchains should run without Nixpkgs
cd ..
popd
done
for dir in "${without_nix[@]}"; do
cd "$dir"
pushd "$dir"
echo
echo "::group::Running $(head -n1 README.md) without Nix"
bazel run :hello
cd ..
popd
done
popd
if [[ ${{ matrix.os }} = ubuntu* ]]; then
pushd examples/python-container
echo
echo "::group::Running $(head -n1 README.md) with Nix"
nix-shell --command 'bazel build :hello_image'
popd
fi
1 change: 0 additions & 1 deletion examples/python-container/.bazelrc
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
build:nix --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host
8 changes: 8 additions & 0 deletions examples/python-container/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ load("@io_bazel_rules_docker//python3:image.bzl", "py3_image")

package(default_visibility = ["//visibility:public"])

platform(
name = "nix_container_platform",
constraint_values = [
"@io_bazel_rules_docker//platforms:run_in_container",
],
parents = ["@io_tweag_rules_nixpkgs//nixpkgs/platforms:host"],
)

py3_image(
name = "hello_image",
srcs = [ "hello.py" ],
Expand Down
6 changes: 3 additions & 3 deletions examples/python-container/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ Expect something like this as output:

## Step 2: Nix-Environment with bazel

We're using Nix 21.11 (stable at the time of writing) and Bazel version 4, so
We're using Nix 22.05 (stable at the time of writing) and Bazel version 5, so
create a file `shell.nix` wit the following content:

```
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/refs/tags/21.11.tar.gz") {} }:
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/refs/tags/22.05.tar.gz") {} }:
pkgs.mkShellNoCC {
nativeBuildInputs = [
Expand All @@ -69,7 +69,7 @@ nix-shell --pure --command 'bazel --version'
You get something like

```
bazel 4.2.1- (@non-git)
bazel 5.1.1- (@non-git)
```

---
Expand Down
30 changes: 25 additions & 5 deletions examples/python-container/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,39 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
######################
# Tweag Nix Support
######################
http_archive(

# Replace with http_archive: https://github.com/tweag/rules_nixpkgs/#setup
local_repository(
name = "io_tweag_rules_nixpkgs",
sha256 = "7aee35c95251c1751e765f7da09c3bb096d41e6d6dca3c72544781a5573be4aa",
strip_prefix = "rules_nixpkgs-0.8.0",
urls = ["https://github.com/tweag/rules_nixpkgs/archive/v0.8.0.tar.gz"],
path = "../../",
)

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

# Define nixpkgs version 21.11
# Define nixpkgs version
load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_git_repository")
nixpkgs_git_repository(
name = "nixpkgs",
revision = "22.05",
sha256 = "0f8c25433a6611fa5664797cd049c80faefec91575718794c701f3b033f2db01",
)

# Configure the C++ toolchain
load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_cc_configure")
nixpkgs_cc_configure(
repository = "@nixpkgs",
exec_constraints = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
"@io_bazel_rules_docker//platforms:run_in_container",
],
target_constraints = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
)

# Configure python
load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_python_configure")
nixpkgs_python_configure(
Expand Down Expand Up @@ -64,3 +79,8 @@ exports_files(["image"])

load("@io_bazel_rules_docker//container:container.bzl", "container_load" )
container_load(name = "python39_base_image", file = "@raw_python39_base_image//:image")

register_execution_platforms(
"@io_tweag_rules_nixpkgs//nixpkgs/platforms:host",
"@//:nix_container_platform",
)
7 changes: 7 additions & 0 deletions examples/python-container/nixpkgs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"owner": "NixOS",
"repo": "nixpkgs",
"branch": "22.05",
"rev": "ce6aa13369b667ac2542593170993504932eb836",
"sha256": "0d643wp3l77hv2pmg2fi7vyxn4rwy0iyr8djcw1h5x72315ck9ik"
}
8 changes: 8 additions & 0 deletions examples/python-container/nixpkgs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let
spec = builtins.fromJSON (builtins.readFile ./nixpkgs.json);
nixpkgs = fetchTarball {
url = "https://github.com/${spec.owner}/${spec.repo}/archive/${spec.rev}.tar.gz";
sha256 = spec.sha256;
};
in
import nixpkgs
2 changes: 1 addition & 1 deletion examples/python-container/shell.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/refs/tags/21.11.tar.gz") {} }:
{ pkgs ? import ./nixpkgs.nix { } }:

pkgs.mkShellNoCC {
nativeBuildInputs = [
Expand Down

0 comments on commit 25e7da5

Please sign in to comment.