Fix crown on NixOS (#30987)

* Fix crown on NixOS

* no need to install libs, because there are none

* fix program name in usage message

* use the cargo provided by each buildPhase

* cargo update --offline can be used to reformat lockfiles

* document how to keep rust-toolchain.toml and etc/shell.nix in sync

* clarify comment about allowBuiltinFetchGit

* fix license

* clarify purpose of filterlock

* explain why crown must not use workspace dependencies
This commit is contained in:
Delan Azabani 2024-01-05 10:15:16 +08:00 committed by GitHub
parent 21d9c2cc63
commit c219204084
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 328 additions and 5 deletions

View file

@ -1,12 +1,23 @@
# This provides a shell with all the necesarry packages required to run mach and build servo
# NOTE: This does not work offline or for nix-build
with import <nixpkgs> {};
with import <nixpkgs> {
overlays = [
(import (builtins.fetchTarball {
# Bumped the channel in rust-toolchain.toml? Bump this commit too!
url = "https://github.com/oxalica/rust-overlay/archive/a0df72e106322b67e9c6e591fe870380bd0da0d5.tar.gz";
}))
];
};
let
pinnedSha = "6adf48f53d819a7b6e15672817fa1e78e5f4e84f";
pinnedNixpkgs = import (builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/${pinnedSha}.tar.gz";
url = "https://github.com/NixOS/nixpkgs/archive/6adf48f53d819a7b6e15672817fa1e78e5f4e84f.tar.gz";
}) {};
rustToolchain = rust-bin.fromRustupToolchainFile ../rust-toolchain.toml;
rustPlatform = makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
in
clangStdenv.mkDerivation rec {
name = "servo-env";
@ -33,6 +44,62 @@ clangStdenv.mkDerivation rec {
# slow as it behaves as if -j1 was passed.
# See https://github.com/servo/mozjs/issues/375
pinnedNixpkgs.gnumake
# crown needs to be in our Cargo workspace so we can test it with `mach test`. This means its
# dependency tree is listed in the main Cargo.lock, making it awkward to build with Nix because
# all of Servos dependencies get pulled into the Nix store too, wasting over 1GB of disk space.
# Filtering the lockfile to only the parts needed by crown saves space and builds faster.
(let
vendorTarball = rustPlatform.fetchCargoTarball {
src = ../support/filterlock;
hash = "sha256-/kJNDtmv2uI7Qlmpi3DMWSw88rzEJSbroO0/QrgQrSc=";
};
vendorConfig = builtins.toFile "toml" ''
[source.crates-io]
replace-with = "vendor"
[source.vendor]
directory = "vendor"
'';
# Build and run filterlock over the main Cargo.lock.
filteredLockFile = (clangStdenv.mkDerivation {
name = "lock";
buildInputs = [ rustToolchain ];
src = ../support/filterlock;
buildPhase = ''
tar xzf ${vendorTarball}
mv cargo-deps-vendor.tar.gz vendor
mkdir .cargo
cp -- ${vendorConfig} .cargo/config.toml
> $out cargo run --offline -- ${../Cargo.lock} crown
'';
});
in (rustPlatform.buildRustPackage rec {
name = "crown";
src = ../support/crown;
doCheck = false;
cargoLock = {
lockFileContents = builtins.readFile filteredLockFile;
# Needed when not filtering (filteredLockFile = ../Cargo.lock), else well get errors like
# “error: No hash was found while vendoring the git dependency blurmac-0.1.0.”
# allowBuiltinFetchGit = true;
};
# Copy the filtered lockfile, making it writable by cargo --offline.
postPatch = ''
install -m 644 ${filteredLockFile} Cargo.lock
'';
# Reformat the filtered lockfile, so that cargo --frozen wont complain
# about the lockfile being dirty.
# TODO maybe this can be avoided by using toml_edit in filterlock?
preConfigure = ''
cargo update --offline
'';
RUSTC_BOOTSTRAP = "crown";
}))
] ++ (lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.AppKit
]);