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

Git workspaces #298

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2,763 changes: 2,634 additions & 129 deletions crate2nix/Cargo.lock

Large diffs are not rendered by default.

10,482 changes: 9,644 additions & 838 deletions crate2nix/Cargo.nix

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion crate2nix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ resolver = "2"

[dependencies]
anyhow = "1.0.28"
cargo = "0.72"
cargo_metadata = "0.14"
cargo-platform = "0.1"
hex = "0.4"
Expand All @@ -24,7 +25,7 @@ semver = { version = "1", features = ["serde"] }
serde = { version = "1.0.107", features = ["derive"] }
serde_json = { version = "1.0.59", features = ["unbounded_depth"] }
tera = { version = "1", default-features = false }
toml = "0.5"
toml = "0.8"
url = "1"
url_serde = "0.2"

Expand Down
31 changes: 31 additions & 0 deletions crate2nix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ pub enum Opt {
)]
output: PathBuf,
},

#[structopt(
name = "resolve-manifest",
about = "Resolve fields inherited from a workspace, so that the manifest can be processed stand-alone."
)]
ResolveManifest {
#[structopt(
short = "f",
long = "cargo-toml",
parse(from_os_str),
help = "The path to the Cargo.toml of the project."
)]
cargo_toml: PathBuf,
},
}

#[derive(Debug, StructOpt, Deserialize, Serialize)]
Expand Down Expand Up @@ -464,7 +478,24 @@ fn main() -> anyhow::Result<()> {
} => {
command.execute(&crate2nix_json)?;
}
Opt::ResolveManifest { cargo_toml } => {
let manifest = resolve_manifest(&cargo_toml)?;
let toml = toml::to_string_pretty(manifest.original())?;
println!("{toml}");
}
}

Ok(())
}

fn resolve_manifest(cargo_toml: &Path) -> cargo::CargoResult<cargo::core::Manifest> {
use cargo::core::SourceId;

let full_path = cargo_toml.canonicalize()?;
let source_id = SourceId::for_path(&full_path)?;

let config = cargo::Config::default()?;
let (pkg, _paths) = cargo::ops::read_package(&full_path, source_id, &config)?;

Ok(pkg.manifest().clone())
}
4 changes: 4 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ let
cssparser-macros = attrs: assert builtins.trace "cssparser" true;{
buildInputs = lib.optionals stdenv.isDarwin [ darwin.apple_sdk.frameworks.Security ];
};
libgit2-sys = old: {
nativeBuildInputs = (old.nativeBuildInputs or []) ++ pkgs.libgit2.nativeBuildInputs;
buildInputs = (old.buildInputs or []) ++ pkgs.libgit2.buildInputs;
};
};
};
set_templates = if release then "" else "--set TEMPLATES_DIR ${./crate2nix/templates}";
Expand Down
3 changes: 2 additions & 1 deletion nix/dependencies.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
binutils
nixpkgs-fmt jq
niv
nix_2_3
pkgconfig
openssl
git
utillinux
cacert
Expand Down
6 changes: 3 additions & 3 deletions nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
"homepage": "https://github.com/NixOS/nixpkgs",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "d9f759f2ea8d265d974a6e1259bd510ac5844c5d",
"sha256": "1r0jjdgx206vgmjqry82qv01cbmafiqwcmqxlnzmhsbzkcgrlnzh",
"rev": "3a2786eea085f040a66ecde1bc3ddc7099f6dbeb",
"sha256": "19rj0v1y2b711mdy384fvmcplmfwns8hixvzjrpkyfxcsw1pwwll",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/d9f759f2ea8d265d974a6e1259bd510ac5844c5d.tar.gz",
"url": "https://github.com/NixOS/nixpkgs/archive/3a2786eea085f040a66ecde1bc3ddc7099f6dbeb.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs-mozilla": {
Expand Down
9 changes: 4 additions & 5 deletions regenerate_cargo_nix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,14 @@ else
echo "Skipping because of --no-cargo-build"
fi

noisily nix run --arg release false $NIX_OPTIONS -c crate2nix generate -n ../nix/nixpkgs.nix \
crate2nix=$(noisily nix-build --arg release false $NIX_OPTIONS)/bin/crate2nix
noisily "$crate2nix" generate -n ../nix/nixpkgs.nix \
-f ./crate2nix/Cargo.toml -o ./crate2nix/Cargo.nix || \
{ echo "Regeneration of ./Cargo.nix failed." >&2 ; exit 1; }

noisily nix build -L --arg release false $NIX_OPTIONS
crate2nix=$(noisily nix-build --arg release false $NIX_OPTIONS)/bin/crate2nix

crate2nix="$(pwd)"/result/bin/crate2nix

nix eval --json -f ./tests.nix buildTestConfigs | \
nix-instantiate tests.nix --eval --strict --json -A buildTestConfigs | \
jq -r .[].pregeneratedBuild | \
while read cargo_nix; do
if [ "$cargo_nix" = "null" ]; then
Expand Down
44 changes: 37 additions & 7 deletions tools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
, strictDeprecation ? true
}:
let
cargoNix = pkgs.callPackage ./crate2nix/Cargo.nix { inherit strictDeprecation; };
defaultCrateOverrides = pkgs.defaultCrateOverrides // {
libgit2-sys = old: {
nativeBuildInputs = (old.nativeBuildInputs or []) ++ pkgs.libgit2.nativeBuildInputs;
buildInputs = (old.buildInputs or []) ++ pkgs.libgit2.buildInputs;
};
};

cargoNix = pkgs.callPackage ./crate2nix/Cargo.nix { inherit strictDeprecation defaultCrateOverrides; };
crate2nix = cargoNix.rootCrate.build;
in
rec {
Expand Down Expand Up @@ -67,12 +74,12 @@ rec {

crate2nix_options=""
if [ -r ./${cargoToml} ]; then
create2nix_options+=" -f ./${cargoToml}"
crate2nix_options+=" -f ./${cargoToml}"
fi

if test -r "./crate2nix.json" ; then
cp "./crate2nix.json" "$out/crate2nix.json"
create2nix_options+=" -c $out/crate2nix.json"
crate2nix_options+=" -c $out/crate2nix.json"
fi

if test -r "${src}/crate2nix-sources" ; then
Expand All @@ -82,7 +89,7 @@ rec {
set -x

crate2nix generate \
$create2nix_options \
$crate2nix_options \
-o "Cargo-generated.nix" \
-h "$crate_hashes" \
${lib.escapeShellArgs additionalCargoNixArgs} || {
Expand Down Expand Up @@ -349,7 +356,9 @@ rec {
replace-with = "vendored-sources"
'';
gitSources = packagesByType."git" or [ ];
gitSourcesUnique = lib.unique gitSources;
uniqueBy = f:
lib.foldl' (acc: e: if lib.elem (f e) (map f acc) then acc else acc ++ [ e ]) [];
gitSourcesUnique = uniqueBy (c: c.source) gitSources;
gitSourceConfigs = builtins.map gitSourceConfig gitSourcesUnique;
gitSourceConfigsString = lib.concatStrings gitSourceConfigs;
in
Expand Down Expand Up @@ -401,14 +410,35 @@ rec {
then parsed.rev
else parsed.urlFragment;
};

rootCargo = builtins.fromTOML (builtins.readFile "${src}/Cargo.toml");
isWorkspace = rootCargo ? "workspace";
isPackage = rootCargo ? "package";
containedCrates = rootCargo.workspace.members ++ (if isPackage then ["."] else []);

getCrateNameFromPath = path: let
cargoTomlCrate = builtins.fromTOML (builtins.readFile "${src}/${path}/Cargo.toml");
in
cargoTomlCrate.package.name;

pathToExtract = if isWorkspace then
builtins.head (builtins.filter (to_filter:
(getCrateNameFromPath to_filter) == name
) containedCrates)
else
".";
in
pkgs.runCommand (lib.removeSuffix ".tar.gz" src.name) { }
''
mkdir -p $out
cp -apR ${src}/* $out
cp -apR ${src}/${pathToExtract}/* $out
cd $out

mv ./Cargo.toml ./Cargo.toml.orig
${crate2nix}/bin/crate2nix resolve-manifest --cargo-toml ${src}/${pathToExtract}/Cargo.toml > ./Cargo.toml

echo '{"package":null,"files":{}}' > $out/.cargo-checksum.json
'';

};
};
};
Expand Down