at 25.11-pre 7.3 kB view raw
1{ 2 config, 3 lib, 4 pkgs, 5 ... 6}: 7 8let 9 10 # cups calls its backends as user `lp` (which is good!), 11 # but cups-pdf wants to be called as `root`, so it can change ownership of files. 12 # We add a suid wrapper and a wrapper script to trick cups into calling the suid wrapper. 13 # Note that a symlink to the suid wrapper alone wouldn't suffice, cups would complain 14 # > File "/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cups-progs/lib/cups/backend/cups-pdf" has insecure permissions (0104554/uid=0/gid=20) 15 16 # wrapper script that redirects calls to the suid wrapper 17 cups-pdf-wrapper = pkgs.writeTextFile { 18 name = "${pkgs.cups-pdf-to-pdf.name}-wrapper.sh"; 19 executable = true; 20 destination = "/lib/cups/backend/cups-pdf"; 21 checkPhase = '' 22 ${pkgs.stdenv.shellDryRun} "$target" 23 ${lib.getExe pkgs.shellcheck} "$target" 24 ''; 25 text = '' 26 #! ${pkgs.runtimeShell} 27 exec "${config.security.wrapperDir}/cups-pdf" "$@" 28 ''; 29 }; 30 31 # wrapped cups-pdf package that uses the suid wrapper 32 cups-pdf-wrapped = pkgs.buildEnv { 33 name = "${pkgs.cups-pdf-to-pdf.name}-wrapped"; 34 # using the wrapper as first path ensures it is used 35 paths = [ 36 cups-pdf-wrapper 37 pkgs.cups-pdf-to-pdf 38 ]; 39 ignoreCollisions = true; 40 }; 41 42 instanceSettings = name: { 43 freeformType = 44 with lib.types; 45 nullOr (oneOf [ 46 int 47 str 48 path 49 package 50 ]); 51 # override defaults: 52 # inject instance name into paths, 53 # also avoid conflicts between user names and special dirs 54 options.Out = lib.mkOption { 55 type = with lib.types; nullOr singleLineStr; 56 default = "/var/spool/cups-pdf-${name}/users/\${USER}"; 57 defaultText = "/var/spool/cups-pdf-{instance-name}/users/\${USER}"; 58 example = "\${HOME}/cups-pdf"; 59 description = '' 60 output directory; 61 `''${HOME}` will be expanded to the user's home directory, 62 `''${USER}` will be expanded to the user name. 63 ''; 64 }; 65 options.AnonDirName = lib.mkOption { 66 type = with lib.types; nullOr singleLineStr; 67 default = "/var/spool/cups-pdf-${name}/anonymous"; 68 defaultText = "/var/spool/cups-pdf-{instance-name}/anonymous"; 69 example = "/var/lib/cups-pdf"; 70 description = "path for anonymously created PDF files"; 71 }; 72 options.Spool = lib.mkOption { 73 type = with lib.types; nullOr singleLineStr; 74 default = "/var/spool/cups-pdf-${name}/spool"; 75 defaultText = "/var/spool/cups-pdf-{instance-name}/spool"; 76 example = "/var/lib/cups-pdf"; 77 description = "spool directory"; 78 }; 79 options.Anonuser = lib.mkOption { 80 type = lib.types.singleLineStr; 81 default = "root"; 82 description = '' 83 User for anonymous PDF creation. 84 An empty string disables this feature. 85 ''; 86 }; 87 options.GhostScript = lib.mkOption { 88 type = with lib.types; nullOr path; 89 default = lib.getExe pkgs.ghostscript; 90 defaultText = lib.literalExpression "lib.getExe pkgs.ghostscript"; 91 example = lib.literalExpression ''''${pkgs.ghostscript}/bin/ps2pdf''; 92 description = "location of GhostScript binary"; 93 }; 94 }; 95 96 instanceConfig = 97 { name, config, ... }: 98 { 99 options = { 100 enable = (lib.mkEnableOption "this cups-pdf instance") // { 101 default = true; 102 }; 103 installPrinter = 104 (lib.mkEnableOption '' 105 a CUPS printer queue for this instance. 106 The queue will be named after the instance and will use the {file}`CUPS-PDF_opt.ppd` ppd file. 107 If this is disabled, you need to add the queue yourself to use the instance 108 '') 109 // { 110 default = true; 111 }; 112 confFileText = lib.mkOption { 113 type = lib.types.lines; 114 description = '' 115 This will contain the contents of {file}`cups-pdf.conf` for this instance, derived from {option}`settings`. 116 You can use this option to append text to the file. 117 ''; 118 }; 119 settings = lib.mkOption { 120 type = lib.types.submodule (instanceSettings name); 121 default = { }; 122 example = { 123 Out = "\${HOME}/cups-pdf"; 124 UserUMask = "0033"; 125 }; 126 description = '' 127 Settings for a cups-pdf instance, see the descriptions in the template config file in the cups-pdf package. 128 The key value pairs declared here will be translated into proper key value pairs for {file}`cups-pdf.conf`. 129 Setting a value to `null` disables the option and removes it from the file. 130 ''; 131 }; 132 }; 133 config.confFileText = lib.pipe config.settings [ 134 (lib.filterAttrs (key: value: value != null)) 135 (lib.mapAttrs (key: builtins.toString)) 136 (lib.mapAttrsToList (key: value: "${key} ${value}\n")) 137 lib.concatStrings 138 ]; 139 }; 140 141 cupsPdfCfg = config.services.printing.cups-pdf; 142 143 copyConfigFileCmds = lib.pipe cupsPdfCfg.instances [ 144 (lib.filterAttrs (name: lib.getAttr "enable")) 145 (lib.mapAttrs (name: lib.getAttr "confFileText")) 146 (lib.mapAttrs (name: pkgs.writeText "cups-pdf-${name}.conf")) 147 (lib.mapAttrsToList ( 148 name: confFile: 149 "ln --symbolic --no-target-directory ${confFile} /var/lib/cups/cups-pdf-${name}.conf\n" 150 )) 151 lib.concatStrings 152 ]; 153 154 printerSettings = lib.pipe cupsPdfCfg.instances [ 155 (lib.filterAttrs (name: lib.getAttr "enable")) 156 (lib.filterAttrs (name: lib.getAttr "installPrinter")) 157 (lib.mapAttrsToList ( 158 name: instance: 159 (lib.mapAttrs (key: lib.mkDefault) { 160 inherit name; 161 model = "CUPS-PDF_opt.ppd"; 162 deviceUri = "cups-pdf:/${name}"; 163 description = "virtual printer for cups-pdf instance ${name}"; 164 location = instance.settings.Out; 165 }) 166 )) 167 ]; 168 169in 170 171{ 172 173 options.services.printing.cups-pdf = { 174 enable = lib.mkEnableOption '' 175 the cups-pdf virtual pdf printer backend. 176 By default, this will install a single printer `pdf`. 177 but this can be changed/extended with {option}`services.printing.cups-pdf.instances` 178 ''; 179 instances = lib.mkOption { 180 type = lib.types.attrsOf (lib.types.submodule instanceConfig); 181 default.pdf = { }; 182 example.pdf.settings = { 183 Out = "\${HOME}/cups-pdf"; 184 UserUMask = "0033"; 185 }; 186 description = '' 187 Permits to raise one or more cups-pdf instances. 188 Each instance is named by an attribute name, and the attribute's values control the instance' configuration. 189 ''; 190 }; 191 }; 192 193 config = lib.mkIf cupsPdfCfg.enable { 194 services.printing.enable = true; 195 services.printing.drivers = [ cups-pdf-wrapped ]; 196 hardware.printers.ensurePrinters = printerSettings; 197 # the cups module will install the default config file, 198 # but we don't need it and it would confuse cups-pdf 199 systemd.services.cups.preStart = lib.mkAfter '' 200 rm -f /var/lib/cups/cups-pdf.conf 201 ${copyConfigFileCmds} 202 ''; 203 security.wrappers.cups-pdf = { 204 group = "lp"; 205 owner = "root"; 206 permissions = "+r,ug+x"; 207 setuid = true; 208 source = "${pkgs.cups-pdf-to-pdf}/lib/cups/backend/cups-pdf"; 209 }; 210 }; 211 212 meta.maintainers = [ lib.maintainers.yarny ]; 213 214}