DotNET: Difference between revisions

Lostmsu (talk | contribs)
m Use the correct command name.
 
(4 intermediate revisions by 4 users not shown)
Line 4: Line 4:
Example build file:
Example build file:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix" line="1" start="1">
{ fetchFromGitHub
{
, dotnetCorePackages
  buildDotnetModule,
, buildDotnetModule
  dotnetCorePackages,
}:
}:


buildDotnetModule rec {
buildDotnetModule {
   pname = "some_program";
   pname = "hello";
   version = "some_version";
   version = "0.1";


   src = fetchFromGitHub {
   src = ./.;
    owner = "some_owner";
    repo = pname;
    rev = "v${version}";
    hash = ""; # use e.g. `nix-prefetch-git`
  };


   projectFile = "SomeProject/SomeProject.csproj";
   projectFile = "Hello/Hello.csproj";
   dotnet-sdk = dotnetCorePackages.sdk_8_0;
   dotnet-sdk = dotnetCorePackages.sdk_8_0;
   dotnet-runtime = dotnetCorePackages.runtime_8_0;
   dotnet-runtime = dotnetCorePackages.runtime_8_0;
   nugetDeps = ./deps.nix; # create a blank file here, then populate it with `nix-build -A fetch-deps && ./result`
   nugetDeps = ./deps.json;
 
  meta = with lib; {
    homepage = "some_homepage";
    description = "some_description";
    license = licenses.mit;
  };
}
}


</syntaxhighlight>
</syntaxhighlight>


If the <code>fetch-deps</code> script isn't working for whatever reason, you can manually run <code>nuget-to-nix</code>:
If the <code>fetch-deps</code> script isn't working for whatever reason, you can manually run <code>nuget-to-json</code>:
<syntaxhighlight lang="sh">
<syntaxhighlight lang="shell-session">
dotnet restore --packages=packageDir ./SomeProject.csproj
$ dotnet restore --packages=packageDir ./SomeProject.csproj
nuget-to-nix packageDir >deps.nix
$ nuget-to-json packageDir > deps.json
rm -r packageDir
$ rm -r packageDir
</syntaxhighlight>
</syntaxhighlight>


Line 120: Line 109:


If running a .NET-build executable you get the above error, make sure the DOTNET_ROOT environment variable is set:
If running a .NET-build executable you get the above error, make sure the DOTNET_ROOT environment variable is set:
<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
environment.sessionVariables = {
environment.sessionVariables = {
   DOTNET_ROOT = "${pkgs.dotnet-sdk}";
   DOTNET_ROOT = "${pkgs.dotnet-sdk}/share/dotnet/";
};
};
</syntaxHighlight>
</syntaxhighlight>


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
Line 213: Line 202:


== Example: Running Rider with dotnet & PowerShell ==
== Example: Running Rider with dotnet & PowerShell ==
Rider has better compatibility when run in FHS mode


Rider package
Rider package<syntaxhighlight lang="nix">
 
<syntaxHighlight lang=nix>
pkgs.jetbrains.rider
pkgs.jetbrains.rider
</syntaxHighlight>
</syntaxhighlight>rider-fhs.nix<syntaxhighlight lang="nix">
 
{ pkgs ? import <nixpkgs> {} }:
dotnet.nix
 
<syntaxHighlight lang=nix>
with import <nixpkgs> {};


mkShell {
(pkgs.buildFHSEnv {
   name = "dotnet-env";
   name = "rider-env";
   packages = [
   targetPkgs = pkgs: (with pkgs; [
    (with dotnetCorePackages; combinePackages [
    dotnetCorePackages.dotnet_8.sdk
      sdk_6_0
     dotnetCorePackages.dotnet_8.aspnetcore
      sdk_7_0
      sdk_8_0
     ])
     powershell
     powershell
   ];
   ]);
}
  multiPkgs = pkgs: (with pkgs; [
</syntaxHighlight>
  ]);
 
  runScript = "nohup rider &";
To execute Rider
}).env
 
</syntaxhighlight><syntaxhighlight lang="nix">
<syntaxHighlight lang=bash>
nix-shell ./rider-fhs.nix
nix-shell ./dotnet.nix --run 'nohup rider &'
</syntaxhighlight>This can be added as an alias to your shell if you update the reference to an absolute address, such as location within your home directory. <syntaxhighlight>
</syntaxHighlight>
run-rider = "nix-shell ~/nix/rider-fhs.nix";
 
</syntaxhighlight>
This can be added as an alias to your shell if you update the reference to an absolute address, such as location within your home directory. e.g. `~/nix/dotnet.nix`


== Example: multi-SDK installation with local workload installation enabled ==
== Example: multi-SDK installation with local workload installation enabled ==
Line 264: Line 244:
* https://learn.microsoft.com/en-us/dotnet/core/introduction
* https://learn.microsoft.com/en-us/dotnet/core/introduction


[[Category: Development]]
 
[[Category:Languages]]
[[Category:Languages]]