Skip to content

Commit

Permalink
Merge pull request #352 from Kranzes/improvements
Browse files Browse the repository at this point in the history
Get rid of no longer needed workarounds, improve cross compilation support, filter src for all nix versions
  • Loading branch information
Kranzes authored Jun 16, 2024
2 parents b4de66a + 4381f1e commit 4fcd1d9
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 99 deletions.
29 changes: 10 additions & 19 deletions crate2nix/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ rec {
{
name = "libc";
packageId = "libc";
target = { target, features }: (pkgs.rust.lib.toRustTarget stdenv.hostPlatform == "aarch64-linux-android");
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "aarch64-linux-android");
}
{
name = "libc";
Expand Down Expand Up @@ -470,11 +470,7 @@ rec {
requiredFeatures = [ ];
}
];
# We can't filter paths with references in Nix 2.4
# See https://github.com/NixOS/nix/issues/5410
src = if ((lib.versionOlder builtins.nixVersion "2.4pre20211007") || (lib.versionOlder "2.5" builtins.nixVersion ))
then lib.cleanSourceWith { filter = sourceFilter; src = ./.; }
else ./.;
src = lib.cleanSourceWith { filter = sourceFilter; src = ./.; };
authors = [
"Peter Kolloch <[email protected]>"
];
Expand Down Expand Up @@ -2695,12 +2691,12 @@ rec {
{
name = "winapi-i686-pc-windows-gnu";
packageId = "winapi-i686-pc-windows-gnu";
target = { target, features }: (pkgs.rust.lib.toRustTarget stdenv.hostPlatform == "i686-pc-windows-gnu");
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "i686-pc-windows-gnu");
}
{
name = "winapi-x86_64-pc-windows-gnu";
packageId = "winapi-x86_64-pc-windows-gnu";
target = { target, features }: (pkgs.rust.lib.toRustTarget stdenv.hostPlatform == "x86_64-pc-windows-gnu");
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "x86_64-pc-windows-gnu");
}
];
features = {
Expand Down Expand Up @@ -2783,14 +2779,11 @@ rec {
fuchsia = true;
test = false;

/* We are choosing an arbitrary rust version to grab `lib` from,
which is unfortunate, but `lib` has been version-agnostic the
whole time so this is good enough for now.
*/
os = pkgs.rust.lib.toTargetOs platform;
arch = pkgs.rust.lib.toTargetArch platform;
family = pkgs.rust.lib.toTargetFamily platform;
vendor = pkgs.rust.lib.toTargetVendor platform;
inherit (platform.rust.platform)
arch
os
vendor;
family = platform.rust.platform.target-family;
env = "gnu";
endian =
if platform.parsed.cpu.significantByte.name == "littleEndian"
Expand Down Expand Up @@ -3033,7 +3026,7 @@ rec {
let
self = {
crates = lib.mapAttrs (packageId: value: buildByPackageIdForPkgsImpl self pkgs packageId) crateConfigs;
target = makeTarget pkgs.stdenv.hostPlatform;
target = makeTarget stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
};
in
Expand Down Expand Up @@ -3108,8 +3101,6 @@ rec {
buildRustCrateForPkgsFunc pkgs
(
crateConfig // {
# https://github.com/NixOS/nixpkgs/issues/218712
dontStrip = stdenv.hostPlatform.isDarwin;
src = crateConfig.src or (
pkgs.fetchurl rec {
name = "${crateConfig.crateName}-${crateConfig.version}.tar.gz";
Expand Down
8 changes: 1 addition & 7 deletions crate2nix/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,8 @@ fn cfg_to_nix_expr_filter(
})?;
Ok(tera::Value::String(cfg_to_nix_expr(&expr)))
} else {
// `lib.toRustTarget` has existed since Nixpkgs 21.05. That is
// hopefully good enough.
//
// We are choosing an arbitrary rust version to grab `lib` from,
// which is unfortunate, but `lib` has been version-agnostic the
// whole time so this is good enough for now.
let condition = format!(
"(pkgs.rust.lib.toRustTarget stdenv.hostPlatform == {})",
"(stdenv.hostPlatform.rust.rustcTarget == {})",
escape_nix_string(key)
);
Ok(tera::Value::String(condition))
Expand Down
6 changes: 1 addition & 5 deletions crate2nix/templates/Cargo.nix.tera
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,7 @@ rec {
{%- elif crate.source.Nix.file.package %}
src = pkgs.callPackage {{crate.source.Nix.file.package | safe}} {};
{%- elif crate.source.LocalDirectory.path %}
# We can't filter paths with references in Nix 2.4
# See https://github.com/NixOS/nix/issues/5410
src = if ((lib.versionOlder builtins.nixVersion "2.4pre20211007") || (lib.versionOlder "2.5" builtins.nixVersion ))
then lib.cleanSourceWith { filter = sourceFilter; src = {{crate.source.LocalDirectory.path | safe}}; }
else {{crate.source.LocalDirectory.path | safe}};
src = lib.cleanSourceWith { filter = sourceFilter; src = {{crate.source.LocalDirectory.path | safe}}; };
{%- elif crate.source.Git %}
workspace_member = null;
src = pkgs.fetchgit {
Expand Down
5 changes: 2 additions & 3 deletions crate2nix/templates/crate2nix-sources.nix.tera
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
, crate2nixJson ? ./crate2nix.json
}:

# unsafeDiscardStringContext is a workaround for https://github.com/NixOS/nix/issues/6647
let config = builtins.fromJSON (builtins.unsafeDiscardStringContext (builtins.readFile crate2nixJson));
let config = lib.importJSON crate2nixJson;
sources = config.sources or (builtins.throw "no sources in ${crate2nixJson}");
in
rec {
Expand Down Expand Up @@ -108,4 +107,4 @@ rec {
tar -xzf ${archive} --strip-components=1 -C $out
'';
};
}
}
17 changes: 6 additions & 11 deletions crate2nix/templates/nix/crate2nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ rec {
fuchsia = true;
test = false;

/* We are choosing an arbitrary rust version to grab `lib` from,
which is unfortunate, but `lib` has been version-agnostic the
whole time so this is good enough for now.
*/
os = pkgs.rust.lib.toTargetOs platform;
arch = pkgs.rust.lib.toTargetArch platform;
family = pkgs.rust.lib.toTargetFamily platform;
vendor = pkgs.rust.lib.toTargetVendor platform;
inherit (platform.rust.platform)
arch
os
vendor;
family = platform.rust.platform.target-family;
env = "gnu";
endian =
if platform.parsed.cpu.significantByte.name == "littleEndian"
Expand Down Expand Up @@ -277,7 +274,7 @@ rec {
let
self = {
crates = lib.mapAttrs (packageId: value: buildByPackageIdForPkgsImpl self pkgs packageId) crateConfigs;
target = makeTarget pkgs.stdenv.hostPlatform;
target = makeTarget stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
};
in
Expand Down Expand Up @@ -352,8 +349,6 @@ rec {
buildRustCrateForPkgsFunc pkgs
(
crateConfig // {
# https://github.com/NixOS/nixpkgs/issues/218712
dontStrip = stdenv.hostPlatform.isDarwin;
src = crateConfig.src or (
pkgs.fetchurl rec {
name = "${crateConfig.crateName}-${crateConfig.version}.tar.gz";
Expand Down
27 changes: 9 additions & 18 deletions sample_projects/bin_with_git_submodule_dep/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ rec {
requiredFeatures = [ ];
}
];
# We can't filter paths with references in Nix 2.4
# See https://github.com/NixOS/nix/issues/5410
src = if ((lib.versionOlder builtins.nixVersion "2.4pre20211007") || (lib.versionOlder "2.5" builtins.nixVersion ))
then lib.cleanSourceWith { filter = sourceFilter; src = ./.; }
else ./.;
src = lib.cleanSourceWith { filter = sourceFilter; src = ./.; };
authors = [
"Phillip Cloud <[email protected]>"
];
Expand Down Expand Up @@ -1257,7 +1253,7 @@ rec {
{
name = "windows_aarch64_gnullvm";
packageId = "windows_aarch64_gnullvm";
target = { target, features }: (pkgs.rust.lib.toRustTarget stdenv.hostPlatform == "aarch64-pc-windows-gnullvm");
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "aarch64-pc-windows-gnullvm");
}
{
name = "windows_aarch64_msvc";
Expand All @@ -1282,7 +1278,7 @@ rec {
{
name = "windows_x86_64_gnullvm";
packageId = "windows_x86_64_gnullvm";
target = { target, features }: (pkgs.rust.lib.toRustTarget stdenv.hostPlatform == "x86_64-pc-windows-gnullvm");
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "x86_64-pc-windows-gnullvm");
}
{
name = "windows_x86_64_msvc";
Expand Down Expand Up @@ -1377,14 +1373,11 @@ rec {
fuchsia = true;
test = false;

/* We are choosing an arbitrary rust version to grab `lib` from,
which is unfortunate, but `lib` has been version-agnostic the
whole time so this is good enough for now.
*/
os = pkgs.rust.lib.toTargetOs platform;
arch = pkgs.rust.lib.toTargetArch platform;
family = pkgs.rust.lib.toTargetFamily platform;
vendor = pkgs.rust.lib.toTargetVendor platform;
inherit (platform.rust.platform)
arch
os
vendor;
family = platform.rust.platform.target-family;
env = "gnu";
endian =
if platform.parsed.cpu.significantByte.name == "littleEndian"
Expand Down Expand Up @@ -1627,7 +1620,7 @@ rec {
let
self = {
crates = lib.mapAttrs (packageId: value: buildByPackageIdForPkgsImpl self pkgs packageId) crateConfigs;
target = makeTarget pkgs.stdenv.hostPlatform;
target = makeTarget stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
};
in
Expand Down Expand Up @@ -1702,8 +1695,6 @@ rec {
buildRustCrateForPkgsFunc pkgs
(
crateConfig // {
# https://github.com/NixOS/nixpkgs/issues/218712
dontStrip = stdenv.hostPlatform.isDarwin;
src = crateConfig.src or (
pkgs.fetchurl rec {
name = "${crateConfig.crateName}-${crateConfig.version}.tar.gz";
Expand Down
27 changes: 9 additions & 18 deletions sample_projects/codegen/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,7 @@ rec {
requiredFeatures = [ ];
}
];
# We can't filter paths with references in Nix 2.4
# See https://github.com/NixOS/nix/issues/5410
src = if ((lib.versionOlder builtins.nixVersion "2.4pre20211007") || (lib.versionOlder "2.5" builtins.nixVersion ))
then lib.cleanSourceWith { filter = sourceFilter; src = ./.; }
else ./.;
src = lib.cleanSourceWith { filter = sourceFilter; src = ./.; };
authors = [
"Peter Kolloch <[email protected]>"
];
Expand Down Expand Up @@ -492,12 +488,12 @@ rec {
{
name = "winapi-i686-pc-windows-gnu";
packageId = "winapi-i686-pc-windows-gnu";
target = { target, features }: (pkgs.rust.lib.toRustTarget stdenv.hostPlatform == "i686-pc-windows-gnu");
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "i686-pc-windows-gnu");
}
{
name = "winapi-x86_64-pc-windows-gnu";
packageId = "winapi-x86_64-pc-windows-gnu";
target = { target, features }: (pkgs.rust.lib.toRustTarget stdenv.hostPlatform == "x86_64-pc-windows-gnu");
target = { target, features }: (stdenv.hostPlatform.rust.rustcTarget == "x86_64-pc-windows-gnu");
}
];
features = {
Expand Down Expand Up @@ -552,14 +548,11 @@ rec {
fuchsia = true;
test = false;

/* We are choosing an arbitrary rust version to grab `lib` from,
which is unfortunate, but `lib` has been version-agnostic the
whole time so this is good enough for now.
*/
os = pkgs.rust.lib.toTargetOs platform;
arch = pkgs.rust.lib.toTargetArch platform;
family = pkgs.rust.lib.toTargetFamily platform;
vendor = pkgs.rust.lib.toTargetVendor platform;
inherit (platform.rust.platform)
arch
os
vendor;
family = platform.rust.platform.target-family;
env = "gnu";
endian =
if platform.parsed.cpu.significantByte.name == "littleEndian"
Expand Down Expand Up @@ -802,7 +795,7 @@ rec {
let
self = {
crates = lib.mapAttrs (packageId: value: buildByPackageIdForPkgsImpl self pkgs packageId) crateConfigs;
target = makeTarget pkgs.stdenv.hostPlatform;
target = makeTarget stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
};
in
Expand Down Expand Up @@ -877,8 +870,6 @@ rec {
buildRustCrateForPkgsFunc pkgs
(
crateConfig // {
# https://github.com/NixOS/nixpkgs/issues/218712
dontStrip = stdenv.hostPlatform.isDarwin;
src = crateConfig.src or (
pkgs.fetchurl rec {
name = "${crateConfig.crateName}-${crateConfig.version}.tar.gz";
Expand Down
23 changes: 7 additions & 16 deletions sample_projects/sub_dir_crates/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ rec {
requiredFeatures = [ ];
}
];
# We can't filter paths with references in Nix 2.4
# See https://github.com/NixOS/nix/issues/5410
src = if ((lib.versionOlder builtins.nixVersion "2.4pre20211007") || (lib.versionOlder "2.5" builtins.nixVersion ))
then lib.cleanSourceWith { filter = sourceFilter; src = ./.; }
else ./.;
src = lib.cleanSourceWith { filter = sourceFilter; src = ./.; };
authors = [
"Peter Kolloch <[email protected]>"
];
Expand Down Expand Up @@ -161,14 +157,11 @@ rec {
fuchsia = true;
test = false;

/* We are choosing an arbitrary rust version to grab `lib` from,
which is unfortunate, but `lib` has been version-agnostic the
whole time so this is good enough for now.
*/
os = pkgs.rust.lib.toTargetOs platform;
arch = pkgs.rust.lib.toTargetArch platform;
family = pkgs.rust.lib.toTargetFamily platform;
vendor = pkgs.rust.lib.toTargetVendor platform;
inherit (platform.rust.platform)
arch
os
vendor;
family = platform.rust.platform.target-family;
env = "gnu";
endian =
if platform.parsed.cpu.significantByte.name == "littleEndian"
Expand Down Expand Up @@ -411,7 +404,7 @@ rec {
let
self = {
crates = lib.mapAttrs (packageId: value: buildByPackageIdForPkgsImpl self pkgs packageId) crateConfigs;
target = makeTarget pkgs.stdenv.hostPlatform;
target = makeTarget stdenv.hostPlatform;
build = mkBuiltByPackageIdByPkgs pkgs.buildPackages;
};
in
Expand Down Expand Up @@ -486,8 +479,6 @@ rec {
buildRustCrateForPkgsFunc pkgs
(
crateConfig // {
# https://github.com/NixOS/nixpkgs/issues/218712
dontStrip = stdenv.hostPlatform.isDarwin;
src = crateConfig.src or (
pkgs.fetchurl rec {
name = "${crateConfig.crateName}-${crateConfig.version}.tar.gz";
Expand Down
3 changes: 1 addition & 2 deletions tools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ rec {

locked =
let
# unsafeDiscardStringContext is a workaround for https://github.com/NixOS/nix/issues/6647
parseFile = cargoLock: builtins.fromTOML (builtins.unsafeDiscardStringContext (builtins.readFile cargoLock));
parseFile = cargoLock: lib.importTOML cargoLock;
allParsedFiles = builtins.map parseFile lockFiles;
merge = merged: lock:
{
Expand Down

0 comments on commit 4fcd1d9

Please sign in to comment.