From 635cd20db6b24ed452b39fee5d6540823836b5d5 Mon Sep 17 00:00:00 2001 From: James Leitch Date: Tue, 21 Nov 2023 20:21:40 -0700 Subject: [PATCH] nixpkgs_flake_package use `path:` syntax by default --- .envrc | 2 ++ .github/build-and-test | 2 +- README.md | 31 ++++++++++++++++++++++++++++++- core/nixpkgs.bzl | 36 +++++++++++++++++++++++++----------- docs/flake.lock | 2 +- flake.lock => nix/flake.lock | 0 flake.nix => nix/flake.nix | 0 7 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 .envrc rename flake.lock => nix/flake.lock (100%) rename flake.nix => nix/flake.nix (100%) diff --git a/.envrc b/.envrc new file mode 100644 index 000000000..f1d2d83c5 --- /dev/null +++ b/.envrc @@ -0,0 +1,2 @@ +# Use `path:` syntax to avoid copying the entire repo to the Nix Store. +use flake path:./nix diff --git a/.github/build-and-test b/.github/build-and-test index 14d8a9291..5276d1e6f 100755 --- a/.github/build-and-test +++ b/.github/build-and-test @@ -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 diff --git a/README.md b/README.md index 6da4e6c0c..c3bbb0101 100644 --- a/README.md +++ b/README.md @@ -538,11 +538,21 @@ Options to forward to the nix command.
 nixpkgs_flake_package(name, nix_flake_file, nix_flake_lock_file, nix_flake_file_deps, package,
-                      build_file, build_file_content, nixopts, quiet, fail_not_supported, kwargs)
+                      build_file, build_file_content, nixopts, quiet, fail_not_supported,
+                      legacy_path_syntax, 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. + + #### Parameters @@ -685,6 +695,20 @@ default is True 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. +

+ + + + + @@ -694,6 +718,11 @@ If set to `True` (default) this rule will fail on platforms which do not support optional. +

+ +Common rule arguments. + +

diff --git a/core/nixpkgs.bzl b/core/nixpkgs.bzl index 540401a5d..8ecb85510 100644 --- a/core/nixpkgs.bzl +++ b/core/nixpkgs.bzl @@ -410,18 +410,18 @@ 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] @@ -429,7 +429,7 @@ def _nixpkgs_build_and_symlink(repository_ctx, nix_build_cmd, expr_args, build_f 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, ), @@ -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 @@ -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), }, ) @@ -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. @@ -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 ` instead of `nix build 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 @@ -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) diff --git a/docs/flake.lock b/docs/flake.lock index 1985b6556..0912df25a 120000 --- a/docs/flake.lock +++ b/docs/flake.lock @@ -1 +1 @@ -../flake.lock \ No newline at end of file +../nix/flake.lock \ No newline at end of file diff --git a/flake.lock b/nix/flake.lock similarity index 100% rename from flake.lock rename to nix/flake.lock diff --git a/flake.nix b/nix/flake.nix similarity index 100% rename from flake.nix rename to nix/flake.nix
legacy_path_syntax + +optional. +default is False + +

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