Printing: Difference between revisions

m Fix HP printer troubleshooting sections
Improve formatting, add more syntax highlighting, add more links (to packages/options)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Printing in NixOS is done via the <code>services.printing</code> module, to configure the local printing services which is provided by the software [https://openprinting.github.io/projects/00-cups CUPS]. Setting up physical printer devices is done using <code>hardware.printers</code> option.
Printing in NixOS is done via the {{nixos:option|services.printing}} module, to configure the local printing services which is provided by the software [https://openprinting.github.io/projects/00-cups CUPS]. Setting up physical printer devices is done using {{nixos:option|hardware.printers}} option.


== Installation ==
== Installation ==
Line 11: Line 11:
== Configuration ==
== Configuration ==


=== Enable autodiscovery of network printers ===
=== Enable auto-discovery of network printers ===


Most printers manufactured after 2013 support the [https://www.pwg.org/ipp/everywhere.html IPP Everywhere] protocol, i.e. printing without installing drivers. This is notably the case of all WiFi printers marketed as Apple-compatible ([https://support.apple.com/en-ca/HT201311 list]).
Most printers manufactured after 2013 support the [https://www.pwg.org/ipp/everywhere.html IPP Everywhere] protocol, i.e. printing without installing drivers. This is notably the case of all WiFi printers marketed as Apple AirPrint-compatible ([https://support.apple.com/en-ca/HT201311 list]).


To detect these printers, add the following to your system configuration:
To detect these printers, add the following to your system configuration:
Line 21: Line 21:
   nssmdns4 = true;
   nssmdns4 = true;
   openFirewall = true;
   openFirewall = true;
};
services.printing = {
  enable = true;
  drivers = with pkgs; [
    cups-filters
    cups-browsed
  ];
};
};
</syntaxhighlight>
</syntaxhighlight>
Line 28: Line 36:
=== Adding printers ===
=== Adding printers ===


Beside manually adding printers with client tools, it is possible to permanently add printers to your system configuration (but be aware of [https://github.com/NixOS/nixpkgs/issues/78535 this bug] that sometimes expect the printer to be plugged in your system). The following example configures a network printer called <code>Dell_1250c</code> to your local system, reachable via IPP at <code>http://192.168.178.2:631/printers/Dell_1250c</code>
Beside manually adding printers with client tools, it is possible to permanently add printers to your system configuration (but be aware of [https://github.com/NixOS/nixpkgs/issues/78535 this bug] that sometimes expect the printer to be plugged in your system). The following example configures a network printer called <code>Dell_1250c</code> to your local system, reachable via IPP at <code><nowiki>http://192.168.178.2:631/printers/Dell_1250c</nowiki></code>


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 68: Line 76:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
hardware.printers = {
hardware.printers.ensurePrinters = [
  ensurePrinters = [
  {
    {
    name = "Dell_1250c";
      name = "Dell_1250c";
    location = "Home";
      location = "Home";
    deviceUri = "usb://Dell/1250c%20Color%20Printer?serial=YNP023240";
      deviceUri = "usb://Dell/1250c%20Color%20Printer?serial=YNP023240";
    model = "Dell-1250c.ppd.gz";
      model = "Dell-1250c.ppd.gz";
    ppdOptions = {
      ppdOptions = {
      PageSize = "A4";
        PageSize = "A4";
    };
      };
  }
    }
];
  ];
};
</syntaxhighlight>
</syntaxhighlight>


To add a printer over the smb protocol, [[Samba]] needs to be enabled:<syntaxhighlight lang="nix">
To add a printer over the smb protocol, [[Samba]] needs to be enabled:
hardware.printers.ensurePrinters = [{
<syntaxhighlight lang="nix">
hardware.printers.ensurePrinters = [
  {
     # (other stuff...)
     # (other stuff...)
     deviceUri = "smb://print-server.com/printername";
     deviceUri = "smb://print-server.com/printername";
   }];
   }
];


services.samba.enable = true;
services.samba.enable = true;
</syntaxhighlight>
</syntaxhighlight>


 
Some local or network printers might need additional drivers. You can add them using the <code>drivers</code> option:
Some local or network printers might need additional drivers. You can add them using the <code>drivers</code> option


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 109: Line 117:
* <tt>pkgs.splix</tt> &mdash; Drivers for printers supporting SPL (Samsung Printer Language).
* <tt>pkgs.splix</tt> &mdash; Drivers for printers supporting SPL (Samsung Printer Language).
* <tt>pkgs.brlaser</tt> &mdash; Drivers for some Brother printers
* <tt>pkgs.brlaser</tt> &mdash; Drivers for some Brother printers
* <tt>pkgs.brgenml1lpr</tt> and <tt>pkgs.brgenml1cupswrapper</tt> &mdash; Generic drivers for more Brother printers [https://support.brother.com/g/s/id/linux/en/instruction_prn1a.html]
* <tt>pkgs.brgenml1lpr</tt> and <tt>pkgs.brgenml1cupswrapper</tt> &mdash; Generic drivers for more Brother printers (Proprietary drivers) [https://support.brother.com/g/s/id/linux/en/instruction_prn1a.html]
* <tt>pkgs.cnijfilter2</tt> &mdash; Drivers for some Canon Pixma devices (Proprietary driver)
* <tt>pkgs.cnijfilter2</tt> &mdash; Drivers for some Canon Pixma devices (Proprietary driver)
* <tt>pkgs.epson-escpr2</tt> &mdash; Drivers for newer Epson devices.
* <tt>pkgs.epson-escpr</tt> &mdash; Drivers for some other Epson devices.


Search for other printer drivers in the NixOS package directory: the official list of packages is [https://search.nixos.org/packages here]. Add the driver to {{nixos:option|services.printing.drivers}}, '''not''' {{nixos:option|environment.systemPackages}}.
Search for other printer drivers in the NixOS package directory: the official list of packages is [https://search.nixos.org/packages here]. Add the driver to {{nixos:option|services.printing.drivers}}, '''not''' {{nixos:option|environment.systemPackages}}.
Line 131: Line 141:
</pre>
</pre>


The <code>model</code> string is the first column. For example, for the Samsung SCX-4300 series, set <code>model = "samsung/SCX-4300.ppd";</code>
The <code>model</code> string is the first column. For example, for the Samsung SCX-4300 series, set <syntaxhighlight inline lang="nix">model = "samsung/SCX-4300.ppd";</syntaxhighlight>.


=== Printer sharing ===
=== Printer sharing ===


Enable network sharing of the default local printer, also known as "AirPrinting". Note that <code>listenAddresses = [ "*:631" ];</code>, <code>allowFrom = [ "all" ];</code> and <code>openFirewall = true;</code> will enable anonymous access to your printer on all interfaces, you might want to restrict this.
Enable network sharing of the default local printer, also known as "AirPrinting". Note that <syntaxhighlight inline lang="nix">listenAddresses = [ "*:631" ];</syntaxhighlight>, <syntaxhighlight inline lang="nix">allowFrom = [ "all" ];</syntaxhighlight> and <syntaxhighlight inline lang="nix">openFirewall = true;</syntaxhighlight> will enable anonymous access to your printer on all interfaces, you might want to restrict this.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 156: Line 166:
</syntaxhighlight>
</syntaxhighlight>


Once printer sharing is enabled, it could be additionally advertised in the home network via the Samba protocol, [[Samba#Printer_sharing|see]].
Once printer sharing is enabled, it could be additionally advertised in the home network via the Samba protocol, see [[Samba#Printer_sharing]].


== Usage ==
== Usage ==
Line 162: Line 172:
After enabling the printing service you'll be able to configure and add network printers via http://localhost:631. You may need to authenticate with your local user when you add the printer.
After enabling the printing service you'll be able to configure and add network printers via http://localhost:631. You may need to authenticate with your local user when you add the printer.


Depending on your desktop environment, there are several graphical tools available which will connect to this backend service and allow you a more convenient printer management, for example <code>system-config-printer</code>.
Depending on your desktop environment, there are several graphical tools available which will connect to this backend service and allow you a more convenient printer management, for example {{nixos:package|system-config-printer}}.


=== Command line ===
=== Command line ===
Line 169: Line 179:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# lpstat -p
lpstat -p
</syntaxhighlight>
</syntaxhighlight>


Line 175: Line 185:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# lp -o job-sheets=standard,none -d HP-LaserJet-1020 /dev/null
lp -o job-sheets=standard,none -d HP-LaserJet-1020 /dev/null
</syntaxhighlight>
</syntaxhighlight>


Line 181: Line 191:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# lpstat
lpstat
</syntaxhighlight>
</syntaxhighlight>


Line 187: Line 197:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# cancel 1
cancel 1
</syntaxhighlight>
</syntaxhighlight>


Line 195: Line 205:


===== Provide the PPD imperatively =====
===== Provide the PPD imperatively =====
If no driver is found for your printer, even when <code>services.printing.drivers</code> is correctly populated (see above),
If no driver is found for your printer, even when {{nixos:option|services.printing.drivers}} is correctly populated (see above), you can try to give cups a PPD file.  
you can try to give cups a PPD file.  
* Download the required PPD file, for example from [https://openprinting.org/printers openprinting.org]
* Download the required PPD file, for example from [https://openprinting.org/printers openprinting.org]
* Open the PPD as a text file, and check that it does not mention FHS paths like <code>/usr/bin</code>. If it does, this method is unlikely to work, as the PPD file depends on executables not present on your system. You can certainly install the binaries yourself and point to the new binary, but it is certainly easier to patch the executables in a derivation (see below) to avoid garbage collection of your binaries.
* Open the PPD as a text file, and check that it does not mention FHS paths like <code>/usr/bin</code>. If it does, this method is unlikely to work, as the PPD file depends on executables not present on your system. You can certainly install the binaries yourself and point to the new binary, but it is certainly easier to patch the executables in a derivation (see below) to avoid garbage collection of your binaries.
Line 255: Line 264:


=== Upgrade required ===
=== Upgrade required ===
Described in: [https://github.com/NixOS/nixpkgs/issues/23993 Github issue 23993]<br />
Described in: [https://github.com/NixOS/nixpkgs/issues/23993 Github issue 23993]
 
'''Problem'''<br />
'''Problem'''<br />
Using the cups web interface, the page tells you "Upgrade Required" and then redirects you to a page that fails to load.<br />
Using the cups web interface, the page tells you "Upgrade Required" and then redirects you to a page that fails to load.<br />
Line 264: Line 274:


'''Solution'''<br />
'''Solution'''<br />
Either we can help cups to get ssl keys, or we can tell it to not use https at all.<br />
Either we can help cups to get ssl keys, or we can tell it to not use https at all.
 
''Generating ssl keys:''<br />
''Generating ssl keys:''<br />
First make sure the directory /etc/cups/ssl exists:<br />
First make sure the directory <code>/etc/cups/ssl</code> exists:
<code>sudo mkdir -p /etc/cups/ssl</code><br />
 
Try restarting cups and using the web interface again. This might be enough to get it working.<br />
<syntaxhighlight lang="bash">
If this didn't help, then check if cups has generated ssl keys in /etc/cups/ssl<br />
sudo mkdir -p /etc/cups/ssl
</syntaxhighlight>
 
Try restarting cups and using the web interface again. This might be enough to get it working.
If this didn't help, then check if cups has generated ssl keys in <code>/etc/cups/ssl</code>.
 
''Disabling ssl:''<br />
''Disabling ssl:''<br />
Edit your <code>/etc/nixos/configuration.nix</code> and add the following lines:<br />
Edit your <code>/etc/nixos/configuration.nix</code> and add the following lines:<br />
<code><pre>services.printing.extraConf = ''
 
    DefaultEncryption Never
<syntaxhighlight lang="nix">
  '';</pre></code>
services.printing.extraConf = ''
  DefaultEncryption Never
'';
</syntaxhighlight>


===Unable to launch Ghostscript: gs: No such file or directory===
===Unable to launch Ghostscript: gs: No such file or directory===
Described in: [https://github.com/NixOS/nixpkgs/issues/20806 Github issue 20806]
Described in: [https://github.com/NixOS/nixpkgs/issues/20806 Github issue 20806]
and [https://github.com/NixOS/nixpkgs/issues/22062 issues 22062]<br />
and [https://github.com/NixOS/nixpkgs/issues/22062 issues 22062]<br />
'''Problem'''<br />
'''Problem'''<br />
When printing, cups will report an error: Unable to launch Ghostscript: gs: No such file or directory<br />
When printing, cups will report an error: <code>Unable to launch Ghostscript: gs: No such file or directory</code><br />


'''Cause'''<br />
'''Cause'''<br />
Some drivers use the ghostscript binary.<br />
Some drivers use the ghostscript binary.<br />
Cups will look for the binary path in it's config file: cupsd.conf<br />
Cups will look for the binary path in it's config file: <code>cupsd.conf</code><br />
This file is normally a link. But it can be overwritten, and consequentially become outdated.<br />
This file is normally a link. But it can be overwritten, and consequentially become outdated.<br />


'''Solution'''<br />
'''Solution'''<br />
You could try to manually fix the path variable in /var/lib/cups/cupsd.conf<br />
You could try to manually fix the path variable in <code>/var/lib/cups/cupsd.conf</code><br />
Alternatively you could try to delete the file and run <code>sudo nixos-rebuild switch</code>
Alternatively you could try to delete the file and run <code>sudo nixos-rebuild switch</code>


Line 296: Line 316:


'''Problem'''<br />
'''Problem'''<br />
But, later you may experience an error like '''"/nix/store/.../lib/cups/filter/pstospl not available: No such file or directory"'''.  
But, later you may experience an error like <code>/nix/store/.../lib/cups/filter/pstospl not available: No such file or directory</code>.  


'''Cause'''<br />
'''Cause'''<br />
Line 306: Line 326:
=== Debugging a broken printer driver ===
=== Debugging a broken printer driver ===


Add to <code>/etc/nixos/configuration.nix</code>
Add to <code>/etc/nixos/configuration.nix</code>:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 314: Line 334:
Rebuild
Rebuild


<pre>
<syntaxhighlight lang="bash">
sudo nixos-rebuild switch
sudo nixos-rebuild switch
</pre>
</syntaxhighlight>


Watch the cups logs
Watch the cups logs


<syntaxhighlight lang=console>
<syntaxhighlight lang="bash">
$ journalctl --follow --unit=cups
journalctl --follow --unit=cups
</syntaxhighlight>
</syntaxhighlight>
or
or
<syntaxhighlight lang=console>
<syntaxhighlight lang="bash">
$ journalctl --follow --unit=cups | grep -C10 --color=always -i -e 'No such file or directory' -e 'error:'
journalctl --follow --unit=cups | grep -C10 --color=always -i -e 'No such file or directory' -e 'error:'
</syntaxhighlight>
</syntaxhighlight>


Line 342: Line 362:


'''Solution'''<br />
'''Solution'''<br />
Use <code>NIXPKGS_ALLOW_UNFREE=1 nix-shell -p hplipWithPlugin --run 'sudo -E hp-setup'</code> to add the printer.
Use <syntaxhighlight inline lang="bash">NIXPKGS_ALLOW_UNFREE=1 nix-shell -p hplipWithPlugin --run 'sudo -E hp-setup'</syntaxhighlight> to add the printer.
[[Category:Hardware]]
[[Category:Hardware]]