What is the proper way to package LaTeX sty/cls files?

Hello there.
Say that i have a tex package: source/example.sty

\NeedsTeXFormat{LaTeX2e}
\ProvidePackage{example}{today -- some example}

\newcommand\helloWorld{Hello World!}

I figured i could package it like this (not sure if its the right way to do it):

pkgs:
pkgs.stdenv.mkDerivation {
  name = "example";
  src = ./source;
  installPhase = ''
    mkdir -p $out/tex/latex
    cp -r $src/example.sty $out/tex/latex
  '';
  passthru.tlType = "run";
}

Finally i want to create a devShell that includes this package and exposes it to texlive to be used, something in the lines:

pkgs:
let
  example = import example.nix {pkgs};
in
pkgs.mkShell { buildInputs = [example]; };

So far the only way i could have this package being added to my config is recreating texlive the following way:

pkgs:
let
  example = import example.nix {pkgs};  texlive = pkgs.texlive.combine {
    pkgFilter = pkg: with pkg; tlType == "run" || tlType == "bin" || tlType == "doc";

    example.pkgs = [ example ];
    inherit (pkgs.texlive) scheme-basic;
  };
in
pkgs.mkShell { buildInputs = [ texlive ]; };

This raises a problem that i would like that my texlive installation to be modular instead of having to build everything every time i change the exaple package. Also being able to use my current texlive package is a bonus, evicting building a whole texlive to add a single package.

Edit: Rewritting question.

1 Like

I think i might have found a solution but not sure how reliable it is, any confirmation is welcome.

I figured i could extend the env var TEXMFHOME to include the package derivation, and add it to a shellHook as the package is built:

pgks:
let
  name = "example";
in
pkgs.stdenv.mkDerivation {
  inherit name;
  src = ./source;
  installPhase = ''
    mkdir -p $out/tex/latex/${name}
    cp -r $src/example.cls $out/tex/latex/${name}

    # create shellhook to expose to texlive
    mkdir -p $out/nix-support
    echo "export TEXMFHOME=\"$TEXMFHOME:$out\"" > $out/nix-support/setup-hook
  '';
  passthru.tlType = "run";
}