at master 235 lines 9.2 kB view raw
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 hook = callPackage ./hook { inherit dotnet-runtime; }; 102 103 inherit (dotnetCorePackages) systemToDotnetRid; 104 in 105 args 106 // { 107 dotnetInstallPath = installPath; 108 dotnetExecutables = executables; 109 dotnetBuildType = buildType; 110 dotnetProjectFiles = projectFiles; 111 dotnetTestProjectFiles = testProjectFiles; 112 dotnetTestFilters = testFilters; 113 dotnetDisabledTests = disabledTests; 114 dotnetRuntimeIds = lib.singleton ( 115 if runtimeId != null then runtimeId else systemToDotnetRid stdenvNoCC.hostPlatform.system 116 ); 117 dotnetRuntimeDeps = map lib.getLib runtimeDeps; 118 dotnetSelfContainedBuild = selfContainedBuild; 119 dotnetUseAppHost = useAppHost; 120 121 inherit 122 enableParallelBuilding 123 dotnetRestoreFlags 124 dotnetBuildFlags 125 dotnetTestFlags 126 dotnetInstallFlags 127 dotnetPackFlags 128 dotnetFlags 129 packNupkg 130 useDotnetFromEnv 131 nugetDeps 132 runtimeId 133 dotnet-sdk 134 ; 135 136 nativeBuildInputs = args.nativeBuildInputs or [ ] ++ [ 137 hook 138 139 cacert 140 makeWrapper 141 dotnet-sdk 142 ]; 143 144 buildInputs = args.buildInputs or [ ] ++ dotnet-sdk.packages ++ projectReferences; 145 146 # Parse the version attr into a format acceptable for the Version msbuild property 147 # The actual version attr is saved in InformationalVersion, which accepts an arbitrary string 148 versionForDotnet = 149 if !(lib.hasAttr "version" args) || args.version == null then 150 null 151 else 152 let 153 components = lib.pipe args.version [ 154 lib.splitVersion 155 (lib.filter (x: (lib.strings.match "[0-9]+" x) != null)) 156 (lib.filter (x: (lib.toIntBase10 x) < 65535)) # one version component in dotnet has to fit in 16 bits 157 ]; 158 in 159 if (lib.length components) == 0 then 160 null 161 else 162 lib.concatStringsSep "." ( 163 (lib.take 4 components) 164 ++ (if (lib.length components) < 4 then lib.replicate (4 - (lib.length components)) "0" else [ ]) 165 ); 166 167 makeWrapperArgs = args.makeWrapperArgs or [ ] ++ [ 168 "--prefix" 169 "LD_LIBRARY_PATH" 170 ":" 171 "${dotnet-sdk.icu}/lib" 172 ]; 173 174 # Stripping breaks the executable 175 dontStrip = args.dontStrip or true; 176 177 # gappsWrapperArgs gets included when wrapping for dotnet, as to avoid double wrapping 178 dontWrapGApps = args.dontWrapGApps or true; 179 180 # propagate the runtime sandbox profile since the contents apply to published 181 # executables 182 propagatedSandboxProfile = lib.optionalString (dotnet-runtime != null) ( 183 toString dotnet-runtime.__propagatedSandboxProfile 184 ); 185 186 meta = (args.meta or { }) // { 187 inherit platforms; 188 }; 189 }; 190 191in 192fnOrAttrs: 193stdenvNoCC.mkDerivation ( 194 finalAttrs: 195 let 196 args = if lib.isFunction fnOrAttrs then fnOrAttrs (args' // finalAttrs) else fnOrAttrs; 197 args' = transformArgs finalAttrs args; 198 inherit (args') 199 nugetDeps 200 runtimeId 201 meta 202 dotnet-sdk 203 ; 204 args'' = removeAttrs args' [ 205 "nugetDeps" 206 "runtimeId" 207 "installPath" 208 "executables" 209 "projectFile" 210 "projectReferences" 211 "runtimeDeps" 212 "disabledTests" 213 "testProjectFile" 214 "buildType" 215 "selfContainedBuild" 216 "useDotnet" 217 "useAppHost" 218 "dotnet-sdk" 219 ]; 220 in 221 if nugetDeps != null then 222 addNuGetDeps { 223 inherit nugetDeps; 224 overrideFetchAttrs = 225 old: 226 lib.optionalAttrs ((args'.runtimeId or null) == null) rec { 227 dotnetRuntimeIds = map (system: dotnetCorePackages.systemToDotnetRid system) meta.platforms; 228 buildInputs = 229 old.buildInputs 230 ++ lib.concatLists (lib.attrValues (lib.getAttrs dotnetRuntimeIds dotnet-sdk.targetPackages)); 231 }; 232 } args'' finalAttrs 233 else 234 args'' 235)