Yes, you can use a tarball declaratively on NixOS by creating a custom Nix package that fetches, unpacks, and configures the tarball within your Nix configuration. This allows you to add the software to your environment.systemPackages or user environment declaratively.
How to Use a Tarball Declaratively on NixOS
1. Create a Custom Nix Expression for the Tarball
You write a Nix derivation that:
- Downloads the tarball using
fetchurl or fetchTarball.
- Unpacks it.
- Optionally runs build or install steps if needed.
- Specifies dependencies in
buildInputs if the binaries need libraries patched.
- Wraps binaries if necessary (e.g., using
autoPatchelfHook).
- Installs desktop files or icons if applicable.
Example snippet:
{ stdenv, fetchurl, autoPatchelfHook, makeWrapper, someDependency }:
stdenv.mkDerivation rec {
pname = "myapp";
version = "1.0";
src = fetchurl {
url = "https://example.com/myapp-1.0.tar.gz";
sha256 = "0v..."; # actual sha256 here
};
nativeBuildInputs = [ autoPatchelfHook makeWrapper ];
buildInputs = [ someDependency ];
unpackPhase = "true"; # if no build needed, just unpack
installPhase = ''
mkdir -p $out/bin
cp -r $src/* $out/
ln -s $out/myapp $out/bin/myapp
'';
meta = with stdenv.lib; {
description = "My custom app from tarball";
homepage = "https://example.com/myapp";
license = licenses.mit;
platforms = platforms.linux;
};
}
2. Add the Package to Your Configuration
In your configuration.nix or flake, import this derivation and add it to environment.systemPackages:
{
environment.systemPackages = with pkgs; [
myapp
];
}
Then run sudo nixos-rebuild switch to install it declaratively.
3. Handling Dependencies and ELF Patching
If the tarball contains binaries that depend on shared libraries, you can use autoPatchelfHook in nativeBuildInputs to fix the ELF headers so they find the correct libraries in the Nix store.
Alternatively, you can wrap the binaries with makeWrapper to set environment variables like LD_LIBRARY_PATH.
4. Using fetchurl Declaratively
You can declaratively fetch the tarball by specifying its URL and sha256 in the Nix expression. This ensures the tarball is stored in the Nix store and the build is reproducible.
5. Alternative: Using buildFHSEnv or