nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 dotnet-sdk,
4 buildPackages, # buildDotnetModule, dotnet-runtime
5 testers,
6 runCommand,
7 removeReferencesTo,
8}:
9let
10 inherit (buildPackages) buildDotnetModule dotnet-runtime;
11
12 app = buildDotnetModule {
13 name = "use-dotnet-from-env-test-application";
14 src = ./src;
15 nugetDeps = ./nuget-deps.json;
16 useDotnetFromEnv = true;
17 env.TargetFramework = "net${lib.versions.majorMinor (lib.getVersion dotnet-sdk)}";
18 };
19
20 appWithoutFallback = app.overrideAttrs (oldAttrs: {
21 nativeBuildInputs = (oldAttrs.nativeBuildInputs or [ ]) ++ [
22 removeReferencesTo
23 ];
24 postFixup = (oldAttrs.postFixup or "") + ''
25 remove-references-to -t ${dotnet-runtime} "$out/bin/Application"
26 '';
27 });
28
29 runtimeVersion = lib.head (lib.splitString "-" (lib.getVersion dotnet-runtime));
30 runtimeVersionFile = builtins.toFile "dotnet-version.txt" runtimeVersion;
31in
32{
33 fallback = testers.testEqualContents {
34 assertion = "buildDotnetModule sets fallback DOTNET_ROOT in wrapper";
35 expected = runtimeVersionFile;
36 actual = runCommand "use-dotnet-from-env-fallback-test" { } ''
37 ${app}/bin/Application >"$out"
38 '';
39 };
40
41 # Check that appWithoutFallback does not use fallback .NET runtime.
42 without-fallback = testers.testBuildFailure (
43 runCommand "use-dotnet-from-env-without-fallback-test" { } ''
44 ${appWithoutFallback}/bin/Application >"$out"
45 ''
46 );
47
48 # NB assumes that without-fallback above to passes.
49 use-dotnet-root-env = testers.testEqualContents {
50 assertion = "buildDotnetModule uses DOTNET_ROOT from environment in wrapper";
51 expected = runtimeVersionFile;
52 actual =
53 runCommand "use-dotnet-from-env-root-test" { env.DOTNET_ROOT = "${dotnet-runtime}/share/dotnet"; }
54 ''
55 ${appWithoutFallback}/bin/Application >"$out"
56 '';
57 };
58 use-dotnet-path-env = testers.testEqualContents {
59 assertion = "buildDotnetModule uses DOTNET_ROOT from dotnet in PATH in wrapper";
60 expected = runtimeVersionFile;
61 actual = runCommand "use-dotnet-from-env-path-test" { dotnetRuntime = dotnet-runtime; } ''
62 PATH=$dotnetRuntime/bin''${PATH+:}$PATH ${appWithoutFallback}/bin/Application >"$out"
63 '';
64 };
65}