nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 runtimeShell,
4 stdenvNoCC,
5 callPackage,
6 writeShellScript,
7 makeWrapper,
8 dotnetCorePackages,
9 cacert,
10 addNuGetDeps,
11 dotnet-sdk,
12}:
13let
14 default-sdk = dotnet-sdk;
15 transformArgs =
16 finalAttrs:
17 {
18 enableParallelBuilding ? true,
19 # Flags to pass to `makeWrapper`. This is done to avoid double wrapping.
20 makeWrapperArgs ? [ ],
21
22 # Flags to pass to `dotnet restore`.
23 dotnetRestoreFlags ? [ ],
24 # Flags to pass to `dotnet build`.
25 dotnetBuildFlags ? [ ],
26 # Flags to pass to `dotnet test`, if running tests is enabled.
27 dotnetTestFlags ? [ ],
28 # Flags to pass to `dotnet install`.
29 dotnetInstallFlags ? [ ],
30 # Flags to pass to `dotnet pack`.
31 dotnetPackFlags ? [ ],
32 # Flags to pass to dotnet in all phases.
33 dotnetFlags ? [ ],
34
35 # The path to publish the project to. When unset, the directory "$out/lib/$pname" is used.
36 installPath ? null,
37 # The binaries that should get installed to `$out/bin`, relative to `$installPath/`. These get wrapped accordingly.
38 # Unfortunately, dotnet has no method for doing this automatically.
39 # If unset, all executables in the projects root will get installed. This may cause bloat!
40 executables ? null,
41 # Packs a project as a `nupkg`, and installs it to `$out/share`. If set to `true`, the derivation can be used as a dependency for another dotnet project by adding it to `projectReferences`.
42 packNupkg ? false,
43 # The packages project file, which contains instructions on how to compile it. This can be an array of multiple project files as well.
44 projectFile ? null,
45 # The NuGet dependency file. This locks all NuGet dependency versions, as otherwise they cannot be deterministically fetched.
46 # This can be generated by running the `passthru.fetch-deps` script.
47 nugetDeps ? null,
48 # A list of derivations containing nupkg packages for local project references.
49 # Referenced derivations can be built with `buildDotnetModule` with `packNupkg=true` flag.
50 # Since we are sharing them as nugets they must be added to csproj/fsproj files as `PackageReference` as well.
51 # For example, your project has a local dependency:
52 # <ProjectReference Include="../foo/bar.fsproj" />
53 # To enable discovery through `projectReferences` you would need to add a line:
54 # <ProjectReference Include="../foo/bar.fsproj" />
55 # <PackageReference Include="bar" Version="*" Condition=" '$(ContinuousIntegrationBuild)'=='true' "/>
56 projectReferences ? [ ],
57 # Libraries that need to be available at runtime should be passed through this.
58 # These get wrapped into `LD_LIBRARY_PATH`.
59 runtimeDeps ? [ ],
60 # The dotnet runtime ID. If null, fetch-deps will gather dependencies for all
61 # platforms in meta.platforms which are supported by the sdk.
62 runtimeId ? null,
63
64 # Test filters. This gets passed to `dotnet test --filter`, concatenated using `&`.
65 # You may also use `disabledTests` to filter tests based on their name.
66 # See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test#filter-option-details for more details.
67 testFilters ? [ ],
68 # Tests to disable. This gets passed to `dotnet test --filter "FullyQualifiedName!={}"`, to ensure compatibility with all frameworks.
69 # You may also use `testFilters` to pass more generic filters to `dotnet test --filter`.
70 # See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test#filter-option-details for more details.
71 disabledTests ? [ ],
72 # The project file to run unit tests against. This is usually referenced in the regular project file, but sometimes it needs to be manually set.
73 # It gets restored and build, but not installed. You may need to regenerate your nuget lockfile after setting this.
74 testProjectFile ? null,
75
76 # The type of build to perform. This is passed to `dotnet` with the `--configuration` flag. Possible values are `Release`, `Debug`, etc.
77 buildType ? "Release",
78 # If set to true, builds the application as a self-contained - removing the runtime dependency on dotnet
79 selfContainedBuild ? false,
80 # Whether to use an alternative wrapper, that executes the application DLL using the dotnet runtime from the user environment. `dotnet-runtime` is provided as a default in case no .NET is installed
81 # This is useful for .NET tools and applications that may need to run under different .NET runtimes
82 useDotnetFromEnv ? false,
83 # Whether to explicitly enable UseAppHost when building. This is redundant if useDotnetFromEnv is enabled
84 useAppHost ? true,
85 # The dotnet SDK to use.
86 dotnet-sdk ? default-sdk,
87 # The dotnet runtime to use.
88 dotnet-runtime ? dotnet-sdk.runtime,
89 ...
90 }@args:
91 let
92 projectFiles = lib.optionals (projectFile != null) (lib.toList projectFile);
93 testProjectFiles = lib.optionals (testProjectFile != null) (lib.toList testProjectFile);
94
95 platforms =
96 if args ? meta.platforms then
97 lib.intersectLists args.meta.platforms dotnet-sdk.meta.platforms
98 else
99 dotnet-sdk.meta.platforms;
100
101 inherit (callPackage ./hooks { inherit dotnet-sdk dotnet-runtime; })
102 dotnetConfigureHook
103 dotnetBuildHook
104 dotnetCheckHook
105 dotnetInstallHook
106 dotnetFixupHook
107 ;
108
109 inherit (dotnetCorePackages) systemToDotnetRid;
110 in
111 args
112 // {
113 dotnetInstallPath = installPath;
114 dotnetExecutables = executables;
115 dotnetBuildType = buildType;
116 dotnetProjectFiles = projectFiles;
117 dotnetTestProjectFiles = testProjectFiles;
118 dotnetTestFilters = testFilters;
119 dotnetDisabledTests = disabledTests;
120 dotnetRuntimeIds = lib.singleton (
121 if runtimeId != null then runtimeId else systemToDotnetRid stdenvNoCC.hostPlatform.system
122 );
123 dotnetRuntimeDeps = map lib.getLib runtimeDeps;
124 dotnetSelfContainedBuild = selfContainedBuild;
125 dotnetUseAppHost = useAppHost;
126
127 inherit
128 enableParallelBuilding
129 dotnetRestoreFlags
130 dotnetBuildFlags
131 dotnetTestFlags
132 dotnetInstallFlags
133 dotnetPackFlags
134 dotnetFlags
135 packNupkg
136 useDotnetFromEnv
137 nugetDeps
138 runtimeId
139 dotnet-sdk
140 ;
141
142 nativeBuildInputs = args.nativeBuildInputs or [ ] ++ [
143 dotnetConfigureHook
144 dotnetBuildHook
145 dotnetCheckHook
146 dotnetInstallHook
147 dotnetFixupHook
148
149 cacert
150 makeWrapper
151 dotnet-sdk
152 ];
153
154 buildInputs = args.buildInputs or [ ] ++ dotnet-sdk.packages ++ projectReferences;
155
156 # Parse the version attr into a format acceptable for the Version msbuild property
157 # The actual version attr is saved in InformationalVersion, which accepts an arbitrary string
158 versionForDotnet =
159 if !(lib.hasAttr "version" args) || args.version == null then
160 null
161 else
162 let
163 components = lib.pipe args.version [
164 lib.splitVersion
165 (lib.filter (x: (lib.strings.match "[0-9]+" x) != null))
166 (lib.filter (x: (lib.toIntBase10 x) < 65535)) # one version component in dotnet has to fit in 16 bits
167 ];
168 in
169 if (lib.length components) == 0 then
170 null
171 else
172 lib.concatStringsSep "." (
173 (lib.take 4 components)
174 ++ (if (lib.length components) < 4 then lib.replicate (4 - (lib.length components)) "0" else [ ])
175 );
176
177 makeWrapperArgs = args.makeWrapperArgs or [ ] ++ [
178 "--prefix"
179 "LD_LIBRARY_PATH"
180 ":"
181 "${dotnet-sdk.icu}/lib"
182 ];
183
184 # Stripping breaks the executable
185 dontStrip = args.dontStrip or true;
186
187 # gappsWrapperArgs gets included when wrapping for dotnet, as to avoid double wrapping
188 dontWrapGApps = args.dontWrapGApps or true;
189
190 # propagate the runtime sandbox profile since the contents apply to published
191 # executables
192 propagatedSandboxProfile = lib.optionalString (dotnet-runtime != null) (
193 toString dotnet-runtime.__propagatedSandboxProfile
194 );
195
196 meta = (args.meta or { }) // {
197 inherit platforms;
198 };
199 };
200
201in
202fnOrAttrs:
203stdenvNoCC.mkDerivation (
204 finalAttrs:
205 let
206 args = if lib.isFunction fnOrAttrs then fnOrAttrs (args' // finalAttrs) else fnOrAttrs;
207 args' = transformArgs finalAttrs args;
208 inherit (args')
209 nugetDeps
210 runtimeId
211 meta
212 dotnet-sdk
213 ;
214 args'' = removeAttrs args' [
215 "nugetDeps"
216 "runtimeId"
217 "installPath"
218 "executables"
219 "projectFile"
220 "projectReferences"
221 "runtimeDeps"
222 "disabledTests"
223 "testProjectFile"
224 "buildType"
225 "selfContainedBuild"
226 "useDotnet"
227 "useAppHost"
228 "dotnet-sdk"
229 ];
230 in
231 if nugetDeps != null then
232 addNuGetDeps {
233 inherit nugetDeps;
234 overrideFetchAttrs =
235 old:
236 lib.optionalAttrs ((args'.runtimeId or null) == null) rec {
237 dotnetRuntimeIds = map (system: dotnetCorePackages.systemToDotnetRid system) meta.platforms;
238 buildInputs =
239 old.buildInputs
240 ++ lib.concatLists (lib.attrValues (lib.getAttrs dotnetRuntimeIds dotnet-sdk.targetPackages));
241 };
242 } args'' finalAttrs
243 else
244 args''
245)