2024-02-21 03:21:08 +00:00
{ clangStdenv
, lib
, fetchurl
, dotnetCorePackages
, jq
, curl
, git
, cmake
, pkg-config
, llvm
, zlib
, icu
, lttng-ust_2_12
2024-08-17 04:56:19 +00:00
, krb5
2024-02-21 03:21:08 +00:00
, glibcLocales
, ensureNewerSourcesForZipFilesHook
, darwin
, xcbuild
, swiftPackages
, openssl
, getconf
, python3
, xmlstarlet
2024-04-03 18:47:33 +00:00
, nodejs
2024-02-21 03:21:08 +00:00
, callPackage
2024-06-15 19:53:16 +00:00
, unzip
, yq
2024-02-21 03:21:08 +00:00
, dotnetSdk
, releaseManifestFile
, tarballHash
} :
let
stdenv = if clangStdenv . isDarwin
then swiftPackages . stdenv
else clangStdenv ;
inherit ( stdenv )
isLinux
isDarwin
buildPlatform
targetPlatform ;
inherit ( swiftPackages ) apple_sdk swift ;
releaseManifest = lib . importJSON releaseManifestFile ;
inherit ( releaseManifest ) release sourceRepository tag ;
buildRid = dotnetCorePackages . systemToDotnetRid buildPlatform . system ;
targetRid = dotnetCorePackages . systemToDotnetRid targetPlatform . system ;
targetArch = lib . elemAt ( lib . splitString " - " targetRid ) 1 ;
sigtool = callPackage ./sigtool.nix { } ;
_icu = if isDarwin then darwin . ICU else icu ;
in stdenv . mkDerivation rec {
pname = " d o t n e t - v m r " ;
version = release ;
# TODO: fix this in the binary sdk packages
preHook = lib . optionalString stdenv . isDarwin ''
addToSearchPath DYLD_LIBRARY_PATH " ${ _icu } / l i b "
export DYLD_LIBRARY_PATH
'' ;
src = fetchurl {
url = " ${ sourceRepository } / a r c h i v e / r e f s / t a g s / ${ tag } . t a r . g z " ;
hash = tarballHash ;
} ;
nativeBuildInputs = [
ensureNewerSourcesForZipFilesHook
jq
curl . bin
git
cmake
pkg-config
python3
xmlstarlet
2024-06-15 19:53:16 +00:00
unzip
yq
2024-02-21 03:21:08 +00:00
]
2024-04-03 18:47:33 +00:00
++ lib . optionals ( lib . versionAtLeast version " 9 " ) [
nodejs
]
2024-02-21 03:21:08 +00:00
++ lib . optionals isDarwin [
getconf
] ;
buildInputs = [
# this gets copied into the tree, but we still want the hooks to run
dotnetSdk
# the propagated build inputs in llvm.dev break swift compilation
llvm . out
zlib
_icu
openssl
]
++ lib . optionals isLinux [
2024-08-17 04:56:19 +00:00
krb5
2024-02-21 03:21:08 +00:00
lttng-ust_2_12
]
++ lib . optionals isDarwin ( with apple_sdk . frameworks ; [
2024-04-03 18:47:33 +00:00
xcbuild
2024-02-21 03:21:08 +00:00
swift
2024-08-17 04:56:19 +00:00
( krb5 . overrideAttrs ( old : {
2024-02-21 03:21:08 +00:00
# the propagated build inputs break swift compilation
buildInputs = old . buildInputs ++ old . propagatedBuildInputs ;
propagatedBuildInputs = [ ] ;
} ) )
sigtool
Foundation
CoreFoundation
CryptoKit
System
2024-04-03 18:47:33 +00:00
]
++ lib . optional ( lib . versionAtLeast version " 9 " ) GSS ) ;
2024-02-21 03:21:08 +00:00
# This is required to fix the error:
# > CSSM_ModuleLoad(): One or more parameters passed to a function were not valid.
# The error occurs during
# AppleCryptoNative_X509ImportCollection -> ReadX509 -> SecItemImport
# while importing trustedroots/codesignctl.pem. This happens during any dotnet
# restore operation.
# Enabling com.apple.system.opendirectoryd.membership causes swiftc to use
# /var/folders for its default cache path, so the swiftc -module-cache-path
# patch below is required.
sandboxProfile = ''
( allow file-read * ( subpath " / p r i v a t e / v a r / d b / m d s / s y s t e m " ) )
( allow mach-lookup ( global-name " c o m . a p p l e . S e c u r i t y S e r v e r " )
( global-name " c o m . a p p l e . s y s t e m . o p e n d i r e c t o r y d . m e m b e r s h i p " ) )
'' ;
2024-04-03 18:47:33 +00:00
patches = lib . optionals ( lib . versionOlder version " 9 " ) [
2024-02-21 03:21:08 +00:00
./fix-aspnetcore-portable-build.patch
] ;
postPatch = ''
# set the sdk version in global.json to match the bootstrap sdk
jq ' ( . tools . dotnet = $ dotnet ) ' global . json - - arg dotnet " $ ( ${ dotnetSdk } / b i n / d o t n e t - - v e r s i o n ) " > global . json ~
mv global . json { ~ , }
patchShebangs $ ( find - name \ * . sh - type f - executable )
# I'm not sure why this is required, but these files seem to use the wrong
# property name.
# TODO: not needed in 9.0?
[ [ ! - f src/xliff-tasks/eng/Versions.props ] ] || \
sed - i ' s : \ bVersionBase \ b:VersionPrefix:g' \
src/xliff-tasks/eng/Versions.props
# at least in 9.0 preview 1, this package depends on a specific beta build
# of System.CommandLine
xmlstarlet ed \
- - inplace \
- s // Project - t elem - n PropertyGroup \
- s \ $ prev - t elem - n NoWarn - v ' $ ( NoWarn ) ; NU1603' \
src/nuget-client/src/NuGet.Core/NuGet.CommandLine.XPlat/NuGet.CommandLine.XPlat.csproj
# AD0001 crashes intermittently in source-build-reference-packages with
# 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.'.
# possibly related to https://github.com/dotnet/runtime/issues/90356
xmlstarlet ed \
- - inplace \
- s // Project - t elem - n PropertyGroup \
- s \ $ prev - t elem - n NoWarn - v ' $ ( NoWarn ) ; AD0001' \
src/source-build-reference-packages/src/referencePackages/Directory.Build.props
# https://github.com/microsoft/ApplicationInsights-dotnet/issues/2848
xmlstarlet ed \
- - inplace \
- u // _ : Project/_ : PropertyGroup/_ : BuildNumber - v 0 \
src/source-build-externals/src/application-insights/.props/_GlobalStaticVersion.props
# this fixes compile errors with clang 15 (e.g. darwin)
substituteInPlace \
src/runtime/src/native/libs/CMakeLists.txt \
- - replace-fail ' add_compile_options ( - Weverything ) ' ' add_compile_options ( - Wall ) '
2024-03-09 21:32:31 +00:00
# strip native symbols in runtime
# see: https://github.com/dotnet/source-build/issues/2543
xmlstarlet ed \
- - inplace \
- s // Project - t elem - n PropertyGroup \
- s \ $ prev - t elem - n KeepNativeSymbols - v false \
src/runtime/Directory.Build.props
2024-02-21 03:21:08 +00:00
''
2024-04-03 18:47:33 +00:00
+ lib . optionalString ( lib . versionAtLeast version " 9 " ) ''
# repro.csproj fails to restore due to missing freebsd packages
xmlstarlet ed \
- - inplace \
- s // Project - t elem - n PropertyGroup \
- s \ $ prev - t elem - n RuntimeIdentifiers - v $ { targetRid } \
src/runtime/src/coreclr/tools/aot/ILCompiler/repro/repro.csproj
# https://github.com/dotnet/runtime/pull/98559#issuecomment-1965338627
xmlstarlet ed \
- - inplace \
- s // Project - t elem - n PropertyGroup \
- s \ $ prev - t elem - n NoWarn - v ' $ ( NoWarn ) ; CS9216' \
src/runtime/Directory.Build.props
# patch packages installed from npm cache
xmlstarlet ed \
- - inplace \
- s // Project - t elem - n Import \
- i \ $ prev - t attr - n Project - v " ${ ./patch-npm-packages.proj } " \
2024-06-02 22:30:08 +00:00
src/aspnetcore/eng/DotNetBuild.props
2024-04-03 18:47:33 +00:00
''
+ lib . optionalString ( lib . versionAtLeast version " 9 " ) ''
# https://github.com/dotnet/source-build/issues/3131#issuecomment-2030215805
substituteInPlace \
src/aspnetcore/eng/Dependencies.props \
- - replace-fail \
" ' \$ ( D o t N e t B u i l d S o u r c e O n l y ) ' = = ' t r u e ' " \
" ' \$ ( D o t N e t B u i l d S o u r c e O n l y ) ' = = ' t r u e ' a n d \$ ( P o r t a b l e B u i l d ) = = ' f a l s e ' "
2024-06-02 22:30:08 +00:00
# https://github.com/dotnet/source-build/issues/4325
xmlstarlet ed \
- - inplace \
- r ' // Target [ @ Name = " U n p a c k T a r b a l l s " ] /Move ' - v Copy \
eng/init-source-only.proj
# error: _FORTIFY_SOURCE requires compiling with optimization (-O) [-Werror,-W#warnings]
substituteInPlace \
src/runtime/src/coreclr/ilasm/CMakeLists.txt \
- - replace-fail ' set_source_files_properties ( prebuilt/asmparse.cpp PROPERTIES COMPILE_FLAGS " - O 0 " ) ' " "
# https://github.com/dotnet/source-build/issues/4444
xmlstarlet ed \
- - inplace \
- s ' // Project/Target/MSBuild [ @ Targets = " R e s t o r e " ] ' \
- t attr - n Properties - v " N U G E T _ P A C K A G E S = ' \$ ( C u r r e n t R e p o S o u r c e B u i l d P a c k a g e C a c h e ) ' " \
src/aspnetcore/eng/Tools.props
2024-04-03 18:47:33 +00:00
''
2024-06-02 22:30:08 +00:00
+ lib . optionalString isLinux ( ''
2024-02-21 03:21:08 +00:00
substituteInPlace \
src/runtime/src/native/libs/System.Security.Cryptography.Native/opensslshim.c \
- - replace-fail ' " l i b s s l . s o " ' ' " ${ openssl . out } / l i b / l i b s s l . s o " '
substituteInPlace \
src/runtime/src/native/libs/System.Net.Security.Native/pal_gssapi.c \
2024-08-17 04:56:19 +00:00
- - replace-fail ' " l i b g s s a p i _ k r b 5 . s o . 2 " ' ' " ${ lib . getLib krb5 } / l i b / l i b g s s a p i _ k r b 5 . s o . 2 " '
2024-02-21 03:21:08 +00:00
substituteInPlace \
src/runtime/src/native/libs/System.Globalization.Native/pal_icushim.c \
- - replace-fail ' " l i b i c u i 1 8 n . s o " ' ' " ${ icu } / l i b / l i b i c u i 1 8 n . s o " ' \
- - replace-fail ' " l i b i c u u c . s o " ' ' " ${ icu } / l i b / l i b i c u u c . s o " '
2024-06-02 22:30:08 +00:00
''
+ lib . optionalString ( lib . versionAtLeast version " 9 " ) ''
substituteInPlace \
src/runtime/src/native/libs/System.Globalization.Native/pal_icushim.c \
- - replace-fail ' #define VERSIONED_LIB_NAME_LEN 64' '#define VERSIONED_LIB_NAME_LEN 256'
''
+ lib . optionalString ( lib . versionOlder version " 9 " ) ''
2024-02-21 03:21:08 +00:00
substituteInPlace \
src/runtime/src/native/libs/System.Globalization.Native/pal_icushim.c \
- - replace-warn ' libicuucName [ 64 ] ' ' libicuucName [ 256 ] ' \
- - replace-warn ' libicui18nName [ 64 ] ' ' libicui18nName [ 256 ] '
2024-06-02 22:30:08 +00:00
'' )
2024-04-03 18:47:33 +00:00
+ lib . optionalString isDarwin ( ''
2024-02-21 03:21:08 +00:00
substituteInPlace \
src/runtime/src/native/libs/System.Globalization.Native/CMakeLists.txt \
- - replace-fail ' /usr/lib/libicucore.dylib ' ' $ { darwin . ICU } /lib/libicucore.dylib '
substituteInPlace \
src/runtime/src/installer/managed/Microsoft.NET.HostModel/HostModelUtils.cs \
src/sdk/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets \
- - replace-fail ' /usr/bin/codesign ' ' $ { sigtool } /bin/codesign '
2024-04-03 18:47:33 +00:00
# fix: strip: error: unknown argument '-n'
2024-02-21 03:21:08 +00:00
substituteInPlace \
2024-04-03 18:47:33 +00:00
src/runtime/eng/native/functions.cmake \
- - replace-fail ' - no_code_signature_warning' " "
2024-02-21 03:21:08 +00:00
# [...]/installer.singlerid.targets(434,5): error MSB3073: The command "pkgbuild [...]" exited with code 127
xmlstarlet ed \
- - inplace \
- s // Project - t elem - n PropertyGroup \
2024-04-03 18:47:33 +00:00
- s \ $ prev - t elem - n SkipInstallerBuild - v true \
src/runtime/Directory.Build.props
2024-07-10 14:19:01 +00:00
# stop passing -sdk without a path
# stop using xcrun
# add -module-cache-path to fix swift errors, see sandboxProfile
# <unknown>:0: error: unable to open output file '/var/folders/[...]/C/clang/ModuleCache/[...]/SwiftShims-[...].pcm': 'Operation not permitted'
# <unknown>:0: error: could not build Objective-C module 'SwiftShims'
substituteInPlace \
src/runtime/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt \
- - replace-fail ' - sdk '' ${ CMAKE_OSX_SYSROOT } ' " " \
- - replace-fail ' xcrun swiftc' ' swiftc - module-cache-path " $ E N V { H O M E } / . c a c h e / m o d u l e - c a c h e " '
2024-04-03 18:47:33 +00:00
''
+ lib . optionalString ( lib . versionAtLeast version " 9 " ) ''
2024-03-25 01:58:13 +00:00
# fix: strip: error: unknown argument '-n'
substituteInPlace \
2024-04-03 18:47:33 +00:00
src/runtime/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets \
2024-08-19 18:06:43 +00:00
src/runtime/src/native/managed/native-library.targets \
2024-03-25 01:58:13 +00:00
- - replace-fail ' - no_code_signature_warning' " "
2024-06-02 22:30:08 +00:00
# ld: library not found for -ld_classic
substituteInPlace \
src/runtime/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets \
src/runtime/src/coreclr/tools/aot/ILCompiler/ILCompiler.csproj \
- - replace-fail ' Include = " - l d _ c l a s s i c " ' " "
2024-04-03 18:47:33 +00:00
''
+ lib . optionalString ( lib . versionOlder version " 9 " ) ''
# [...]/build.proj(123,5): error : Did not find PDBs for the following SDK files:
# [...]/build.proj(123,5): error : sdk/8.0.102/System.Resources.Extensions.dll
# [...]/build.proj(123,5): error : sdk/8.0.102/System.CodeDom.dll
# [...]/build.proj(123,5): error : sdk/8.0.102/FSharp/System.Resources.Extensions.dll
# [...]/build.proj(123,5): error : sdk/8.0.102/FSharp/System.CodeDom.dll
substituteInPlace \
build . proj \
- - replace-fail ' FailOnMissingPDBs = " t r u e " ' ' FailOnMissingPDBs = " f a l s e " '
2024-06-02 22:30:08 +00:00
substituteInPlace \
src/runtime/src/mono/CMakeLists.txt \
- - replace-fail ' /usr/lib/libicucore.dylib ' ' $ { darwin . ICU } /lib/libicucore.dylib '
2024-04-03 18:47:33 +00:00
'' ) ;
2024-02-21 03:21:08 +00:00
prepFlags = [
" - - n o - a r t i f a c t s "
" - - n o - p r e b u i l t s "
2024-06-02 22:30:08 +00:00
" - - w i t h - p a c k a g e s " dotnetSdk . artifacts
2024-02-21 03:21:08 +00:00
] ;
2024-06-02 22:30:08 +00:00
configurePhase = let
prepScript =
if ( lib . versionAtLeast version " 9 " )
then " . / p r e p - s o u r c e - b u i l d . s h "
else " . / p r e p . s h " ;
in ''
2024-02-21 03:21:08 +00:00
runHook preConfigure
# The build process tries to overwrite some things in the sdk (e.g.
# SourceBuild.MSBuildSdkResolver.dll), so it needs to be mutable.
cp - Tr $ { dotnetSdk } . dotnet
chmod - R + w . dotnet
2024-06-02 22:30:08 +00:00
$ { prepScript } $ prepFlags
2024-02-21 03:21:08 +00:00
runHook postConfigure
'' ;
2024-04-03 18:47:33 +00:00
postConfigure = lib . optionalString ( lib . versionAtLeast version " 9 " ) ''
# see patch-npm-packages.proj
typeset - f isScript patchShebangs > src/aspnetcore/patch-shebangs.sh
'' ;
2024-06-15 19:53:16 +00:00
dontConfigureNuget = true ; # NUGET_PACKAGES breaks the build
2024-02-21 03:21:08 +00:00
dontUseCmakeConfigure = true ;
# https://github.com/NixOS/nixpkgs/issues/38991
# bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
LOCALE_ARCHIVE = lib . optionalString isLinux
" ${ glibcLocales } / l i b / l o c a l e / l o c a l e - a r c h i v e " ;
buildFlags = [
" - - w i t h - p a c k a g e s " dotnetSdk . artifacts
" - - c l e a n - w h i l e - b u i l d i n g "
" - - r e l e a s e - m a n i f e s t " releaseManifestFile
2024-04-03 18:47:33 +00:00
]
++ lib . optionals ( lib . versionAtLeast version " 9 " ) [
" - - s o u r c e - b u i l d "
]
++ [
2024-02-21 03:21:08 +00:00
" - - "
" - p : P o r t a b l e B u i l d = t r u e "
] ++ lib . optional ( targetRid != buildRid ) " - p : T a r g e t R i d = ${ targetRid } " ;
buildPhase = ''
runHook preBuild
# on darwin, in a sandbox, this causes:
# CSSM_ModuleLoad(): One or more parameters passed to a function were not valid.
export DOTNET_GENERATE_ASPNET_CERTIFICATE = 0
# CLR_CC/CXX need to be set to stop the build system from using clang-11,
# which is unwrapped
2024-04-03 18:47:33 +00:00
# dotnet needs to be in PATH to fix:
# src/sdk/eng/restore-toolset.sh: line 114: /nix/store/[...]-dotnet-sdk-9.0.100-preview.2.24157.14//.version: Read-only file system
2024-02-21 03:21:08 +00:00
version = \
CLR_CC = $ ( command - v clang ) \
CLR_CXX = $ ( command - v clang ++ ) \
2024-04-03 18:47:33 +00:00
PATH = $ PWD/.dotnet : $ PATH \
2024-02-21 03:21:08 +00:00
./build.sh $ buildFlags
runHook postBuild
'' ;
2024-04-03 18:47:33 +00:00
installPhase = let
assets = if ( lib . versionAtLeast version " 9 " ) then " a s s e t s " else targetArch ;
in ''
2024-02-21 03:21:08 +00:00
runHook preInstall
mkdir " $ o u t "
2024-04-03 18:47:33 +00:00
pushd " a r t i f a c t s / ${ assets } / R e l e a s e "
2024-06-02 22:30:08 +00:00
find . - name \ * . tar . gz | while read archive ; do
target = $ out / $ ( basename " $ a r c h i v e " . tar . gz )
# dotnet 9 currently has two copies of the sdk tarball
[ [ ! - e " $ t a r g e t " ] ] || continue
2024-02-21 03:21:08 +00:00
mkdir " $ t a r g e t "
tar - C " $ t a r g e t " - xzf " $ P W D / $ a r c h i v e "
done
popd
2024-06-15 19:53:16 +00:00
local - r unpacked = " $ P W D / . u n p a c k e d "
for nupkg in $ out/Private.SourceBuilt.Artifacts. * . ${ targetRid } / { , SourceBuildReferencePackages / } * . nupkg ; do
rm - rf " $ u n p a c k e d "
unzip - qd " $ u n p a c k e d " " $ n u p k g "
chmod - R + rw " $ u n p a c k e d "
rm " $ n u p k g "
mv " $ u n p a c k e d " " $ n u p k g "
# TODO: should we fix executable flags here? see dotnetInstallHook
done
2024-02-21 03:21:08 +00:00
runHook postInstall
'' ;
2024-03-12 01:32:42 +00:00
# dotnet cli is in the root, so we need to strip from there
# TODO: should we install in $out/share/dotnet?
stripDebugList = [ " . " ] ;
# stripping dlls results in:
# Failed to load System.Private.CoreLib.dll (error code 0x8007000B)
2024-06-15 19:53:16 +00:00
# stripped crossgen2 results in:
# Failure processing application bundle; possible file corruption.
# this needs to be a bash array
preFixup = ''
stripExclude = ( \ * . dll crossgen2 )
'' ;
2024-03-12 01:32:42 +00:00
2024-02-21 03:21:08 +00:00
passthru = {
inherit releaseManifest buildRid targetRid ;
icu = _icu ;
2024-09-14 18:29:55 +00:00
# ilcompiler is currently broken: https://github.com/dotnet/source-build/issues/1215
hasILCompiler = lib . versionAtLeast version " 9 " ;
2024-02-21 03:21:08 +00:00
} ;
meta = with lib ; {
description = " C o r e f u n c t i o n a l i t y n e e d e d t o c r e a t e . N E T C o r e p r o j e c t s , t h a t i s s h a r e d b e t w e e n V i s u a l S t u d i o a n d C L I " ;
homepage = " h t t p s : / / d o t n e t . g i t h u b . i o / " ;
license = licenses . mit ;
maintainers = with maintainers ; [ corngood ] ;
mainProgram = " d o t n e t " ;
platforms = [
" x 8 6 _ 6 4 - l i n u x "
" a a r c h 6 4 - l i n u x "
" x 8 6 _ 6 4 - d a r w i n "
" a a r c h 6 4 - d a r w i n "
] ;
} ;
}