Need help packaging a .NET project (SourceGit)

I know the basics of packaging software with nix (which was enough so far), but I have run into issues trying to package SourceGit, a .NET project. I also opened a package request for it. What I have so far is the following:

{ buildDotnetModule
, fetchFromGitHub
, xorg
}:
buildDotnetModule rec {
  pname = "sourcegit";
  version = "8.7";

  src = fetchFromGitHub {
    owner = "sourcegit-scm";
    repo = "sourcegit";
    rev = "v${version}";
    hash = "";
  };

  projectFile = "src/SourceGit.csproj";
  nugetDeps = ./deps.nix;

  # for issue with UI framework, see https://github.com/AvaloniaUI/Avalonia/issues/3020#issuecomment-1902270097
  runtimeDeps = with xorg; [
    libICE
    libSM
    libX11
    libX11.dev
  ];
}

The way I understood it after reading the NixOS Wiki, I now need to run nix-build -A sourcegit.fetch-deps to generate the deps.nix. As all of my NixOS systems use Flakes, I am supposed to use nix build instead though, which does not have a -A option. I did not find any way to do this with nix build.

But even without that option, running nix build leads to another issue:

> nix build
error: flake [...] does not provide attribute 'packages.x86_64-linux.default' or 'defaultPackage.x86_64-linux'

I have tried setting outputs.packages.x86_64-linux.default in my Flake to something like inputs.nixpkgs, which doesn’t work. I also could not find out what I need to set here to make it work. My flake is here.

So… how can I get this working, ultimately resulting in a working Nix package for SourceGit? Any help appreciated!

1 Like

I just saw that @TomaSajt opened a PR to close my packaging request, I will close this topic if/when this is merged.

By the way, here’s a simple template flake you can use if you don’t want to use the nixpkgs repo to write your packages:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  };

  outputs =
    { self, nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs { inherit system; };
    in
    {
      my-package = pkgs.callPackage ./my-package.nix { };
    };
}

To build this you should be able to do nix build .#my-package

If you want to be able to build it only using nix build you can swap
my-package =
to
packages.${system}.default =

2 Likes

if you don’t want to use the nixpkgs repo to write your packages

Haha, so far I just put my local packages directly in environment.systemPackages of my NixOS config. That was fine so far, but also a pain when debugging.

Thank you so much! This is really useful to me.

How were you able to generate the deps.nix though? As said before, I couldn’t figure out how to do it with nix build…

The .#my-package part of the command is the derivation to evaluate. You can change it over to .#my-package.passthru.fetch-deps. Building this derivation should create a file named result, which actually is an executable. You can run this with ./result deps.nix, which should create the deps.nix file.

A weird thing: when doing this, it will complain that nugetDeps doesn’t exist, so you’ll need to temporarily set nugetDeps to any non-null value you’d like (e.g. 1) so it doesn’t complain and then after generating deps.nix change nugetDeps to ./deps.nix

2 Likes

alright, thank you so much! :slight_smile: