Serge Bazanski | 194b1c8 | 2020-09-25 20:24:17 +0000 | [diff] [blame] | 1 | load("@io_tweag_rules_nixpkgs//nixpkgs:repositories.bzl", "rules_nixpkgs_dependencies") |
| 2 | load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_git_repository", "nixpkgs_package") |
| 3 | |
| 4 | def has_nix(ctx): |
| 5 | return ctx.which("nix-build") != None |
| 6 | |
| 7 | def _hscloud_gen_go_imports_impl(ctx): |
| 8 | ctx.file("BUILD", "") |
| 9 | |
| 10 | imports_for_nix = """ |
| 11 | load("@io_tweag_rules_nixpkgs//nixpkgs:toolchains/go.bzl", "nixpkgs_go_configure") |
| 12 | |
| 13 | def hscloud_go_register_toolchains(): |
| 14 | nixpkgs_go_configure(repository = "@nixpkgs") |
| 15 | """ |
| 16 | imports_for_non_nix = """ |
| 17 | load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains") |
| 18 | def hscloud_go_register_toolchains(): |
| 19 | go_register_toolchains() |
| 20 | """ |
| 21 | |
| 22 | if has_nix(ctx): |
| 23 | ctx.file("imports.bzl", imports_for_nix) |
| 24 | else: |
| 25 | ctx.file("imports.bzl", imports_for_non_nix) |
| 26 | |
| 27 | # Generate repository containing either a call to go_register_toolchains() or |
| 28 | # nixpkgs_go_configure(), depending on nix presence. |
| 29 | hscloud_gen_go_imports = repository_rule( |
| 30 | implementation = _hscloud_gen_go_imports_impl, |
| 31 | attrs = dict(), |
| 32 | ) |
| 33 | |
| 34 | def _hscloud_gen_pip_imports_impl(ctx): |
| 35 | ctx.file("BUILD", "") |
| 36 | |
| 37 | # For Nix, we have to both pass our interpreter to pip3_import, and also |
| 38 | # register it as a toolchain. |
| 39 | imports_for_nix = """ |
| 40 | load("@rules_python//python:pip.bzl", "pip3_import") |
| 41 | def hscloud_pip3_import(name, requirements): |
| 42 | pip3_import( |
| 43 | name = name, |
| 44 | requirements = requirements, |
| 45 | python_interpreter_target = "@hscloud_nix_python3//:python3", |
| 46 | ) |
| 47 | native.register_toolchains("//third_party/nix:py_toolchain") |
| 48 | """ |
| 49 | imports_for_non_nix = """ |
| 50 | load("@rules_python//python:pip.bzl", "pip3_import") |
| 51 | def hscloud_pip3_import(name, requirements): |
| 52 | pip3_import( |
| 53 | name = name, |
| 54 | requirements = requirements, |
| 55 | ) |
| 56 | """ |
| 57 | if has_nix(ctx): |
| 58 | ctx.file("imports.bzl", imports_for_nix) |
| 59 | else: |
| 60 | ctx.file("imports.bzl", imports_for_non_nix) |
| 61 | |
| 62 | # Generate repository containing a wrapped pip3_import that either uses the |
| 63 | # host Python interpreter or one from nixpkgs, depending on nix presence. |
| 64 | hscloud_gen_pip_imports = repository_rule( |
| 65 | implementation = _hscloud_gen_pip_imports_impl, |
| 66 | attrs = dict(), |
| 67 | ) |
| 68 | |
| 69 | def hscloud_setup_nix(revision, sha256): |
| 70 | rules_nixpkgs_dependencies() |
| 71 | nixpkgs_git_repository( |
| 72 | name = "nixpkgs", |
| 73 | revision = "1179840f9a88b8a548f4b11d1a03aa25a790c379", |
| 74 | sha256 = "8b64041bfb9760de9e797c0a985a4830880c21732489f397e217d877edd9a990", |
| 75 | ) |
| 76 | |
Serge Bazanski | 27885a9 | 2020-10-03 16:54:39 +0200 | [diff] [blame] | 77 | # Load python from nixpkgs. Python is a large source of non-hermiticity, |
Serge Bazanski | 194b1c8 | 2020-09-25 20:24:17 +0000 | [diff] [blame] | 78 | # and loading it from nix vastly hermeticizes the build - well, at least to |
| 79 | # also be dependent on this Nix store state. That's still better than just |
| 80 | # grabbing whatever random system Python a user might have. |
| 81 | nixpkgs_package( |
Serge Bazanski | 27885a9 | 2020-10-03 16:54:39 +0200 | [diff] [blame] | 82 | name = "hscloud_nix_python2", |
| 83 | repositories = { "nixpkgs": "@nixpkgs//:default.nix" }, |
| 84 | nix_file = "//third_party/nix:python.nix", |
| 85 | attribute_path = "python2", |
| 86 | build_file_content = """ |
| 87 | package(default_visibility = ["//visibility:public"]) |
| 88 | exports_files(["python2"]) |
| 89 | """, |
| 90 | ) |
| 91 | nixpkgs_package( |
Serge Bazanski | 194b1c8 | 2020-09-25 20:24:17 +0000 | [diff] [blame] | 92 | name = "hscloud_nix_python3", |
| 93 | repositories = { "nixpkgs": "@nixpkgs//:default.nix" }, |
| 94 | nix_file = "//third_party/nix:python.nix", |
Serge Bazanski | 27885a9 | 2020-10-03 16:54:39 +0200 | [diff] [blame] | 95 | attribute_path = "python3", |
Serge Bazanski | 194b1c8 | 2020-09-25 20:24:17 +0000 | [diff] [blame] | 96 | build_file_content = """ |
| 97 | package(default_visibility = ["//visibility:public"]) |
| 98 | exports_files(["python3"]) |
| 99 | """, |
| 100 | ) |
| 101 | |
| 102 | # Generate a Go toolchain setup workspace rule. |
| 103 | hscloud_gen_go_imports( |
| 104 | name = "hscloud_go_toolchain", |
| 105 | ) |
| 106 | hscloud_gen_pip_imports( |
| 107 | name = "hscloud_pip_imports", |
| 108 | ) |