nixpkgs/pkgs/build-support
Yarny0 335a9083b0 patchPpdFilesHook: new setup hook for absolute executable paths
PostScript Printer Description (ppd) files
describe printer features and capabilities.
They are usually evaluated by CUPS to convert
print jobs into a format suitable for a printer.
The conversion is often accomplished by commands
or even short shell scripts inside the ppd files.

ppd files are included in many printer driver packages.
Their scripts sometimes refer to other executables;
some of them are more common (like `perl`),
others are more exotic (like `rastertohp`).
If an executable is called with its name alone,
the effects of the ppd file depend on whether
the executable is in the PATH of CUPS,
and on the executable's version.
If an executable is called with an absolut path
(like `/usr/bin/perl`), it won't work at all in NixOS.

The commit at hand adds a setup hook that uses
the `fixupPhase` to substitute certain executable's
invocations in pdd files with absolute paths.
To use it, add the hook to `nativeBuildInputs` and
provide a list of executable names in `ppdFileCommands`.
Each executable must be available in the
package that is being built, or in `buildInputs`.
The setup hook's script then looks for ppd files in
`share/cups/model` and `share/ppds` in each output,
and replaces executable names with their absolute paths.
If ppd files need to be patched in unorthodox locations or
the setup hook needs to be invoked manually for other reasons,
one may leave the list `ppdFileCommands` empty to
avoid automatic processing of ppd files, then call
the shell function `patchPpdFileCommands` directly.
Details are described in the file `patch-ppd-hook.sh`.

Notes on the motivation for this setup hook:

Most packages in nixpkgs that provide
ppd files do not patch those ppd files at all.
This is not fatal when the executables are just called
with their names since the user can add packages
with the executables to `services.printing.drivers`.
E.g. if the user adds `pkgs.perl`, then all ppd
files that invoke `perl` will work as expected.
Nevertheless, to make these ppd files independent of
their execution environment, command invocations should
be substituted with absolut paths into the nix store.
This is similar to patching shebang lines so scripts can be
called independently of having the interpreter in the PATH.

The hook script in this commit is meant to support new packages
`foomatic-db*` which will generate several thousands of
ppd files referencing a plethora of different executables.
During development of these packages, I realized that
it's quite hard to patch ppd files in a robust way.
While binary names like `rastertokpsl` seem to be sufficiently
unique to be patched with `sed`, names like `date` or `gs`
are hard to patch without producing "false positives",
i.e., coincidental occurences of the executable's name that do
*not* refer to the executable and should not be patched at all.
As this problem also affects other packages,
it seems reasonable to put a robust implementation
in its own setup hook so that other
packages can use it without much effort.

Notes on the implementation:

The ppd file format is far from trivial.
The basic structure are key-value pairs;
keys may occur multiple times.
Only a small subset of keys may contain
executable names or shell scripts in their values.
Some values may span multiple lines;
a linebreak might even occur in the middle of a token.
Some executable names also occur in other keys by accident
where they must not be substituted (e.g. `gs` or `date`).

It is necessary to provide the list of command
names that will be patched for two reasons:
ppd files often contain "tokens" that might look
like commands (e.g. "file" or "host") but aren't;
these would erroneously get patched.
Also, looking for everything that might be a command
would slow down the patching process considerably.

The implementation uses `awk` to detect
keys that might contain executable names;
only their values are treated for substitution.
This avoids most cases of "overzealous" substitutions.
Since values may span multiple lines,
`sed` alone (while faster than `awk`) cannot focus
its substitution capabilities on relevant keys.
An elaborate set of regular expressions further helps
to minimize the probability of "false positives".
Several tricks are employed to speed up `awk`.
Notably, relevant files are identified with
`grep` before `awk` is applied to those files only.
Note that the script probably cannot handle fancy command
names (like spaces or backslashes as part of the name).
Also, there are still edge cases that the script would
mistakenly skip, e.g. if a shell script contains a
line break in the middle of an executable's name;
although ppd files permit such constellations,
I have yet to see one.

ppd files may be gzipped.
The setup hook accepts gzipped ppd files:
It decompresses them, substitutes paths, then recompresses them.
However, Nix cannot detect substituted paths as
runtime dependencies in compressed ppd files.
To ensure substituted paths are propagated as
runtime dependencies, the script adds each substituted
path to the variable `propagatedBuildInputs`.
Since this might not be enough for multi-output packages,
those paths are also written directly to
`nix-support/propagated-build-inputs`.
See the comment in `patch-ppd-hook.sh` for details.

Finally, the setup hook comes with a small test that
probes some edge cases with an artificial ppd file.

References:

* https://www.cups.org/doc/spec-ppd.html
  * general ppd file specification
  * lists some keys that may contain
    executable names or shell scripts

* https://refspecs.linuxfoundation.org/LSB_4.0.0/LSB-Printing/LSB-Printing/ppdext.html
  * lists some keys that may contain
    executable names or shell scripts

* https://en.wikipedia.org/wiki/PostScript_Printer_Description#CUPS
  * lists the usual locations of ppd files
2022-11-06 16:19:22 +01:00
..
add-opengl-runpath
agda
alternatives
appimage appimage-run: add vulkan-loader and libpulseaudio 2022-08-23 17:45:54 +02:00
bintools-wrapper bintools-wrapper, cc-wrapper: avoid invalid export of 'expand-response-params' 2022-07-31 16:31:13 +01:00
build-bazel-package buildBazelPackage: optionally run bazel tests in checkPhase 2022-10-03 12:52:57 +02:00
build-fhs-userenv
build-fhs-userenv-bubblewrap FHSEnv: export /etc/shells 2022-10-18 22:59:26 -07:00
build-graalvm-native-image
build-setupcfg
buildenv
cc-wrapper Revert "Merge #191724: cc-wrapper: remove duplicate include flags" 2022-10-28 10:28:12 +02:00
coq coqPackages.mkCoqDerivation: upgrade to Dune 3 2022-10-02 14:42:28 +02:00
dhall
docker Merge pull request #141050 from koenw/dockertools_too_many_arguments 2022-10-13 21:45:10 +01:00
dotnet build-dotnet-module: don't end with exit code 1 when update was sucessfull 2022-10-25 16:12:22 +02:00
emacs emacs: remove warning of xargs when doing AOT native-comp 2022-10-04 02:53:55 +08:00
expand-response-params
fake-nss
fetchbitbucket
fetchbower
fetchbzr
fetchcvs
fetchdarcs
fetchdocker
fetchfirefoxaddon
fetchfossil
fetchgit fetchgit: allow disabling cone mode for sparse checkouts, fix test 2022-08-14 23:03:07 +02:00
fetchgitea
fetchgithub
fetchgitiles
fetchgitlab fetchFromGitLab: don't passthruAttrs fetchGit specific attrs to fetchUrl 2022-10-29 19:12:07 +03:00
fetchgitlocal
fetchgx
fetchhg
fetchipfs
fetchmavenartifact
fetchmtn
fetchnextcloudapp fetchNextcloudApp: rewrite with fetchzip & applyPatches 2022-09-26 20:32:33 +02:00
fetchpatch
fetchrepoorcz
fetchrepoproject
fetchs3
fetchsavannah
fetchsourcehut
fetchsvn
fetchsvnrevision
fetchsvnssh
fetchurl fixup! fetchurl: add pname+version support 2022-10-05 15:54:57 +02:00
fetchzip fetchzip: add pname+version support 2022-10-04 18:57:46 +02:00
flutter
go buildGo{Module,Package}: don't run vet linter 2022-08-20 07:37:14 +10:00
icon-conv-tools
install-shell-files
java
kernel make-initrd-ng: clean up a bit 2022-08-02 10:54:55 +03:00
libredirect
make-darwin-bundle
make-desktopitem
make-pkgconfigitem
make-startupitem
make-symlinks
mkshell
mono-dll-fixer
nix-gitignore
node/fetch-yarn-deps fetchYarnDeps: support passing src attribute (#193047) 2022-10-02 22:41:31 +02:00
nuke-references
ocaml ocamlPackages.buildOcaml: remove 2022-10-25 09:40:57 +02:00
oci-tools
pkg-config-wrapper
portable-service portableService: tooling to create portable service images 2022-09-22 20:11:25 +02:00
prefer-remote-fetch
references-by-popularity
release treewide: *Flags convert to list from str 2022-10-10 15:30:59 +03:00
remove-references-to removeReferencesTo: kill lone hashes 2022-08-25 17:25:58 +02:00
replace-secret
rust Merge branch 'master' into staging-next 2022-10-08 10:20:07 +02:00
setup-hooks patchPpdFilesHook: new setup hook for absolute executable paths 2022-11-06 16:19:22 +01:00
singularity-tools
snap
src-only srcOnly: remove unused binding 2022-09-26 22:18:55 -04:00
substitute
substitute-files
templaterpm
testers nixos/testing: Fix release.nix tests evaluation 2022-09-29 10:49:09 +02:00
trivial-builders
vm
wrapper-common
writers writers.writeJS: pass arguments to script 2022-10-11 20:16:37 +02:00
build-maven.nix
build-pecl.nix
closure-info.nix
plugins.nix
replace-dependency.nix
setup-systemd-units.nix
source-from-head-fun.nix
trivial-builders.nix Merge pull request #186323 from ShamrockLee/make-setuphook-passthru 2022-09-19 11:30:37 +01:00