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 lapack,blas: allow for more flexible use of ILP64 interface 2022-02-11 17:51:35 +01:00
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 buildFHSUserEnv: append graphics share to XDG_DATA_DIR 2022-03-24 12:53:39 -07:00
build-fhs-userenv-bubblewrap FHSEnv: export /etc/shells 2022-10-18 22:59:26 -07:00
build-graalvm-native-image buildGraalvmNativeImage: allow LC_ALL overrides 2022-07-03 18:16:38 +01:00
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 dhallDirectoryToNix: add this utility function 2021-12-07 13:36:21 +09:00
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 treewide: add enableParallelBuilding's to bootstrap packages so hashes stay the same 2022-05-25 16:03:14 +03:00
fake-nss fakeNss: move to toplevel 2022-03-24 18:47:30 +01:00
fetchbitbucket
fetchbower
fetchbzr
fetchcvs treewide: fix bash exit handlers 2022-07-02 16:13:12 +02:00
fetchdarcs
fetchdocker
fetchfirefoxaddon testers.invalidateFetcherByDrvHash: Move from top-level 2022-05-05 12:08:50 +02:00
fetchfossil
fetchgit fetchgit: allow disabling cone mode for sparse checkouts, fix test 2022-08-14 23:03:07 +02:00
fetchgitea
fetchgithub unstableGitUpdater: fix updating fetchzip-based sources 2022-05-24 19:12:12 +02:00
fetchgitiles
fetchgitlab fetchFromGitLab: don't passthruAttrs fetchGit specific attrs to fetchUrl 2022-10-29 19:12:07 +03:00
fetchgitlocal
fetchgx
fetchhg
fetchipfs treewide: clean up obsolete version checks 2022-03-22 10:54:11 +00:00
fetchmavenartifact
fetchmtn
fetchnextcloudapp fetchNextcloudApp: rewrite with fetchzip & applyPatches 2022-09-26 20:32:33 +02:00
fetchpatch testers.invalidateFetcherByDrvHash: Move from top-level 2022-05-05 12:08:50 +02:00
fetchrepoorcz
fetchrepoproject
fetchs3
fetchsavannah
fetchsourcehut treewide: extraPostFetch -> postFetch 2022-05-23 17:18:50 +03:00
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 flutter.mkFlutterApp: fix installing .desktop 2022-02-23 15:10:59 +01:00
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 libredirect: Fix cross compilation buildPackages 2022-07-20 10:26:48 +12:00
make-darwin-bundle Merge pull request #165111 from toonn/darwin-bundle-outputBin 2022-04-22 12:04:09 +01:00
make-desktopitem build-support/makeDesktopItem: make overridable 2022-06-09 15:32:44 +01:00
make-pkgconfigitem makePkgconfigItem: init new function to generate pc files 2022-07-20 06:22:39 +03:00
make-startupitem
make-symlinks
mkshell mkShell: make it buildable (#153194) 2022-01-08 01:54:04 +01:00
mono-dll-fixer
nix-gitignore treewide: clean up obsolete version checks 2022-03-22 10:54:11 +00:00
node/fetch-yarn-deps fetchYarnDeps: support passing src attribute (#193047) 2022-10-02 22:41:31 +02:00
nuke-references treewide: add enableParallelBuilding's to bootstrap packages so hashes stay the same 2022-05-25 16:03:14 +03:00
ocaml ocamlPackages.buildOcaml: remove 2022-10-25 09:40:57 +02:00
oci-tools
pkg-config-wrapper treewide: add enableParallelBuilding's to bootstrap packages so hashes stay the same 2022-05-25 16:03:14 +03:00
portable-service portableService: tooling to create portable service images 2022-09-22 20:11:25 +02:00
prefer-remote-fetch prefer-remote-fetch: don't overwrite fetcher's which set preferLocalBuild explicitly 2022-06-03 14:05:37 +02:00
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 singularity: enable setting vm mem in buildImage 2022-03-03 17:09:48 -05:00
snap
src-only srcOnly: remove unused binding 2022-09-26 22:18:55 -04:00
substitute
substitute-files
templaterpm maintainers: remove tstrobel 2022-06-29 00:54:53 +02:00
testers nixos/testing: Fix release.nix tests evaluation 2022-09-29 10:49:09 +02:00
trivial-builders treewide: nixosTest -> testers.nixosTest 2022-05-09 14:16:06 +02:00
vm treewide: change some glibc to stdenv.cc.libc 2022-05-27 05:57:43 +03:00
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 replace-dependency: fix a syntax error while generating references.nix 2022-06-12 06:44:30 +04:00
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