Skip to content

Commit

Permalink
Merge pull request #450 from ConsumingChaos/flake_path
Browse files Browse the repository at this point in the history
nixpkgs_flake_package use `path:` syntax by default
  • Loading branch information
mergify[bot] authored Nov 23, 2023
2 parents e632be8 + 903a7d0 commit c638024
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Use `path:` syntax to avoid copying the entire repo to the Nix Store.
use flake path:./nix
2 changes: 1 addition & 1 deletion .github/build-and-test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env nix-shell
#! nix-shell ../shell.nix -i bash
#! nix-shell ../nix/shell.nix -i bash

set -euo pipefail

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/update-flake-lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ jobs:
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- name: Update flake.lock
uses: DeterminateSystems/update-flake-lock@v20
with:
path-to-flake-dir: nix
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,21 @@ Options to forward to the nix command.

<pre>
nixpkgs_flake_package(<a href="#nixpkgs_flake_package-name">name</a>, <a href="#nixpkgs_flake_package-nix_flake_file">nix_flake_file</a>, <a href="#nixpkgs_flake_package-nix_flake_lock_file">nix_flake_lock_file</a>, <a href="#nixpkgs_flake_package-nix_flake_file_deps">nix_flake_file_deps</a>, <a href="#nixpkgs_flake_package-package">package</a>,
<a href="#nixpkgs_flake_package-build_file">build_file</a>, <a href="#nixpkgs_flake_package-build_file_content">build_file_content</a>, <a href="#nixpkgs_flake_package-nixopts">nixopts</a>, <a href="#nixpkgs_flake_package-quiet">quiet</a>, <a href="#nixpkgs_flake_package-fail_not_supported">fail_not_supported</a>, <a href="#nixpkgs_flake_package-kwargs">kwargs</a>)
<a href="#nixpkgs_flake_package-build_file">build_file</a>, <a href="#nixpkgs_flake_package-build_file_content">build_file_content</a>, <a href="#nixpkgs_flake_package-nixopts">nixopts</a>, <a href="#nixpkgs_flake_package-quiet">quiet</a>, <a href="#nixpkgs_flake_package-fail_not_supported">fail_not_supported</a>,
<a href="#nixpkgs_flake_package-legacy_path_syntax">legacy_path_syntax</a>, <a href="#nixpkgs_flake_package-kwargs">kwargs</a>)
</pre>

Make the content of a local Nix Flake package available in the Bazel workspace.

**IMPORTANT NOTE**: Calling `nix build` copies the entirety of the Nix Flake
into the Nix Store. When using the `path:` syntax, this means the directory
containing `flake.nix` and any subdirectories. Without specifying `path:`
Nix may infer that the flake is the Git repository and copy the entire thing.
As a consequence, you may want to isolate your flake from the rest of the
repository to minimize the amount of unnecessary data that gets copied into
the Nix Store whenever the flake is rebuilt.


#### Parameters

<table class="params-table">
Expand Down Expand Up @@ -685,6 +695,20 @@ default is <code>True</code>

If set to `True` (default) this rule will fail on platforms which do not support Nix (e.g. Windows). If set to `False` calling this rule will succeed but no output will be generated.

</p>
</td>
</tr>
<tr id="nixpkgs_flake_package-legacy_path_syntax">
<td><code>legacy_path_syntax</code></td>
<td>

optional.
default is <code>False</code>

<p>

If set to True (not default), the Nix Flake invocation will directly call `nix build <path>` instead of `nix build path:<path>` which may involve copying the entirety of the Git repo into the Nix Store instead of just the path and its children.

</p>
</td>
</tr>
Expand All @@ -694,6 +718,11 @@ If set to `True` (default) this rule will fail on platforms which do not support

optional.

<p>

Common rule arguments.

</p>
</td>
</tr>
</tbody>
Expand Down
36 changes: 25 additions & 11 deletions core/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -410,26 +410,26 @@ def _nixpkgs_build_and_symlink(repository_ctx, nix_build_cmd, expr_args, build_f
extra_msg = "See: https://nixos.org/nix/",
)

nix_host = repository_ctx.os.environ.get('BAZEL_NIX_REMOTE', '')
nix_host = repository_ctx.os.environ.get("BAZEL_NIX_REMOTE", "")
if nix_host:
nix_store = "ssh-ng://{host}?max-connections=1".format(host = nix_host)
repository_ctx.report_progress("Remote-building Nix derivation")
exec_result = execute_or_fail(
repository_ctx,
nix_build_cmd + ["--store", nix_store, "--eval-store", "auto"] + expr_args,
failure_message = "Cannot build Nix attribute '{}'.".format(
repository_ctx.attr.name,
),
quiet = repository_ctx.attr.quiet,
timeout = timeout,
repository_ctx,
nix_build_cmd + ["--store", nix_store, "--eval-store", "auto"] + expr_args,
failure_message = "Cannot build Nix attribute '{}'.".format(
repository_ctx.attr.name,
),
quiet = repository_ctx.attr.quiet,
timeout = timeout,
)
output_path = exec_result.stdout.splitlines()[-1]

ssh_path = repository_ctx.which("ssh")
repository_ctx.report_progress("Creating remote store root")
exec_result = execute_or_fail(
repository_ctx,
[ssh_path] + [nix_host, "nix-store --add-root ~/rules_nixpkgs_gcroots/{root} -r {path}".format(root = output_path.split('/')[-1], path = output_path) ],
[ssh_path] + [nix_host, "nix-store --add-root ~/rules_nixpkgs_gcroots/{root} -r {path}".format(root = output_path.split("/")[-1], path = output_path)],
failure_message = "Cannot create remote store root for Nix attribute '{}'.".format(
repository_ctx.attr.name,
),
Expand Down Expand Up @@ -753,7 +753,8 @@ def _nixpkgs_flake_package_impl(repository_ctx):
for dep_lbl, dep_str in repository_ctx.attr.nix_flake_file_deps.items():
nix_flake_file_deps[dep_str] = cp(repository_ctx, dep_lbl)

nix_build_target = str(repository_ctx.path(repository_ctx.attr.nix_flake_file).dirname)
nix_build_target = "path:" if not repository_ctx.attr.legacy_path_syntax else ""
nix_build_target += str(repository_ctx.path(repository_ctx.attr.nix_flake_file).dirname)
if repository_ctx.attr.package:
nix_build_target += "#" + repository_ctx.attr.package

Expand Down Expand Up @@ -804,7 +805,8 @@ _nixpkgs_flake_package = repository_rule(
"quiet": attr.bool(),
"fail_not_supported": attr.bool(default = True, doc = """
If set to True (default) this rule will fail on platforms which do not support Nix (e.g. Windows). If set to False calling this rule will succeed but no output will be generated.
"""),
"""),
"legacy_path_syntax": attr.bool(default = False),
},
)

Expand All @@ -819,9 +821,18 @@ def nixpkgs_flake_package(
nixopts = [],
quiet = False,
fail_not_supported = True,
legacy_path_syntax = False,
**kwargs):
"""Make the content of a local Nix Flake package available in the Bazel workspace.
**IMPORTANT NOTE**: Calling `nix build` copies the entirety of the Nix Flake
into the Nix Store. When using the `path:` syntax, this means the directory
containing `flake.nix` and any subdirectories. Without specifying `path:`
Nix may infer that the flake is the Git repository and copy the entire thing.
As a consequence, you may want to isolate your flake from the rest of the
repository to minimize the amount of unnecessary data that gets copied into
the Nix Store whenever the flake is rebuilt.
Args:
name: A unique name for this repository.
nix_flake_file: Label to `flake.nix` that will be evaluated.
Expand All @@ -833,6 +844,8 @@ def nixpkgs_flake_package(
nixopts: Extra flags to pass when calling Nix. See [`nixpkgs_package`](#nixpkgs_package-nixopts) for more information.
quiet: Whether to hide the output of the Nix command.
fail_not_supported: If set to `True` (default) this rule will fail on platforms which do not support Nix (e.g. Windows). If set to `False` calling this rule will succeed but no output will be generated.
legacy_path_syntax: If set to True (not default), the Nix Flake invocation will directly call `nix build <path>` instead of `nix build path:<path>` which may involve copying the entirety of the Git repo into the Nix Store instead of just the path and its children.
**kwargs: Common rule arguments.
"""
if kwargs.pop("_bzlmod", None):
# The workaround to map canonicalized labels to the user provided
Expand All @@ -857,6 +870,7 @@ def nixpkgs_flake_package(
nixopts = nixopts,
quiet = quiet,
fail_not_supported = fail_not_supported,
legacy_path_syntax = legacy_path_syntax,
)

_nixpkgs_flake_package(**kwargs)
2 changes: 1 addition & 1 deletion docs/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
5 changes: 2 additions & 3 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
(import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
let lock = builtins.fromJSON (builtins.readFile ./nix/flake.lock); in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
{ src = ./nix; }
).shellNix

2 changes: 1 addition & 1 deletion testing/cc/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testing/go-bzlmod/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testing/go-workspace/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testing/java/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testing/nodejs/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testing/posix/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testing/python/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testing/rust/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c638024

Please sign in to comment.