1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-11-17 19:21:04 +00:00

nixos/services.klipper: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-24 22:05:46 +02:00
parent cac7b5e266
commit 14f18ffb06

View file

@ -1,25 +1,24 @@
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.klipper;
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 { } ":";
if builtins.length l == 1 then lib.generators.mkValueStringDefault { } (lib.head l)
else lib.concatMapStrings (s: "\n ${lib.generators.mkValueStringDefault {} s}") l;
mkKeyValue = lib.generators.mkKeyValueDefault { } ":";
};
in
{
##### interface
options = {
services.klipper = {
enable = mkEnableOption "Klipper, the 3D printer firmware";
enable = lib.mkEnableOption "Klipper, the 3D printer firmware";
package = mkPackageOption pkgs "klipper" { };
package = lib.mkPackageOption pkgs "klipper" { };
logFile = mkOption {
type = types.nullOr types.path;
logFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
example = "/var/lib/klipper/klipper.log";
description = ''
@ -28,20 +27,20 @@ in
'';
};
inputTTY = mkOption {
type = types.path;
inputTTY = lib.mkOption {
type = lib.types.path;
default = "/run/klipper/tty";
description = "Path of the virtual printer symlink to create.";
};
apiSocket = mkOption {
type = types.nullOr types.path;
apiSocket = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = "/run/klipper/api";
description = "Path of the API socket to create.";
};
mutableConfig = mkOption {
type = types.bool;
mutableConfig = lib.mkOption {
type = lib.types.bool;
default = false;
example = true;
description = ''
@ -50,28 +49,28 @@ in
'';
};
mutableConfigFolder = mkOption {
type = types.path;
mutableConfigFolder = lib.mkOption {
type = lib.types.path;
default = "/var/lib/klipper";
description = "Path to mutable Klipper config file.";
};
configFile = mkOption {
type = types.nullOr types.path;
configFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
Path to default Klipper config.
'';
};
octoprintIntegration = mkOption {
type = types.bool;
octoprintIntegration = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Allows Octoprint to control Klipper.";
};
user = mkOption {
type = types.nullOr types.str;
user = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
User account under which Klipper runs.
@ -80,8 +79,8 @@ in
'';
};
group = mkOption {
type = types.nullOr types.str;
group = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
Group account under which Klipper runs.
@ -90,8 +89,8 @@ in
'';
};
settings = mkOption {
type = types.nullOr format.type;
settings = lib.mkOption {
type = lib.types.nullOr format.type;
default = null;
description = ''
Configuration for Klipper. See the [documentation](https://www.klipper3d.org/Overview.html#configuration-and-tuning-guides)
@ -99,24 +98,24 @@ in
'';
};
firmwares = mkOption {
firmwares = lib.mkOption {
description = "Firmwares klipper should manage";
default = { };
type = with types; attrsOf
type = with lib.types; attrsOf
(submodule {
options = {
enable = mkEnableOption ''
enable = lib.mkEnableOption ''
building of firmware for manual flashing
'';
enableKlipperFlash = mkEnableOption ''
enableKlipperFlash = lib.mkEnableOption ''
flashings scripts for firmware. This will add `klipper-flash-$mcu` scripts to your environment which can be called to flash the firmware.
Please check the configs at [klipper](https://github.com/Klipper3d/klipper/tree/master/config) whether your board supports flashing via `make flash`
'';
serial = mkOption {
type = types.nullOr path;
serial = lib.mkOption {
type = lib.types.nullOr path;
description = "Path to serial port this printer is connected to. Leave `null` to derive it from `service.klipper.settings`.";
};
configFile = mkOption {
configFile = lib.mkOption {
type = path;
description = "Path to firmware config which is generated using `klipper-genconf`";
};
@ -127,7 +126,7 @@ in
};
##### implementation
config = mkIf cfg.enable {
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.octoprintIntegration -> config.services.octoprint.enable;
@ -138,7 +137,7 @@ in
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);
assertion = cfg.settings != null -> lib.foldl (a: b: a && b) true (lib.mapAttrsToList (mcu: _: mcu != null -> (lib.hasAttrByPath [ "${mcu}" "serial" ] cfg.settings)) cfg.firmwares);
message = "Option services.klipper.settings.$mcu.serial must be set when settings.klipper.firmware.$mcu is specified";
}
{
@ -147,11 +146,11 @@ in
}
];
environment.etc = mkIf (!cfg.mutableConfig) {
environment.etc = lib.mkIf (!cfg.mutableConfig) {
"klipper.cfg".source = if cfg.settings != null then format.generate "klipper.cfg" cfg.settings else cfg.configFile;
};
services.klipper = mkIf cfg.octoprintIntegration {
services.klipper = lib.mkIf cfg.octoprintIntegration {
user = config.services.octoprint.user;
group = config.services.octoprint.group;
};
@ -159,8 +158,8 @@ in
systemd.services.klipper =
let
klippyArgs = "--input-tty=${cfg.inputTTY}"
+ optionalString (cfg.apiSocket != null) " --api-server=${cfg.apiSocket}"
+ optionalString (cfg.logFile != null) " --logfile=${cfg.logFile}"
+ lib.optionalString (cfg.apiSocket != null) " --api-server=${cfg.apiSocket}"
+ lib.optionalString (cfg.logFile != null) " --logfile=${cfg.logFile}"
;
printerConfigPath =
if cfg.mutableConfig
@ -211,7 +210,7 @@ in
with pkgs;
let
default = a: b: if a != null then a else b;
firmwares = filterAttrs (n: v: v != null) (mapAttrs
firmwares = lib.filterAttrs (n: v: v != null) (lib.mapAttrs
(mcu: { enable, enableKlipperFlash, configFile, serial }:
if enable then
pkgs.klipper-firmware.override
@ -220,18 +219,18 @@ in
firmwareConfig = configFile;
} else null)
cfg.firmwares);
firmwareFlasher = mapAttrsToList
firmwareFlasher = lib.mapAttrsToList
(mcu: firmware: pkgs.klipper-flash.override {
mcu = lib.strings.sanitizeDerivationName mcu;
klipper-firmware = firmware;
flashDevice = default cfg.firmwares."${mcu}".serial cfg.settings."${mcu}".serial;
firmwareConfig = cfg.firmwares."${mcu}".configFile;
})
(filterAttrs (mcu: firmware: cfg.firmwares."${mcu}".enableKlipperFlash) firmwares);
(lib.filterAttrs (mcu: firmware: cfg.firmwares."${mcu}".enableKlipperFlash) firmwares);
in
[ klipper-genconf ] ++ firmwareFlasher ++ attrValues firmwares;
[ klipper-genconf ] ++ firmwareFlasher ++ lib.attrValues firmwares;
};
meta.maintainers = [
maintainers.cab404
lib.maintainers.cab404
];
}