Merge pull request #324526 from Aleksanaa/nugetHash

dotnet: add SRI hash support to dotnet packages
This commit is contained in:
David McFarland 2024-07-04 20:59:56 -03:00 committed by GitHub
commit 4f7953d244
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 60 additions and 51 deletions

View File

@ -194,7 +194,7 @@ This helper has the same arguments as `buildDotnetModule`, with a few difference
* `pname` and `version` are required, and will be used to find the NuGet package of the tool
* `nugetName` can be used to override the NuGet package name that will be downloaded, if it's different from `pname`
* `nugetSha256` is the hash of the fetched NuGet package. Set this to `lib.fakeHash256` for the first build, and it will error out, giving you the proper hash. Also remember to update it during version updates (it will not error out if you just change the version while having a fetched package in `/nix/store`)
* `nugetHash` is the hash of the fetched NuGet package. `nugetSha256` is also supported, but not recommended. Set this to `lib.fakeHash` for the first build, and it will error out, giving you the proper hash. Also remember to update it during version updates (it will not error out if you just change the version while having a fetched package in `/nix/store`)
* `dotnet-runtime` is set to `dotnet-sdk` by default. When changing this, remember that .NET tools fetched from NuGet require an SDK.
Here is an example of packaging `pbm`, an unfree binary without source available:
@ -205,7 +205,7 @@ buildDotnetGlobalTool {
pname = "pbm";
version = "1.3.1";
nugetSha256 = "sha256-ZG2HFyKYhVNVYd2kRlkbAjZJq88OADe3yjxmLuxXDUo=";
nugetHash = "sha256-ZG2HFyKYhVNVYd2kRlkbAjZJq88OADe3yjxmLuxXDUo=";
meta = {
homepage = "https://cmd.petabridge.com/index.html";
@ -241,15 +241,15 @@ $ nuget-to-nix out > deps.nix
Which `nuget-to-nix` will generate an output similar to below
```nix
{ fetchNuGet }: [
(fetchNuGet { pname = "FosterFramework"; version = "0.1.15-alpha"; sha256 = "0pzsdfbsfx28xfqljcwy100xhbs6wyx0z1d5qxgmv3l60di9xkll"; })
(fetchNuGet { pname = "Microsoft.AspNetCore.App.Runtime.linux-x64"; version = "8.0.1"; sha256 = "1gjz379y61ag9whi78qxx09bwkwcznkx2mzypgycibxk61g11da1"; })
(fetchNuGet { pname = "Microsoft.NET.ILLink.Tasks"; version = "8.0.1"; sha256 = "1drbgqdcvbpisjn8mqfgba1pwb6yri80qc4mfvyczqwrcsj5k2ja"; })
(fetchNuGet { pname = "Microsoft.NETCore.App.Runtime.linux-x64"; version = "8.0.1"; sha256 = "1g5b30f4l8a1zjjr3b8pk9mcqxkxqwa86362f84646xaj4iw3a4d"; })
(fetchNuGet { pname = "SharpGLTF.Core"; version = "1.0.0-alpha0031"; sha256 = "0ln78mkhbcxqvwnf944hbgg24vbsva2jpih6q3x82d3h7rl1pkh6"; })
(fetchNuGet { pname = "SharpGLTF.Runtime"; version = "1.0.0-alpha0031"; sha256 = "0lvb3asi3v0n718qf9y367km7qpkb9wci38y880nqvifpzllw0jg"; })
(fetchNuGet { pname = "Sledge.Formats"; version = "1.2.2"; sha256 = "1y0l66m9rym0p1y4ifjlmg3j9lsmhkvbh38frh40rpvf1axn2dyh"; })
(fetchNuGet { pname = "Sledge.Formats.Map"; version = "1.1.5"; sha256 = "1bww60hv9xcyxpvkzz5q3ybafdxxkw6knhv97phvpkw84pd0jil6"; })
(fetchNuGet { pname = "System.Numerics.Vectors"; version = "4.5.0"; sha256 = "1kzrj37yzawf1b19jq0253rcs8hsq1l2q8g69d7ipnhzb0h97m59"; })
(fetchNuGet { pname = "FosterFramework"; version = "0.1.15-alpha"; hash = "sha256-lM6eYgOGjl1fx6WFD7rnRi/YAQieM0mx60h0p5dr+l8="; })
(fetchNuGet { pname = "Microsoft.AspNetCore.App.Runtime.linux-x64"; version = "8.0.1"; hash = "sha256-QbUQXjCzr8j8u/5X0af9jE++EugdoxMhT08F49MZX74="; })
(fetchNuGet { pname = "Microsoft.NET.ILLink.Tasks"; version = "8.0.1"; hash = "sha256-SopZpGaZ48/8dpUwDFDM3ix+g1rP4Yqs1PGuzRp+K7c="; })
(fetchNuGet { pname = "Microsoft.NETCore.App.Runtime.linux-x64"; version = "8.0.1"; hash = "sha256-jajBI5GqG2IIcsIMgxTHfXbMapoXrZGl/EEhShwYq7w="; })
(fetchNuGet { pname = "SharpGLTF.Core"; version = "1.0.0-alpha0031"; hash = "sha256-Bs4baD5wNIH6wAbGK4Xaem0i3luQkOQs37izBWdFx1I="; })
(fetchNuGet { pname = "SharpGLTF.Runtime"; version = "1.0.0-alpha0031"; hash = "sha256-TwJO6b8ubmwBQh6NyHha8+JT5zHDJ4dROBbsEbUaa1M="; })
(fetchNuGet { pname = "Sledge.Formats"; version = "1.2.2"; hash = "sha256-0Ddhuwpu3wwIzA4NuPaEVdMkx6tUukh8uKD6nKoxFPg="; })
(fetchNuGet { pname = "Sledge.Formats.Map"; version = "1.1.5"; hash = "sha256-hkYJ2iWIz7vhPWlDOw2fvTenlh+4/D/37Z71tCEwnK8="; })
(fetchNuGet { pname = "System.Numerics.Vectors"; version = "4.5.0"; hash = "sha256-qdSTIFgf2htPS+YhLGjAGiLN8igCYJnCCo6r78+Q+c8="; })
]
```

View File

@ -5,6 +5,8 @@
# Name of the nuget package to install, if different from pname
, nugetName ? pname
# Hash of the nuget package to install, will be given on first build
# nugetHash uses SRI hash and should be preferred
, nugetHash ? ""
, nugetSha256 ? ""
# Additional nuget deps needed by the tool package
, nugetDeps ? (_: [])
@ -24,7 +26,7 @@ buildDotnetModule (args // {
nugetDeps = mkNugetDeps {
name = pname;
nugetDeps = { fetchNuGet }: [
(fetchNuGet { pname = nugetName; inherit version; sha256 = nugetSha256; })
(fetchNuGet { pname = nugetName; inherit version; sha256 = nugetSha256; hash = nugetHash; })
] ++ (nugetDeps fetchNuGet);
};

View File

@ -5,15 +5,19 @@ attrs @
, version
, url ? "https://www.nuget.org/api/v2/package/${pname}/${version}"
, sha256 ? ""
, hash ? ""
, md5 ? ""
, ...
}:
if md5 != "" then
throw "fetchnuget does not support md5 anymore, please use sha256"
throw "fetchnuget does not support md5 anymore, please use 'hash' attribute with SRI hash"
# This is also detected in fetchurl, but we just throw here to avoid confusion
else if (sha256 != "" && hash != "") then
throw "multiple hashes passed to fetchNuGet"
else
buildDotnetPackage ({
src = fetchurl {
inherit url sha256;
inherit url sha256 hash;
name = "${pname}.${version}.zip";
};

View File

@ -1,11 +1,14 @@
{ linkFarmFromDrvs, fetchurl }:
{ name, nugetDeps ? import sourceFile, sourceFile ? null }:
linkFarmFromDrvs "${name}-nuget-deps" (nugetDeps {
fetchNuGet = { pname, version, sha256
fetchNuGet = { pname, version, sha256 ? "", hash ? ""
, url ? "https://www.nuget.org/api/v2/package/${pname}/${version}" }:
fetchurl {
name = "${pname}.${version}.nupkg";
inherit url sha256;
# There is no need to verify whether both sha256 and hash are
# valid here, because nuget-to-nix does not generate a deps.nix
# containing both.
inherit url sha256 hash;
};
}) // {
inherit sourceFile;

View File

@ -65,11 +65,11 @@ for package in *; do
for source in "${remote_sources[@]}"; do
url="${base_addresses[$source]}$package/$version/$package.$version.nupkg"
if [[ "$source" == "$used_source" ]]; then
sha256="$(nix-hash --type sha256 --flat --base32 "$version/$package.$version".nupkg)"
hash="$(nix-hash --type sha256 --flat --sri "$version/$package.$version".nupkg)"
found=true
break
else
if sha256=$(nix-prefetch-url "$url" 2>"$tmp"/error); then
if hash=$(nix-prefetch-url "$url" 2>"$tmp"/error); then
# If multiple remote sources are enabled, nuget will try them all
# concurrently and use the one that responds first. We always use the
# first source that has the package.
@ -91,9 +91,9 @@ for package in *; do
fi
if [[ "$source" != https://api.nuget.org/v3/index.json ]]; then
echo " (fetchNuGet { pname = \"$id\"; version = \"$version\"; sha256 = \"$sha256\"; url = \"$url\"; })"
echo " (fetchNuGet { pname = \"$id\"; version = \"$version\"; hash = \"$hash\"; url = \"$url\"; })"
else
echo " (fetchNuGet { pname = \"$id\"; version = \"$version\"; sha256 = \"$sha256\"; })"
echo " (fetchNuGet { pname = \"$id\"; version = \"$version\"; hash = \"$hash\"; })"
fi
done
cd ..

View File

@ -5,7 +5,7 @@ buildDotnetGlobalTool {
version = "0.28.2";
executables = "dotnet-csharpier";
nugetSha256 = "sha256-fXyE25niM80pPXCLC80Hm9XEHGUMx0XZOMxdVoA0h18=";
nugetHash = "sha256-fXyE25niM80pPXCLC80Hm9XEHGUMx0XZOMxdVoA0h18=";
meta = with lib; {
description = "Opinionated code formatter for C#";

View File

@ -8,7 +8,7 @@ buildDotnetGlobalTool {
dotnet-sdk = dotnetCorePackages.sdk_8_0;
dotnet-runtime = dotnetCorePackages.runtime_8_0;
nugetSha256 = "sha256-VkZGnfD8p6oAJ7i9tlfwJfmKfZBHJU7Wdq+K4YjPoRs=";
nugetHash = "sha256-VkZGnfD8p6oAJ7i9tlfwJfmKfZBHJU7Wdq+K4YjPoRs=";
meta = with lib; {
description = "C# REPL with syntax highlighting";

View File

@ -4,7 +4,7 @@ buildDotnetGlobalTool {
pname = "fantomas";
version = "6.3.9";
nugetSha256 = "sha256-XRPC5cXMoTJLLHe3f5A3+uXakbL+D7DlX0sA52KMZKw=";
nugetHash = "sha256-XRPC5cXMoTJLLHe3f5A3+uXakbL+D7DlX0sA52KMZKw=";
meta = with lib; {
description = "F# source code formatter";

View File

@ -7,7 +7,7 @@ buildDotnetGlobalTool {
nugetName = "GitVersion.Tool";
version = "5.12.0";
nugetSha256 = "sha256-dclYG2D0uSYqf++y33JCefkYLwbuRCuKd3qLMnx3BDI=";
nugetHash = "sha256-dclYG2D0uSYqf++y33JCefkYLwbuRCuKd3qLMnx3BDI=";
meta = with lib; {
description = "From git log to SemVer in no time";

View File

@ -2,27 +2,27 @@
# Please dont edit it manually, your changes might get overwritten!
{ fetchNuGet }: [
(fetchNuGet { pname = "AtkSharp"; version = "3.24.24.38"; sha256 = "12dv3j8nzhjb5c0093djajdnv8n7m0q7vq2d5ry2v4xk9wqzxpr7"; })
(fetchNuGet { pname = "CairoSharp"; version = "3.24.24.38"; sha256 = "0n3y5w088k81apxik9amfvjdwcic4k2ixxvnrk9cw6d2wh1d5r8d"; })
(fetchNuGet { pname = "dotnet-outdated-tool"; version = "4.1.0"; sha256 = "1i3i2hnf4qszlp3yzwggnvz4qpp3lgrzf19ygsmbvqzw1g7w150c"; })
(fetchNuGet { pname = "Eto.Forms"; version = "2.7.4"; sha256 = "0q50fw646h25an5j8c4p1gck8ja20vy344w1gb7rdp167d573mb3"; })
(fetchNuGet { pname = "Eto.Platform.Gtk"; version = "2.7.4"; sha256 = "0zy35zw1s4al305y4b496f5nf9vmmm7i0jf8j0dlq6df8bfjifcn"; })
(fetchNuGet { pname = "GdkSharp"; version = "3.24.24.38"; sha256 = "0c5gzg106bnnc4wwwhch6lja68623a9hk8r2sjcv35hl5dh21616"; })
(fetchNuGet { pname = "GioSharp"; version = "3.24.24.38"; sha256 = "1b3irarxjbbpf24fw2avdglcslb5653gn6m829yhlcm5ay37pds4"; })
(fetchNuGet { pname = "GLibSharp"; version = "3.24.24.38"; sha256 = "1a0ixdq1gdb46gkb2nnlydsi10bjrbd3risfyaphsy8fbsyzrzvm"; })
(fetchNuGet { pname = "GtkSharp"; version = "3.24.24.38"; sha256 = "0cn8aggci6n088y5giiaxmyzv01rcz37r8pm738q2bsb57zppz2j"; })
(fetchNuGet { pname = "Lidgren.Network"; version = "1.0.2"; sha256 = "0ka0vwyy060wd1dn1551m56fv957mmc7dxlj71g7n6qmsi1mdhif"; })
(fetchNuGet { pname = "Microsoft.NETCore.Platforms"; version = "1.1.0"; sha256 = "08vh1r12g6ykjygq5d3vq09zylgb84l63k49jc4v8faw9g93iqqm"; })
(fetchNuGet { pname = "Mono.Nat"; version = "3.0.3"; sha256 = "1b3alh1wz28y62cflwl1jppigv499cndm8sds32xsmvwpdwiq4yl"; })
(fetchNuGet { pname = "NETStandard.Library"; version = "2.0.3"; sha256 = "1fn9fxppfcg4jgypp2pmrpr6awl3qz1xmnri0cygpkwvyx27df1y"; })
(fetchNuGet { pname = "Newtonsoft.Json"; version = "13.0.2"; sha256 = "1p9splg1min274dpz7xdfgzrwkyfd3xlkygwpr1xgjvvyjvs6b0i"; })
(fetchNuGet { pname = "PangoSharp"; version = "3.24.24.38"; sha256 = "0cma8j4cy4j3fw0nvsxlqi0azjkvfjsw0wb6k6b2k21rdpy5rbbn"; })
(fetchNuGet { pname = "SharpCompress"; version = "0.32.2"; sha256 = "1p198bl08ia89rf4n6yjpacj3yrz6s574snsfl40l8vlqcdrc1pm"; })
(fetchNuGet { pname = "System.Buffers"; version = "4.5.1"; sha256 = "04kb1mdrlcixj9zh1xdi5as0k0qi8byr5mi3p3jcxx72qz93s2y3"; })
(fetchNuGet { pname = "System.ComponentModel.Annotations"; version = "5.0.0"; sha256 = "021h7x98lblq9avm1bgpa4i31c2kgsa7zn4sqhxf39g087ar756j"; })
(fetchNuGet { pname = "System.Memory"; version = "4.5.4"; sha256 = "14gbbs22mcxwggn0fcfs1b062521azb9fbb7c113x0mq6dzq9h6y"; })
(fetchNuGet { pname = "System.Numerics.Vectors"; version = "4.4.0"; sha256 = "0rdvma399070b0i46c4qq1h2yvjj3k013sqzkilz4bz5cwmx1rba"; })
(fetchNuGet { pname = "System.Runtime.CompilerServices.Unsafe"; version = "4.5.3"; sha256 = "1afi6s2r1mh1kygbjmfba6l4f87pi5sg13p4a48idqafli94qxln"; })
(fetchNuGet { pname = "System.Runtime.CompilerServices.Unsafe"; version = "6.0.0"; sha256 = "0qm741kh4rh57wky16sq4m0v05fxmkjjr87krycf5vp9f0zbahbc"; })
(fetchNuGet { pname = "System.Text.Encoding.CodePages"; version = "6.0.0"; sha256 = "0gm2kiz2ndm9xyzxgi0jhazgwslcs427waxgfa30m7yqll1kcrww"; })
(fetchNuGet { pname = "AtkSharp"; version = "3.24.24.38"; hash = "sha256-J9/+MU+zky18Lk3gfTCox6Jtm1SyjQQAK0vCb5Ecu4k="; })
(fetchNuGet { pname = "CairoSharp"; version = "3.24.24.38"; hash = "sha256-DeXSAuSiGc7SzHb3HsUkLDLe5HZVpRn7VQFNhAAvflg="; })
(fetchNuGet { pname = "dotnet-outdated-tool"; version = "4.1.0"; hash = "sha256-DJTAzwv8472qfj4F9/Oj415M/rbv8e/HpV9j4iwUccQ="; })
(fetchNuGet { pname = "Eto.Forms"; version = "2.7.4"; hash = "sha256-Y9VxSjsm3JbPeoETMvwGQkk02QuXMCSLVUVAQwx3oGA="; })
(fetchNuGet { pname = "Eto.Platform.Gtk"; version = "2.7.4"; hash = "sha256-lrko3UKuGUwbkMhJEE+tdSdnizOJLOILGFQRHfgvw38="; })
(fetchNuGet { pname = "GdkSharp"; version = "3.24.24.38"; hash = "sha256-JpggYCsUlrGZ1CKjCZMawiCjJDWQQc45YdYuA8L7rzA="; })
(fetchNuGet { pname = "GioSharp"; version = "3.24.24.38"; hash = "sha256-RLd7hlelMgp9Eqga+0YxZVHN6GtbCe6IcHct2bPKcaw="; })
(fetchNuGet { pname = "GLibSharp"; version = "3.24.24.38"; hash = "sha256-df/8vV4OeQ2v8k7HPNrKcoEQdfPUWrHmM2S1F3DrEag="; })
(fetchNuGet { pname = "GtkSharp"; version = "3.24.24.38"; hash = "sha256-Uvx7/ylLL4HROPWifMZnOYD9fe0qxlc8QsCayN5TyDI="; })
(fetchNuGet { pname = "Lidgren.Network"; version = "1.0.2"; hash = "sha256-LsJWQ9QVG3teOJL2dlitp6TtTKmhlGBbaBwY4D3fQE0="; })
(fetchNuGet { pname = "Microsoft.NETCore.Platforms"; version = "1.1.0"; hash = "sha256-FeM40ktcObQJk4nMYShB61H/E8B7tIKfl9ObJ0IOcCM="; })
(fetchNuGet { pname = "Mono.Nat"; version = "3.0.3"; hash = "sha256-1BMcebt8V93F0E2j2ixLiewX75WBcuqYMB6JzwOkaqw="; })
(fetchNuGet { pname = "NETStandard.Library"; version = "2.0.3"; hash = "sha256-Prh2RPebz/s8AzHb2sPHg3Jl8s31inv9k+Qxd293ybo="; })
(fetchNuGet { pname = "Newtonsoft.Json"; version = "13.0.2"; hash = "sha256-ESyjt/R7y9dDvvz5Sftozk+e/3Otn38bOcLGGh69Ot0="; })
(fetchNuGet { pname = "PangoSharp"; version = "3.24.24.38"; hash = "sha256-dq1c/G05iCmWmWZxwLV0e8qvQMS0620Bd0MSz4hEqjI="; })
(fetchNuGet { pname = "SharpCompress"; version = "0.32.2"; hash = "sha256-9QaWG8N0IwoIddpqcoo2P/shmbrSG0tcTkhFBOhCKdw="; })
(fetchNuGet { pname = "System.Buffers"; version = "4.5.1"; hash = "sha256-wws90sfi9M7kuCPWkv1CEYMJtCqx9QB/kj0ymlsNaxI="; })
(fetchNuGet { pname = "System.ComponentModel.Annotations"; version = "5.0.0"; hash = "sha256-0pST1UHgpeE6xJrYf5R+U7AwIlH3rVC3SpguilI/MAg="; })
(fetchNuGet { pname = "System.Memory"; version = "4.5.4"; hash = "sha256-3sCEfzO4gj5CYGctl9ZXQRRhwAraMQfse7yzKoRe65E="; })
(fetchNuGet { pname = "System.Numerics.Vectors"; version = "4.4.0"; hash = "sha256-auXQK2flL/JpnB/rEcAcUm4vYMCYMEMiWOCAlIaqu2U="; })
(fetchNuGet { pname = "System.Runtime.CompilerServices.Unsafe"; version = "4.5.3"; hash = "sha256-lnZMUqRO4RYRUeSO8HSJ9yBHqFHLVbmenwHWkIU20ak="; })
(fetchNuGet { pname = "System.Runtime.CompilerServices.Unsafe"; version = "6.0.0"; hash = "sha256-bEG1PnDp7uKYz/OgLOWs3RWwQSVYm+AnPwVmAmcgp2I="; })
(fetchNuGet { pname = "System.Text.Encoding.CodePages"; version = "6.0.0"; hash = "sha256-nGc2A6XYnwqGcq8rfgTRjGr+voISxNe/76k2K36coj4="; })
]

View File

@ -4,7 +4,7 @@ buildDotnetGlobalTool {
pname = "fable";
version = "4.18.0";
nugetSha256 = "sha256-PbrFjpltRx4lnQDgQrOKSVHwttePMfOnjljOddkFbmY=";
nugetHash = "sha256-PbrFjpltRx4lnQDgQrOKSVHwttePMfOnjljOddkFbmY=";
passthru.updateScript = ./update.sh;
meta = with lib; {

View File

@ -36,4 +36,4 @@ NUGET_URL="$(curl -f "https://api.nuget.org/v3/index.json" | jq --raw-output '.r
HASH=$(nix-hash --to-sri --type sha256 "$(nix-prefetch-url "$NUGET_URL")")
sed -i "s/version = \".*\"/version = \"$VER\"/" "$NIX_DRV"
sed -i "s#nugetSha256 = \"sha256-.\{44\}\"#nugetSha256 = \"$HASH\"#" "$NIX_DRV"
sed -i "s#nugetHash = \"sha256-.\{44\}\"#nugetHash = \"$HASH\"#" "$NIX_DRV"

View File

@ -10,7 +10,7 @@ buildDotnetGlobalTool rec {
pname = "csharp-ls";
version = "0.14.0";
nugetSha256 = "sha256-agcx7VPIqGhl3NzdGLPwXYJsRuvSjL4SdbNg9vFjIh4=";
nugetHash = "sha256-agcx7VPIqGhl3NzdGLPwXYJsRuvSjL4SdbNg9vFjIh4=";
dotnet-sdk = sdk_8_0;
dotnet-runtime = sdk_8_0;

View File

@ -4,7 +4,7 @@ buildDotnetGlobalTool {
pname = "pbm";
version = "1.3.2";
nugetSha256 = "sha256-xu3g8NFLZYnHzBuoIhIiAzaPJqY0xhLWLYi+ORRADH8=";
nugetHash = "sha256-xu3g8NFLZYnHzBuoIhIiAzaPJqY0xhLWLYi+ORRADH8=";
meta = with lib; {
description = "CLI for managing Akka.NET applications and Akka.NET Clusters";