-
-
Notifications
You must be signed in to change notification settings - Fork 18k
cups-pdf(-to-pdf): init #182360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cups-pdf(-to-pdf): init #182360
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| { config, lib, pkgs, ... }: | ||
|
|
||
| let | ||
|
|
||
| # cups calls its backends as user `lp` (which is good!), | ||
| # but cups-pdf wants to be called as `root`, so it can change ownership of files. | ||
| # We add a suid wrapper and a wrapper script to trick cups into calling the suid wrapper. | ||
| # Note that a symlink to the suid wrapper alone wouldn't suffice, cups would complain | ||
| # > File "/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cups-progs/lib/cups/backend/cups-pdf" has insecure permissions (0104554/uid=0/gid=20) | ||
|
|
||
| # wrapper script that redirects calls to the suid wrapper | ||
| cups-pdf-wrapper = pkgs.writeTextFile { | ||
| name = "${pkgs.cups-pdf-to-pdf.name}-wrapper.sh"; | ||
| executable = true; | ||
| destination = "/lib/cups/backend/cups-pdf"; | ||
| checkPhase = '' | ||
| ${pkgs.stdenv.shellDryRun} "$target" | ||
| ${lib.getExe pkgs.shellcheck} "$target" | ||
| ''; | ||
| text = '' | ||
| #! ${pkgs.runtimeShell} | ||
| exec "${config.security.wrapperDir}/cups-pdf" "$@" | ||
| ''; | ||
| }; | ||
|
|
||
| # wrapped cups-pdf package that uses the suid wrapper | ||
| cups-pdf-wrapped = pkgs.buildEnv { | ||
| name = "${pkgs.cups-pdf-to-pdf.name}-wrapped"; | ||
| # using the wrapper as first path ensures it is used | ||
| paths = [ cups-pdf-wrapper pkgs.cups-pdf-to-pdf ]; | ||
| ignoreCollisions = true; | ||
| }; | ||
SuperSandro2000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| instanceSettings = name: { | ||
| freeformType = with lib.types; nullOr (oneOf [ int str path package ]); | ||
| # override defaults: | ||
| # inject instance name into paths, | ||
| # also avoid conflicts between user names and special dirs | ||
| options.Out = lib.mkOption { | ||
| type = with lib.types; nullOr singleLineStr; | ||
| default = "/var/spool/cups-pdf-${name}/users/\${USER}"; | ||
| defaultText = "/var/spool/cups-pdf-{instance-name}/users/\${USER}"; | ||
| example = "\${HOME}/cups-pdf"; | ||
|
||
| description = lib.mdDoc '' | ||
| output directory; | ||
| `''${HOME}` will be expanded to the user's home directory, | ||
| `''${USER}` will be expanded to the user name. | ||
| ''; | ||
| }; | ||
| options.AnonDirName = lib.mkOption { | ||
| type = with lib.types; nullOr singleLineStr; | ||
| default = "/var/spool/cups-pdf-${name}/anonymous"; | ||
| defaultText = "/var/spool/cups-pdf-{instance-name}/anonymous"; | ||
| example = "/var/lib/cups-pdf"; | ||
| description = lib.mdDoc "path for anonymously created PDF files"; | ||
| }; | ||
| options.Spool = lib.mkOption { | ||
| type = with lib.types; nullOr singleLineStr; | ||
| default = "/var/spool/cups-pdf-${name}/spool"; | ||
| defaultText = "/var/spool/cups-pdf-{instance-name}/spool"; | ||
| example = "/var/lib/cups-pdf"; | ||
| description = lib.mdDoc "spool directory"; | ||
| }; | ||
| options.Anonuser = lib.mkOption { | ||
| type = lib.types.singleLineStr; | ||
| default = "root"; | ||
| description = lib.mdDoc '' | ||
| User for anonymous PDF creation. | ||
| An empty string disables this feature. | ||
| ''; | ||
| }; | ||
| options.GhostScript = lib.mkOption { | ||
| type = with lib.types; nullOr path; | ||
| default = lib.getExe pkgs.ghostscript; | ||
| defaultText = lib.literalExpression "lib.getExe pkgs.ghostscript"; | ||
| example = lib.literalExpression ''''${pkgs.ghostscript}/bin/ps2pdf''; | ||
| description = lib.mdDoc "location of GhostScript binary"; | ||
| }; | ||
| }; | ||
|
|
||
| instanceConfig = { name, config, ... }: { | ||
| options = { | ||
| enable = (lib.mkEnableOption (lib.mdDoc "this cups-pdf instance")) // { default = true; }; | ||
| installPrinter = (lib.mkEnableOption (lib.mdDoc '' | ||
| a CUPS printer queue for this instance. | ||
| The queue will be named after the instance and will use the {file}`CUPS-PDF_opt.ppd` ppd file. | ||
| If this is disabled, you need to add the queue yourself to use the instance | ||
| '')) // { default = true; }; | ||
| confFileText = lib.mkOption { | ||
| type = lib.types.lines; | ||
| description = lib.mdDoc '' | ||
| This will contain the contents of {file}`cups-pdf.conf` for this instance, derived from {option}`settings`. | ||
| You can use this option to append text to the file. | ||
| ''; | ||
| }; | ||
| settings = lib.mkOption { | ||
| type = lib.types.submodule (instanceSettings name); | ||
| default = {}; | ||
| example = { | ||
| Out = "\${HOME}/cups-pdf"; | ||
| UserUMask = "0033"; | ||
| }; | ||
| description = lib.mdDoc '' | ||
| Settings for a cups-pdf instance, see the descriptions in the template config file in the cups-pdf package. | ||
| The key value pairs declared here will be translated into proper key value pairs for {file}`cups-pdf.conf`. | ||
| Setting a value to `null` disables the option and removes it from the file. | ||
| ''; | ||
| }; | ||
| }; | ||
| config.confFileText = lib.pipe config.settings [ | ||
| (lib.filterAttrs (key: value: value != null)) | ||
| (lib.mapAttrs (key: builtins.toString)) | ||
| (lib.mapAttrsToList (key: value: "${key} ${value}\n")) | ||
| lib.concatStrings | ||
| ]; | ||
| }; | ||
|
|
||
| cupsPdfCfg = config.services.printing.cups-pdf; | ||
|
|
||
| copyConfigFileCmds = lib.pipe cupsPdfCfg.instances [ | ||
| (lib.filterAttrs (name: lib.getAttr "enable")) | ||
| (lib.mapAttrs (name: lib.getAttr "confFileText")) | ||
| (lib.mapAttrs (name: pkgs.writeText "cups-pdf-${name}.conf")) | ||
| (lib.mapAttrsToList (name: confFile: "ln --symbolic --no-target-directory ${confFile} /var/lib/cups/cups-pdf-${name}.conf\n")) | ||
| lib.concatStrings | ||
| ]; | ||
|
|
||
| printerSettings = lib.pipe cupsPdfCfg.instances [ | ||
| (lib.filterAttrs (name: lib.getAttr "enable")) | ||
| (lib.filterAttrs (name: lib.getAttr "installPrinter")) | ||
| (lib.mapAttrsToList (name: instance: (lib.mapAttrs (key: lib.mkDefault) { | ||
| inherit name; | ||
| model = "CUPS-PDF_opt.ppd"; | ||
| deviceUri = "cups-pdf:/${name}"; | ||
| description = "virtual printer for cups-pdf instance ${name}"; | ||
| location = instance.settings.Out; | ||
| }))) | ||
| ]; | ||
|
|
||
| in | ||
|
|
||
| { | ||
|
|
||
| options.services.printing.cups-pdf = { | ||
| enable = lib.mkEnableOption (lib.mdDoc '' | ||
| the cups-pdf virtual pdf printer backend. | ||
| By default, this will install a single printer `pdf`. | ||
| but this can be changed/extended with {option}`services.printing.cups-pdf.instances` | ||
| ''); | ||
| instances = lib.mkOption { | ||
| type = lib.types.attrsOf (lib.types.submodule instanceConfig); | ||
| default.pdf = {}; | ||
| example.pdf.settings = { | ||
| Out = "\${HOME}/cups-pdf"; | ||
| UserUMask = "0033"; | ||
| }; | ||
| description = lib.mdDoc '' | ||
| Permits to raise one or more cups-pdf instances. | ||
| Each instance is named by an attribute name, and the attribute's values control the instance' configuration. | ||
| ''; | ||
| }; | ||
| }; | ||
|
|
||
| config = lib.mkIf cupsPdfCfg.enable { | ||
| services.printing.enable = true; | ||
| services.printing.drivers = [ cups-pdf-wrapped ]; | ||
| hardware.printers.ensurePrinters = printerSettings; | ||
| # the cups module will install the default config file, | ||
| # but we don't need it and it would confuse cups-pdf | ||
| systemd.services.cups.preStart = lib.mkAfter '' | ||
| rm -f /var/lib/cups/cups-pdf.conf | ||
| ${copyConfigFileCmds} | ||
| ''; | ||
| security.wrappers.cups-pdf = { | ||
| group = "lp"; | ||
| owner = "root"; | ||
| permissions = "+r,ug+x"; | ||
| setuid = true; | ||
| source = "${pkgs.cups-pdf-to-pdf}/lib/cups/backend/cups-pdf"; | ||
| }; | ||
| }; | ||
|
|
||
| meta.maintainers = [ lib.maintainers.yarny ]; | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import ./make-test-python.nix ({ lib, pkgs, ... }: { | ||
| name = "cups-pdf"; | ||
|
|
||
| nodes.machine = { pkgs, ... }: { | ||
| imports = [ ./common/user-account.nix ]; | ||
| environment.systemPackages = [ pkgs.poppler_utils ]; | ||
| fonts.fonts = [ pkgs.dejavu_fonts ]; # yields more OCR-able pdf | ||
| services.printing.cups-pdf.enable = true; | ||
| services.printing.cups-pdf.instances = { | ||
| opt = {}; | ||
| noopt.installPrinter = false; | ||
| }; | ||
| hardware.printers.ensurePrinters = [{ | ||
| name = "noopt"; | ||
| model = "CUPS-PDF_noopt.ppd"; | ||
| deviceUri = "cups-pdf:/noopt"; | ||
| }]; | ||
| }; | ||
|
|
||
| # we cannot check the files with pdftotext, due to | ||
| # https://github.com/alexivkin/CUPS-PDF-to-PDF/issues/7 | ||
| # we need `imagemagickBig` as it has ghostscript support | ||
|
|
||
| testScript = '' | ||
| from subprocess import run | ||
| machine.wait_for_unit("cups.service") | ||
| for name in ("opt", "noopt"): | ||
| text = f"test text {name}".upper() | ||
| machine.wait_until_succeeds(f"lpstat -v {name}") | ||
| machine.succeed(f"su - alice -c 'echo -e \"\n {text}\" | lp -d {name}'") | ||
| # wait until the pdf files are completely produced and readable by alice | ||
| machine.wait_until_succeeds(f"su - alice -c 'pdfinfo /var/spool/cups-pdf-{name}/users/alice/*.pdf'") | ||
| machine.succeed(f"cp /var/spool/cups-pdf-{name}/users/alice/*.pdf /tmp/{name}.pdf") | ||
| machine.copy_from_vm(f"/tmp/{name}.pdf", "") | ||
| run(f"${pkgs.imagemagickBig}/bin/convert -density 300 $out/{name}.pdf $out/{name}.jpeg", shell=True, check=True) | ||
SuperSandro2000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert text.encode() in run(f"${lib.getExe pkgs.tesseract} $out/{name}.jpeg stdout", shell=True, check=True, capture_output=True).stdout | ||
| ''; | ||
|
|
||
| meta.maintainers = [ lib.maintainers.yarny ]; | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| { lib | ||
| , stdenv | ||
| , fetchFromGitHub | ||
| , cups | ||
| , coreutils | ||
| , nixosTests | ||
| }: | ||
|
|
||
| stdenv.mkDerivation rec { | ||
| pname = "cups-pdf-to-pdf"; | ||
| version = "unstable-2021-12-22"; | ||
|
|
||
| src = fetchFromGitHub { | ||
| owner = "alexivkin"; | ||
| repo = "CUPS-PDF-to-PDF"; | ||
| rev = "c14428c2ca8e95371daad7db6d11c84046b1a2d4"; | ||
| hash = "sha256-pa4PFf8OAFSra0hSazmKUfbMYL/cVWvYA1lBf7c7jmY="; | ||
| }; | ||
|
|
||
| buildInputs = [ cups ]; | ||
|
|
||
| postPatch = '' | ||
| sed -r 's|(gscall, size, ")cp |\1${coreutils}/bin/cp |' cups-pdf.c -i | ||
| ''; | ||
|
|
||
| # gcc command line is taken from original cups-pdf's README file | ||
| # https://fossies.org/linux/cups-pdf/README | ||
| # however, we replace gcc with $CC following | ||
| # https://nixos.org/manual/nixpkgs/stable/#sec-darwin | ||
| buildPhase = '' | ||
| runHook preBuild | ||
| $CC -O9 -s cups-pdf.c -o cups-pdf -lcups | ||
| runHook postBuild | ||
| ''; | ||
|
|
||
| installPhase = '' | ||
| runHook preInstall | ||
| install -Dt $out/lib/cups/backend cups-pdf | ||
| install -Dm 0644 -t $out/etc/cups cups-pdf.conf | ||
| install -Dm 0644 -t $out/share/cups/model *.ppd | ||
| runHook postInstall | ||
| ''; | ||
|
|
||
| passthru.tests.vmtest = nixosTests.cups-pdf; | ||
|
|
||
| meta = with lib; { | ||
| description = "A CUPS backend that turns print jobs into searchable PDF files"; | ||
| homepage = "https://github.com/alexivkin/CUPS-PDF-to-PDF"; | ||
| license = licenses.gpl2Only; | ||
| maintainers = [ maintainers.yarny ]; | ||
| longDescription = '' | ||
| cups-pdf is a CUPS backend that generates a PDF file for each print job and puts this file | ||
| into a folder on the local machine such that the print job's owner can access the file. | ||
|
|
||
| https://www.cups-pdf.de/ | ||
|
|
||
| cups-pdf-to-pdf is a fork of cups-pdf which tries hard to preserve the original text of the print job by avoiding rasterization. | ||
|
|
||
| Note that in order to use this package, you have to make sure that the cups-pdf program is called with root privileges. | ||
| ''; | ||
| }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this wrapper script if we already have a wrapper? Why can't we use the suid wrapper alone?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CUPS is looking for its backend binaries in
$CUPS_PATH/lib/cups/backend/, but the suid wrapper is placed in/run/wrappers/bin/cups-pdf. So something must redirect CUPS from the former to the latter place. I also tried this with a symlink, i.e.but CUPS complains about this having insecure permissions and refuses to execute it. Putting a suid wrapper directly into
$CUPS_PATH/lib/cups/backend/is also not possible since that is a store path. So apart from completely reorganizing the way CUPS finds its backend binaries, using two wrappers is the only method I can come up with.I added a note about that implementation detail in the commit message of the corresponding commit so whoever wants to find out how I came up with this construction can look it up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move/copy this into a code, otherwise this question will come up again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I move it into the wrapper-introducing comment at the top of the nixos module.