Building a dotnet projet

I’m trying to package this dotnet project and I’m having trouble on how to handle dependencies :

{ stdenv
, fetchFromGitHub
, dotnetCorePackages
}:

let
  buildDotnet = with dotnetCorePackages; combinePackages [
    sdk_3_1
  ];
in stdenv.mkDerivation rec {
  name = "baget";
  version = "0.3.0-preview2";

  src = fetchFromGitHub {
    owner = "loic-sharma";
    repo = "BaGet";
    rev = "v${version}";
    sha256 = "0g0qqwvmy60i40mk9ncr7dqwrbzir1zy0l71iwldyvkx323wifg5";
  };

  nativeBuildInputs = [ buildDotnet ];

  buildPhase = ''
    HOME=$PWD/fake-home
    mkdir -p $HOME
    dotnet publish -o ./build
  '';
}

I can’t find how to put dependencies inside this package What is the procedure to do so ? I see that there is a fetchnuget helper, but I cannot figure out how to use it.

1 Like

Unfortunately, nix ecosystem doesn’t currently allow for packaging of dotnet packages in nixpkgs

You can see I have it as a todo https://github.com/NixOS/nixpkgs/blob/183aeb2df252e6ace695b88b9c543d8178eeb0f9/doc/languages-frameworks/dotnet.section.md#packaging-a-dotnet-application :frowning:

Work still requires me to mostly use python, so I mostly focus on that ecosystem.

For development, you can just use an impure nix-shell, and that is “supported”.

1 Like

Hello @jonringer, thanks for your answer. I’m currently deploying a production environement where I have to dpeloy dotnet apps, I can leverage Docker as a temporary workaround.

I also have some time that I can use to help on this (since I’m being confined right now) : what would be the correct approach ? How would you see the dotnet packaging working ?
I recently played with a Ruby app with the bundlerApp helper (with bundix) and it was a really smooth experience to deploy an app that works in production.

Maybe we could have a dotnetApp helper that could do the same with a definition of the dependencies (that could maybe in the future be generated with a tool that is bundix-like) ?

adding a shellHook for adding the needed dependencies for published dotnet projects. Also, a nice have would be export the dotnet-runtime, instead dotnet-sdk to reduce closure size. And for nuget dependencies, some way to vendor them so they can be fixed output derivations, and not cause an error during dotnet restore or dotnet build

1 Like

Thank you for detailing your vision on this, I’ll try to make some minimal working setup next week or so :slight_smile:.

Hello,

I had some time to play with this recently.

I’ve been trying to create an helper to download NuGet dependencies to package dotnet applications properly. I have the feeling it can be made using only Nix, but I struggle to do it. I use the dotnet option to generate a Lockfile.

Here is an example of it:

"Microsoft.Extensions.DependencyInjection": {
  "type": "Transitive",
  "resolved": "3.0.0",
  "contentHash": "yDsuNA/BT4j9qrcRs0NUNHQAJfywFWX18ZZ+shxXJL+/nIfz3vhuRTfnYgvFeQlNBlgmgdSjOcs4ajgoS6Q/Ng==",
  "dependencies": {
    "Microsoft.Extensions.DependencyInjection.Abstractions": "3.0.0"
  }
}

I’ve tried to create a derivation using the content of this file directly: https://gist.github.com/Elyhaka/9d4690352cf9f08c0b91e08d55877964

Unfortunately, I have a hash mismatch error with this solution, and I could not figure out why.
I have the feeling that the way that the hash is calculated is different, but I could find a specification on how this hash is calculated.

Any idea ?

Hi @Elyhaka , I’m also interested in this, did you manage to get it to work?

I just gave this a try (independently) and also failed to make the hashes match (WIP WIP get nuget packages from packages.lock.json for nix · GitHub ) .

Also noticed this other thread of yours Work in progress : dotnet2nix / builtDotNetProject helper

As far as I can tell it’s pretty straightforward how NuGet calculates the hash, see e.g.:

Still doesn’t work though…

If lock files were more prevalent in the dotnet community, it would be a lot easier to package them with nix :frowning:

2 Likes

Paket is generating a proper lock file. See The paket.lock file

if the lock file includes some metadata which can be extrapolated to a stable url, and there’s some type of checksum, then we can use the lock file directly from src. If not, then we need something like a dotnet2nix which can generate a file which has those things.

With buildDotnetModule: init by IvarWithoutBones · Pull Request #139222 · NixOS/nixpkgs · GitHub just being merged, packaging dotnet applications is now pretty simple! See the PR for documentation/examples.

All you have to do for nuget deps is run nuget-to-nix on the projects source, which will generate a custom dependency lockfile. In the future I would like to automate the dependeny fetching, but for now this works :slight_smile:

Official lockfiles are not yet supported, but I do plan to make them work.

11 Likes