| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cfg = config.services.octoprint; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-12 17:52:09 +03:00
										 |  |  |   baseConfig = { | 
					
						
							| 
									
										
										
										
											2019-05-30 15:28:25 +02:00
										 |  |  |     plugins.curalegacy.cura_engine = "${pkgs.curaengine_stable}/bin/CuraEngine"; | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  |     server.host = cfg.host; | 
					
						
							|  |  |  |     server.port = cfg.port; | 
					
						
							| 
									
										
										
										
											2016-04-10 22:50:31 +03:00
										 |  |  |     webcam.ffmpeg = "${pkgs.ffmpeg.bin}/bin/ffmpeg"; | 
					
						
							| 
									
										
										
										
											2016-03-12 17:52:09 +03:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fullConfig = recursiveUpdate cfg.extraConfig baseConfig; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cfgUpdate = pkgs.writeText "octoprint-config.yaml" (builtins.toJSON fullConfig); | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 11:02:07 +01:00
										 |  |  |   pluginsEnv = package.python.withPackages (ps: [ps.octoprint] ++ (cfg.plugins ps)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   package = pkgs.octoprint; | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | in | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   ##### interface | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   options = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     services.octoprint = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       enable = mkEnableOption "OctoPrint, web interface for 3D printers"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       host = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         default = "0.0.0.0"; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           Host to bind OctoPrint to. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       port = mkOption { | 
					
						
							|  |  |  |         type = types.int; | 
					
						
							|  |  |  |         default = 5000; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           Port to bind OctoPrint to. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       user = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         default = "octoprint"; | 
					
						
							|  |  |  |         description = "User for the daemon."; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       group = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         default = "octoprint"; | 
					
						
							|  |  |  |         description = "Group for the daemon."; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       stateDir = mkOption { | 
					
						
							|  |  |  |         type = types.path; | 
					
						
							|  |  |  |         default = "/var/lib/octoprint"; | 
					
						
							|  |  |  |         description = "State directory of the daemon."; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       plugins = mkOption { | 
					
						
							| 
									
										
										
										
											2018-08-07 18:53:56 +02:00
										 |  |  |         type = types.functionTo (types.listOf types.package); | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  |         default = plugins: []; | 
					
						
							| 
									
										
										
										
											2016-02-29 01:47:12 +01:00
										 |  |  |         defaultText = "plugins: []"; | 
					
						
							| 
									
										
										
										
											2020-11-06 00:39:50 -08:00
										 |  |  |         example = literalExample "plugins: with plugins; [ themeify stlviewer ]"; | 
					
						
							| 
									
										
										
										
											2020-08-24 13:33:34 -07:00
										 |  |  |         description = "Additional plugins to be used. Available plugins are passed through the plugins input."; | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-12 17:52:09 +03:00
										 |  |  |       extraConfig = mkOption { | 
					
						
							|  |  |  |         type = types.attrs; | 
					
						
							|  |  |  |         default = {}; | 
					
						
							|  |  |  |         description = "Extra options which are added to OctoPrint's YAML configuration file."; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ##### implementation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   config = mkIf cfg.enable { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 19:51:29 +02:00
										 |  |  |     users.users = optionalAttrs (cfg.user == "octoprint") { | 
					
						
							|  |  |  |       octoprint = { | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  |         group = cfg.group; | 
					
						
							|  |  |  |         uid = config.ids.uids.octoprint; | 
					
						
							| 
									
										
										
										
											2019-09-14 19:51:29 +02:00
										 |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 19:51:29 +02:00
										 |  |  |     users.groups = optionalAttrs (cfg.group == "octoprint") { | 
					
						
							|  |  |  |       octoprint.gid = config.ids.gids.octoprint; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-24 09:08:21 -05:00
										 |  |  |     systemd.tmpfiles.rules = [ | 
					
						
							|  |  |  |       "d '${cfg.stateDir}' - ${cfg.user} ${cfg.group} - -" | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  |     systemd.services.octoprint = { | 
					
						
							|  |  |  |       description = "OctoPrint, web interface for 3D printers"; | 
					
						
							|  |  |  |       wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  |       after = [ "network.target" ]; | 
					
						
							|  |  |  |       path = [ pluginsEnv ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       preStart = ''
 | 
					
						
							|  |  |  |         if [ -e "${cfg.stateDir}/config.yaml" ]; then | 
					
						
							|  |  |  |           ${pkgs.yaml-merge}/bin/yaml-merge "${cfg.stateDir}/config.yaml" "${cfgUpdate}" > "${cfg.stateDir}/config.yaml.tmp" | 
					
						
							|  |  |  |           mv "${cfg.stateDir}/config.yaml.tmp" "${cfg.stateDir}/config.yaml" | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           cp "${cfgUpdate}" "${cfg.stateDir}/config.yaml" | 
					
						
							|  |  |  |           chmod 600 "${cfg.stateDir}/config.yaml" | 
					
						
							|  |  |  |         fi | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       serviceConfig = { | 
					
						
							| 
									
										
										
										
											2020-03-14 11:02:07 +01:00
										 |  |  |         ExecStart = "${pluginsEnv}/bin/octoprint serve -b ${cfg.stateDir}"; | 
					
						
							| 
									
										
										
										
											2016-02-14 14:58:09 +03:00
										 |  |  |         User = cfg.user; | 
					
						
							|  |  |  |         Group = cfg.group; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |