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 = lib.mdDoc ''
34 Ensures the named printer is the default CUPS printer / printer queue.
35 '';
36 };
37 ensurePrinters = mkOption {
38 description = lib.mdDoc ''
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`
43 and remove printers with {command}`lpadmin -x <printer-name>`.
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 = lib.mdDoc ''
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 = lib.mdDoc ''
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 = lib.mdDoc ''
70 Optional human-readable description.
71 '';
72 };
73 deviceUri = mkOption {
74 type = types.str;
75 example = literalExpression ''
76 "ipp://printserver.local/printers/BrotherHL_Workroom"
77 "usb://HP/DESKJET%20940C?serial=CN16E6C364BH"
78 '';
79 description = lib.mdDoc ''
80 How to reach the printer.
81 {command}`lpinfo -v` shows a list of supported device URIs and schemes.
82 '';
83 };
84 model = mkOption {
85 type = types.str;
86 example = literalExpression ''
87 "gutenprint.''${lib.versions.majorMinor (lib.getVersion pkgs.gutenprint)}://brother-hl-5140/expert"
88 '';
89 description = lib.mdDoc ''
90 Location of the ppd driver file for the printer.
91 {command}`lpinfo -m` 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 = lib.mdDoc ''
102 Sets PPD options for the printer.
103 {command}`lpoptions [-p printername] -l` shows supported 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 = {
114 description = "Ensure NixOS-configured CUPS printers";
115 wantedBy = [ "multi-user.target" ];
116 wants = [ "cups.service" ];
117 after = [ "cups.service" ];
118
119 serviceConfig = {
120 Type = "oneshot";
121 RemainAfterExit = true;
122 };
123
124 script = concatStringsSep "\n" [
125 (concatMapStrings ensurePrinter cfg.ensurePrinters)
126 (optionalString (cfg.ensureDefaultPrinter != null)
127 (ensureDefaultPrinter cfg.ensureDefaultPrinter))
128 # Note: if cupsd is "stateless" the service can't be stopped,
129 # otherwise the configuration will be wiped on the next start.
130 (optionalString (with config.services.printing; startWhenNeeded && !stateless)
131 "systemctl stop cups.service")
132 ];
133 };
134 };
135}