@@ -124,6 +124,16 @@ in
124124 '';
125125 };
126126
127+ startWhenNeeded = mkOption {
128+ type = types.bool;
129+ default = false;
130+ description = ''
131+ If set, CUPS is socket-activated; that is,
132+ instead of having it permanently running as a daemon,
133+ systemd will start it on the first incoming connection.
134+ '';
135+ };
136+
127137 listenAddresses = mkOption {
128138 type = types.listOf types.str;
129139 default = [ "localhost:631" ];
@@ -287,8 +297,13 @@ in
287297
288298 systemd.packages = [ cups.out ];
289299
300+ systemd.sockets.cups = mkIf cfg.startWhenNeeded {
301+ wantedBy = [ "sockets.target" ];
302+ listenStreams = map (x: replaceStrings ["localhost"] ["127.0.0.1"] (removePrefix "*:" x)) cfg.listenAddresses;
303+ };
304+
290305 systemd.services.cups =
291− { wantedBy = [ "multi-user.target" ];
306+ { wantedBy = optionals (!cfg.startWhenNeeded) [ "multi-user.target" ];
292307 wants = [ "network.target" ];
293308 after = [ "network.target" ];
294309