How can I get Emacs pdf-tools working?

I use doom-emacs. One of its modules is a PDF module, which uses the package pdf-tools. Pdf-tools is really annoying on Nixos, since if needs some binary and wants to build it. But then of course it fails with:

/home/jon/.emacs.d/.local/straight/build-27.1/pdf-tools/build/server/autobuild -i /home/jon/.emacs.d/.local/straight/build-27.1/pdf-tools/
/nix/store/qdp56fi357fgxxnkjrwx1g67hrk775im-bash-4.4-p23/bin/sh: /home/jon/.emacs.d/.local/straight/build-27.1/pdf-tools/build/server/autobuild: Permission denied

I noticed that I can get pdf-tools already compiled and in emacs by using emacsWithPackages:

     (emacsWithPackages (epkgs: (with epkgs.melpaStablePackages; [
       pdf-tools
     ])))

And I added this to my config.el (although I’m not sure what it does or why I need it):

;; Enables Nixos-installed packages to be loaded
(require 'package)
(setq package-enable-at-startup nil)
(package-initialize)

But it doesn’t seem to take, since Emacs still complains: “Need to (re)build the epdfinfo program, do it now? (y or n)”. Amazingly, if I choose “n,” it still tries to build it.

Emacs apparently sees pdf-tools, from what I can tell, since describe-package pdf-tools gives this:

Package pdf-tools is external.

     Status: External in ‘/nix/store/nr0xzc92spcic1kwymv7nd5jcjs54p2w-emacs-packages-deps/share/emacs/site-lisp/elpa/pdf-tools-20200512.1524/’ (unsigned).
    Version: 20200512.1524
    Summary: Support library for PDF documents.

Installing this package should install and build epdfinfo, from poppler, according to this section in Nixpkgs, no?

And indeed, I seem to have a file nr0xzc92spcic1kwymv7nd5jcjs54p2w-emacs-packages-deps/share/emacs/site-lisp/elpa/pdf-tools-20200512.1524/epdfinfo.

So I just want to get it to where pdf-tools will work. Any ideas?

1 Like

Is Emacs looking for epdfinfo in your PATH? You could try adding that pdf-tools-20200512.1524 subdirectory to your PATH variable before starting Emacs (or after starting with setenv).

If that test works, you could try wrapping the emacs program to include that path; or create a derivation for wrapping pdf-tools that links epdfinfo into $out/bin/ and adding that derivation to your environment.systemPackages. Here’s an example for the epdfinfo derivation:

{ pkgs ? import <nixpkgs> {} }:
pkgs.stdenv.mkDerivation {
  name = "epdfinfo";
  phases = "installPhase";
  installPhase = ''
    mkdir -p $out/bin
    ln -s $(${pkgs.findutils}/bin/find ${pkgs.emacsPackages.pdf-tools}/ -name epdfinfo) $out/bin/
  '';
}

A few things to check. What is the value of pdf-info-epdfinfo-program? It should fall back to epdfinfo in the same directory as pdf-info.el: https://github.com/politza/pdf-tools/blob/c510442ab89c8a9e9881230eeb364f4663f59e76/lisp/pdf-info.el#L85-L86 If epdfinfo exists in this directory, it should work.

On my system it looks like this:

pdf-info-epdfinfo-program is a variable defined in ‘pdf-info.el’.
Its value is
"/nix/store/vdx3pvn3ghndyvrdrq8hc11r5wa708bb-emacs-packages-deps/share/emacs/site-lisp/elpa/pdf-tools-20200512.1524/epdfinfo"

Maybe the pdf-tools package is also installed outside of nix, and this is conflicting. I think list-load-path-shadows will show you if you have a package defined in multiple locations.

This is how I have pdf-tools working. I have an overlay that looks like this:

self: super:
{
  emacsWithConfig = super.emacsWithPackages (epkgs:
    (with epkgs.melpaPackages; [
      pdf-tools
    ])
  );
}

In my emacs config, I call (pdf-tools-install) via use-package:

(use-package pdf-tools
  :init
  (pdf-tools-install))

Looks like my value is:

pdf-info-epdfinfo-program is a variable defined in pdf-info.el.

Value
"/home/jon/.emacs.d/.local/straight/build-27.1/pdf-tools/epdfinfo"

So it looks like it’s shadowed by Doom’s version. Do you know how to set the precedence of packages, so that I can use the Nix version, instead?

I believe Emacs uses load-path. EmacsWiki: Load Path

It may be simplest to uninstall the Doom pdf-tools package, if possible.

Thanks. I eventually got this to work with this line in my doomdir’s package.el:

(package! pdf-tools :built-in 'prefer)

And by purging old packages, manually removing the old package, and doing doom sync -u a bunch.

4 Likes

If you want to org-store-link to specific PDF pages in PDFView (just like DocView supports OOTB) you also need to install org-pdftools. Installing it from MELPA broke my ~/.emacs.d/elpa/; org-tools appears to be non-trivial also so it needs to be installed via Nix. Via home-manager:

        programs.emacs = {
          enable = true;
          extraPackages = epkgs: [ epkgs.pdf-tools epkgs.org-pdftools ]; # non-trivial
        };
1 Like

How exactly is this overlay supposed to work? I’ve done as you said, but I still have to set the path manually.

This is what happens when I use your config:

pdf-info-epdfinfo-program is a variable defined in ‘pdf-info.el’.

Its value is "/home/b0ef/.emacs.d/straight/build/pdf-tools/epdfinfo"
Original value was 
"/home/b0ef/epdfinfo"

I think it sets that path, but there is no binary inside that path

BASHY $ ls .emacs.d/straight/build/pdf-tools/
build            pdf-links.el     pdf-sync.elc
pdf-annot.el     pdf-links.elc    pdf-tools-autoloads.el
pdf-annot.elc    pdf-loader.el    pdf-tools.el
pdf-cache.el     pdf-loader.elc   pdf-tools.elc
pdf-cache.elc    pdf-macs.el      pdf-util.el
pdf-dev.el       pdf-macs.elc     pdf-util.elc
pdf-dev.elc      pdf-misc.el      pdf-view.el
pdf-history.el   pdf-misc.elc     pdf-view.elc
pdf-history.elc  pdf-occur.el     pdf-virtual.el
pdf-info.el      pdf-occur.elc    pdf-virtual.elc
pdf-info.elc     pdf-outline.el   README
pdf-isearch.el   pdf-outline.elc
pdf-isearch.elc  pdf-sync.el
BASHY $ find /nix/store/ -iname "epdfinfo"
/nix/store/zvsc7lwssq2pgx19rkldy8pm8ac6xm74-emacs-pdf-tools-20230611.239/share/emacs/site-lisp/elpa/pdf-tools-20230611.239/epdfinfo
/nix/store/14n6s3w62nnsnn647bb15d7k9p8gphdi-emacs-packages-deps/share/emacs/site-lisp/elpa/pdf-tools-20230611.239/epdfinfo
/nix/store/2byw9sabnrhzqlj7hqal7brkbd34g5v1-emacs-pdf-tools-20230611.239/share/emacs/site-lisp/elpa/pdf-tools-20230611.239/epdfinfo

You have no clue here?:wink: