Printing: Difference between revisions

imported>Onny
Further cleanup
imported>Onny
Further cleanup
Line 19: Line 19:
=== Adding printers ===
=== Adding printers ===


Beside manually adding printers with client tools, it is possible to permanently add printers to your system configuration:
Beside manually adding printers with client tools, it is possible to permanently add printers to your system configuration. The following example adds the printer <code>Dell_1250c</code> via local USB address by also defining which driver to use using the <code>model</code> option.


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


Line 46: Line 59:
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}}.


==== With a raw PPD ====
The following example configures a network printer to your local system, reachable via IPP at <code>http://192.168.178.2:631/printers/Dell_1250c</code>
====== Provide the PPD imperatively ======
 
<syntaxhighlight lang="nix">
hardware.printers = {
  ensurePrinters = [
    {
      name = "Dell_1250c";
      location = "Home";
      deviceUri = "http://192.168.178.2:631/printers/Dell_1250c";
      model = "drv:///sample.drv/generic.ppd";
      ppdOptions = {
        PageSize = "A4";
      };
    }
  ];
};
</syntaxhighlight>
 
=== Enable autodiscovery 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]).
 
To detect these printers, add the following to your system configuration:
<syntaxhighlight lang="nix">
services.avahi = {
  enable = true;
  nssmdns = true;
  openFirewall = true;
}
</syntaxhighlight>
 
Discovery is done via the opened UDP port <code>5353</code>. Printers should get automatically detected and visible in your printer configuration client.
 
=== Printer sharing ===
 
Enable network sharing of the default local printer. Note that <code>listenAddresses = [ "*:631" ];</code>  and <code>allowFrom = [ "all" ];</code> will enable anonymous access to your printer on all interfaces, you might want to restrict this.
 
<syntaxhighlight lang="nix">
services.avahi = {
  enable = true;
  nssmdns = true;
  openFirewall = true;
  publish = {
    enable = true;
    userServices = true;
  };
};
services.printing = {
  listenAddresses = [ "*:631" ];
  allowFrom = [ "all" ];
  browsing = true;
  defaultShared = true;
};
networking.firewall = {
  allowedTCPPorts = [ 631 ];
  allowedUDPPorts = [ 631 ];
};
</syntaxhighlight>
 
Once printer sharing is enabled, it could be additionally advertised in the home network via the Samba protocol, [[Samba#Printer_sharing|see]].
 
== Tips and tricks ==
 
==== Manually supplying printer driver  ====
 
===== 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 <code>services.printing.drivers</code> is correctly populated (see above),
you can try to give cups a PPD file.  
you can try to give cups a PPD file.  
Line 54: Line 131:
* add the printer with <code>system-config-printer</code> (for example) and at the 'choose driver' screen choose 'provide PPD file'
* add the printer with <code>system-config-printer</code> (for example) and at the 'choose driver' screen choose 'provide PPD file'


====== Provide the PPD declaratively ======
===== Provide the PPD declaratively =====


You can also declaratively add the PPD as a new driver by creating a simple derivation. You just need to create a derivation that puts the PPD file in <code>$out/share/cups/model/yourfile.ppd</code> (you can also put it in a subfolder like <code>$out/share/cups/model/HP/yourfile.ppd</code> to limit conflicts between ppd having the same name). Note that the name of the file does not change the way cups will list it as the model/manufacturer is written inside the (text) ppd.
You can also declaratively add the PPD as a new driver by creating a simple derivation. You just need to create a derivation that puts the PPD file in <code>$out/share/cups/model/yourfile.ppd</code> (you can also put it in a subfolder like <code>$out/share/cups/model/HP/yourfile.ppd</code> to limit conflicts between ppd having the same name). Note that the name of the file does not change the way cups will list it as the model/manufacturer is written inside the (text) ppd.
Line 104: Line 181:


For debugging purpose, it may be interesting to note that the data folder used by cups (containing the drivers and more) can be obtained by looking in the environment <code>$CUPS_DATADIR</code> (the contents of <code>$out/share/cups/</code> contained in your drivers are linked in this folder).
For debugging purpose, it may be interesting to note that the data folder used by cups (containing the drivers and more) can be obtained by looking in the environment <code>$CUPS_DATADIR</code> (the contents of <code>$out/share/cups/</code> contained in your drivers are linked in this folder).
=== Enable autodiscovery 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]).
To detect these printers, add the following to your system configuration:
<syntaxhighlight lang="nix">
services.avahi = {
  enable = true;
  nssmdns = true;
  openFirewall = true;
}
</syntaxhighlight>
Discovery is done via the opened UDP port <code>5353</code>. Printers should get automatically detected and visible in your printer configuration client.
=== Printer sharing ===
Enable network sharing of the default local printer. Note that <code>listenAddresses = [ "*:631" ];</code>  and <code>allowFrom = [ "all" ];</code> will enable anonymous access to your printer on all interfaces, you might want to restrict this.
<syntaxhighlight lang="nix">
services.avahi = {
  enable = true;
  nssmdns = true;
  openFirewall = true;
  publish = {
    enable = true;
    userServices = true;
  };
};
services.printing = {
  listenAddresses = [ "*:631" ];
  allowFrom = [ "all" ];
  browsing = true;
  defaultShared = true;
};
networking.firewall = {
  allowedTCPPorts = [ 631 ];
  allowedUDPPorts = [ 631 ];
};
</syntaxhighlight>
Once printer sharing is enabled, it could be additionally advertised in the home network via the Samba protocol, [[Samba#Printer_sharing|see]].


== Troubleshooting ==
== Troubleshooting ==