NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.46k stars 13.66k forks source link

Providing Nix / TeXLive dependencies with a custom LaTeX package #213632

Closed leolavaur closed 1 year ago

leolavaur commented 1 year ago

Problem

I've made my custom LaTeX package that I pretty much use in all my projects, following the example in the nixpkgs manual. This is all done in a flake so I can import it directly in my other flake-based projetcs:

inputs = {
    # ...
    mypkg = {
      # ...
    };
  };
# ...
(texlive.combine {
  inherit (texlive) scheme-small latexmk ieeetran biblatex-ieee;
  inherit (mypkg) mypkg_tex;
})

However, mypkg relies on a lot of both TeXLive and Nixpkgs dependencies, and I've found no way to install these dependencies from a document's flake. I've tried the following so far:

From what I can tell, you cannot have multiple combined packages in your buildInputs, neither can you compose calls to combine. Is there a documented way somewhere?

Thank you for your help, I hope this can serve improving the docs somewhere.

Checklist

Proposal

Ideally, the method for providing said dependencies should be detailed in the manual, in the same Custom packages section.

erooke commented 1 year ago

I hit the same stumbling block, a couple hours later I have a solution which seems to be working. So texlive.combine is expecting a set with the attribute pkgs: [derivation] the problem is this pkgs attribute has to refer to the derivation itself for this to all work. It looks like nix uses a recursive thing in mkDerivation to get that to happen. So the final diddy I ended up on was this:

{pkgs}:
with pkgs; let
  pkg = stdenv.mkDerivation (finalAttrs: {
    name = "uiowa-thesis";

    # texlive.combine expects these I think?
    pname = finalAttrs.name;
    tlType = "run";

    src = fetchFromGitHub {
      owner = "uiowa-mgb";
      repo = "uiowa-thesis";
      rev = "ab2bb48";
      sha256 = "5L0+0ykenZGYLZq9/iG2A3Ym8VvkD28ElghgYETOTEc=";
    };

    phases = ["installPhase"];

    passthru.pkgs = with texlive;
      lib.lists.foldr (a: b: a.pkgs ++ b) [pkg] [
        memoir
        biber
        biblatex
      ];

    installPhase = ''
      mkdir -p $out/tex/latex/
      cp -t $out/tex/latex/ $src/uiowa-thesis.cls
    '';
  });
in
  pkg

you then can call this like so:

{
  description = "A very basic flake";

  outputs = {
    self,
    nixpkgs,
    rooke,
  }: let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
    tex =
      pkgs.texlive.combine {
        inherit (pkgs.texlive) scheme-medium;
        uiowa-thesis = rooke.packages.${system}.uiowa-thesis;
      } ;
  in {
    formatter.${system} = pkgs.alejandra;

    devShells.${system}.default = pkgs.mkShell {
      buildInputs = [
        tex
      ];
    };
  };
}

I have no idea what I'm doing so there may be a cleaner or more idiomatic way of doing this?

leolavaur commented 1 year ago

Thank you very much @erooke ! I managed to reproduce your approach, even though I'm not sure to understand it fully. Would you mind to elaborate a bit on the following?

lib.lists.foldr (a: b: a.pkgs ++ b) [pkg] [
        memoir
        biber
        biblatex
      ];

While it solves my problem, I'd like to let this issue open while we do not have a proper documentation on the subject.

erooke commented 1 year ago

You're welcome :smile:

That's merging the list of all the derivations the dependencies are expecting. So for example memoir.pkgs is a list of derivations which memoir needs to function. This snippet creates a list with your derviation in it then goes through all the dependencies, extracts their requirements, and then appends them.

So that bit there is the same as saying

[pkg] ++ memoir.pkgs ++ biber.pkgs ++ biblatex.pkgs

it just generalizes better.

leolavaur commented 1 year ago

Much clearer, thanks again!

xworld21 commented 1 year ago

Hi all, we changed mechanism for TeX Live dependencies in #218331 (the attribute passthru.tlDeps) and wrote new documentation. @phdcybersec could you please confirm if the new system works for you? If so, we should close the issue!

drupol commented 1 year ago

Where is the documentation about this change? I have issue loading my custom packages.

Edit: found it here: https://nixos.org/manual/nixpkgs/unstable/#sec-language-texlive-custom-packages

That said, I'm still unable to get my custom packages working... investigations ongoing...

Ok I got everything working, see the commit at https://code.europa.eu/pol/european-commission-latex-beamer-theme/-/commit/169bd27c1934cde8ea105fefea2197f529937b3b

One thing I didn't like so much is that we now MUST add the version attributes.

Besides that, it's working again.

xworld21 commented 1 year ago

One thing I didn't like so much is that we now MUST add the version attributes.

Thanks for the report! I am writing a tiny PR as we speak. The version attribute is not essential and we can make Nixpkgs accept packages without them.

If you spot something else that's weird, please let me know. Apart from the pkgs = [ finalAttrs.finalPackage ]; thing, it's cumbersome and I want to get rid of it, but it's another big change, so probably something for the 23.11 release.

drupol commented 1 year ago

How can I be aware of such changes in Texlive in Nix ? To be honest, I was quite surprised this morning when a colleague told me "It's not working" !

xworld21 commented 1 year ago

How can I be aware of such changes in Texlive in Nix ? To be honest, I was quite surprised this morning when a colleague told me "It's not working" !

Good question, unstable is a moving target so I don't think there is an official channel announcing these changes. For stable releases, there are release notes, although I didn't propose one about TeX Live – maybe I should in this case.

At any rate, the tlDeps upgrade was meant to be backward compatible and invisible, so the breakage was a genuine new bug I introduced.

xworld21 commented 1 year ago

I think this is essentially solved: we have new documentation in place, as well as a less obscure mechanism for specifying TeX Live dependencies. If not, please comment and/or reopen the issue.