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

PoC: Add cross-compilation support #139

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

szlend
Copy link
Contributor

@szlend szlend commented Jun 29, 2024

WIP PoC for #138. This is not ready for review, just a starting point. Though all the tests on my machine do seem to pass.

  1. Wrap nix2container with makeScopeWithSplicing'. This gives us a way to express different platform variants of our builders/derivations (e.g. buildHost, hostTarget, etc).
  2. Wrap all builders/derivations into the callPackage pattern. This enables package splicing so nativeBuildInputs/buildInputs automatically pick the correct platform variants of our derivations when cross-compiling.
  3. (Optional) Since the callPackage pattern can be pretty noisy, I would suggest extracting individual builders/derivations to their own nix files where it makes sense. Similar to how crane does it here: https://github.com/ipetkov/crane/tree/master/lib

Happy to revert 3. if you feel this is going too far, but using callPackage in a single file is gonna add a lot of noise and additional indentation.

Comment on lines +29 to +30
inherit (nix2container) nix2container;
inherit examples tests;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to move these to legacyPackages because they're not derivations.

runCommand "image-${baseNameOf name}.json"
{
inherit imageName meta;
nativeBuildInputs = [ nix2container-bin ];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the core change across most derivations. We reference nix2container-bin through nativeBuildInputs instead of ${nix2container-bin}/bin/nix2container. Paired with makeScopeWithSplicing/callPackage this automatically resolves to the correct variant of the package.

}
''
nix2container image \
--arch ${go.GOARCH} \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We explicitly set the image architecture here to go.GOARCH. This is the target architecture when cross-compiling.

Comment on lines +118 to +121
copyToDockerDaemon = selfBuildHost.copyToDockerDaemon image;
copyToRegistry = selfBuildHost.copyToRegistry image;
copyToPodman = selfBuildHost.copyToPodman image;
copyTo = selfBuildHost.copyTo image;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want these to run on the build platform. These need to be explicitly picked from the buildHost variant because it's not possible to automatically infer which variant we want here.

@szlend
Copy link
Contributor Author

szlend commented Oct 27, 2024

I added an alternative implementation that should be easier to review in #153

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