nixos/hardware.fw-fanctrl: add package option and refactor using lib.attrsets.recursiveUpdate

Typically services have a `package` option, so it can be set externally
if users are running the stable version but want the package from
unstable, or devs want to test a package from their flake in production.
Really useful in many situations!

Also, the previous implementation was using `pkgs.runCommand` which is
discouraged due to
[IFD](https://nix.dev/manual/nix/2.26/language/import-from-derivation)
(import from derivation) leading to potential slowdowns during
evaluation. I opted for reading the json file and using
[lib.attrsets.recursiveUpdate](https://ryantm.github.io/nixpkgs/functions/library/attrsets/#function-library-lib.attrsets.recursiveUpdate)
to update the default values with the user provided ones.
This commit is contained in:
Mirza Arnaut 2025-07-24 14:08:33 +02:00
parent e60c6223f6
commit 15ad26705b

View File

@ -4,21 +4,18 @@
pkgs,
...
}:
let
cfg = config.hardware.fw-fanctrl;
configFormat = pkgs.formats.json { };
configOption = configFormat.generate "config.json" cfg.config;
configFile = pkgs.runCommand "configFile" { } ''
${lib.getExe pkgs.jq} -s '.[0] * .[1]' ${pkgs.fw-fanctrl}/share/fw-fanctrl/config.json ${configOption} > $out
'';
cfg = config.hardware.fw-fanctrl;
in
{
options.hardware.fw-fanctrl = {
enable = lib.mkEnableOption "the fw-fanctrl systemd service and install the needed packages";
package = lib.mkPackageOption pkgs "fw-fanctrl" { };
ectoolPackage = lib.mkPackageOption pkgs "fw-ectool" { };
disableBatteryTempCheck = lib.mkOption {
type = lib.types.bool;
default = false;
@ -98,30 +95,36 @@ in
};
};
config = lib.mkIf cfg.enable {
environment.systemPackages = with pkgs; [
fw-fanctrl
fw-ectool
];
config =
let
defaultConfig = builtins.fromJSON (builtins.readFile "${cfg.package}/share/fw-fanctrl/config.json");
finalConfig = lib.attrsets.recursiveUpdate defaultConfig cfg.config;
configFile = configFormat.generate "custom.json" finalConfig;
in
lib.mkIf cfg.enable {
environment.systemPackages = [
cfg.package
cfg.ectoolPackage
];
systemd.services.fw-fanctrl = {
description = "Framework Fan Controller";
after = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
Restart = "always";
ExecStart = "${lib.getExe pkgs.fw-fanctrl} --output-format JSON run --config ${configFile} --silent ${lib.optionalString cfg.disableBatteryTempCheck "--no-battery-sensors"}";
ExecStopPost = "${lib.getExe pkgs.fw-ectool} autofanctrl";
systemd.services.fw-fanctrl = {
description = "Framework Fan Controller";
after = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
Restart = "always";
ExecStart = "${lib.getExe cfg.package} --output-format JSON run --config ${configFile} --silent ${lib.optionalString cfg.disableBatteryTempCheck "--no-battery-sensors"}";
ExecStopPost = "${lib.getExe cfg.ectoolPackage} autofanctrl";
};
wantedBy = [ "multi-user.target" ];
};
wantedBy = [ "multi-user.target" ];
# Create suspend config
environment.etc."systemd/system-sleep/fw-fanctrl-suspend.sh".source =
"${cfg.package}/share/fw-fanctrl/fw-fanctrl-suspend";
};
# Create suspend config
environment.etc."systemd/system-sleep/fw-fanctrl-suspend.sh".source =
"${pkgs.fw-fanctrl}/share/fw-fanctrl/fw-fanctrl-suspend";
};
meta = {
maintainers = pkgs.fw-fanctrl.meta.maintainers;
maintainers = [ lib.maintainers.Svenum ];
};
}