Declare evironment.systemPackages installation of a TarBall

For a project I need multiple programs which are not yet available as Nix-packages, but as tarballs or .tar.gz - namely:

…and I am wondering if there is any way to declare them to be installed as systemPackackages.

Can someone please shed some light on this topic - if it is possible or if there are alternative routes to get these programs working on NixOS.

Thanks a lot in advance.

Yes, you can basically create your own package, and add your package into your environment.systemPackages. For instance if you create a file like
jtrace.nix containing something like:

{stdenv, autoPatchelfHook, ...}:
stdenv.mkDerivation {
  src = ./yourTarball.tar.gz;
  nativeBuildInput = [ autoPatchelfHook ];
  buildInputs = [ ]; # put your runtime deps here
  installPhase = ''
    mkdir -p $out/{bin,lib}
    mv the_program_binaries $out/bin # To adapt to your program
    mv the_program_libraries $out/lib # To adapt to your program
    # Potentially other stuff    
  '';
}

(the exact install phase depend on the form of the tarball)

Then you can put in your systemPackage:

environment.systemPackages = [
  (pkgs.callPackage ./jtrace.nix {})
];

to install your package.

For more detail, I explain [packaging - How to package my software in nix or write my own package derivation for nixpkgs - Unix & Linux Stack Exchange here] how to do a generic derivation and here the different methods to patch pre-built binaries Different methods to run a non-nixos executable on Nixos - Unix & Linux Stack Exchange

Now, note that it may actually be simpler to package the source (if available), or even the .deb as .deb already provide the library/binaries/share/… in the appropriate position. What you need to do is add a phase:

unpackCmd =''
  runHook preUnpack
  dpkg-deb -x $src .
  runHook postUnpack
'';

and then the install phase can just copy the [/usr]/{bin,sbin}… folders at $out/bin and similarly for the lib in $out/lib, for the share folder in $out/share etc…

Also, a good idea is often to get inspired by nixpkgs. I typically have a local clone that I use to do search (the online github search often miss stuff, not sure why):

git clone https://github.com/NixOS/nixpkgs/
cd nixpkgs
rg "dpkg" # <--- install rg, it's great. This will list all files containing dpkg. Hopefully you can get inspired by these derivations.

If after these advices you are still unable to package the programs, then feel free to ask :smiley:

1 Like