Non-Python dependencies in Python environment?

How to add non-Python dependencies (executables) to a Python environment?

EDIT: Actually, see the next post for the actual usecase.

As an example, I have the following files:

default.nix

let
  pkgs = import <nixpkgs> { };
in pkgs.python311.withPackages (ps: [
  pkgs.texliveFull
])

shell.nix

(import ./default.nix).env

foo.py

import os
os.system("pdflatex --version")

Then, I run the python script but it doesn’t find the executable:

$ nix-shell --pure --run "python foo.py"
sh: line 1: pdflatex: command not found

How to get pdflatex into PATH so that it’s found in the Python environment?

Seems that I could make it work by using mkShell but actually my usecase is a bit different…

The actual usecase, sorry for not providing this in the first place:

I have a systemd service in NixOS which runs a Python script. This Python script tries to run pdflatex but fails because it’s not in PATH. The relevant parts from the NixOS config:

let
  env = pkgs.python311.withPackages (ps: [
    # Some python dependencies here...
    numpy

    # Non-python dependencies
    pkgs.texlive.combined.scheme-full
  ]);
in {
  systemd.services.foobar = {
    serviceConfig = {
      ExecStart = "${env}/bin/python -m some_script";
    };
  };
};

How should I define the environment or run the python script so that pdflatex is found?

You can modify the path of the service, or package the script up (or inline/interpolate if it’s short enough for that to make sense).

Here’s how I run a python script with external dependencies in a systemd service:

  systemd.services."picture_copy" = {
    script = "${pkgs.python3}/bin/python3 /home/firecat53/docs/family/scott/src/scripts.git/bin/pix.py";
    path = [pkgs.python3 pkgs.rsync pkgs.exiftool];
    serviceConfig = {
      User = "firecat53";
    };
  };

Hope that helps!

1 Like