Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(buck2): allow rustc to use Nix environment when compiling #2266

Merged
merged 1 commit into from
May 31, 2023

Conversation

fnichol
Copy link
Contributor

@fnichol fnichol commented May 31, 2023

This change enables our project to build using the Nix environment running on Linux and macOS platforms.

Background

The upstream rustc_action.py script which invokes rustc filters the environment in an effort to maintain a better sandboxed build. When we build in a Nix environment (currently used for development and shortly in CI), there are several environment variables that cause the correct linking behavior under Nix.

The Nix environment provides wrapper scripts for clang and clang++ which use these environment variables. The rustc program is responsible for invoking the linker (by default this tends to be cc/clang) which means these environment variables need to be present in the rustc process so that its children can inherit them.

The Fix

While there were many attempts at a solution to this problem, we went with a "patch the upstream" approach. In other words, the rustc_action.py script is what prevents our correct Nix behavior, so we slightly augmented it with support for more environment variables. This script is provided to us via the upstream
facebookincubator/buck2-prelude Git repository which we currently vendor using git subtree. This means that our "patch" will be wiped out when we re-vendor/update against upstream changes. With this in mind, we provide a third-party/patches/nix_rustc_action.patch file which should allow us to patch over upstream updates similar to upstream patching in a packaging system.

This change enables our project to build using the Nix environment
running on Linux and macOS platforms.

Background
----------

The upstream `rustc_action.py` script which invokes `rustc` filters the
environment in an effort to maintain a better sandboxed build. When we
build in a Nix environment (currently used for development and shortly
in CI), there are several environment variables that cause the correct
linking behavior under Nix.

The Nix environment provides wrapper scripts for `clang` and `clang++`
which use these environment variables. The `rustc` program is
responsible for invoking the linker (by default this tends to be
`cc`/`clang`) which means these environment variables need to be present
in the `rustc` process so that its children can inherit them.

The Fix
-------

While there were many attempts at a solution to this problem, we went
with a "patch the upstream" approach. In other words, the
`rustc_action.py` script is what prevents our correct Nix behavior, so
we slightly augmented it with support for more environment variables.
This script is provided to us via the upstream
`facebookincubator/buck2-prelude` Git repository which we currently
vendor using `git subtree`. This means that our "patch" will be wiped
out when we re-vendor/update against upstream changes. With this in
mind, we provide a `third-party/patches/nix_rustc_action.patch` file
which should allow us to patch over upstream updates similar to upstream
patching in a packaging system.

Co-authored-by: Nick Gerace <[email protected]>
Signed-off-by: Fletcher Nichol <[email protected]>
@fnichol
Copy link
Contributor Author

fnichol commented May 31, 2023

bors merge

@si-bors-ng
Copy link
Contributor

si-bors-ng bot commented May 31, 2023

@si-bors-ng si-bors-ng bot merged commit 400095c into main May 31, 2023
@si-bors-ng si-bors-ng bot deleted the fnichol/nix-rustc branch May 31, 2023 20:41
fnichol added a commit that referenced this pull request Nov 23, 2023
Update to patch required after change in
facebook/buck2-prelude@9e1cfaf
resulted in a merge conflict (trivial to resolve but there nonetheless).

References: #2266
References: facebook/buck2#466

Signed-off-by: Fletcher Nichol <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant