2022-06-18 09:40:57 +02:00
{ lib , stdenvNoCC , linkFarmFromDrvs , callPackage , nuget-to-nix , writeScript , makeWrapper , fetchurl , xml2 , dotnetCorePackages , dotnetPackages , mkNugetSource , mkNugetDeps , cacert , srcOnly }:
2021-07-27 00:46:18 +02:00
{ name ? " ${ args . pname } - ${ args . version } "
2022-01-13 07:39:44 +01:00
, pname ? name
2021-07-27 00:46:18 +02:00
, enableParallelBuilding ? true
2021-10-27 20:36:50 +02:00
, doCheck ? false
2021-07-27 00:46:18 +02:00
# 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 ? [ ]
2021-10-27 20:36:50 +02:00
# Flags to pass to `dotnet test`, if running tests is enabled.
, dotnetTestFlags ? [ ]
2021-07-27 00:46:18 +02:00
# Flags to pass to `dotnet install`.
, dotnetInstallFlags ? [ ]
2021-12-09 18:19:59 +01:00
# Flags to pass to `dotnet pack`.
, dotnetPackFlags ? [ ]
2021-07-27 00:46:18 +02:00
# Flags to pass to dotnet in all phases.
, dotnetFlags ? [ ]
2022-01-13 07:39:44 +01:00
# The path to publish the project to. When unset, the directory "$out/lib/$pname" is used.
, installPath ? null
2021-07-27 00:46:18 +02:00
# The binaries that should get installed to `$out/bin`, relative to `$out/lib/$pname/`. 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
2021-12-09 18:19:59 +01:00
# 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
2021-11-21 22:47:53 +01:00
# The packages project file, which contains instructions on how to compile it. This can be an array of multiple project files as well.
2021-07-27 00:46:18 +02:00
, projectFile ? null
# The NuGet dependency file. This locks all NuGet dependency versions, as otherwise they cannot be deterministically fetched.
2021-11-22 02:12:42 +01:00
# This can be generated by running the `passthru.fetch-deps` script.
2021-07-27 00:46:18 +02:00
, nugetDeps ? null
2021-12-09 20:17:12 +01:00
# 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 ? [ ]
2021-07-27 00:46:18 +02:00
# Libraries that need to be available at runtime should be passed through this.
# These get wrapped into `LD_LIBRARY_PATH`.
, runtimeDeps ? [ ]
2021-10-27 20:36:50 +02:00
# Tests to disable. This gets passed to `dotnet test --filter "FullyQualifiedName!={}"`, to ensure compatibility with all frameworks.
# See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test#filter-option-details for more details.
, disabledTests ? [ ]
2021-12-20 22:31:18 +01:00
# 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 ? " "
2021-10-27 20:36:50 +02:00
2021-07-27 00:46:18 +02:00
# The type of build to perform. This is passed to `dotnet` with the `--configuration` flag. Possible values are `Release`, `Debug`, etc.
, buildType ? " R e l e a s e "
2022-07-20 22:37:14 +02:00
# If set to true, builds the application as a self-contained - removing the runtime dependency on dotnet
, selfContainedBuild ? false
2021-07-27 00:46:18 +02:00
# The dotnet SDK to use.
2022-05-16 21:07:52 +02:00
, dotnet-sdk ? dotnetCorePackages . sdk_6_0
2021-07-27 00:46:18 +02:00
# The dotnet runtime to use.
2022-05-16 21:07:52 +02:00
, dotnet-runtime ? dotnetCorePackages . runtime_6_0
2021-10-27 20:36:50 +02:00
# The dotnet SDK to run tests against. This can differentiate from the SDK compiled against.
, dotnet-test-sdk ? dotnet-sdk
2021-07-27 00:46:18 +02:00
, . . . } @ args :
assert projectFile == null -> throw " D e f i n i n g t h e ` p r o j e c t F i l e ` a t t r i b u t e i s r e q u i r e d . T h i s i s u s u a l l y a n ` . c s p r o j ` , o r ` . s l n ` f i l e . " ;
# TODO: Automatically generate a dependency file when a lockfile is present.
# This file is unfortunately almost never present, as Microsoft recommands not to push this in upstream repositories.
2021-11-22 02:12:42 +01:00
assert nugetDeps == null -> throw " D e f i n i n g t h e ` n u g e t D e p s ` a t t r i b u t e i s r e q u i r e d , a s t o l o c k t h e N u G e t d e p e n d e n c i e s . T h i s f i l e c a n b e g e n e r a t e d b y r u n n i n g t h e ` p a s s t h r u . f e t c h - d e p s ` s c r i p t . " ;
2021-07-27 00:46:18 +02:00
let
2022-01-13 07:39:44 +01:00
inherit ( callPackage ./hooks {
inherit dotnet-sdk dotnet-test-sdk disabledTests nuget-source dotnet-runtime runtimeDeps buildType ;
} ) dotnetConfigureHook dotnetBuildHook dotnetCheckHook dotnetInstallHook dotnetFixupHook ;
2022-04-25 18:18:50 +02:00
localDeps = if ( projectReferences != [ ] )
then linkFarmFromDrvs " ${ name } - p r o j e c t - r e f e r e n c e s " projectReferences
else null ;
2022-06-21 15:37:15 +02:00
_nugetDeps = if lib . isDerivation nugetDeps
then nugetDeps
else mkNugetDeps { inherit name ; nugetDeps = import nugetDeps ; } ;
2021-07-27 00:46:18 +02:00
2022-02-28 23:40:43 +01:00
nuget-source = mkNugetSource {
2022-04-25 18:18:50 +02:00
name = " ${ name } - n u g e t - s o u r c e " ;
description = " A N u g e t s o u r c e w i t h t h e d e p e n d e n c i e s f o r ${ name } " ;
deps = [ _nugetDeps ] ++ lib . optional ( localDeps != null ) localDeps ;
2021-10-26 07:27:02 +02:00
} ;
2022-01-13 07:39:44 +01:00
in stdenvNoCC . mkDerivation ( args // {
nativeBuildInputs = args . nativeBuildInputs or [ ] ++ [
dotnetConfigureHook
dotnetBuildHook
dotnetCheckHook
dotnetInstallHook
dotnetFixupHook
2021-12-30 18:22:17 +01:00
2022-01-13 07:39:44 +01:00
cacert
makeWrapper
2022-05-21 15:33:02 +02:00
dotnet-sdk
] ;
makeWrapperArgs = args . makeWrapperArgs or [ ] ++ [
" - - p r e f i x L D _ L I B R A R Y _ P A T H : ${ dotnet-sdk . icu } / l i b "
2022-01-13 07:39:44 +01:00
] ;
2021-12-30 18:22:17 +01:00
2022-01-13 07:39:44 +01:00
# Stripping breaks the executable
dontStrip = args . dontStrip or true ;
2021-12-30 18:22:17 +01:00
2022-01-13 07:39:44 +01:00
# gappsWrapperArgs gets included when wrapping for dotnet, as to avoid double wrapping
dontWrapGApps = args . dontWrapGApps or true ;
2021-12-30 18:22:17 +01:00
2022-01-13 07:39:44 +01:00
passthru = {
2022-04-25 18:18:50 +02:00
inherit nuget-source ;
2022-01-13 07:39:44 +01:00
fetch-deps = writeScript " f e t c h - ${ pname } - d e p s " ''
set - euo pipefail
cd " $ ( d i r n a m e " '' ${ BASH_SOURCE [ 0 ] } " ) "
2021-07-27 00:46:18 +02:00
export HOME = $ ( mktemp - d )
2022-01-13 07:39:44 +01:00
deps_file = " / t m p / ${ pname } - d e p s . n i x "
2021-07-27 00:46:18 +02:00
2022-06-18 09:40:57 +02:00
store_src = " ${ srcOnly args } "
2022-01-13 07:39:44 +01:00
src = " $ ( m k t e m p - d / t m p / ${ pname } . X X X ) "
cp - rT " $ s t o r e _ s r c " " $ s r c "
chmod - R + w " $ s r c "
2021-07-27 00:46:18 +02:00
2022-01-13 07:39:44 +01:00
trap " r m - r f $ s r c $ H O M E " EXIT
pushd " $ s r c "
2021-07-27 00:46:18 +02:00
2022-01-13 07:39:44 +01:00
export DOTNET_NOLOGO = 1
export DOTNET_CLI_TELEMETRY_OPTOUT = 1
2021-07-27 00:46:18 +02:00
2022-01-13 07:39:44 +01:00
mkdir - p " $ H O M E / n u g e t _ p k g s "
2021-07-27 00:46:18 +02:00
2022-01-13 07:39:44 +01:00
for project in " ${ lib . concatStringsSep " \" \" " ( ( lib . toList projectFile ) ++ lib . optionals ( testProjectFile != " " ) ( lib . toList testProjectFile ) ) } " ; do
$ { dotnet-sdk } /bin/dotnet restore " $ p r o j e c t " \
$ { lib . optionalString ( ! enableParallelBuilding ) " - - d i s a b l e - p a r a l l e l " } \
2021-11-21 22:47:53 +01:00
- p:ContinuousIntegrationBuild=true \
- p:Deterministic=true \
2022-01-13 07:39:44 +01:00
- - packages " $ H O M E / n u g e t _ p k g s " \
$ { lib . optionalString ( dotnetRestoreFlags != [ ] ) ( builtins . toString dotnetRestoreFlags ) } \
$ { lib . optionalString ( dotnetFlags != [ ] ) ( builtins . toString dotnetFlags ) }
2021-11-21 22:47:53 +01:00
done
2021-10-27 20:36:50 +02:00
2022-01-13 07:39:44 +01:00
echo " W r i t i n g l o c k f i l e . . . "
$ { nuget-to-nix } /bin/nuget-to-nix " $ H O M E / n u g e t _ p k g s " > " $ d e p s _ f i l e "
echo " S u c c e s f u l l y w r o t e l o c k f i l e t o : $ d e p s _ f i l e "
2021-10-27 20:36:50 +02:00
'' ;
2022-01-13 07:39:44 +01:00
} // args . passthru or { } ;
} )