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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
{
stdenv,
stdenvNoCC,
lib,
writeText,
testers,
runCommand,
runCommandWith,
darwin,
expect,
curl,
installShellFiles,
callPackage,
zlib,
swiftPackages,
icu,
lndir,
replaceVars,
nugetPackageHook,
xmlstarlet,
pkgs,
}:
type: unwrapped:
stdenvNoCC.mkDerivation (finalAttrs: {
pname = "${unwrapped.pname}-wrapped";
inherit (unwrapped) version;
meta = {
description = "${unwrapped.meta.description or "dotnet"} (wrapper)";
mainProgram = "dotnet";
inherit (unwrapped.meta)
homepage
license
maintainers
platforms
broken
;
};
src = unwrapped;
dontUnpack = true;
setupHooks = [
./dotnet-setup-hook.sh
]
++ lib.optional (type == "sdk") (
replaceVars ./dotnet-sdk-setup-hook.sh {
inherit lndir xmlstarlet;
}
);
propagatedSandboxProfile = toString unwrapped.__propagatedSandboxProfile;
propagatedBuildInputs = lib.optional (type == "sdk") nugetPackageHook;
nativeBuildInputs = [ installShellFiles ];
outputs = [ "out" ] ++ lib.optional (unwrapped ? man) "man";
installPhase = ''
runHook preInstall
mkdir -p "$out"/bin "$out"/share
ln -s "$src"/bin/* "$out"/bin
ln -s "$src"/share/dotnet "$out"/share
runHook postInstall
'';
postInstall = ''
# completions snippets taken from https://learn.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete
installShellCompletion --cmd dotnet \
--bash ${./completions/dotnet.bash} \
--zsh ${./completions/dotnet.zsh} \
--fish ${./completions/dotnet.fish}
'';
doInstallCheck = true;
installCheckPhase = ''
runHook preInstallCheck
(
source ${./dotnet-setup-hook.sh}
$out/bin/dotnet --info
)
runHook postInstallCheck
'';
postFixup = lib.optionalString (unwrapped ? man) ''
ln -s ${unwrapped.man} "$man"
'';
passthru =
unwrapped.passthru
// lib.optionalAttrs (unwrapped ? artifacts) {
inherit (unwrapped) artifacts;
}
// {
inherit unwrapped;
tests =
let
mkDotnetTest =
{
name,
stdenv ? stdenvNoCC,
template,
lang ? null,
usePackageSource ? false,
build,
buildInputs ? [ ],
runtime ? finalAttrs.finalPackage.runtime,
runInputs ? [ ],
run ? null,
runAllowNetworking ? false,
}:
let
sdk = finalAttrs.finalPackage;
built = stdenv.mkDerivation {
name = "${sdk.name}-test-${name}";
buildInputs = [ sdk ] ++ buildInputs ++ lib.optionals usePackageSource sdk.packages;
# make sure ICU works in a sandbox
propagatedSandboxProfile = toString sdk.__propagatedSandboxProfile;
unpackPhase =
let
unpackArgs = [
template
]
++ lib.optionals (lang != null) [
"-lang"
lang
];
in
''
mkdir test
cd test
dotnet new ${lib.escapeShellArgs unpackArgs} -o . --no-restore
'';
buildPhase = build;
dontPatchELF = true;
};
in
# older SDKs don't include an embedded FSharp.Core package
if lang == "F#" && lib.versionOlder sdk.version "6.0.400" then
null
else if run == null then
built
else
runCommand "${built.name}-run"
(
{
src = built;
nativeBuildInputs = [ built ] ++ runInputs;
passthru = {
inherit built;
};
}
// lib.optionalAttrs (stdenv.hostPlatform.isDarwin && runAllowNetworking) {
sandboxProfile = ''
(allow network-inbound (local ip))
(allow mach-lookup (global-name "com.apple.FSEvents"))
'';
__darwinAllowLocalNetworking = true;
}
)
(
lib.optionalString (runtime != null) ''
export DOTNET_ROOT=${runtime}/share/dotnet
''
+ run
);
mkConsoleTests =
lang: suffix: output:
let
# Setting LANG to something other than 'C' forces the runtime to search
# for ICU, which will be required in most user environments.
checkConsoleOutput = command: ''
output="$(LANG=C.UTF-8 ${command})"
[[ "$output" =~ ${output} ]] && touch "$out"
'';
mkConsoleTest =
{ name, ... }@args:
mkDotnetTest (
args
// {
name = "console-${name}-${suffix}";
template = "console";
inherit lang;
}
);
in
lib.recurseIntoAttrs {
run = mkConsoleTest {
name = "run";
build = checkConsoleOutput "dotnet run";
};
publish = mkConsoleTest {
name = "publish";
build = "dotnet publish -o $out/bin";
run = checkConsoleOutput "$src/bin/test";
};
self-contained = mkConsoleTest {
name = "self-contained";
usePackageSource = true;
build = "dotnet publish --use-current-runtime --sc -o $out";
runtime = null;
run = checkConsoleOutput "$src/test";
};
single-file = mkConsoleTest {
name = "single-file";
usePackageSource = true;
build = "dotnet publish --use-current-runtime -p:PublishSingleFile=true -o $out/bin";
runtime = null;
run = checkConsoleOutput "$src/bin/test";
};
ready-to-run = mkConsoleTest {
name = "ready-to-run";
usePackageSource = true;
build = "dotnet publish --use-current-runtime -p:PublishReadyToRun=true -o $out/bin";
run = checkConsoleOutput "$src/bin/test";
};
}
// lib.optionalAttrs finalAttrs.finalPackage.hasILCompiler {
aot = mkConsoleTest {
name = "aot";
stdenv = if stdenv.hostPlatform.isDarwin then swiftPackages.stdenv else stdenv;
usePackageSource = true;
buildInputs = [
zlib
]
++ lib.optionals stdenv.hostPlatform.isDarwin [
swiftPackages.swift
darwin.ICU
];
build = ''
dotnet restore -p:PublishAot=true
dotnet publish -p:PublishAot=true -o $out/bin
'';
runtime = null;
run = checkConsoleOutput "$src/bin/test";
};
};
mkWebTest =
lang: suffix:
mkDotnetTest {
name = "web-${suffix}";
template = "web";
inherit lang;
build = "dotnet publish -o $out/bin";
runtime = finalAttrs.finalPackage.aspnetcore;
runInputs = [
expect
curl
];
run = ''
expect <<"EOF"
set status 1
spawn $env(src)/bin/test
proc abort { } { exit 2 }
expect_before default abort
expect -re {Now listening on: ([^\r]+)\r} {
set url $expect_out(1,string)
}
expect "Application started. Press Ctrl+C to shut down."
set output [exec curl -sSf $url]
if {$output != "Hello World!"} {
send_error "Unexpected output: $output\n"
exit 1
}
send \x03
expect_before timeout abort
expect eof
catch wait result
exit [lindex $result 3]
EOF
touch $out
'';
runAllowNetworking = true;
};
in
unwrapped.passthru.tests or { }
// {
version = testers.testVersion {
package = finalAttrs.finalPackage;
command = "HOME=$(mktemp -d) dotnet " + (if type == "sdk" then "--version" else "--info");
};
}
// lib.optionalAttrs (type == "sdk") {
buildDotnetModule = lib.recurseIntoAttrs (
(pkgs.appendOverlays [
(self: super: {
dotnet-sdk = finalAttrs.finalPackage;
dotnet-runtime = finalAttrs.finalPackage.runtime;
})
]).callPackage
../../../test/dotnet/default.nix
{ }
);
console = lib.recurseIntoAttrs {
# yes, older SDKs omit the comma
cs = mkConsoleTests "C#" "cs" "Hello,?\\ World!";
fs = mkConsoleTests "F#" "fs" "Hello\\ from\\ F#";
vb = mkConsoleTests "VB" "vb" "Hello,?\\ World!";
};
web = lib.recurseIntoAttrs {
cs = mkWebTest "C#" "cs";
fs = mkWebTest "F#" "fs";
};
};
};
})
|