2021-12-05 21:45:35 +01:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2021-07-21 04:21:03 +02:00
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.moonraker;
|
2023-06-12 20:05:18 +02:00
|
|
|
pkg = cfg.package;
|
2021-12-05 21:45:35 +01:00
|
|
|
opt = options.services.moonraker;
|
2021-07-21 04:21:03 +02:00
|
|
|
format = pkgs.formats.ini {
|
|
|
|
# https://github.com/NixOS/nixpkgs/pull/121613#issuecomment-885241996
|
|
|
|
listToValue = l:
|
|
|
|
if builtins.length l == 1 then generators.mkValueStringDefault {} (head l)
|
|
|
|
else lib.concatMapStrings (s: "\n ${generators.mkValueStringDefault {} s}") l;
|
|
|
|
mkKeyValue = generators.mkKeyValueDefault {} ":";
|
|
|
|
};
|
2022-11-19 21:37:01 +01:00
|
|
|
|
|
|
|
unifiedConfigDir = cfg.stateDir + "/config";
|
2021-07-21 04:21:03 +02:00
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
services.moonraker = {
|
|
|
|
enable = mkEnableOption (lib.mdDoc "Moonraker, an API web server for Klipper");
|
|
|
|
|
2023-06-12 20:05:18 +02:00
|
|
|
package = mkOption {
|
|
|
|
type = with types; nullOr package;
|
|
|
|
default = pkgs.moonraker;
|
|
|
|
defaultText = literalExpression "pkgs.moonraker";
|
|
|
|
example = literalExpression "pkgs.moonraker.override { useGpiod = true; }";
|
|
|
|
description = lib.mdDoc "Moonraker package to use";
|
|
|
|
};
|
|
|
|
|
2021-07-21 04:21:03 +02:00
|
|
|
klipperSocket = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = config.services.klipper.apiSocket;
|
|
|
|
defaultText = literalExpression "config.services.klipper.apiSocket";
|
|
|
|
description = lib.mdDoc "Path to Klipper's API socket.";
|
|
|
|
};
|
|
|
|
|
|
|
|
stateDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/moonraker";
|
|
|
|
description = lib.mdDoc "The directory containing the Moonraker databases.";
|
|
|
|
};
|
|
|
|
|
|
|
|
configDir = mkOption {
|
2022-11-19 21:37:01 +01:00
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2021-07-21 04:21:03 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-11-19 21:37:01 +01:00
|
|
|
Deprecated directory containing client-writable configuration files.
|
2021-07-21 04:21:03 +02:00
|
|
|
|
|
|
|
Clients will be able to edit files in this directory via the API. This directory must be writable.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "moonraker";
|
|
|
|
description = lib.mdDoc "User account under which Moonraker runs.";
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "moonraker";
|
|
|
|
description = lib.mdDoc "Group account under which Moonraker runs.";
|
|
|
|
};
|
|
|
|
|
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
|
|
|
example = "0.0.0.0";
|
|
|
|
description = lib.mdDoc "The IP or host to listen on.";
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.ints.unsigned;
|
|
|
|
default = 7125;
|
|
|
|
description = lib.mdDoc "The port to listen on.";
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = format.type;
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
authorization = {
|
|
|
|
trusted_clients = [ "10.0.0.0/24" ];
|
2023-05-07 07:40:51 +02:00
|
|
|
cors_domains = [ "https://app.fluidd.xyz" "https://my.mainsail.xyz" ];
|
2021-07-21 04:21:03 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Configuration for Moonraker. See the [documentation](https://moonraker.readthedocs.io/en/latest/configuration/)
|
|
|
|
for supported values.
|
|
|
|
'';
|
|
|
|
};
|
2022-03-22 23:35:38 +01:00
|
|
|
|
|
|
|
allowSystemControl = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to allow Moonraker to perform system-level operations.
|
|
|
|
|
|
|
|
Moonraker exposes APIs to perform system-level operations, such as
|
|
|
|
reboot, shutdown, and management of systemd units. See the
|
|
|
|
[documentation](https://moonraker.readthedocs.io/en/latest/web_api/#machine-commands)
|
|
|
|
for details on what clients are able to do.
|
|
|
|
'';
|
|
|
|
};
|
2021-07-21 04:21:03 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2022-11-19 21:37:01 +01:00
|
|
|
warnings = []
|
|
|
|
++ optional (cfg.settings ? update_manager)
|
|
|
|
''Enabling update_manager is not supported on NixOS and will lead to non-removable warnings in some clients.''
|
|
|
|
++ optional (cfg.configDir != null)
|
|
|
|
''
|
|
|
|
services.moonraker.configDir has been deprecated upstream and will be removed.
|
|
|
|
|
|
|
|
Action: ${
|
|
|
|
if cfg.configDir == unifiedConfigDir then "Simply remove services.moonraker.configDir from your config."
|
|
|
|
else "Move files from `${cfg.configDir}` to `${unifiedConfigDir}` then remove services.moonraker.configDir from your config."
|
|
|
|
}
|
|
|
|
'';
|
2021-07-21 04:21:03 +02:00
|
|
|
|
2022-03-22 23:35:38 +01:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.allowSystemControl -> config.security.polkit.enable;
|
|
|
|
message = "services.moonraker.allowSystemControl requires polkit to be enabled (security.polkit.enable).";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-07-21 04:21:03 +02:00
|
|
|
users.users = optionalAttrs (cfg.user == "moonraker") {
|
|
|
|
moonraker = {
|
|
|
|
group = cfg.group;
|
|
|
|
uid = config.ids.uids.moonraker;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups = optionalAttrs (cfg.group == "moonraker") {
|
|
|
|
moonraker.gid = config.ids.gids.moonraker;
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.etc."moonraker.cfg".source = let
|
|
|
|
forcedConfig = {
|
|
|
|
server = {
|
|
|
|
host = cfg.address;
|
|
|
|
port = cfg.port;
|
|
|
|
klippy_uds_address = cfg.klipperSocket;
|
2022-09-23 00:27:27 +02:00
|
|
|
};
|
2022-11-19 21:37:01 +01:00
|
|
|
machine = {
|
|
|
|
validate_service = false;
|
|
|
|
};
|
2022-11-19 21:37:01 +01:00
|
|
|
} // (lib.optionalAttrs (cfg.configDir != null) {
|
|
|
|
file_manager = {
|
|
|
|
config_path = cfg.configDir;
|
|
|
|
};
|
|
|
|
});
|
2021-07-21 04:21:03 +02:00
|
|
|
fullConfig = recursiveUpdate cfg.settings forcedConfig;
|
|
|
|
in format.generate "moonraker.cfg" fullConfig;
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.stateDir}' - ${cfg.user} ${cfg.group} - -"
|
2022-11-19 21:37:01 +01:00
|
|
|
] ++ lib.optional (cfg.configDir != null) "d '${cfg.configDir}' - ${cfg.user} ${cfg.group} - -";
|
2021-07-21 04:21:03 +02:00
|
|
|
|
|
|
|
systemd.services.moonraker = {
|
|
|
|
description = "Moonraker, an API web server for Klipper";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ]
|
|
|
|
++ optional config.services.klipper.enable "klipper.service";
|
|
|
|
|
|
|
|
# Moonraker really wants its own config to be writable...
|
|
|
|
script = ''
|
2022-11-19 21:37:01 +01:00
|
|
|
config_path=${
|
|
|
|
# Deprecated separate config dir
|
|
|
|
if cfg.configDir != null then "${cfg.configDir}/moonraker-temp.cfg"
|
|
|
|
# Config in unified data path
|
|
|
|
else "${unifiedConfigDir}/moonraker-temp.cfg"
|
|
|
|
}
|
|
|
|
mkdir -p $(dirname "$config_path")
|
|
|
|
cp /etc/moonraker.cfg "$config_path"
|
|
|
|
chmod u+w "$config_path"
|
|
|
|
exec ${pkg}/bin/moonraker -d ${cfg.stateDir} -c "$config_path"
|
2021-07-21 04:21:03 +02:00
|
|
|
'';
|
|
|
|
|
2022-03-22 23:35:38 +01:00
|
|
|
# Needs `ip` command
|
|
|
|
path = [ pkgs.iproute2 ];
|
|
|
|
|
2021-07-21 04:21:03 +02:00
|
|
|
serviceConfig = {
|
|
|
|
WorkingDirectory = cfg.stateDir;
|
2022-09-23 00:27:27 +02:00
|
|
|
PrivateTmp = true;
|
2021-07-21 04:21:03 +02:00
|
|
|
Group = cfg.group;
|
|
|
|
User = cfg.user;
|
|
|
|
};
|
|
|
|
};
|
2022-03-22 23:35:38 +01:00
|
|
|
|
|
|
|
security.polkit.extraConfig = lib.optionalString cfg.allowSystemControl ''
|
|
|
|
// nixos/moonraker: Allow Moonraker to perform system-level operations
|
|
|
|
//
|
|
|
|
// This was enabled via services.moonraker.allowSystemControl.
|
|
|
|
polkit.addRule(function(action, subject) {
|
|
|
|
if ((action.id == "org.freedesktop.systemd1.manage-units" ||
|
|
|
|
action.id == "org.freedesktop.login1.power-off" ||
|
|
|
|
action.id == "org.freedesktop.login1.power-off-multiple-sessions" ||
|
|
|
|
action.id == "org.freedesktop.login1.reboot" ||
|
|
|
|
action.id == "org.freedesktop.login1.reboot-multiple-sessions" ||
|
|
|
|
action.id.startsWith("org.freedesktop.packagekit.")) &&
|
|
|
|
subject.user == "${cfg.user}") {
|
|
|
|
return polkit.Result.YES;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
'';
|
2021-07-21 04:21:03 +02:00
|
|
|
};
|
2022-09-23 00:27:27 +02:00
|
|
|
|
|
|
|
meta.maintainers = with maintainers; [
|
|
|
|
cab404
|
|
|
|
vtuan10
|
2022-11-19 21:37:01 +01:00
|
|
|
zhaofengli
|
2022-09-23 00:27:27 +02:00
|
|
|
];
|
2021-07-21 04:21:03 +02:00
|
|
|
}
|