nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv, fetchurl, fetchpatch, makeWrapper, glibcLocales, mono, dotnetPackages, unzip, dotnetCorePackages, writeText, roslyn }:
2
3let
4
5 dotnet-sdk = dotnetCorePackages.sdk_6_0;
6
7 xplat = fetchurl {
8 url = "https://github.com/mono/msbuild/releases/download/v16.9.0/mono_msbuild_6.12.0.137.zip";
9 sha256 = "1wnzbdpk4s9bmawlh359ak2b8zi0sgx1qvcjnvfncr1wsck53v7q";
10 };
11
12 deps = map (package: package.src)
13 (import ./deps.nix { inherit fetchurl; });
14
15 nuget-config = writeText "NuGet.config" ''
16 <?xml version="1.0" encoding="utf-8"?>
17 <configuration>
18 <packageSources>
19 <clear />
20 </packageSources>
21 </configuration>
22 '';
23
24 inherit (stdenv.hostPlatform.extensions) sharedLibrary;
25
26in
27
28stdenv.mkDerivation rec {
29 pname = "msbuild";
30 version = "16.10.1+xamarinxplat.2021.05.26.14.00";
31
32 src = fetchurl {
33 url = "https://download.mono-project.com/sources/msbuild/msbuild-${version}.tar.xz";
34 sha256 = "05ghqqkdj4s3d0xkp7mkdzjig5zj3k6ajx71j0g2wv6rdbvg6899";
35 };
36
37 nativeBuildInputs = [
38 dotnet-sdk
39 mono
40 unzip
41 makeWrapper
42 ];
43
44 buildInputs = [
45 dotnetPackages.Nuget
46 glibcLocales
47 ];
48
49 # https://github.com/NixOS/nixpkgs/issues/38991
50 # bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
51 LOCALE_ARCHIVE = lib.optionalString stdenv.isLinux
52 "${glibcLocales}/lib/locale/locale-archive";
53
54 postPatch = ''
55 # not patchShebangs, there is /bin/bash in the body of the script as well
56 substituteInPlace ./eng/cibuild_bootstrapped_msbuild.sh --replace /bin/bash ${stdenv.shell}
57
58 patchShebangs eng/*.sh mono/build/*.sh
59
60 sed -i -e "/<\/projectImportSearchPaths>/a <property name=\"MSBuildExtensionsPath\" value=\"$out/lib/mono/xbuild\"/>" \
61 src/MSBuild/app.config
62
63 # license check is case sensitive
64 mv LICENSE license.bak && mv license.bak license
65 '';
66
67 buildPhase = ''
68 # nuget would otherwise try to base itself in /homeless-shelter
69 export HOME=$(pwd)/fake-home
70
71 cp ${nuget-config} NuGet.config
72 nuget sources Add -Name nixos -Source $(pwd)/nixos
73
74 for package in ${toString deps}; do
75 nuget add $package -Source nixos
76 done
77
78 mkdir -p artifacts
79 unzip ${xplat} -d artifacts
80 mv artifacts/msbuild artifacts/mono-msbuild
81 chmod +x artifacts/mono-msbuild/MSBuild.dll
82
83 # The provided libhostfxr.dylib is for x86_64-darwin, so we remove it
84 rm artifacts/mono-msbuild/SdkResolvers/Microsoft.DotNet.MSBuildSdkResolver/libhostfxr.dylib
85
86 ln -s $(find ${dotnet-sdk} -name libhostfxr${sharedLibrary}) artifacts/mono-msbuild/SdkResolvers/Microsoft.DotNet.MSBuildSdkResolver/
87
88 # overwrite the file
89 echo "#!${stdenv.shell}" > eng/common/dotnet-install.sh
90 echo "#!${stdenv.shell}" > mono/build/get_sdk_files.sh
91
92 # Prevent msbuild from downloading a new libhostfxr
93 echo "#!${stdenv.shell}" > mono/build/extract_and_copy_hostfxr.sh
94
95 mkdir -p mono/dotnet-overlay/msbuild-bin
96 cp ${dotnet-sdk}/sdk/*/{Microsoft.NETCoreSdk.BundledVersions.props,RuntimeIdentifierGraph.json} mono/dotnet-overlay/msbuild-bin
97
98 # DisableNerdbankVersioning https://gitter.im/Microsoft/msbuild/archives/2018/06/27?at=5b33dbc4ce3b0f268d489bfa
99 # TODO there are some (many?) failing tests
100 ./eng/cibuild_bootstrapped_msbuild.sh --host_type mono --configuration Release --skip_tests /p:DisableNerdbankVersioning=true
101 patchShebangs stage1/mono-msbuild/msbuild
102 '';
103
104 installPhase = ''
105 stage1/mono-msbuild/msbuild mono/build/install.proj /p:MonoInstallPrefix="$out" /p:Configuration=Release-MONO
106
107 ln -s ${roslyn}/lib/dotnet/microsoft.net.compilers.toolset/*/tasks/net472 $out/lib/mono/msbuild/Current/bin/Roslyn
108
109 makeWrapper ${mono}/bin/mono $out/bin/msbuild \
110 --set-default MONO_GC_PARAMS "nursery-size=64m" \
111 --add-flags "$out/lib/mono/msbuild/15.0/bin/MSBuild.dll"
112
113 ln -s $(find ${dotnet-sdk} -name libhostfxr${sharedLibrary}) $out/lib/mono/msbuild/Current/bin/SdkResolvers/Microsoft.DotNet.MSBuildSdkResolver/
114 '';
115
116 doInstallCheck = true;
117
118 # https://docs.microsoft.com/cs-cz/visualstudio/msbuild/walkthrough-creating-an-msbuild-project-file-from-scratch?view=vs-2019
119 installCheckPhase = ''
120 cat > Helloworld.cs <<EOF
121using System;
122
123class HelloWorld
124{
125 static void Main()
126 {
127#if DebugConfig
128 Console.WriteLine("WE ARE IN THE DEBUG CONFIGURATION");
129#endif
130
131 Console.WriteLine("Hello, world!");
132 }
133}
134EOF
135
136 cat > Helloworld.csproj <<EOF
137<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
138 <ItemGroup>
139 <Compile Include="Helloworld.cs" />
140 </ItemGroup>
141 <Target Name="Build">
142 <Csc Sources="@(Compile)"/>
143 </Target>
144</Project>
145EOF
146
147 $out/bin/msbuild Helloworld.csproj -t:Build
148 ${mono}/bin/mono Helloworld.exe | grep "Hello, world!"
149 '';
150
151 meta = with lib; {
152 description = "Mono version of Microsoft Build Engine, the build platform for .NET, and Visual Studio";
153 homepage = "https://github.com/mono/msbuild";
154 sourceProvenance = with sourceTypes; [
155 fromSource
156 binaryNativeCode # dependencies
157 ];
158 license = licenses.mit;
159 maintainers = with maintainers; [ jdanek ];
160 platforms = platforms.unix;
161 };
162}