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

Non broken fetchcargo #46362

Merged
merged 7 commits into from Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions pkgs/applications/misc/alacritty/default.nix
@@ -1,6 +1,6 @@
{ stdenv,
lib,
fetchgit,
fetchFromGitHub,
rustPlatform,
cmake,
makeWrapper,
Expand Down Expand Up @@ -51,18 +51,16 @@ let
];
in buildRustPackage rec {
name = "alacritty-unstable-${version}";
version = "2018-08-30";

# At the moment we cannot handle git dependencies in buildRustPackage.
# This fork only replaces rust-fontconfig/libfontconfig with a git submodules.
src = fetchgit {
url = https://github.com/Mic92/alacritty.git;
rev = "rev-${version}";
sha256 = "0izvg7dwwb763jc6gnmn47i5zrkxvmh3vssn6vzrrmqhd4j3msmf";
fetchSubmodules = true;
version = "2018-08-05";

src = fetchFromGitHub {
Copy link
Member

Choose a reason for hiding this comment

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

I added this as a test, because it makes use of the new .cargo/config

owner = "jwilm";
repo = "alacritty";
rev = "1adb5cb7fc05054197aa08e0d1fa957df94888ab";
sha256 = "06rc7dy1vn59lc5hjh953h9lh0f39c0n0jmrz472mrya722fl2ab";
};

cargoSha256 = "1ijgkwv9ij4haig1h6n2b9xbhp5vahy9vp1sx72wxaaj9476msjx";
cargoSha256 = "0ms0248bb2lgbzcqks6i0qhn1gaiim3jf1kl17qw52c8an3rc652";

nativeBuildInputs = [
cmake
Expand Down
41 changes: 41 additions & 0 deletions pkgs/build-support/rust/cargo-vendor-normalise.py
@@ -0,0 +1,41 @@
#!/usr/bin/env python

import sys

import toml


def quote(s: str) -> str:
escaped = s.replace('"', r"\"").replace("\n", r"\n").replace("\\", "\\\\")
return '"{}"'.format(escaped)


def main() -> None:
Copy link
Member

Choose a reason for hiding this comment

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

I added type checking here and reformatted the code with black + some minor readability improvements.

data = toml.load(sys.stdin)

assert list(data.keys()) == ["source"]

# this value is non deterministic
data["source"]["vendored-sources"]["directory"] = "@vendor@"

lines = []
inner = data["source"]
for source, attrs in sorted(inner.items()):
lines.append("[source.{}]".format(quote(source)))
if source == "vendored-sources":
lines.append('"directory" = "@vendor@"\n')
else:
for key, value in sorted(attrs.items()):
attr = "{} = {}".format(quote(key), quote(value))
lines.append(attr)
lines.append("")

result = "\n".join(lines)
real = toml.loads(result)
assert real == data, "output = {} while input = {}".format(real, data)

print(result)


if __name__ == "__main__":
main()
18 changes: 8 additions & 10 deletions pkgs/build-support/rust/default.nix
@@ -1,7 +1,7 @@
{ stdenv, cacert, git, rust, cargo-vendor }:
{ stdenv, cacert, git, rust, cargo-vendor, python3 }:
let
fetchcargo = import ./fetchcargo.nix {
inherit stdenv cacert git rust cargo-vendor;
inherit stdenv cacert git rust cargo-vendor python3;
};
in
{ name, cargoSha256 ? "unset"
Expand Down Expand Up @@ -61,14 +61,12 @@ in stdenv.mkDerivation (args // {
${setupVendorDir}

mkdir .cargo
cat >.cargo/config <<-EOF
[source.crates-io]
registry = 'https://github.com/rust-lang/crates.io-index'
replace-with = 'vendored-sources'

[source.vendored-sources]
directory = '$(pwd)/$cargoDepsCopy'
EOF
config="$(pwd)/$cargoDepsCopy/.cargo/config";
if [[ ! -e $config ]]; then
config=${./fetchcargo-default-config.toml};
fi;
substitute $config .cargo/config \
--subst-var-by vendor "$(pwd)/$cargoDepsCopy"

unset cargoDepsCopy

Expand Down
7 changes: 7 additions & 0 deletions pkgs/build-support/rust/fetchcargo-default-config.toml
@@ -0,0 +1,7 @@
[source."crates-io"]
"replace-with" = "vendored-sources"

[source."vendored-sources"]
"directory" = "@vendor@"


35 changes: 30 additions & 5 deletions pkgs/build-support/rust/fetchcargo.nix
@@ -1,8 +1,26 @@
{ stdenv, cacert, git, rust, cargo-vendor }:
{ stdenv, cacert, git, rust, cargo-vendor, python3 }:
let cargo-vendor-normalise = stdenv.mkDerivation {
name = "cargo-vendor-normalise";
src = ./cargo-vendor-normalise.py;
nativeBuildInputs = [ python3.pkgs.wrapPython ];
unpackPhase = ":";
installPhase = "install -D $src $out/bin/cargo-vendor-normalise";
pythonPath = [ python3.pkgs.toml ];
postFixup = "wrapPythonPrograms";
doInstallCheck = true;
installCheckPhase = ''
# check that ./fetchcargo-default-config.toml is a fix point
reference=${./fetchcargo-default-config.toml}
< $reference $out/bin/cargo-vendor-normalise > test;
cmp test $reference
'';
preferLocalBuild = true;
};
in
{ name ? "cargo-deps", src, srcs, patches, sourceRoot, sha256, cargoUpdateHook ? "" }:
stdenv.mkDerivation {
name = "${name}-vendor";
nativeBuildInputs = [ cacert cargo-vendor git rust.cargo ];
nativeBuildInputs = [ cacert cargo-vendor git cargo-vendor-normalise rust.cargo ];
inherit src srcs patches sourceRoot;

phases = "unpackPhase patchPhase installPhase";
Expand All @@ -23,9 +41,16 @@ stdenv.mkDerivation {

${cargoUpdateHook}

cargo vendor

cp -ar vendor $out
mkdir -p $out
cargo vendor $out | cargo-vendor-normalise > config
# fetchcargo used to never keep the config output by cargo vendor
# and instead hardcode the config in ./fetchcargo-default-config.toml.
# This broke on packages needing git dependencies, so now we keep the config.
# But not to break old cargoSha256, if the previous behavior was enough,
# we don't store the config.
if ! cmp config ${./fetchcargo-default-config.toml} > /dev/null; then
install -Dt $out/.cargo config;
fi;
'';

outputHashAlgo = "sha256";
Expand Down