From e6797b93c3c5d46a7e2771127c05433661eba97d Mon Sep 17 00:00:00 2001 From: Ilan Joselevich Date: Mon, 10 Jun 2024 22:09:48 +0300 Subject: [PATCH 1/4] rust.lib -> stdenv.hostPlatform.rust context: https://github.com/NixOS/nixpkgs/commit/e3e57b8f1885bf1dc4787728479711f46a4171cc https://github.com/NixOS/nixpkgs/pull/271707 --- crate2nix/Cargo.nix | 21 ++++++++----------- crate2nix/src/render.rs | 8 +------ crate2nix/templates/nix/crate2nix/default.nix | 15 ++++++------- .../bin_with_git_submodule_dep/Cargo.nix | 19 +++++++---------- sample_projects/codegen/Cargo.nix | 19 +++++++---------- sample_projects/sub_dir_crates/Cargo.nix | 15 ++++++------- 6 files changed, 38 insertions(+), 59 deletions(-) diff --git a/crate2nix/Cargo.nix b/crate2nix/Cargo.nix index a5b7ebc5..87bf6f2d 100644 --- a/crate2nix/Cargo.nix +++ b/crate2nix/Cargo.nix @@ -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"; @@ -2695,12 +2695,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 = { @@ -2783,14 +2783,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" @@ -3033,7 +3030,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 diff --git a/crate2nix/src/render.rs b/crate2nix/src/render.rs index a269fc7e..bb878d3f 100644 --- a/crate2nix/src/render.rs +++ b/crate2nix/src/render.rs @@ -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)) diff --git a/crate2nix/templates/nix/crate2nix/default.nix b/crate2nix/templates/nix/crate2nix/default.nix index 5c937924..95d3730f 100644 --- a/crate2nix/templates/nix/crate2nix/default.nix +++ b/crate2nix/templates/nix/crate2nix/default.nix @@ -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" @@ -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 diff --git a/sample_projects/bin_with_git_submodule_dep/Cargo.nix b/sample_projects/bin_with_git_submodule_dep/Cargo.nix index 8212fe3b..4d4f28c6 100644 --- a/sample_projects/bin_with_git_submodule_dep/Cargo.nix +++ b/sample_projects/bin_with_git_submodule_dep/Cargo.nix @@ -1257,7 +1257,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"; @@ -1282,7 +1282,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"; @@ -1377,14 +1377,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" @@ -1627,7 +1624,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 diff --git a/sample_projects/codegen/Cargo.nix b/sample_projects/codegen/Cargo.nix index c3351881..d127bc47 100644 --- a/sample_projects/codegen/Cargo.nix +++ b/sample_projects/codegen/Cargo.nix @@ -492,12 +492,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 = { @@ -552,14 +552,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" @@ -802,7 +799,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 diff --git a/sample_projects/sub_dir_crates/Cargo.nix b/sample_projects/sub_dir_crates/Cargo.nix index deeec281..183c6d31 100644 --- a/sample_projects/sub_dir_crates/Cargo.nix +++ b/sample_projects/sub_dir_crates/Cargo.nix @@ -161,14 +161,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" @@ -411,7 +408,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 From dd8003b545c3561b53f103d4c74ba65af18b6612 Mon Sep 17 00:00:00 2001 From: Ilan Joselevich Date: Tue, 11 Jun 2024 18:33:31 +0300 Subject: [PATCH 2/4] always filter src using filterSource Previously it was not possible, but it is now with https://github.com/NixOS/nix/pull/5494 --- crate2nix/Cargo.nix | 6 +----- crate2nix/templates/Cargo.nix.tera | 6 +----- sample_projects/bin_with_git_submodule_dep/Cargo.nix | 6 +----- sample_projects/codegen/Cargo.nix | 6 +----- sample_projects/sub_dir_crates/Cargo.nix | 6 +----- 5 files changed, 5 insertions(+), 25 deletions(-) diff --git a/crate2nix/Cargo.nix b/crate2nix/Cargo.nix index 87bf6f2d..a8b05a14 100644 --- a/crate2nix/Cargo.nix +++ b/crate2nix/Cargo.nix @@ -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 " ]; diff --git a/crate2nix/templates/Cargo.nix.tera b/crate2nix/templates/Cargo.nix.tera index 50c9f8e0..8c89935e 100644 --- a/crate2nix/templates/Cargo.nix.tera +++ b/crate2nix/templates/Cargo.nix.tera @@ -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 { diff --git a/sample_projects/bin_with_git_submodule_dep/Cargo.nix b/sample_projects/bin_with_git_submodule_dep/Cargo.nix index 4d4f28c6..a1c452ea 100644 --- a/sample_projects/bin_with_git_submodule_dep/Cargo.nix +++ b/sample_projects/bin_with_git_submodule_dep/Cargo.nix @@ -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 " ]; diff --git a/sample_projects/codegen/Cargo.nix b/sample_projects/codegen/Cargo.nix index d127bc47..783f3624 100644 --- a/sample_projects/codegen/Cargo.nix +++ b/sample_projects/codegen/Cargo.nix @@ -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 " ]; diff --git a/sample_projects/sub_dir_crates/Cargo.nix b/sample_projects/sub_dir_crates/Cargo.nix index 183c6d31..161c6727 100644 --- a/sample_projects/sub_dir_crates/Cargo.nix +++ b/sample_projects/sub_dir_crates/Cargo.nix @@ -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 " ]; From 0209f258cda8a9972a785e26d92fb477ce4d1b0e Mon Sep 17 00:00:00 2001 From: Ilan Joselevich Date: Tue, 11 Jun 2024 18:14:06 +0300 Subject: [PATCH 3/4] Get rid of dontStrip for Darwin as it's no longer needed Fixed in https://github.com/NixOS/nixpkgs/pull/255900 --- crate2nix/Cargo.nix | 2 -- crate2nix/templates/nix/crate2nix/default.nix | 2 -- sample_projects/bin_with_git_submodule_dep/Cargo.nix | 2 -- sample_projects/codegen/Cargo.nix | 2 -- sample_projects/sub_dir_crates/Cargo.nix | 2 -- 5 files changed, 10 deletions(-) diff --git a/crate2nix/Cargo.nix b/crate2nix/Cargo.nix index a8b05a14..b6b855d3 100644 --- a/crate2nix/Cargo.nix +++ b/crate2nix/Cargo.nix @@ -3101,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"; diff --git a/crate2nix/templates/nix/crate2nix/default.nix b/crate2nix/templates/nix/crate2nix/default.nix index 95d3730f..c53925e7 100644 --- a/crate2nix/templates/nix/crate2nix/default.nix +++ b/crate2nix/templates/nix/crate2nix/default.nix @@ -349,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"; diff --git a/sample_projects/bin_with_git_submodule_dep/Cargo.nix b/sample_projects/bin_with_git_submodule_dep/Cargo.nix index a1c452ea..1764a256 100644 --- a/sample_projects/bin_with_git_submodule_dep/Cargo.nix +++ b/sample_projects/bin_with_git_submodule_dep/Cargo.nix @@ -1695,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"; diff --git a/sample_projects/codegen/Cargo.nix b/sample_projects/codegen/Cargo.nix index 783f3624..e13c9c7e 100644 --- a/sample_projects/codegen/Cargo.nix +++ b/sample_projects/codegen/Cargo.nix @@ -870,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"; diff --git a/sample_projects/sub_dir_crates/Cargo.nix b/sample_projects/sub_dir_crates/Cargo.nix index 161c6727..5ae19586 100644 --- a/sample_projects/sub_dir_crates/Cargo.nix +++ b/sample_projects/sub_dir_crates/Cargo.nix @@ -479,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"; From 4381f1e90e7031a992be490c9ac81f0888a49adf Mon Sep 17 00:00:00 2001 From: Ilan Joselevich Date: Tue, 11 Jun 2024 18:42:23 +0300 Subject: [PATCH 4/4] Remove no longer needed unsafeDiscardStringContext workaround Fixed in: https://github.com/NixOS/nix/pull/7260 --- crate2nix/templates/crate2nix-sources.nix.tera | 5 ++--- tools.nix | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/crate2nix/templates/crate2nix-sources.nix.tera b/crate2nix/templates/crate2nix-sources.nix.tera index 613d103c..4f3328e8 100644 --- a/crate2nix/templates/crate2nix-sources.nix.tera +++ b/crate2nix/templates/crate2nix-sources.nix.tera @@ -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 { @@ -108,4 +107,4 @@ rec { tar -xzf ${archive} --strip-components=1 -C $out ''; }; -} \ No newline at end of file +} diff --git a/tools.nix b/tools.nix index 46fdc23a..3a867d4d 100644 --- a/tools.nix +++ b/tools.nix @@ -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: {