summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/dotnet/build-dotnet-module/default.nix
blob: e7bc9e80cbfd1c28d08e7642670f6f5353750e09 (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
{
  lib,
  runtimeShell,
  stdenvNoCC,
  callPackage,
  writeShellScript,
  makeWrapper,
  dotnetCorePackages,
  cacert,
  addNuGetDeps,
  dotnet-sdk,
}:
let
  default-sdk = dotnet-sdk;
  transformArgs =
    finalAttrs:
    {
      enableParallelBuilding ? true,
      # Flags to pass to `makeWrapper`. This is done to avoid double wrapping.
      makeWrapperArgs ? [ ],

      # Flags to pass to `dotnet restore`.
      dotnetRestoreFlags ? [ ],
      # Flags to pass to `dotnet build`.
      dotnetBuildFlags ? [ ],
      # Flags to pass to `dotnet test`, if running tests is enabled.
      dotnetTestFlags ? [ ],
      # Flags to pass to `dotnet install`.
      dotnetInstallFlags ? [ ],
      # Flags to pass to `dotnet pack`.
      dotnetPackFlags ? [ ],
      # Flags to pass to dotnet in all phases.
      dotnetFlags ? [ ],

      # The path to publish the project to. When unset, the directory "$out/lib/$pname" is used.
      installPath ? null,
      # The binaries that should get installed to `$out/bin`, relative to `$installPath/`. These get wrapped accordingly.
      # Unfortunately, dotnet has no method for doing this automatically.
      # If unset, all executables in the projects root will get installed. This may cause bloat!
      executables ? null,
      dontPublish ? false,
      # 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`.
      packNupkg ? false,
      # The packages project file, which contains instructions on how to compile it. This can be an array of multiple project files as well.
      projectFile ? null,
      # The NuGet dependency file. This locks all NuGet dependency versions, as otherwise they cannot be deterministically fetched.
      # This can be generated by running the `passthru.fetch-deps` script.
      nugetDeps ? null,
      # A list of derivations containing nupkg packages for local project references.
      # Referenced derivations can be built with `buildDotnetModule` with `packNupkg=true` flag.
      # Since we are sharing them as nugets they must be added to csproj/fsproj files as `PackageReference` as well.
      # For example, your project has a local dependency:
      #     <ProjectReference Include="../foo/bar.fsproj" />
      # To enable discovery through `projectReferences` you would need to add a line:
      #     <ProjectReference Include="../foo/bar.fsproj" />
      #     <PackageReference Include="bar" Version="*" Condition=" '$(ContinuousIntegrationBuild)'=='true' "/>
      projectReferences ? [ ],
      # Libraries that need to be available at runtime should be passed through this.
      # These get wrapped into `LD_LIBRARY_PATH`.
      runtimeDeps ? [ ],
      # The dotnet runtime ID. If null, fetch-deps will gather dependencies for all
      # platforms in meta.platforms which are supported by the sdk.
      runtimeId ? null,

      # Test filters. This gets passed to `dotnet test --filter`, concatenated using `&`.
      # You may also use `disabledTests` to filter tests based on their name.
      # See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test#filter-option-details for more details.
      testFilters ? [ ],
      # Tests to disable. This gets passed to `dotnet test --filter "FullyQualifiedName!={}"`, to ensure compatibility with all frameworks.
      # You may also use `testFilters` to pass more generic filters to `dotnet test --filter`.
      # See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test#filter-option-details for more details.
      disabledTests ? [ ],
      # 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.
      # It gets restored and build, but not installed. You may need to regenerate your nuget lockfile after setting this.
      testProjectFile ? null,

      # The type of build to perform. This is passed to `dotnet` with the `--configuration` flag. Possible values are `Release`, `Debug`, etc.
      buildType ? "Release",
      # If set to true, builds the application as a self-contained - removing the runtime dependency on dotnet
      selfContainedBuild ? false,
      # 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
      # This is useful for .NET tools and applications that may need to run under different .NET runtimes
      useDotnetFromEnv ? false,
      # Whether to explicitly enable UseAppHost when building. This is redundant if useDotnetFromEnv is enabled
      useAppHost ? true,
      # The dotnet SDK to use.
      dotnet-sdk ? default-sdk,
      # The dotnet runtime to use.
      dotnet-runtime ? dotnet-sdk.runtime,
      ...
    }@args:
    let
      projectFiles = lib.optionals (projectFile != null) (lib.toList projectFile);
      testProjectFiles = lib.optionals (testProjectFile != null) (lib.toList testProjectFile);

      platforms =
        if args ? meta.platforms then
          lib.intersectLists args.meta.platforms dotnet-sdk.meta.platforms
        else
          dotnet-sdk.meta.platforms;

      hook = callPackage ./hook { inherit dotnet-runtime; };

      inherit (dotnetCorePackages) systemToDotnetRid;
    in
    args
    // {
      dotnetInstallPath = installPath;
      dotnetExecutables = executables;
      dotnetBuildType = buildType;
      dotnetProjectFiles = projectFiles;
      dotnetTestProjectFiles = testProjectFiles;
      dotnetTestFilters = testFilters;
      dotnetDisabledTests = disabledTests;
      dotnetRuntimeIds = lib.singleton (
        if runtimeId != null then runtimeId else systemToDotnetRid stdenvNoCC.hostPlatform.system
      );
      dotnetRuntimeDeps = map lib.getLib runtimeDeps;
      dotnetSelfContainedBuild = selfContainedBuild;
      dotnetUseAppHost = useAppHost;

      inherit
        enableParallelBuilding
        dotnetRestoreFlags
        dotnetBuildFlags
        dotnetTestFlags
        dotnetInstallFlags
        dotnetPackFlags
        dotnetFlags
        packNupkg
        useDotnetFromEnv
        nugetDeps
        runtimeId
        dotnet-sdk
        ;

      nativeBuildInputs = args.nativeBuildInputs or [ ] ++ [
        hook

        cacert
        makeWrapper
        dotnet-sdk
      ];

      buildInputs = args.buildInputs or [ ] ++ dotnet-sdk.packages ++ projectReferences;

      # Parse the version attr into a format acceptable for the Version msbuild property
      # The actual version attr is saved in InformationalVersion, which accepts an arbitrary string
      versionForDotnet =
        if !(lib.hasAttr "version" args) || args.version == null then
          null
        else
          let
            components = lib.pipe args.version [
              lib.splitVersion
              (lib.filter (x: (lib.strings.match "[0-9]+" x) != null))
              (lib.filter (x: (lib.toIntBase10 x) < 65535)) # one version component in dotnet has to fit in 16 bits
            ];
          in
          if (lib.length components) == 0 then
            null
          else
            lib.concatStringsSep "." (
              (lib.take 4 components)
              ++ (if (lib.length components) < 4 then lib.replicate (4 - (lib.length components)) "0" else [ ])
            );

      makeWrapperArgs = args.makeWrapperArgs or [ ] ++ [
        "--prefix"
        "LD_LIBRARY_PATH"
        ":"
        "${dotnet-sdk.icu}/lib"
      ];

      # Stripping breaks the executable
      dontStrip = args.dontStrip or true;

      # gappsWrapperArgs gets included when wrapping for dotnet, as to avoid double wrapping
      dontWrapGApps = args.dontWrapGApps or true;

      # propagate the runtime sandbox profile since the contents apply to published
      # executables
      propagatedSandboxProfile = lib.optionalString (dotnet-runtime != null) (
        toString dotnet-runtime.__propagatedSandboxProfile
      );

      meta = (args.meta or { }) // {
        inherit platforms;
      };
    };

in
fnOrAttrs:
stdenvNoCC.mkDerivation (
  finalAttrs:
  let
    args = if lib.isFunction fnOrAttrs then fnOrAttrs (args' // finalAttrs) else fnOrAttrs;
    args' = transformArgs finalAttrs args;
    inherit (args')
      nugetDeps
      runtimeId
      meta
      dotnet-sdk
      ;
    args'' = removeAttrs args' [
      "nugetDeps"
      "runtimeId"
      "installPath"
      "executables"
      "projectFile"
      "projectReferences"
      "runtimeDeps"
      "disabledTests"
      "testProjectFile"
      "buildType"
      "selfContainedBuild"
      "useDotnet"
      "useAppHost"
      "dotnet-sdk"
    ];
  in
  if nugetDeps != null then
    addNuGetDeps {
      inherit nugetDeps;
      overrideFetchAttrs =
        old:
        lib.optionalAttrs ((args'.runtimeId or null) == null) rec {
          dotnetRuntimeIds = map (system: dotnetCorePackages.systemToDotnetRid system) meta.platforms;
          buildInputs =
            old.buildInputs
            ++ lib.concatLists (lib.attrValues (lib.getAttrs dotnetRuntimeIds dotnet-sdk.targetPackages));
        };
    } args'' finalAttrs
  else
    args''
)