summaryrefslogtreecommitdiff
path: root/pkgs/development/compilers/dotnet/binary/build-dotnet.nix
blob: 7711cc2fee6567289e4c95c9e8a85c43abc08950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
{
  type,
  version,
  srcs,
  commonPackages ? null,
  hostPackages ? null,
  targetPackages ? null,
  runtime ? null,
  aspnetcore ? null,
}:

assert builtins.elem type [
  "aspnetcore"
  "runtime"
  "sdk"
];
assert
  if type == "sdk" then
    commonPackages != null
    && hostPackages != null
    && targetPackages != null
    && runtime != null
    && aspnetcore != null
  else
    true;

{
  lib,
  stdenv,
  fetchurl,
  writeText,
  autoPatchelfHook,
  makeWrapper,
  libunwind,
  icu,
  libuuid,
  zlib,
  libkrb5,
  openssl,
  curl,
  lttng-ust_2_12,
  testers,
  runCommand,
  writeShellScript,
  mkNugetDeps,
  callPackage,
  systemToDotnetRid,
  xmlstarlet,
}:

let
  pname =
    if type == "aspnetcore" then
      "aspnetcore-runtime"
    else if type == "runtime" then
      "dotnet-runtime"
    else
      "dotnet-sdk";

  descriptions = {
    aspnetcore = "ASP.NET Core Runtime ${version}";
    runtime = ".NET Runtime ${version}";
    sdk = ".NET SDK ${version}";
  };

  mkWrapper = callPackage ../wrapper.nix { };

  hostRid = systemToDotnetRid stdenv.hostPlatform.system;
  targetRid = systemToDotnetRid stdenv.targetPlatform.system;

  sigtool = callPackage ../sigtool.nix { };
  signAppHost = callPackage ./sign-apphost.nix { };

  hasILCompiler = lib.versionAtLeast version (if hostRid == "osx-arm64" then "8" else "7");

  extraTargets = writeText "extra.targets" (
    ''
      <Project>
    ''
    + lib.optionalString hasILCompiler ''
      <ItemGroup>
        <CustomLinkerArg Include="-Wl,-rpath,'${
          lib.makeLibraryPath [
            icu
            zlib
            openssl
          ]
        }'" />
      </ItemGroup>
    ''
    + lib.optionalString stdenv.hostPlatform.isDarwin ''
      <Import Project="${signAppHost}" />
    ''
    + ''
      </Project>
    ''
  );

in
mkWrapper type (
  stdenv.mkDerivation (finalAttrs: {
    inherit pname version;

    # Some of these dependencies are `dlopen()`ed.
    nativeBuildInputs = [
      makeWrapper
    ]
    ++ lib.optional stdenv.hostPlatform.isLinux autoPatchelfHook
    ++ lib.optionals (type == "sdk" && stdenv.hostPlatform.isDarwin) [
      xmlstarlet
      sigtool
    ];

    buildInputs = [
      stdenv.cc.cc
      zlib
      icu
      libkrb5
      curl
      xmlstarlet
    ]
    ++ lib.optional stdenv.hostPlatform.isLinux lttng-ust_2_12;

    src = fetchurl (
      srcs.${hostRid} or (throw "Missing source (url and hash) for host RID: ${hostRid}")
    );

    sourceRoot = ".";

    postPatch =
      if type == "sdk" then
        (
          ''
            xmlstarlet ed \
              --inplace \
              -s //_:Project -t elem -n Import \
              -i \$prev -t attr -n Project -v "${extraTargets}" \
              sdk/*/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets
          ''
          + lib.optionalString (stdenv.hostPlatform.isDarwin && lib.versionOlder version "10") ''
            codesign --remove-signature packs/Microsoft.NETCore.App.Host.osx-*/*/runtimes/osx-*/native/{apphost,singlefilehost}
          ''
        )
      else
        null;

    dontPatchELF = true;
    noDumpEnvVars = true;

    installPhase = ''
      runHook preInstall

      mkdir -p $out/share/doc/$pname/$version
      mv LICENSE.txt $out/share/doc/$pname/$version/
      mv ThirdPartyNotices.txt $out/share/doc/$pname/$version/

      mkdir -p $out/share/dotnet
      cp -r ./ $out/share/dotnet

      mkdir -p $out/bin
      ln -s $out/share/dotnet/dotnet $out/bin/dotnet
    ''
    + lib.optionalString (type == "sdk" && lib.versionAtLeast version "10") ''
      ln -s "$out"/share/dotnet/dnx "$out"/bin/dnx
    ''
    + ''

      runHook postInstall
    '';

    # Tell autoPatchelf about runtime dependencies.
    # (postFixup phase is run before autoPatchelfHook.)
    postFixup = lib.optionalString stdenv.hostPlatform.isLinux ''
      patchelf \
        --add-needed libicui18n.so \
        --add-needed libicuuc.so \
        $out/share/dotnet/shared/Microsoft.NETCore.App/*/libcoreclr.so \
        $out/share/dotnet/shared/Microsoft.NETCore.App/*/*System.Globalization.Native.so \
        $out/share/dotnet/packs/Microsoft.NETCore.App.Host.${hostRid}/*/runtimes/${hostRid}/native/*host
      patchelf \
        --add-needed libgssapi_krb5.so \
        $out/share/dotnet/shared/Microsoft.NETCore.App/*/*System.Net.Security.Native.so \
        $out/share/dotnet/packs/Microsoft.NETCore.App.Host.${hostRid}/*/runtimes/${hostRid}/native/*host
      patchelf \
        --add-needed libssl.so \
        $out/share/dotnet/shared/Microsoft.NETCore.App/*/*System.Security.Cryptography.Native.OpenSsl.so \
        $out/share/dotnet/packs/Microsoft.NETCore.App.Host.${hostRid}/*/runtimes/${hostRid}/native/*host
    '';

    # fixes: Could not load ICU data. UErrorCode: 2
    propagatedSandboxProfile = lib.optionalString stdenv.hostPlatform.isDarwin ''
      (allow file-read* (subpath "/usr/share/icu"))
      (allow file-read* (subpath "/private/var/db/mds/system"))
      (allow mach-lookup (global-name "com.apple.SecurityServer")
                         (global-name "com.apple.system.opendirectoryd.membership"))
    '';

    passthru = {
      inherit icu hasILCompiler;
      # https://github.com/dotnet/sdk/pull/53405
      hasCrossTargetBug = lib.versionAtLeast version "10";
    }
    // lib.optionalAttrs (type == "sdk") (
      let
        # force evaluation of the SDK package to ensure evaluation failures
        # (e.g. due to vulnerabilities) propagate to the nuget packages
        forceSDKEval = builtins.seq finalAttrs.finalPackage.drvPath;
      in
      {
        packages = map forceSDKEval (
          commonPackages ++ hostPackages.${hostRid} ++ targetPackages.${targetRid}
        );
        targetPackages = lib.mapAttrs (_: map forceSDKEval) targetPackages;
        inherit runtime aspnetcore;
      }
    );

    meta = {
      description = builtins.getAttr type descriptions;
      homepage = "https://dotnet.github.io/";
      license = lib.licenses.mit;
      maintainers = with lib.maintainers; [
        kuznero
        mdarocha
        corngood
      ];
      mainProgram = "dotnet";
      platforms = lib.filter (
        platform:
        let
          e = builtins.tryEval (systemToDotnetRid platform);
        in
        e.success && srcs ? "${e.value}"
      ) lib.platforms.all;
      sourceProvenance = with lib.sourceTypes; [
        binaryBytecode
        binaryNativeCode
      ];
      knownVulnerabilities =
        lib.optionals
          (lib.elem (lib.head (lib.splitVersion version)) [
            "6"
            "7"
          ])
          [
            "Dotnet SDK ${version} is EOL, please use 8.0 (LTS) or 9.0 (Current)"
          ];
    };
  })
)