DotNET: Difference between revisions

imported>WhiteBlackGoose
m Links fixed
imported>Milahu
update buildDotnetPackage, add errors and fixes
Line 4: Line 4:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
/*
some_program/default.nix
nix-build -E 'with import <nixpkgs> { }; callPackage ./default.nix { }'
*/
{ lib
{ lib
, stdenv
, stdenv
Line 19: Line 14:
buildDotnetPackage rec {
buildDotnetPackage rec {
   pname = "some_program";
   pname = "some_program";
  baseName = pname; # workaround for "called without baseName"
   version = "some_version";
   version = "some_version";
   src = fetchFromGitHub {
   src = fetchFromGitHub {
     owner = "some_owner";
     owner = "some_owner";
     repo = pname;
     repo = pname;
     rev = "v${version}";
     rev = "v${version}";
     sha256 = ""; # todo
     sha256 = "";
   };
   };
   projectFile = ["path/to/some_project.csproj"];
 
   xBuildFiles = [
    "SomeProject/SomeProject.csproj"
  ];
 
  nativeBuildInputs = [
    pkg-config
  ];
 
  buildInputs = [
  ];
 
   propagatedBuildInputs = [
   propagatedBuildInputs = [
   ];
   ];
   buildInputs = [
 
    # unit tests
   checkInputs = [
     dotnetPackages.NUnit
     dotnetPackages.NUnit
     dotnetPackages.NUnitRunners
     dotnetPackages.NUnitRunners
   ];
   ];
  nativeBuildInputs = [
 
    pkg-config
  ];
   meta = with lib; {
   meta = with lib; {
     homepage = "some_homepage";
     homepage = "some_homepage";
Line 56: Line 60:
</blockquote>
</blockquote>


Workaround: in <code>buildPhase</code>, replace <code>xbuild</code> with <code>msbuild</code>  
Fix: in <code>buildPhase</code>, replace <code>xbuild</code> with <code>msbuild</code>  


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
{ lib
{ # ...
# ...
, msbuild
, msbuild
}:
}:
let
  arrayToShell = (a: toString (map (lib.escape (lib.stringToCharacters "\\ ';$`()|<>\t") ) a));
in


buildDotnetPackage rec {
buildDotnetPackage rec {
   # ...
   # ...
   projectFile = "path/to/some_project.csproj";
 
  # ...
   msBuildFiles = [
   buildInputs = [
    "SomeProject/SomeProject.csproj"
  ];
 
   nativeBuildInputs = [
    pkg-config
     msbuild
     msbuild
   ];
   ];
  msBuildFlags = [
    "/p:Configuration=Release"
  ];
  # based on https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/dotnet/build-dotnet-package/default.nix
   buildPhase = ''
   buildPhase = ''
     runHook preBuild
     runHook preBuild
     msbuild /p:Configuration=Release ${projectFile}
 
     echo Building dotNET packages...
 
    # Probably needs to be moved to fsharp
    if pkg-config FSharp.Core
    then
      export FSharpTargetsPath="$(dirname $(pkg-config FSharp.Core --variable=Libraries))/Microsoft.FSharp.Targets"
    fi
 
    ran=""
    for msBuildFile in ${arrayToShell msBuildFiles} ''${msBuildFilesExtra}
    do
      ran="yes"
      msbuild ${arrayToShell msBuildFlags} ''${msBuildFlagsArray} $msBuildFile
    done
 
    [ -z "$ran" ] && msbuild ${arrayToShell msBuildFlags} ''${msBuildFlagsArray}
 
     runHook postBuild
     runHook postBuild
   '';
   '';
Line 92: Line 126:


See : https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#net-sdk-and-cli-environment-variables
See : https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#net-sdk-and-cli-environment-variables
== No .NET SDKs were found ==
Fix:
<syntaxHighlight lang=nix>
  nativeBuildInputs = [
    # ...
    dotnet-sdk
  ];
</syntaxHighlight>
== TargetFramework value was not recognized ==
<blockquote>
error NETSDK1013: The TargetFramework value 'net6.0-windows' was not recognized. It may be misspelled. If not, then the TargetFrameworkIdentifier and/or TargetFrameworkVersion properties must be specified explicitly.
</blockquote>
Fix:
<syntaxHighlight lang=nix>
  postPatch = ''
    substituteInPlace SomeProject/SomeProject.csproj \
      --replace \
        "<TargetFramework>net6.0-windows</TargetFramework>" \
        "<TargetFramework>net6.0</TargetFramework>"
  '';
</syntaxHighlight>


== Missing NuGet packages ==
== Missing NuGet packages ==
Line 107: Line 171:
These are upstream bugs
These are upstream bugs


As workaround, patch all <code>*.csproj</code> files, to remove all XML tags that contain the "missing file" paths, for example
Fix: patch all <code>*.csproj</code> files, to remove all XML tags that contain the "missing file" paths, for example


<blockquote>
<blockquote>