nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 llvmPackages_20,
3 lib,
4 fetchurl,
5 fetchpatch,
6 dotnetCorePackages,
7 jq,
8 curl,
9 git,
10 cmake,
11 pkg-config,
12 zlib,
13 icu,
14 lttng-ust_2_12,
15 krb5,
16 glibcLocales,
17 ensureNewerSourcesForZipFilesHook,
18 darwin,
19 xcbuild,
20 swiftPackages,
21 openssl,
22 getconf,
23 python3,
24 xmlstarlet,
25 nodejs,
26 cpio,
27 callPackage,
28 unzip,
29 yq,
30 installShellFiles,
31
32 baseName ? "dotnet",
33 bootstrapSdk,
34 releaseManifestFile,
35 tarballHash,
36}:
37
38let
39 llvmPackages = llvmPackages_20;
40
41 stdenv = llvmPackages.stdenv;
42
43 inherit (stdenv)
44 isLinux
45 isDarwin
46 buildPlatform
47 targetPlatform
48 ;
49 inherit (swiftPackages) swift;
50
51 releaseManifest = lib.importJSON releaseManifestFile;
52 inherit (releaseManifest) release sourceRepository tag;
53
54 buildRid = dotnetCorePackages.systemToDotnetRid buildPlatform.system;
55 targetRid = dotnetCorePackages.systemToDotnetRid targetPlatform.system;
56 targetArch = lib.elemAt (lib.splitString "-" targetRid) 1;
57
58 sigtool = callPackage ./sigtool.nix { };
59
60 _icu = if isDarwin then darwin.ICU else icu;
61
62 version = release;
63in
64stdenv.mkDerivation {
65 pname = "${baseName}-vmr";
66 inherit version;
67
68 # TODO: fix this in the binary sdk packages
69 preHook = lib.optionalString stdenv.hostPlatform.isDarwin ''
70 addToSearchPath DYLD_LIBRARY_PATH "${_icu}/lib"
71 export DYLD_LIBRARY_PATH
72 '';
73
74 src = fetchurl {
75 url = "${sourceRepository}/archive/refs/tags/${tag}.tar.gz";
76 hash = tarballHash;
77 };
78
79 nativeBuildInputs = [
80 ensureNewerSourcesForZipFilesHook
81 jq
82 curl.bin
83 git
84 cmake
85 pkg-config
86 python3
87 xmlstarlet
88 unzip
89 yq
90 installShellFiles
91 ]
92 ++ lib.optionals (lib.versionAtLeast version "9") [
93 nodejs
94 ]
95 ++ lib.optionals (lib.versionAtLeast version "10") [
96 cpio
97 ]
98 ++ lib.optionals isDarwin [
99 getconf
100 ];
101
102 buildInputs = [
103 # this gets copied into the tree, but we still need the sandbox profile
104 bootstrapSdk
105 # the propagated build inputs in llvm.dev break swift compilation
106 llvmPackages.llvm.out
107 zlib
108 _icu
109 openssl
110 ]
111 ++ lib.optionals isLinux [
112 krb5
113 lttng-ust_2_12
114 ]
115 ++ lib.optionals isDarwin [
116 xcbuild
117 swift
118 krb5
119 sigtool
120 ];
121
122 # This is required to fix the error:
123 # > CSSM_ModuleLoad(): One or more parameters passed to a function were not valid.
124 # The error occurs during
125 # AppleCryptoNative_X509ImportCollection -> ReadX509 -> SecItemImport
126 # while importing trustedroots/codesignctl.pem. This happens during any dotnet
127 # restore operation.
128 # Enabling com.apple.system.opendirectoryd.membership causes swiftc to use
129 # /var/folders for its default cache path, so the swiftc -module-cache-path
130 # patch below is required.
131 sandboxProfile = ''
132 (allow file-read* (subpath "/private/var/db/mds/system"))
133 (allow mach-lookup (global-name "com.apple.SecurityServer")
134 (global-name "com.apple.system.opendirectoryd.membership"))
135 '';
136
137 patches =
138 lib.optionals (lib.versionAtLeast version "9" && lib.versionOlder version "10") [
139 ./UpdateNuGetConfigPackageSourcesMappings-don-t-add-em.patch
140 ./vmr-compiler-opt-v9.patch
141 ]
142 ++ lib.optionals (lib.versionOlder version "9") [
143 ./fix-aspnetcore-portable-build.patch
144 ./vmr-compiler-opt-v8.patch
145 ];
146
147 postPatch = ''
148 # set the sdk version in global.json to match the bootstrap sdk
149 sdk_version=$(HOME=$(mktemp -d) ${bootstrapSdk}/bin/dotnet --version)
150 jq '(.tools.dotnet=$dotnet)' global.json --arg dotnet "$sdk_version" > global.json~
151 mv global.json{~,}
152
153 patchShebangs $(find -name \*.sh -type f -executable)
154
155 # I'm not sure why this is required, but these files seem to use the wrong
156 # property name.
157 # TODO: not needed in 9.0?
158 [[ ! -f src/xliff-tasks/eng/Versions.props ]] || \
159 sed -i 's:\bVersionBase\b:VersionPrefix:g' \
160 src/xliff-tasks/eng/Versions.props
161
162 # at least in 9.0 preview 1, this package depends on a specific beta build
163 # of System.CommandLine
164 xmlstarlet ed \
165 --inplace \
166 -s //Project -t elem -n PropertyGroup \
167 -s \$prev -t elem -n NoWarn -v '$(NoWarn);NU1603' \
168 src/nuget-client/src/NuGet.Core/NuGet.CommandLine.XPlat/NuGet.CommandLine.XPlat.csproj
169
170 # AD0001 crashes intermittently in source-build-reference-packages with
171 # CSC : error AD0001: Analyzer 'Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpDetectPreviewFeatureAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.
172 # possibly related to https://github.com/dotnet/runtime/issues/90356
173 xmlstarlet ed \
174 --inplace \
175 -s //Project -t elem -n PropertyGroup \
176 -s \$prev -t elem -n NoWarn -v '$(NoWarn);AD0001' \
177 src/source-build-reference-packages/src/referencePackages/Directory.Build.props
178
179 ''
180 + lib.optionalString (lib.versionOlder version "10") ''
181 # https://github.com/microsoft/ApplicationInsights-dotnet/issues/2848
182 xmlstarlet ed \
183 --inplace \
184 -u //_:Project/_:PropertyGroup/_:BuildNumber -v 0 \
185 src/source-build-externals/src/application-insights/.props/_GlobalStaticVersion.props
186 ''
187 + ''
188
189 # this fixes compile errors with clang 15 (e.g. darwin)
190 substituteInPlace \
191 src/runtime/src/native/libs/CMakeLists.txt \
192 --replace-fail 'add_compile_options(-Weverything)' 'add_compile_options(-Wall)'
193 ''
194 + lib.optionalString (lib.versionAtLeast version "9") (
195 ''
196 # repro.csproj fails to restore due to missing freebsd packages
197 xmlstarlet ed \
198 --inplace \
199 -s //Project -t elem -n PropertyGroup \
200 -s \$prev -t elem -n RuntimeIdentifiers -v ${targetRid} \
201 src/runtime/src/coreclr/tools/aot/ILCompiler/repro/repro.csproj
202
203 # https://github.com/dotnet/runtime/pull/98559#issuecomment-1965338627
204 xmlstarlet ed \
205 --inplace \
206 -s //Project -t elem -n PropertyGroup \
207 -s \$prev -t elem -n NoWarn -v '$(NoWarn);CS9216' \
208 src/runtime/Directory.Build.props
209
210 # https://github.com/dotnet/source-build/issues/3131#issuecomment-2030215805
211 substituteInPlace \
212 src/aspnetcore/eng/Dependencies.props \
213 --replace-fail \
214 "'\$(DotNetBuildSourceOnly)' == 'true'" \
215 "'\$(DotNetBuildSourceOnly)' == 'true' and \$(PortableBuild) == 'false'"
216
217 # https://github.com/dotnet/source-build/issues/4325
218 xmlstarlet ed \
219 --inplace \
220 -r '//Target[@Name="UnpackTarballs"]/Move' -v Copy \
221 eng/init-source-only.proj
222
223 # error: _FORTIFY_SOURCE requires compiling with optimization (-O) [-Werror,-W#warnings]
224 substituteInPlace \
225 src/runtime/src/coreclr/ilasm/CMakeLists.txt \
226 --replace-fail 'set_source_files_properties( prebuilt/asmparse.cpp PROPERTIES COMPILE_FLAGS "-O0" )' ""
227 ''
228 + lib.optionalString (lib.versionOlder version "10") ''
229
230 # https://github.com/dotnet/source-build/issues/4444
231 xmlstarlet ed \
232 --inplace \
233 -s '//Project/Target/MSBuild[@Targets="Restore"]' \
234 -t attr -n Properties -v "NUGET_PACKAGES='\$(CurrentRepoSourceBuildPackageCache)'" \
235 src/aspnetcore/eng/Tools.props
236 # patch packages installed from npm cache
237 xmlstarlet ed \
238 --inplace \
239 -s //Project -t elem -n Import \
240 -i \$prev -t attr -n Project -v "${./patch-npm-packages.proj}" \
241 src/aspnetcore/eng/DotNetBuild.props
242 ''
243 )
244 + lib.optionalString isLinux (
245 ''
246 substituteInPlace \
247 src/runtime/src/native/libs/System.Security.Cryptography.Native/opensslshim.c \
248 --replace-fail '"libssl.so"' '"${openssl.out}/lib/libssl.so"'
249
250 substituteInPlace \
251 src/runtime/src/native/libs/System.Net.Security.Native/pal_gssapi.c \
252 --replace-fail '"libgssapi_krb5.so.2"' '"${lib.getLib krb5}/lib/libgssapi_krb5.so.2"'
253
254 substituteInPlace \
255 src/runtime/src/native/libs/System.Globalization.Native/pal_icushim.c \
256 --replace-fail '"libicui18n.so"' '"${icu}/lib/libicui18n.so"' \
257 --replace-fail '"libicuuc.so"' '"${icu}/lib/libicuuc.so"'
258 ''
259 + lib.optionalString (lib.versionAtLeast version "9") ''
260 substituteInPlace \
261 src/runtime/src/native/libs/System.Globalization.Native/pal_icushim.c \
262 --replace-fail '#define VERSIONED_LIB_NAME_LEN 64' '#define VERSIONED_LIB_NAME_LEN 256'
263 ''
264 + lib.optionalString (lib.versionOlder version "9") ''
265 substituteInPlace \
266 src/runtime/src/native/libs/System.Globalization.Native/pal_icushim.c \
267 --replace-warn 'libicuucName[64]' 'libicuucName[256]' \
268 --replace-warn 'libicui18nName[64]' 'libicui18nName[256]'
269 ''
270 )
271 + lib.optionalString isDarwin (
272 ''
273 substituteInPlace \
274 src/runtime/src/native/libs/System.Globalization.Native/CMakeLists.txt \
275 --replace-fail '/usr/lib/libicucore.dylib' '${darwin.ICU}/lib/libicucore.dylib'
276
277 substituteInPlace \
278 src/runtime/src/installer/managed/Microsoft.NET.HostModel/HostModelUtils.cs \
279 ''
280 + lib.optionalString (lib.versionOlder version "10") " src/sdk/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets \\\n"
281 + ''
282 --replace-fail '/usr/bin/codesign' '${sigtool}/bin/codesign'
283
284 # fix: strip: error: unknown argument '-n'
285 substituteInPlace \
286 src/runtime/eng/native/functions.cmake \
287 --replace-fail ' -no_code_signature_warning' ""
288
289 # [...]/installer.singlerid.targets(434,5): error MSB3073: The command "pkgbuild [...]" exited with code 127
290 xmlstarlet ed \
291 --inplace \
292 -s //Project -t elem -n PropertyGroup \
293 -s \$prev -t elem -n SkipInstallerBuild -v true \
294 src/runtime/Directory.Build.props
295 ''
296 + lib.optionalString (lib.versionAtLeast version "10") ''
297 xmlstarlet ed \
298 --inplace \
299 -s //Project -t elem -n PropertyGroup \
300 -s \$prev -t elem -n SkipInstallerBuild -v true \
301 src/aspnetcore/Directory.Build.props
302 ''
303 + ''
304 # stop passing -sdk without a path
305 # stop using xcrun
306 # add -module-cache-path to fix swift errors, see sandboxProfile
307 # <unknown>:0: error: unable to open output file '/var/folders/[...]/C/clang/ModuleCache/[...]/SwiftShims-[...].pcm': 'Operation not permitted'
308 # <unknown>:0: error: could not build Objective-C module 'SwiftShims'
309 substituteInPlace \
310 src/runtime/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt \
311 --replace-fail ' -sdk ''${CMAKE_OSX_SYSROOT}' "" \
312 --replace-fail 'xcrun swiftc' 'swiftc -module-cache-path "$ENV{HOME}/.cache/module-cache"'
313
314 # fix: strip: error: unknown argument '-n'
315 substituteInPlace \
316 src/runtime/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets \
317 ''
318 + lib.optionalString (lib.versionAtLeast version "9") " src/runtime/src/native/managed/native-library.targets \\\n"
319 + ''
320 --replace-fail ' -no_code_signature_warning' ""
321
322 # ld: library not found for -ld_classic
323 substituteInPlace \
324 src/runtime/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets \
325 ''
326 + lib.optionalString (lib.versionOlder version "10") " src/runtime/src/coreclr/tools/aot/ILCompiler/ILCompiler.csproj \\\n"
327 + " --replace-fail 'Include=\"-ld_classic\"' \"\"\n"
328 + lib.optionalString (lib.versionOlder version "9") ''
329 # [...]/build.proj(123,5): error : Did not find PDBs for the following SDK files:
330 # [...]/build.proj(123,5): error : sdk/8.0.102/System.Resources.Extensions.dll
331 # [...]/build.proj(123,5): error : sdk/8.0.102/System.CodeDom.dll
332 # [...]/build.proj(123,5): error : sdk/8.0.102/FSharp/System.Resources.Extensions.dll
333 # [...]/build.proj(123,5): error : sdk/8.0.102/FSharp/System.CodeDom.dll
334 substituteInPlace \
335 build.proj \
336 --replace-fail 'FailOnMissingPDBs="true"' 'FailOnMissingPDBs="false"'
337
338 substituteInPlace \
339 src/runtime/src/mono/CMakeLists.txt \
340 --replace-fail '/usr/lib/libicucore.dylib' '${darwin.ICU}/lib/libicucore.dylib'
341 ''
342 );
343
344 prepFlags = [
345 "--no-artifacts"
346 "--no-prebuilts"
347 "--with-packages"
348 bootstrapSdk.artifacts
349
350 ]
351 # https://github.com/dotnet/source-build/issues/5286#issuecomment-3097872768
352 ++ lib.optional (lib.versionAtLeast version "10") "-p:SkipArcadeSdkImport=true";
353
354 configurePhase =
355 let
356 prepScript = if (lib.versionAtLeast version "9") then "./prep-source-build.sh" else "./prep.sh";
357 in
358 ''
359 runHook preConfigure
360
361 # The build process tries to overwrite some things in the sdk (e.g.
362 # SourceBuild.MSBuildSdkResolver.dll), so it needs to be mutable.
363 cp -Tr ${bootstrapSdk}/share/dotnet .dotnet
364 chmod -R +w .dotnet
365
366 export HOME=$(mktemp -d)
367 ''
368 + lib.optionalString (lib.versionAtLeast version "10") ''
369 dotnet nuget add source "${bootstrapSdk.artifacts}"
370 ''
371 + ''
372 ${prepScript} $prepFlags
373
374 runHook postConfigure
375 '';
376
377 postConfigure = lib.optionalString (lib.versionAtLeast version "9") ''
378 # see patch-npm-packages.proj
379 typeset -f isScript patchShebangs > src/aspnetcore/patch-shebangs.sh
380 '';
381
382 dontConfigureNuget = true; # NUGET_PACKAGES breaks the build
383 dontUseCmakeConfigure = true;
384
385 # https://github.com/NixOS/nixpkgs/issues/38991
386 # bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
387 LOCALE_ARCHIVE = lib.optionalString (
388 isLinux && glibcLocales != null
389 ) "${glibcLocales}/lib/locale/locale-archive";
390
391 # clang: error: argument unused during compilation: '-Wa,--compress-debug-sections' [-Werror,-Wunused-command-line-argument]
392 # caused by separateDebugInfo
393 NIX_CFLAGS_COMPILE = "-Wno-unused-command-line-argument";
394
395 buildFlags = [
396 "--with-packages"
397 bootstrapSdk.artifacts
398 "--clean-while-building"
399 "--release-manifest"
400 releaseManifestFile
401 ]
402 ++ lib.optionals (lib.versionAtLeast version "9") [
403 "--source-build"
404 ]
405 ++ [
406 "--"
407 "-p:PortableBuild=true"
408 ]
409 ++ lib.optional (targetRid != buildRid) "-p:TargetRid=${targetRid}";
410
411 buildPhase = ''
412 runHook preBuild
413
414 # on darwin, in a sandbox, this causes:
415 # CSSM_ModuleLoad(): One or more parameters passed to a function were not valid.
416 export DOTNET_GENERATE_ASPNET_CERTIFICATE=0
417
418 # CLR_CC/CXX need to be set to stop the build system from using clang-11,
419 # which is unwrapped
420 # dotnet needs to be in PATH to fix:
421 # src/sdk/eng/restore-toolset.sh: line 114: /nix/store/[...]-dotnet-sdk-9.0.100-preview.2.24157.14//.version: Read-only file system
422 version= \
423 CLR_CC=$(command -v clang) \
424 CLR_CXX=$(command -v clang++) \
425 PATH=$PWD/.dotnet:$PATH \
426 ./build.sh $buildFlags
427
428 runHook postBuild
429 '';
430
431 outputs = [
432 "out"
433 "man"
434 ];
435
436 installPhase =
437 let
438 assets = if (lib.versionAtLeast version "9") then "assets" else targetArch;
439 # 10.0.0-preview.6 ends up creating duplicate files in .nupkgs, for example in
440 # Microsoft.Internal.Runtime.AspNetCore.Transport.10.0.0-preview.6.25358.103.nupkg
441 #
442 # lib/net10.0//System.Diagnostics.EventLog.pdb
443 # lib/net10.0/System.Diagnostics.EventLog.pdb
444 unzipFlags = "-q" + lib.optionalString (lib.versionAtLeast version "10") "o" + "d";
445 in
446 ''
447 runHook preInstall
448
449 mkdir -p "$out"/lib
450
451 pushd "artifacts/${assets}/Release"
452 find . -name \*.tar.gz | while read archive; do
453 target=$out/lib/$(basename "$archive" .tar.gz)
454 # dotnet 9 currently has two copies of the sdk tarball
455 [[ ! -e "$target" ]] || continue
456 mkdir "$target"
457 tar -C "$target" -xzf "$PWD/$archive"
458 done
459 popd
460
461 local -r unpacked="$PWD/.unpacked"
462 for nupkg in $out/lib/Private.SourceBuilt.Artifacts.*.${targetRid}/{,SourceBuildReferencePackages/}*.nupkg; do
463 rm -rf "$unpacked"
464 unzip ${unzipFlags} "$unpacked" "$nupkg"
465 chmod -R +rw "$unpacked"
466 rm "$nupkg"
467 mv "$unpacked" "$nupkg"
468 # TODO: should we fix executable flags here? see dotnetInstallHook
469 done
470
471 installManPage src/sdk/documentation/manpages/sdk/*
472
473 runHook postInstall
474 '';
475
476 ${if stdenv.isDarwin && lib.versionAtLeast version "10" then "postInstall" else null} = ''
477 mkdir -p "$out"/nix-support
478 echo ${sigtool} > "$out"/nix-support/manual-sdk-deps
479 '';
480
481 # stripping dlls results in:
482 # Failed to load System.Private.CoreLib.dll (error code 0x8007000B)
483 # stripped crossgen2 results in:
484 # Failure processing application bundle; possible file corruption.
485 # this needs to be a bash array
486 preFixup = ''
487 stripExclude=(\*.dll crossgen2)
488 '';
489
490 separateDebugInfo = true;
491
492 passthru = {
493 inherit releaseManifest buildRid targetRid;
494 icu = _icu;
495 # ilcompiler is currently broken: https://github.com/dotnet/source-build/issues/1215
496 hasILCompiler = lib.versionAtLeast version "9";
497 };
498
499 meta = {
500 description = "Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI";
501 homepage = "https://dotnet.github.io/";
502 license = lib.licenses.mit;
503 maintainers = with lib.maintainers; [ corngood ];
504 mainProgram = "dotnet";
505 platforms = [
506 "x86_64-linux"
507 "aarch64-linux"
508 "x86_64-darwin"
509 "aarch64-darwin"
510 ];
511 # build deadlocks intermittently on rosetta
512 # https://github.com/dotnet/runtime/issues/111628
513 broken = stdenv.hostPlatform.system == "x86_64-darwin";
514 };
515}