From 47d9e7d3d7d8ad19e29be1445171726bf7d602b6 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Mon, 28 Nov 2022 14:50:41 +0100 Subject: [PATCH] nixos/hardware/printers: stop cupsd when unneeded If socket activation is enable (the default) and printers are configured declaratively, the ensure-printers service will always start cupsd and leave it running, thus defeating the point of socket activation. With this change ensure-printers continues to start the cups.service at boot, but automatically stops it afterwards if socket activation is enabled. Note: Later restarts of ensure-printers will also restart cupsd, but it's not an issue since it will be reactivate, if necessary. --- nixos/modules/hardware/printers.nix | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/nixos/modules/hardware/printers.nix b/nixos/modules/hardware/printers.nix index 64c29bb0a5b3..a8211b8c17cd 100644 --- a/nixos/modules/hardware/printers.nix +++ b/nixos/modules/hardware/printers.nix @@ -110,21 +110,26 @@ in { }; config = mkIf (cfg.ensurePrinters != [] && config.services.printing.enable) { - systemd.services.ensure-printers = let - cupsUnit = if config.services.printing.startWhenNeeded then "cups.socket" else "cups.service"; - in { + systemd.services.ensure-printers = { description = "Ensure NixOS-configured CUPS printers"; wantedBy = [ "multi-user.target" ]; - requires = [ cupsUnit ]; - after = [ cupsUnit ]; + wants = [ "cups.service" ]; + after = [ "cups.service" ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; - script = concatMapStringsSep "\n" ensurePrinter cfg.ensurePrinters - + optionalString (cfg.ensureDefaultPrinter != null) (ensureDefaultPrinter cfg.ensureDefaultPrinter); + script = concatStringsSep "\n" [ + (concatMapStrings ensurePrinter cfg.ensurePrinters) + (optionalString (cfg.ensureDefaultPrinter != null) + (ensureDefaultPrinter cfg.ensureDefaultPrinter)) + # Note: if cupsd is "stateless" the service can't be stopped, + # otherwise the configuration will be wiped on the next start. + (optionalString (with config.services.printing; startWhenNeeded && !stateless) + "systemctl stop cups.service") + ]; }; }; }