2020-10-16 20:44:37 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.klipper;
|
2021-07-24 12:14:16 +00:00
|
|
|
format = pkgs.formats.ini {
|
|
|
|
# https://github.com/NixOS/nixpkgs/pull/121613#issuecomment-885241996
|
|
|
|
listToValue = l:
|
2022-08-12 12:06:08 +00:00
|
|
|
if builtins.length l == 1 then generators.mkValueStringDefault { } (head l)
|
2021-07-24 12:14:16 +00:00
|
|
|
else lib.concatMapStrings (s: "\n ${generators.mkValueStringDefault {} s}") l;
|
2022-08-12 12:06:08 +00:00
|
|
|
mkKeyValue = generators.mkKeyValueDefault { } ":";
|
2021-07-24 12:14:16 +00:00
|
|
|
};
|
2020-10-16 20:44:37 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
##### interface
|
|
|
|
options = {
|
|
|
|
services.klipper = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "Klipper, the 3D printer firmware");
|
2020-10-16 20:44:37 +00:00
|
|
|
|
2021-07-21 07:28:18 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.klipper;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "pkgs.klipper";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The Klipper package.";
|
2021-07-21 07:28:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inputTTY = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/run/klipper/tty";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Path of the virtual printer symlink to create.";
|
2021-07-21 07:28:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
apiSocket = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
2021-08-05 21:33:18 +00:00
|
|
|
default = "/run/klipper/api";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Path of the API socket to create.";
|
2021-07-21 07:28:18 +00:00
|
|
|
};
|
|
|
|
|
2022-09-30 11:47:45 +00:00
|
|
|
mutableConfig = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
example = true;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to copy the config to a mutable directory instead of using the one directly from the nix store.
|
|
|
|
This will only copy the config if the file at `services.klipper.mutableConfigPath` doesn't exist.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mutableConfigFolder = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/klipper";
|
|
|
|
description = lib.mdDoc "Path to mutable Klipper config file.";
|
|
|
|
};
|
|
|
|
|
|
|
|
configFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Path to default Klipper config.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-10-16 20:44:37 +00:00
|
|
|
octoprintIntegration = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Allows Octoprint to control Klipper.";
|
2020-10-16 20:44:37 +00:00
|
|
|
};
|
|
|
|
|
2021-07-21 07:28:18 +00:00
|
|
|
user = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-21 07:28:18 +00:00
|
|
|
User account under which Klipper runs.
|
|
|
|
|
|
|
|
If null is specified (default), a temporary user will be created by systemd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-21 07:28:18 +00:00
|
|
|
Group account under which Klipper runs.
|
|
|
|
|
|
|
|
If null is specified (default), a temporary user will be created by systemd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-10-16 20:44:37 +00:00
|
|
|
settings = mkOption {
|
2022-09-30 11:47:45 +00:00
|
|
|
type = types.nullOr format.type;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Configuration for Klipper. See the [documentation](https://www.klipper3d.org/Overview.html#configuration-and-tuning-guides)
|
2020-10-16 20:44:37 +00:00
|
|
|
for supported values.
|
|
|
|
'';
|
|
|
|
};
|
2022-08-12 12:06:08 +00:00
|
|
|
|
|
|
|
firmwares = mkOption {
|
|
|
|
description = lib.mdDoc "Firmwares klipper should manage";
|
|
|
|
default = { };
|
|
|
|
type = with types; attrsOf
|
|
|
|
(submodule {
|
|
|
|
options = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc ''
|
2022-08-12 12:06:08 +00:00
|
|
|
building of firmware and addition of klipper-flash tools for manual flashing.
|
|
|
|
This will add `klipper-flash-$mcu` scripts to your environment which can be called to flash the firmware.
|
2022-09-09 14:08:57 +00:00
|
|
|
'');
|
2022-09-30 11:47:45 +00:00
|
|
|
serial = mkOption {
|
|
|
|
type = types.nullOr path;
|
|
|
|
description = lib.mdDoc "Path to serial port this printer is connected to. Leave `null` to derive it from `service.klipper.settings`.";
|
|
|
|
};
|
2022-08-12 12:06:08 +00:00
|
|
|
configFile = mkOption {
|
|
|
|
type = path;
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc "Path to firmware config which is generated using `klipper-genconf`";
|
2022-08-12 12:06:08 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2020-10-16 20:44:37 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
##### implementation
|
|
|
|
config = mkIf cfg.enable {
|
2021-07-21 07:28:18 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.octoprintIntegration -> config.services.octoprint.enable;
|
2022-09-30 11:47:45 +00:00
|
|
|
message = "Option services.klipper.octoprintIntegration requires Octoprint to be enabled on this system. Please enable services.octoprint to use it.";
|
2021-07-21 07:28:18 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = cfg.user != null -> cfg.group != null;
|
2022-09-30 11:47:45 +00:00
|
|
|
message = "Option services.klipper.group is not set when services.klipper.user is specified.";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = cfg.settings != null -> foldl (a: b: a && b) true (mapAttrsToList (mcu: _: mcu != null -> (hasAttrByPath [ "${mcu}" "serial" ] cfg.settings)) cfg.firmwares);
|
|
|
|
message = "Option services.klipper.settings.$mcu.serial must be set when settings.klipper.firmware.$mcu is specified";
|
2021-07-21 07:28:18 +00:00
|
|
|
}
|
2022-08-12 12:06:08 +00:00
|
|
|
{
|
2022-09-30 11:47:45 +00:00
|
|
|
assertion = (cfg.configFile != null) != (cfg.settings != null);
|
2023-03-04 12:14:45 +00:00
|
|
|
message = "You need to either specify services.klipper.settings or services.klipper.configFile.";
|
2022-08-12 12:06:08 +00:00
|
|
|
}
|
2021-07-21 07:28:18 +00:00
|
|
|
];
|
2020-10-16 20:44:37 +00:00
|
|
|
|
2022-09-30 11:47:45 +00:00
|
|
|
environment.etc = mkIf (!cfg.mutableConfig) {
|
|
|
|
"klipper.cfg".source = if cfg.settings != null then format.generate "klipper.cfg" cfg.settings else cfg.configFile;
|
|
|
|
};
|
2020-10-16 20:44:37 +00:00
|
|
|
|
2021-07-21 07:28:18 +00:00
|
|
|
services.klipper = mkIf cfg.octoprintIntegration {
|
|
|
|
user = config.services.octoprint.user;
|
|
|
|
group = config.services.octoprint.group;
|
|
|
|
};
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
systemd.services.klipper =
|
|
|
|
let
|
|
|
|
klippyArgs = "--input-tty=${cfg.inputTTY}"
|
|
|
|
+ optionalString (cfg.apiSocket != null) " --api-server=${cfg.apiSocket}";
|
2022-09-30 11:47:45 +00:00
|
|
|
printerConfigPath =
|
|
|
|
if cfg.mutableConfig
|
|
|
|
then cfg.mutableConfigFolder + "/printer.cfg"
|
|
|
|
else "/etc/klipper.cfg";
|
|
|
|
printerConfigFile =
|
|
|
|
if cfg.settings != null
|
|
|
|
then format.generate "klipper.cfg" cfg.settings
|
|
|
|
else cfg.configFile;
|
2022-08-12 12:06:08 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
description = "Klipper 3D Printer Firmware";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
2022-09-30 11:47:45 +00:00
|
|
|
preStart = ''
|
|
|
|
mkdir -p ${cfg.mutableConfigFolder}
|
|
|
|
${lib.optionalString (cfg.mutableConfig) ''
|
|
|
|
[ -e ${printerConfigPath} ] || {
|
|
|
|
cp ${printerConfigFile} ${printerConfigPath}
|
|
|
|
chmod +w ${printerConfigPath}
|
|
|
|
}
|
|
|
|
''}
|
|
|
|
mkdir -p ${cfg.mutableConfigFolder}/gcodes
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
|
|
|
|
serviceConfig = {
|
2022-09-30 11:47:45 +00:00
|
|
|
ExecStart = "${cfg.package}/lib/klipper/klippy.py ${klippyArgs} ${printerConfigPath}";
|
2022-08-12 12:06:08 +00:00
|
|
|
RuntimeDirectory = "klipper";
|
2022-09-30 11:47:45 +00:00
|
|
|
StateDirectory = "klipper";
|
2022-08-12 12:06:08 +00:00
|
|
|
SupplementaryGroups = [ "dialout" ];
|
|
|
|
WorkingDirectory = "${cfg.package}/lib";
|
|
|
|
OOMScoreAdjust = "-999";
|
|
|
|
CPUSchedulingPolicy = "rr";
|
|
|
|
CPUSchedulingPriority = 99;
|
|
|
|
IOSchedulingClass = "realtime";
|
|
|
|
IOSchedulingPriority = 0;
|
2022-09-30 11:47:45 +00:00
|
|
|
UMask = "0002";
|
2022-08-12 12:06:08 +00:00
|
|
|
} // (if cfg.user != null then {
|
|
|
|
Group = cfg.group;
|
|
|
|
User = cfg.user;
|
|
|
|
} else {
|
|
|
|
DynamicUser = true;
|
|
|
|
User = "klipper";
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.systemPackages =
|
|
|
|
with pkgs;
|
|
|
|
let
|
2022-09-30 11:47:45 +00:00
|
|
|
default = a: b: if a != null then a else b;
|
2022-08-12 12:06:08 +00:00
|
|
|
firmwares = filterAttrs (n: v: v!= null) (mapAttrs
|
2022-09-30 11:47:45 +00:00
|
|
|
(mcu: { enable, configFile, serial }: if enable then pkgs.klipper-firmware.override {
|
2022-08-12 12:06:08 +00:00
|
|
|
mcu = lib.strings.sanitizeDerivationName mcu;
|
|
|
|
firmwareConfig = configFile;
|
|
|
|
} else null)
|
|
|
|
cfg.firmwares);
|
|
|
|
firmwareFlasher = mapAttrsToList
|
|
|
|
(mcu: firmware: pkgs.klipper-flash.override {
|
|
|
|
mcu = lib.strings.sanitizeDerivationName mcu;
|
|
|
|
klipper-firmware = firmware;
|
2022-09-30 11:47:45 +00:00
|
|
|
flashDevice = default cfg.firmwares."${mcu}".serial cfg.settings."${mcu}".serial;
|
2022-08-12 12:06:08 +00:00
|
|
|
firmwareConfig = cfg.firmwares."${mcu}".configFile;
|
|
|
|
})
|
|
|
|
firmwares;
|
|
|
|
in
|
|
|
|
[ klipper-genconf ] ++ firmwareFlasher ++ attrValues firmwares;
|
2020-10-16 20:44:37 +00:00
|
|
|
};
|
2022-09-30 11:47:45 +00:00
|
|
|
meta.maintainers = [
|
|
|
|
maintainers.cab404
|
|
|
|
];
|
2020-10-16 20:44:37 +00:00
|
|
|
}
|