OctoPrint as a service?

Hi all,

I am able to successfully run OctoPrint on NixOS just executing it from the command line, and would like to run it as a service. I have tried services.octoprint.enable = true;, and this does indeed configure a systemd service, but it does not run, and I see an error of Permission denied when I run systemctl status octoprint.service. I have also tried configuring the service to run as my user, but that doesn’t seem to help either.

Does anyone have a working example of running OctoPrint as a service that I could take a gander at?

On 19.09, services.octoprint.enable = true; seems to start the service fine. Can you post the whole log output (journalctl -b -u octoprint) somewhere?

On 20.03 the package doesn’t even build but fixed version is on the way:

Thanks for the reply! I am on 19.09, and this is the output I’m seeing:

-- Logs begin at Mon 2019-07-22 17:26:02 -04, end at Fri 2020-03-27 09:10:59 -03. --
Mar 27 09:09:25 zorrito systemd[1]: Starting OctoPrint, web interface for 3D printers...
Mar 27 09:09:26 zorrito ivavjgyvy5vwv4dizlq76x7gv2jk6jpi-unit-script-octoprint-pre-start[768]: /nix/store/rd7pr4z48asvwjgd5yv40da88rh9ib6y-yaml-merge-2016-02-16/bin/.yaml-merge-wrapped:28: YAMLLoadWarning: callin>
Mar 27 09:09:26 zorrito ivavjgyvy5vwv4dizlq76x7gv2jk6jpi-unit-script-octoprint-pre-start[768]:   file1 = yaml.load(f)
Mar 27 09:09:26 zorrito ivavjgyvy5vwv4dizlq76x7gv2jk6jpi-unit-script-octoprint-pre-start[768]: /nix/store/rd7pr4z48asvwjgd5yv40da88rh9ib6y-yaml-merge-2016-02-16/bin/.yaml-merge-wrapped:32: YAMLLoadWarning: callin>
Mar 27 09:09:26 zorrito ivavjgyvy5vwv4dizlq76x7gv2jk6jpi-unit-script-octoprint-pre-start[768]:   file2 = yaml.load(f)
Mar 27 09:09:26 zorrito systemd[1]: Started OctoPrint, web interface for 3D printers.
Mar 27 09:09:26 zorrito octoprint[792]: 2020-03-27 09:09:26,762 - octoprint.startup - INFO - ******************************************************************************
Mar 27 09:09:26 zorrito octoprint[792]: 2020-03-27 09:09:26,763 - octoprint.startup - INFO - Starting OctoPrint 1.3.11
Mar 27 09:09:26 zorrito octoprint[792]: 2020-03-27 09:09:26,763 - octoprint.startup - INFO - Starting in SAFE MODE. Third party plugins will be disabled!
Mar 27 09:09:26 zorrito octoprint[792]: 2020-03-27 09:09:26,763 - octoprint.startup - INFO - Reason for safe mode: problem during last startup
Mar 27 09:09:26 zorrito octoprint[792]: 2020-03-27 09:09:26,764 - octoprint.startup - INFO - ******************************************************************************
Mar 27 09:09:27 zorrito octoprint[792]: 2020-03-27 09:09:27,067 - octoprint.plugin.core - INFO - Loading plugins from /nix/store/sgn67hal2czvk5rj8fkgmjmjcj97srn2-OctoPrint-1.3.11/lib/python2.7/site-packages/octop>
Mar 27 09:09:27 zorrito octoprint[792]: 2020-03-27 09:09:27,557 - octoprint.plugin.core - INFO - Plugin Pi Support Plugin did not pass check, not loading.
Mar 27 09:09:27 zorrito octoprint[792]: 2020-03-27 09:09:27,620 - octoprint.plugins.octoprint.plugins.discovery - INFO - pybonjour is not installed, Zeroconf Discovery won't be available
Mar 27 09:09:27 zorrito octoprint[792]: 2020-03-27 09:09:27,623 - octoprint.plugin.core - INFO - Found 14 plugin(s) providing 14 mixin implementations, 22 hook handlers
Mar 27 09:09:27 zorrito octoprint[792]: 2020-03-27 09:09:27,645 - octoprint.server.heartbeat - INFO - Starting server heartbeat, 900.0s interval
Mar 27 09:09:27 zorrito octoprint[792]: 2020-03-27 09:09:27,649 - octoprint.cli.server - ERROR - Uncaught exception
Mar 27 09:09:27 zorrito octoprint[792]: Traceback (most recent call last):
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/sgn67hal2czvk5rj8fkgmjmjcj97srn2-OctoPrint-1.3.11/bin/.octoprint-wrapped", line 11, in <module>
Mar 27 09:09:27 zorrito octoprint[792]:     sys.exit(main())
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/sgn67hal2czvk5rj8fkgmjmjcj97srn2-OctoPrint-1.3.11/lib/python2.7/site-packages/octoprint/__init__.py", line 609, in main
Mar 27 09:09:27 zorrito octoprint[792]:     octo(args=args, prog_name="octoprint", auto_envvar_prefix="OCTOPRINT")
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/gf5gllqj4aida85hdxy5adafj746bqn9-python2.7-click-7.0/lib/python2.7/site-packages/click/core.py", line 764, in __call__
Mar 27 09:09:27 zorrito octoprint[792]:     return self.main(*args, **kwargs)
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/gf5gllqj4aida85hdxy5adafj746bqn9-python2.7-click-7.0/lib/python2.7/site-packages/click/core.py", line 717, in main
Mar 27 09:09:27 zorrito octoprint[792]:     rv = self.invoke(ctx)
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/gf5gllqj4aida85hdxy5adafj746bqn9-python2.7-click-7.0/lib/python2.7/site-packages/click/core.py", line 1137, in invoke
Mar 27 09:09:27 zorrito octoprint[792]:     return _process_result(sub_ctx.command.invoke(sub_ctx))
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/gf5gllqj4aida85hdxy5adafj746bqn9-python2.7-click-7.0/lib/python2.7/site-packages/click/core.py", line 956, in invoke
Mar 27 09:09:27 zorrito octoprint[792]:     return ctx.invoke(self.callback, **ctx.params)
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/gf5gllqj4aida85hdxy5adafj746bqn9-python2.7-click-7.0/lib/python2.7/site-packages/click/core.py", line 555, in invoke
Mar 27 09:09:27 zorrito octoprint[792]:     return callback(*args, **kwargs)
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/gf5gllqj4aida85hdxy5adafj746bqn9-python2.7-click-7.0/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
Mar 27 09:09:27 zorrito octoprint[792]:     return f(get_current_context(), *args, **kwargs)
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/sgn67hal2czvk5rj8fkgmjmjcj97srn2-OctoPrint-1.3.11/lib/python2.7/site-packages/octoprint/cli/server.py", line 206, in serve_command
Mar 27 09:09:27 zorrito octoprint[792]:     ignore_blacklist)
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/sgn67hal2czvk5rj8fkgmjmjcj97srn2-OctoPrint-1.3.11/lib/python2.7/site-packages/octoprint/cli/server.py", line 118, in run_server
Mar 27 09:09:27 zorrito octoprint[792]:     octoprint_server.run()
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/sgn67hal2czvk5rj8fkgmjmjcj97srn2-OctoPrint-1.3.11/lib/python2.7/site-packages/octoprint/server/__init__.py", line 267, in run
Mar 27 09:09:27 zorrito octoprint[792]:     self._start_intermediary_server()
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/sgn67hal2czvk5rj8fkgmjmjcj97srn2-OctoPrint-1.3.11/lib/python2.7/site-packages/octoprint/server/__init__.py", line 1730, in _start_intermediary_server
Mar 27 09:09:27 zorrito octoprint[792]:     self._intermediary_server.server_bind()
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/8aljwxikgxmffjzc417mc089fsp1qnrm-python-2.7.17/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
Mar 27 09:09:27 zorrito octoprint[792]:     SocketServer.TCPServer.server_bind(self)
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/8aljwxikgxmffjzc417mc089fsp1qnrm-python-2.7.17/lib/python2.7/SocketServer.py", line 434, in server_bind
Mar 27 09:09:27 zorrito octoprint[792]:     self.socket.bind(self.server_address)
Mar 27 09:09:27 zorrito octoprint[792]:   File "/nix/store/8aljwxikgxmffjzc417mc089fsp1qnrm-python-2.7.17/lib/python2.7/socket.py", line 228, in meth
Mar 27 09:09:27 zorrito octoprint[792]:     return getattr(self._sock,name)(*args)
Mar 27 09:09:27 zorrito octoprint[792]: error: [Errno 13] Permission denied
Mar 27 09:09:27 zorrito systemd[1]: octoprint.service: Main process exited, code=exited, status=1/FAILURE
Mar 27 09:09:27 zorrito systemd[1]: octoprint.service: Failed with result 'exit-code'.
Mar 27 09:09:27 zorrito systemd[1]: octoprint.service: Consumed 1.204s CPU time, no IP traffic.

My config looks like the below, and I have added my user (ertw) to the octoprint group. I’m not sure if that’s the correct way to do it? Just running the service with its default user and group also gave me a permissions issue.

octoprint = {
      enable = true;
      port = 80;
      user = "ertw";
      stateDir = "/home/ertw/octoprint";
    };

The port is the reason for Permission denied - traditionally, non-root users cannot listen on ports lower than 1024. You can either change the port or add systemd.services.octoprint.serviceConfig.AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]; to your config.

Unless you need to work with files in stateDir directly it’s probably better to leave stateDir and user on their default value. That way, if the service is buggy and is compromised it won’t be able to give access to your personal files.

Thanks, I will try this as soon as my current print finishes.

Changing the port back to 5000 did indeed allow the service to auto-start. Thanks!