blob: 35c3d2d3c4a3cbf3828ae8ed6f6cc29a31b1c611 [file] [log] [blame]
Serge Bazanski194b1c82020-09-25 20:24:17 +00001load("@io_tweag_rules_nixpkgs//nixpkgs:repositories.bzl", "rules_nixpkgs_dependencies")
2load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_git_repository", "nixpkgs_package")
3
4def has_nix(ctx):
5 return ctx.which("nix-build") != None
6
7def _hscloud_gen_go_imports_impl(ctx):
8 ctx.file("BUILD", "")
9
10 imports_for_nix = """
11load("@io_tweag_rules_nixpkgs//nixpkgs:toolchains/go.bzl", "nixpkgs_go_configure")
12
13def hscloud_go_register_toolchains():
14 nixpkgs_go_configure(repository = "@nixpkgs")
15"""
16 imports_for_non_nix = """
17load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
18def 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.
29hscloud_gen_go_imports = repository_rule(
30 implementation = _hscloud_gen_go_imports_impl,
31 attrs = dict(),
32)
33
34def _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 = """
40load("@rules_python//python:pip.bzl", "pip3_import")
41def 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 = """
50load("@rules_python//python:pip.bzl", "pip3_import")
51def 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.
64hscloud_gen_pip_imports = repository_rule(
65 implementation = _hscloud_gen_pip_imports_impl,
66 attrs = dict(),
67)
68
69def 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 Bazanski27885a92020-10-03 16:54:39 +020077 # Load python from nixpkgs. Python is a large source of non-hermiticity,
Serge Bazanski194b1c82020-09-25 20:24:17 +000078 # 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 Bazanski27885a92020-10-03 16:54:39 +020082 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 = """
87package(default_visibility = ["//visibility:public"])
88exports_files(["python2"])
89 """,
90 )
91 nixpkgs_package(
Serge Bazanski194b1c82020-09-25 20:24:17 +000092 name = "hscloud_nix_python3",
93 repositories = { "nixpkgs": "@nixpkgs//:default.nix" },
94 nix_file = "//third_party/nix:python.nix",
Serge Bazanski27885a92020-10-03 16:54:39 +020095 attribute_path = "python3",
Serge Bazanski194b1c82020-09-25 20:24:17 +000096 build_file_content = """
97package(default_visibility = ["//visibility:public"])
98exports_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 )