1 { config, lib, pkgs, ... }:
3 cfg = config.hardware.printers;
6 args = lib.cli.toGNUCommandLineShell {} ({
10 } // lib.optionalAttrs (p.location != null) {
12 } // lib.optionalAttrs (p.description != null) {
14 } // lib.optionalAttrs (p.ppdOptions != {}) {
15 o = lib.mapAttrsToList (name: value: "${name}=${value}") p.ppdOptions;
18 ${pkgs.cups}/bin/lpadmin ${args} -E
21 ensureDefaultPrinter = name: ''
22 ${pkgs.cups}/bin/lpadmin -d '${name}'
25 # "graph but not # or /" can't be implemented as regex alone due to missing lookahead support
26 noInvalidChars = str: lib.all (c: c != "#" && c != "/") (lib.stringToCharacters str);
27 printerName = (lib.types.addCheck (lib.types.strMatching "[[:graph:]]+") noInvalidChars)
28 // { description = "printable string without spaces, # and /"; };
34 ensureDefaultPrinter = lib.mkOption {
35 type = lib.types.nullOr printerName;
38 Ensures the named printer is the default CUPS printer / printer queue.
41 ensurePrinters = lib.mkOption {
43 Will regularly ensure that the given CUPS printers are configured as declared here.
44 If a printer's options are manually changed afterwards, they will be overwritten eventually.
45 This option will never delete any printer, even if removed from this list.
46 You can check existing printers with {command}`lpstat -s`
47 and remove printers with {command}`lpadmin -x <printer-name>`.
48 Printers not listed here can still be manually configured.
51 type = lib.types.listOf (lib.types.submodule {
55 example = "BrotherHL_Workroom";
57 Name of the printer / printer queue.
58 May contain any printable characters except "/", "#", and space.
61 location = lib.mkOption {
62 type = lib.types.nullOr lib.types.str;
66 Optional human-readable location.
69 description = lib.mkOption {
70 type = lib.types.nullOr lib.types.str;
72 example = "Brother HL-5140";
74 Optional human-readable description.
77 deviceUri = lib.mkOption {
79 example = lib.literalExpression ''
80 "ipp://printserver.local/printers/BrotherHL_Workroom"
81 "usb://HP/DESKJET%20940C?serial=CN16E6C364BH"
84 How to reach the printer.
85 {command}`lpinfo -v` shows a list of supported device URIs and schemes.
88 model = lib.mkOption {
90 example = lib.literalExpression ''
91 "gutenprint.''${lib.versions.majorMinor (lib.getVersion pkgs.gutenprint)}://brother-hl-5140/expert"
94 Location of the ppd driver file for the printer.
95 {command}`lpinfo -m` shows a list of supported models.
98 ppdOptions = lib.mkOption {
99 type = lib.types.attrsOf lib.types.str;
102 Duplex = "DuplexNoTumble";
106 Sets PPD options for the printer.
107 {command}`lpoptions [-p printername] -l` shows supported PPD options for the given printer.
116 config = lib.mkIf (cfg.ensurePrinters != [] && config.services.printing.enable) {
117 systemd.services.ensure-printers = {
118 description = "Ensure NixOS-configured CUPS printers";
119 wantedBy = [ "multi-user.target" ];
120 wants = [ "cups.service" ];
121 after = [ "cups.service" ];
125 RemainAfterExit = true;
128 script = lib.concatStringsSep "\n" [
129 (lib.concatMapStrings ensurePrinter cfg.ensurePrinters)
130 (lib.optionalString (cfg.ensureDefaultPrinter != null)
131 (ensureDefaultPrinter cfg.ensureDefaultPrinter))
132 # Note: if cupsd is "stateless" the service can't be stopped,
133 # otherwise the configuration will be wiped on the next start.
134 (lib.optionalString (with config.services.printing; startWhenNeeded && !stateless)
135 "systemctl stop cups.service")