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
123 changes: 122 additions & 1 deletion core/extensions/repository.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Defines the nix_repo module extension.
"""

load("//:nixpkgs.bzl", "nixpkgs_http_repository")
load("//:nixpkgs.bzl", "nixpkgs_http_repository", "nixpkgs_local_repository")
load("//:util.bzl", "fail_on_err")
load("//private:module_registry.bzl", "registry")
load("@bazel_skylib//lib:dicts.bzl", "dicts")
Expand Down Expand Up @@ -56,6 +56,39 @@ def _github_repo(github):
strip_prefix = strip_prefix,
)

def _http_repo(http):
url_set = bool(http.url)
urls_set = bool(http.urls)

if url_set and urls_set:
fail("Specify only one of `url` or `urls`.")

if not url_set and not urls_set:
fail("Missing URL. Specify one of `url` or `urls`.")

return partial.make(
nixpkgs_http_repository,
url = http.url if url_set else None,
urls = http.urls if urls_set else None,
integrity = http.integrity,
sha256 = http.sha256,
strip_prefix = http.strip_prefix,
)

def _file_repo(file):
return partial.make(
nixpkgs_local_repository,
nix_file = file.file,
nix_file_deps = file.file_deps,
)

def _expr_repo(expr):
return partial.make(
nixpkgs_local_repository,
nix_file_content = expr.expr,
nix_file_deps = expr.file_deps,
)

def _nix_repo_impl(module_ctx):
r = registry.make()

Expand All @@ -79,6 +112,39 @@ def _nix_repo_impl(module_ctx):
prefix = "Cannot import GitHub repository: ",
)

for http in mod.tags.http:
fail_on_err(
registry.add_local_repo(
r,
key = key,
name = http.name,
repo = _http_repo(http),
),
prefix = "Cannot import HTTP repository: ",
)

for file in mod.tags.file:
fail_on_err(
registry.add_local_repo(
r,
key = key,
name = file.name,
repo = _file_repo(file),
),
prefix = "Cannot import file repository: ",
)

for expr in mod.tags.expr:
fail_on_err(
registry.add_local_repo(
r,
key = key,
name = expr.name,
repo = _expr_repo(expr),
),
prefix = "Cannot import expression repository: ",
)

for override in mod.tags.override:
prefix = "Cannot override global default repository: "
repo = fail_on_err(
Expand Down Expand Up @@ -145,6 +211,43 @@ _GITHUB_ATTRS = {
),
}

_HTTP_ATTRS = {
"url": attr.string(
doc = "URL to download from. Specify one of `url` or `urls`.",
mandatory = False,
),
"urls": attr.string_list(
doc = "List of URLs to download from. Specify one of `url` or `urls`.",
mandatory = False,
),
"strip_prefix": attr.string(
doc = "A directory prefix to strip from the extracted files.",
mandatory = False,
),
}

_FILE_DEPS_ATTRS = {
"file_deps": attr.label_list(
doc = "List of files required by the Nix expression.",
mandatory = False,
),
}

_FILE_ATTRS = {
"file": attr.label(
doc = "The file containing the Nix expression.",
mandatory = True,
allow_single_file = True,
),
}

_EXPR_ATTRS = {
"expr": attr.string(
doc = "The Nix expression.",
mandatory = True,
),
}

_OVERRIDE_ATTRS = {
"name": attr.string(
doc = "The name of the global default repository to set.",
Expand All @@ -162,6 +265,21 @@ _github_tag = tag_class(
doc = "Import a Nix repository from Github.",
)

_http_tag = tag_class(
attrs = dicts.add(_NAME_ATTRS, _HTTP_ATTRS, _INTEGRITY_ATTRS),
doc = "Import a Nix repository from an HTTP URL.",
)

_file_tag = tag_class(
attrs = dicts.add(_NAME_ATTRS, _FILE_ATTRS, _FILE_DEPS_ATTRS),
doc = "Import a Nix repository from a local file.",
)

_expr_tag = tag_class(
attrs = dicts.add(_NAME_ATTRS, _EXPR_ATTRS, _FILE_DEPS_ATTRS),
doc = "Import a Nix repository from a Nix expression.",
)

_override_tag = tag_class(
attrs = _OVERRIDE_ATTRS,
doc = "Define the global default Nix repository. May only be used in the root module or rules_nixpkgs_core.",
Expand All @@ -172,6 +290,9 @@ nix_repo = module_extension(
tag_classes = {
"default": _default_tag,
"github": _github_tag,
"http": _http_tag,
"file": _file_tag,
"expr": _expr_tag,
"override": _override_tag,
},
)
3 changes: 2 additions & 1 deletion design/bzlmod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,8 @@ offers tags to define Nix repositories:
* `file_deps`: optional, List of `Label`, files required by `file`.
* `expr(name, expression)`\
* `name`: `String`; unique name.
* `expression`: `String`; the Nix expression.
* `expr`: `String`; the Nix expression.
* `file_deps`: optional, List of `Label`, files required by `expr`.
* `override(repo)` (only allowed in rules\_nixpkgs\_core and root)\
* `repo`: `String`; The name of the repository to override.

Expand Down
24 changes: 22 additions & 2 deletions testing/core/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ nix_repo.github(
tag = "22.05",
sha256 = "0f8c25433a6611fa5664797cd049c80faefec91575718794c701f3b033f2db01",
)
nix_repo.http(
name = "http_nixpkgs",
url = "https://github.com/NixOS/nixpkgs/archive/refs/tags/22.05.tar.gz",
sha256 = "0f8c25433a6611fa5664797cd049c80faefec91575718794c701f3b033f2db01",
strip_prefix = "nixpkgs-22.05",
)
nix_repo.file(
name = "file_nixpkgs",
file = "//:nixpkgs.nix",
file_deps = [
"//:flake.lock",
],
)
nix_repo.expr(
name = "nixpkgs_content",
expr = "import ./nixpkgs.nix",
file_deps = [
"//:nixpkgs.nix",
"//:flake.lock",
],
)

nix_pkg = use_extension("@rules_nixpkgs_core//extensions:package.bzl", "nix_pkg")
use_repo(nix_pkg, "nixpkgs_packages")
Expand All @@ -30,14 +51,13 @@ nix_pkg.local_attr(name = "attribute-test", attr = "hello")
nix_pkg.local_attr(name = "nixpkgs-git-repository-test", attr = "hello", repo = "remote_nixpkgs")

non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps")
use_repo(non_module_deps, "http_nixpkgs")
use_repo(non_module_deps, "nixpkgs_content")
use_repo(non_module_deps, "expr-test")
use_repo(non_module_deps, "expr-attribute-test")
use_repo(non_module_deps, "extra-args-test")
use_repo(non_module_deps, "nix-file-test")
use_repo(non_module_deps, "nix-file-deps-test")
use_repo(non_module_deps, "nixpkgs-http-repository-test")
use_repo(non_module_deps, "nixpkgs-file-repository-test")
use_repo(non_module_deps, "nixpkgs-local-repository-test")
use_repo(non_module_deps, "relative-imports")
use_repo(non_module_deps, "output-filegroup-test")
Expand Down
1 change: 1 addition & 0 deletions testing/core/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ expand_location_unit_test_suite()
"nix-file-test",
"nix-file-deps-test",
"nixpkgs-http-repository-test",
"nixpkgs-file-repository-test",
"nixpkgs-local-repository-test",
"relative-imports",
]
Expand Down
57 changes: 38 additions & 19 deletions testing/core/tests/nixpkgs_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def nixpkgs_repositories(*, bzlmod):
if bzlmod:
nixpkgs = nix_repo("rules_nixpkgs_core_testing", "nixpkgs")
remote_nixpkgs = nix_repo("rules_nixpkgs_core_testing", "remote_nixpkgs")
http_nixpkgs = nix_repo("rules_nixpkgs_core_testing", "http_nixpkgs")
file_nixpkgs = nix_repo("rules_nixpkgs_core_testing", "file_nixpkgs")
nixpkgs_content = nix_repo("rules_nixpkgs_core_testing", "nixpkgs_content")
else:
nixpkgs = "@nixpkgs"
nixpkgs_local_repository(
Expand All @@ -28,6 +31,33 @@ def nixpkgs_repositories(*, bzlmod):
sha256 = "0f8c25433a6611fa5664797cd049c80faefec91575718794c701f3b033f2db01",
)

http_nixpkgs = "@http_nixpkgs"
nixpkgs_http_repository(
name = "http_nixpkgs",
url = "https://github.com/NixOS/nixpkgs/archive/refs/tags/22.05.tar.gz",
strip_prefix = "nixpkgs-22.05",
sha256 = "0f8c25433a6611fa5664797cd049c80faefec91575718794c701f3b033f2db01",
)

# same as @nixpkgs, only needed for bzlmod tests to distinguish `default` and `file`.
file_nixpkgs = "@file_nixpkgs"
nixpkgs_local_repository(
name = "file_nixpkgs",
nix_file = "//:nixpkgs.nix",
nix_file_deps = ["//:flake.lock"],
)

# same as @nixpkgs but using the `nix_file_content` parameter
nixpkgs_content = "@nixpkgs_content"
nixpkgs_local_repository(
name = "nixpkgs_content",
nix_file_content = "import ./nixpkgs.nix",
nix_file_deps = [
"//:nixpkgs.nix",
"//:flake.lock",
],
)

nixpkgs_package(
name = "hello",
# Deliberately not repository, to test whether repositories works.
Expand All @@ -46,23 +76,6 @@ def nixpkgs_repositories(*, bzlmod):
repositories = {"nixpkgs": remote_nixpkgs},
)

nixpkgs_http_repository(
name = "http_nixpkgs",
url = "https://github.com/NixOS/nixpkgs/archive/refs/tags/22.05.tar.gz",
strip_prefix = "nixpkgs-22.05",
sha256 = "0f8c25433a6611fa5664797cd049c80faefec91575718794c701f3b033f2db01",
)

# same as @nixpkgs but using the `nix_file_content` parameter
nixpkgs_local_repository(
name = "nixpkgs_content",
nix_file_content = "import ./nixpkgs.nix",
nix_file_deps = [
"//:nixpkgs.nix",
"//:flake.lock",
],
)

nixpkgs_package(
name = "expr-test",
nix_file_content = "let pkgs = import <nixpkgs> { config = {}; overlays = []; }; in pkgs.hello",
Expand Down Expand Up @@ -108,13 +121,19 @@ def nixpkgs_repositories(*, bzlmod):
nixpkgs_package(
name = "nixpkgs-http-repository-test",
attribute_path = "hello",
repositories = {"nixpkgs": "@http_nixpkgs"},
repositories = {"nixpkgs": http_nixpkgs},
)

nixpkgs_package(
name = "nixpkgs-file-repository-test",
nix_file_content = "with import <nixpkgs> {}; hello",
repositories = {"nixpkgs": file_nixpkgs},
)

nixpkgs_package(
name = "nixpkgs-local-repository-test",
nix_file_content = "with import <nixpkgs> {}; hello",
repositories = {"nixpkgs": "@nixpkgs_content"},
repositories = {"nixpkgs": nixpkgs_content},
)

nixpkgs_package(
Expand Down