at 21.11-pre 5.3 kB view raw
1{ config, lib, pkgs, ... }: 2with lib; 3let 4 cfg = config.hardware.printers; 5 ppdOptionsString = options: optionalString (options != {}) 6 (concatStringsSep " " 7 (mapAttrsToList (name: value: "-o '${name}'='${value}'") options) 8 ); 9 ensurePrinter = p: '' 10 ${pkgs.cups}/bin/lpadmin -p '${p.name}' -E \ 11 ${optionalString (p.location != null) "-L '${p.location}'"} \ 12 ${optionalString (p.description != null) "-D '${p.description}'"} \ 13 -v '${p.deviceUri}' \ 14 -m '${p.model}' \ 15 ${ppdOptionsString p.ppdOptions} 16 ''; 17 ensureDefaultPrinter = name: '' 18 ${pkgs.cups}/bin/lpadmin -d '${name}' 19 ''; 20 21 # "graph but not # or /" can't be implemented as regex alone due to missing lookahead support 22 noInvalidChars = str: all (c: c != "#" && c != "/") (stringToCharacters str); 23 printerName = (types.addCheck (types.strMatching "[[:graph:]]+") noInvalidChars) 24 // { description = "printable string without spaces, # and /"; }; 25 26 27in { 28 options = { 29 hardware.printers = { 30 ensureDefaultPrinter = mkOption { 31 type = types.nullOr printerName; 32 default = null; 33 description = '' 34 Ensures the named printer is the default CUPS printer / printer queue. 35 ''; 36 }; 37 ensurePrinters = mkOption { 38 description = '' 39 Will regularly ensure that the given CUPS printers are configured as declared here. 40 If a printer's options are manually changed afterwards, they will be overwritten eventually. 41 This option will never delete any printer, even if removed from this list. 42 You can check existing printers with <command>lpstat -s</command> 43 and remove printers with <command>lpadmin -x &lt;printer-name&gt;</command>. 44 Printers not listed here can still be manually configured. 45 ''; 46 default = []; 47 type = types.listOf (types.submodule { 48 options = { 49 name = mkOption { 50 type = printerName; 51 example = "BrotherHL_Workroom"; 52 description = '' 53 Name of the printer / printer queue. 54 May contain any printable characters except "/", "#", and space. 55 ''; 56 }; 57 location = mkOption { 58 type = types.nullOr types.str; 59 default = null; 60 example = "Workroom"; 61 description = '' 62 Optional human-readable location. 63 ''; 64 }; 65 description = mkOption { 66 type = types.nullOr types.str; 67 default = null; 68 example = "Brother HL-5140"; 69 description = '' 70 Optional human-readable description. 71 ''; 72 }; 73 deviceUri = mkOption { 74 type = types.str; 75 example = [ 76 "ipp://printserver.local/printers/BrotherHL_Workroom" 77 "usb://HP/DESKJET%20940C?serial=CN16E6C364BH" 78 ]; 79 description = '' 80 How to reach the printer. 81 <command>lpinfo -v</command> shows a list of supported device URIs and schemes. 82 ''; 83 }; 84 model = mkOption { 85 type = types.str; 86 example = literalExample '' 87 gutenprint.''${lib.versions.majorMinor (lib.getVersion pkgs.gutenprint)}://brother-hl-5140/expert 88 ''; 89 description = '' 90 Location of the ppd driver file for the printer. 91 <command>lpinfo -m</command> shows a list of supported models. 92 ''; 93 }; 94 ppdOptions = mkOption { 95 type = types.attrsOf types.str; 96 example = { 97 PageSize = "A4"; 98 Duplex = "DuplexNoTumble"; 99 }; 100 default = {}; 101 description = '' 102 Sets PPD options for the printer. 103 <command>lpoptions [-p printername] -l</command> shows suported PPD options for the given printer. 104 ''; 105 }; 106 }; 107 }); 108 }; 109 }; 110 }; 111 112 config = mkIf (cfg.ensurePrinters != [] && config.services.printing.enable) { 113 systemd.services.ensure-printers = let 114 cupsUnit = if config.services.printing.startWhenNeeded then "cups.socket" else "cups.service"; 115 in { 116 description = "Ensure NixOS-configured CUPS printers"; 117 wantedBy = [ "multi-user.target" ]; 118 requires = [ cupsUnit ]; 119 # in contrast to cups.socket, for cups.service, this is actually not enough, 120 # as the cups service reports its activation before clients can actually interact with it. 121 # Because of this, commands like `lpinfo -v` will report a bad file descriptor 122 # due to the missing UNIX socket without sufficient sleep time. 123 after = [ cupsUnit ]; 124 125 serviceConfig = { 126 Type = "oneshot"; 127 }; 128 129 # sleep 10 is required to wait until cups.service is actually initialized and has created its UNIX socket file 130 script = (optionalString (!config.services.printing.startWhenNeeded) "sleep 10\n") 131 + (concatMapStringsSep "\n" ensurePrinter cfg.ensurePrinters) 132 + optionalString (cfg.ensureDefaultPrinter != null) (ensureDefaultPrinter cfg.ensureDefaultPrinter); 133 }; 134 }; 135}