Skip to content

Commit

Permalink
Decouple self-dependencies and impl lib.mkRustBin interface (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxalica committed Jun 3, 2024
2 parents 07098b4 + ca56164 commit 711ff40
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 216 deletions.
22 changes: 17 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,16 @@ jobs:
[[ "$(< out)" == *"warning: this loop never actually loops"* ]]
test-cross-examples:
name: Test cross compilation examples
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
example: [cross-aarch64, cross-wasi, cross-mingw]
kind: [shell]
include:
- example: cross-aarch64
kind: flake
name: Test cross compilation examples (${{ matrix.kind }})

steps:
- name: Checkout
Expand All @@ -158,15 +162,23 @@ jobs:
- name: Don't let ~/.cargo/bin mess things up
run: rm -rf ~/.cargo/bin

- name: Prepare nix-shell
- name: Prepare dev env
run: |
cd examples/${{ matrix.example }}
nix-shell --pure --command true
if [[ ${{ matrix.kind }} == 'shell' ]]; then
nix-shell --pure --command true
else
nix develop --ignore-environment --override-input rust-overlay ../..
fi
- name: Build and run example
run: |
cd examples/${{ matrix.example }}
set -o pipefail
# No --pure since it requires CA cert access for crates downloading.
nix-shell --command "make run" | tee out
if [[ ${{ matrix.kind }} == 'shell' ]]; then
# No --pure since it requires CA cert access for crates downloading.
nix-shell --pure --command "make run" | tee out
else
nix develop --ignore-environment --override-input rust-overlay ../.. --command make run | tee out
fi
[[ "$(< out)" == *"Hello, world!"* ]]
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ target
result
result-*
*.tmp

examples/cross-aarch64/flake.lock
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
[![CI status](https://github.com/oxalica/rust-overlay/workflows/CI/badge.svg)](https://github.com/oxalica/rust-overlay/actions/workflows/ci.yaml)
[![sync-channels status](https://github.com/oxalica/rust-overlay/workflows/sync-channels/badge.svg)](https://github.com/oxalica/rust-overlay/actions/workflows/sync-channels.yaml)

*Pure and reproducible* overlay for binary distributed rust toolchains.
A compatible but better replacement for rust overlay of [nixpkgs-mozilla].
*Pure and reproducible* packaging of binary distributed rust toolchains.
A compatible but better replacement for rust overlay of [nixpkgs-mozilla],
with also non-overlay and [Nix Flake][flake] interfaces (despite the project name).

Hashes of toolchain components are pre-fetched in tree, so the evaluation is *pure* and
no need to have network access. It also works well with [Nix Flakes](https://nixos.wiki/wiki/Flakes).
[flake]: https://wiki.nixos.org/wiki/Flakes

For migration from [nixpkgs-mozilla], see [this section](#migration-from-nixpkgs-mozilla).

Features:

- Hashes of toolchain components are pre-fetched in tree, so the evaluation is
*pure* and no need to have network access.

- These hashes are auto-updated daily using GitHub Actions.

- The toolchain hashes are auto-updated daily using GitHub Actions.
- Current oldest supported version is stable 1.29.0 and beta/nightly 2018-09-13
(which are randomly picked and may change over time).

rust-overlay targets nixos-unstable and supported releases of NixOS, on x86_64-linux.
They are tested on CI. It may also work on other channels but is not guarenteed.
- We targets nixos-unstable and supported releases of NixOS, on x86\_64-linux.
They are tested on CI. Other platforms and nixpkgs channels may also work but
is not guaranteed.

For migration from [nixpkgs-mozilla], see [this section](#migration-from-nixpkgs-mozilla).
Documentations:

- [API References](docs/reference.md)
- [Cross compilation](docs/cross_compilation.md)

## Installation

Expand All @@ -41,8 +53,6 @@ And then feel free to use it anywhere like

### Nix Flakes

This repository already has flake support.

**Warning: Only the output `overlay`/`overlays` are currently stable. Use other outputs at your own risk!**

For a quick play, just use `nix shell` to bring the latest stable rust toolchain into scope.
Expand Down Expand Up @@ -218,11 +228,6 @@ Running `nix develop` will create a shell with the default beta Rust toolchain i

- There also an cross-compilation example in [`examples/cross-aarch64`].

## More documentations

- [Reference of all public attributes](docs/reference.md)
- [Cross compilation](docs/cross_compilation.md)

## License

MIT licensed.
Expand Down
72 changes: 71 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
@@ -1,2 +1,72 @@
# Overlay interface for non-flake Nix.
final: prev:
(import ./rust-overlay.nix final) (prev // (import ./manifest.nix final prev))
let
inherit (builtins) mapAttrs readFile;

inherit (final) lib rust-bin;
inherit (rust-bin._internal) toolchainFromManifest selectManifest;

# Same as `toolchainFromManifest` but read from a manifest file.
toolchainFromManifestFile = path: toolchainFromManifest (fromTOML (readFile path));

# Override all pkgs of a toolchain set.
overrideToolchain = attrs: mapAttrs (name: pkg: pkg.override attrs);

# This is eagerly evaluated and disallow overriding. Get this from `final`
# will easily encounter infinite recursion without manually expand all attrs
# from `rust-bin.nix` output like `mkIf` does.
# This is considered internal anyway.
manifests = import ./lib/manifests.nix {
inherit lib;
inherit (rust-bin) distRoot;
};

in {
rust-bin = (prev.rust-bin or { }) // {
# The overridable dist url for fetching.
distRoot = import ./lib/dist-root.nix;
} // import ./lib/rust-bin.nix {
inherit lib manifests;
inherit (final.rust) toRustTarget;
inherit (rust-bin) nightly;
pkgs = final;
};

# All attributes below are for compatibility with mozilla overlay.

lib = (prev.lib or { }) // {
rustLib = (prev.lib.rustLib or { }) // {
manifest_v2_url = throw ''
`manifest_v2_url` is not supported.
Select a toolchain from `rust-bin` or using `rustChannelOf` instead.
See also README at https://github.com/oxalica/rust-overlay
'';
fromManifest = throw ''
`fromManifest` is not supported due to network access during evaluation.
Select a toolchain from `rust-bin` or using `rustChannelOf` instead.
See also README at https://github.com/oxalica/rust-overlay
'';
fromManifestFile = manifestFilePath: { stdenv, fetchurl, patchelf }@deps: builtins.trace ''
`fromManifestFile` is deprecated.
Select a toolchain from `rust-bin` or using `rustChannelOf` instead.
See also README at https://github.com/oxalica/rust-overlay
'' (overrideToolchain deps (toolchainFromManifestFile manifestFilePath));
};
};

rustChannelOf = manifestArgs: toolchainFromManifest (selectManifest manifestArgs);

latest = (prev.latest or {}) // {
rustChannels = {
stable = rust-bin.stable.latest;
beta = rust-bin.beta.latest;
nightly = rust-bin.nightly.latest;
};
};

rustChannelOfTargets = channel: date: targets:
(final.rustChannelOf { inherit channel date; })
.rust.override { inherit targets; };

rustChannels = final.latest.rustChannels;
}
36 changes: 34 additions & 2 deletions docs/cross_compilation.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Cross compilation with `rust-overlay`

## `shell.nix` and `nix shell` development environment

There are examples for cross compilation in [`example` directory](../examples).
To try examples,
1. `cd` into `example/cross-aarch64` (or other directory).
Expand Down Expand Up @@ -42,7 +44,7 @@ mkShell {
# See: https://github.com/NixOS/nixpkgs/pull/146583
depsBuildBuild = [ qemu ];
# Run-time dependencies. build = your-matchine, host = target = aarch64
# Run-time dependencies. build = your-machine, host = target = aarch64
# Usually are libraries to be linked.
buildInputs = [ openssl ];
Expand All @@ -57,4 +59,34 @@ mkShell {
```

For more details about these different kinds of dependencies,
see also [Nix Wiki - Cross Compiling](https://nixos.wiki/wiki/Cross_Compiling#How_to_specify_dependencies).
see also [Nix Wiki - Cross Compiling][wiki-cross]

## Flakes and `nix develop` development environment

Unfortunately flake output layout does not natively support cross-compilation
(see [NixOS/nix#5157][flake-cross-issue]). We provide `mkRustBin` to allow
construction of `rust-bin` on an existing nixpkgs to leverage flake benefits of
not-importing nixpkgs again, with the cost of re-instantiating `rust-bin` for
each host-target tuples.

Pass the spliced packages for your cross-compilation target to `mkRustBin` to
get corresponding compiler toolchains for them.

```nix
let
pkgs = nixpkgs.legacyPackages.x86_64-linux.pkgsCross.aarch64-multiplatform;
rust-bin = rust-overlay.lib.mkRustBin { } pkgs.buildPackages;
in pkgs.mkShell {
nativeBuildInputs = [ rust-bin.stable.latest.minimal ];
}
```

The full example can be seen in
[`examples/cross-aarch64/flake.nix`](../examples/cross-aarch64/flake.nix).
To try it,
1. `cd` into `example/cross-aarch64`.
2. `nix develop` to enter the development environment.
3. `make run` to build and run the program in an emulator.

[wiki-cross]: https://wiki.nixos.org/wiki/Cross_Compiling#How_to_specify_dependencies
[flake-cross-issue]: https://github.com/NixOS/nix/issues/5157
Loading

0 comments on commit 711ff40

Please sign in to comment.