summaryrefslogtreecommitdiff
path: root/hosts/gnuslashprinter/klipper.nix
blob: 331a0c74ab646712091fb533753599d57c786110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{
	# Klipper firmware
	services.klipper = {
		enable = true;
		firmwares = {
			mcu = {
				enable = true;
				# Serial port connected to the microcontroller
				serial = "/dev/gsp-control";
				# Klipper flash must be enabled in order to build mcu firmware
				# The resulting `klipper-flash-mcu` command will show the location of the firmware bin in the nix store
				enableKlipperFlash = true;
				# Run klipper-genconf to generate this
				configFile = ./resources/klipper/mcu/config;
			};
		};
		configFile = ./resources/klipper/printer.cfg;
		logFile = "/var/lib/klipper/klipper.log";
	};
	# Mutable config
	services.klipper.mutableConfig = true;
	#configDir = "/var/lib/moonraker/config";  # Accessible by moonraker  # TODO

	# Moonraker web-api
	security.polkit.enable = true;  # required for services.moonraker.allowSystemControl
	services.moonraker = {
		user = "root";
		enable = true;
		address = "0.0.0.0";
		allowSystemControl = true;
		settings = {
			authorization = {
				force_logins = true;
				cors_domains = [
					"*.local"
					"*.lan"
					"*://app.fluidd.xyz"
					"*://my.mainsail.xyz"
				];
				trusted_clients = [
					"10.0.0.0/8"
					"127.0.0.0/8"
					"169.254.0.0/16"
					"172.16.0.0/12"
					"192.168.0.0/16"
					"FE80::/10"
					"::1/128"
				];
			};
			#file_manager.check_klipper_config_path = false;  # Disable warning when klipper config is not accessible by moonraker
			# mainsail.cfg
			#"update_manager mainsail-config" = {
			#	type = "git_repo";
			#	primary_branch = "master";
			#	path = "~/mainsail-config";
			#	origin = "https://github.com/mainsail-crew/mainsail-config.git";
			#	managed_services = "klipper";
			#};

			"power printer" = let
				relayApiHost = "http://localhost:5050";
			in {
				type = "http";
				on_url = "${relayApiHost}/on";
				off_url = "${relayApiHost}/off";
				status_url = "${relayApiHost}/status";
				response_template = ''

					# The module will perform the "GET" request using the appropriate url.
					# We use the `last_response` method to fetch the result and decode the
					# json response.
					{% set resp = http_request.last_response().json() %}
					# The expression below will render "on" or "off".
					{resp["state"].lower()}
				'';
				off_when_shutdown = true;
				on_when_job_queued = true;
				locked_while_printing = true;
				restart_klipper_when_powered = true;
				restart_delay = "1";
				bound_services = "klipper";
				initial_state = "off";
			};
		};
	};

	# Mainsail web-interface
	services.mainsail = {
		enable = true;
		hostName = "0.0.0.0";
		nginx.listenAddresses = [ "0.0.0.0" ];
	};
	services.nginx.clientMaxBodySize = "1000m";  # Allow large gcodes, etc.
	networking.firewall.allowedTCPPorts = [ 80 ];  # Port for mainsail via nginx

	# Webcam support in mainsail
	# TODO hook to restart ustreamer when webcam is connected
	services.ustreamer.enable = true;
	services.mainsail.nginx.locations."/webcam/".proxyPass = "http://localhost:8080/stream";  # Default location for ustreamer stream
}