2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.octoprint;
|
|
|
|
|
|
|
|
baseConfig = {
|
|
|
|
plugins.curalegacy.cura_engine = "${pkgs.curaengine_stable}/bin/CuraEngine";
|
|
|
|
server.host = cfg.host;
|
|
|
|
server.port = cfg.port;
|
|
|
|
webcam.ffmpeg = "${pkgs.ffmpeg.bin}/bin/ffmpeg";
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
fullConfig = lib.recursiveUpdate cfg.extraConfig baseConfig;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
cfgUpdate = pkgs.writeText "octoprint-config.yaml" (builtins.toJSON fullConfig);
|
|
|
|
|
2022-12-02 08:20:57 +00:00
|
|
|
pluginsEnv = package.python.withPackages (ps: [ ps.octoprint ] ++ (cfg.plugins ps));
|
2020-05-03 17:38:23 +00:00
|
|
|
|
|
|
|
package = pkgs.octoprint;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
{
|
|
|
|
##### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.octoprint = {
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
enable = lib.mkEnableOption "OctoPrint, web interface for 3D printers";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
host = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "0.0.0.0";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Host to bind OctoPrint to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
port = lib.mkOption {
|
|
|
|
type = lib.types.port;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = 5000;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Port to bind OctoPrint to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
openFirewall = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2022-12-02 08:20:57 +00:00
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Open ports in the firewall for OctoPrint.";
|
2022-12-02 08:20:57 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
user = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "octoprint";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "User for the daemon.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
group = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "octoprint";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Group for the daemon.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
stateDir = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "/var/lib/octoprint";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "State directory of the daemon.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
plugins = lib.mkOption {
|
|
|
|
type = lib.types.functionTo (lib.types.listOf lib.types.package);
|
2022-12-02 08:20:57 +00:00
|
|
|
default = plugins: [ ];
|
2024-09-19 14:19:46 +00:00
|
|
|
defaultText = lib.literalExpression "plugins: []";
|
|
|
|
example = lib.literalExpression "plugins: with plugins; [ themeify stlviewer ]";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Additional plugins to be used. Available plugins are passed through the plugins input.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
extraConfig = lib.mkOption {
|
|
|
|
type = lib.types.attrs;
|
2022-12-02 08:20:57 +00:00
|
|
|
default = { };
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Extra options which are added to OctoPrint's YAML configuration file.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
##### implementation
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
users.users = lib.optionalAttrs (cfg.user == "octoprint") {
|
2020-04-24 23:36:52 +00:00
|
|
|
octoprint = {
|
|
|
|
group = cfg.group;
|
|
|
|
uid = config.ids.uids.octoprint;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
users.groups = lib.optionalAttrs (cfg.group == "octoprint") {
|
2020-04-24 23:36:52 +00:00
|
|
|
octoprint.gid = config.ids.gids.octoprint;
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.stateDir}' - ${cfg.user} ${cfg.group} - -"
|
2023-02-02 18:25:31 +00:00
|
|
|
# this will allow octoprint access to raspberry specific hardware to check for throttling
|
|
|
|
# read-only will not work: "VCHI initialization failed" error
|
|
|
|
"a /dev/vchiq - - - - u:octoprint:rw"
|
2020-04-24 23:36:52 +00: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-05-03 17:38:23 +00:00
|
|
|
ExecStart = "${pluginsEnv}/bin/octoprint serve -b ${cfg.stateDir}";
|
2020-04-24 23:36:52 +00:00
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
2021-08-27 14:25:00 +00:00
|
|
|
SupplementaryGroups = [
|
|
|
|
"dialout"
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.port ];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
}
|