nixpkgs/nixos/modules/services/misc/octoprint.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

139 lines
3.8 KiB
Nix
Raw Normal View History

2016-02-14 11:58:09 +00:00
{ config, lib, pkgs, ... }:
let
cfg = config.services.octoprint;
2016-03-12 14:52:09 +00:00
baseConfig = {
2019-05-30 13:28:25 +00:00
plugins.curalegacy.cura_engine = "${pkgs.curaengine_stable}/bin/CuraEngine";
2016-02-14 11:58:09 +00:00
server.port = cfg.port;
webcam.ffmpeg = "${pkgs.ffmpeg.bin}/bin/ffmpeg";
2024-10-27 17:58:02 +00:00
} // lib.optionalAttrs (cfg.host != null) {server.host = cfg.host;};
2016-03-12 14:52:09 +00:00
fullConfig = lib.recursiveUpdate cfg.extraConfig baseConfig;
2016-03-12 14:52:09 +00:00
cfgUpdate = pkgs.writeText "octoprint-config.yaml" (builtins.toJSON fullConfig);
2016-02-14 11:58:09 +00:00
pluginsEnv = package.python.withPackages (ps: [ ps.octoprint ] ++ (cfg.plugins ps));
package = pkgs.octoprint;
2016-02-14 11:58:09 +00:00
in
{
##### interface
options = {
services.octoprint = {
enable = lib.mkEnableOption "OctoPrint, web interface for 3D printers";
2016-02-14 11:58:09 +00:00
host = lib.mkOption {
2024-10-27 17:58:02 +00:00
type = lib.types.nullOr lib.types.str;
default = null;
2016-02-14 11:58:09 +00:00
description = ''
Host to bind OctoPrint to.
'';
};
port = lib.mkOption {
type = lib.types.port;
2016-02-14 11:58:09 +00:00
default = 5000;
description = ''
Port to bind OctoPrint to.
'';
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Open ports in the firewall for OctoPrint.";
};
user = lib.mkOption {
type = lib.types.str;
2016-02-14 11:58:09 +00:00
default = "octoprint";
description = "User for the daemon.";
};
group = lib.mkOption {
type = lib.types.str;
2016-02-14 11:58:09 +00:00
default = "octoprint";
description = "Group for the daemon.";
};
stateDir = lib.mkOption {
type = lib.types.path;
2016-02-14 11:58:09 +00:00
default = "/var/lib/octoprint";
description = "State directory of the daemon.";
};
plugins = lib.mkOption {
type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = plugins: [ ];
defaultText = lib.literalExpression "plugins: []";
example = lib.literalExpression "plugins: with plugins; [ themeify stlviewer ]";
2020-08-24 20:33:34 +00:00
description = "Additional plugins to be used. Available plugins are passed through the plugins input.";
2016-02-14 11:58:09 +00:00
};
extraConfig = lib.mkOption {
type = lib.types.attrs;
default = { };
2016-03-12 14:52:09 +00:00
description = "Extra options which are added to OctoPrint's YAML configuration file.";
};
2016-02-14 11:58:09 +00:00
};
};
##### implementation
config = lib.mkIf cfg.enable {
2016-02-14 11:58:09 +00:00
users.users = lib.optionalAttrs (cfg.user == "octoprint") {
octoprint = {
2016-02-14 11:58:09 +00:00
group = cfg.group;
uid = config.ids.uids.octoprint;
};
};
2016-02-14 11:58:09 +00:00
users.groups = lib.optionalAttrs (cfg.group == "octoprint") {
octoprint.gid = config.ids.gids.octoprint;
};
2016-02-14 11:58:09 +00:00
systemd.tmpfiles.rules = [
"d '${cfg.stateDir}' - ${cfg.user} ${cfg.group} - -"
# 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"
];
2016-02-14 11:58:09 +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 = {
ExecStart = "${pluginsEnv}/bin/octoprint serve -b ${cfg.stateDir}";
2016-02-14 11:58:09 +00:00
User = cfg.user;
Group = cfg.group;
SupplementaryGroups = [
"dialout"
];
2016-02-14 11:58:09 +00:00
};
};
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.port ];
2016-02-14 11:58:09 +00:00
};
}