Skip to content

Commit

Permalink
nixos/services.thinkfan: remove with lib;
Browse files Browse the repository at this point in the history
  • Loading branch information
Stunkymonkey committed Aug 29, 2024
1 parent f4fdd33 commit d9dc50d
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions nixos/modules/services/hardware/thinkfan.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{ config, lib, pkgs, ... }:

with lib;

let

cfg = config.services.thinkfan;
Expand All @@ -10,25 +7,25 @@ let
thinkfan = pkgs.thinkfan.override { inherit (cfg) smartSupport; };

# fan-speed and temperature levels
levelType = with types;
levelType = with lib.types;
let
tuple = ts: mkOptionType {
tuple = ts: lib.mkOptionType {
name = "tuple";
merge = mergeOneOption;
merge = lib.mergeOneOption;
check = xs: all id (zipListsWith (t: x: t.check x) ts xs);
description = "tuple of" + concatMapStrings (t: " (${t.description})") ts;
description = "tuple of" + lib.concatMapStrings (t: " (${t.description})") ts;
};
level = ints.unsigned;
special = enum [ "level auto" "level full-speed" "level disengaged" ];
in
tuple [ (either level special) level level ];

# sensor or fan config
sensorType = name: types.submodule {
freeformType = types.attrsOf settingsFormat.type;
sensorType = name: lib.types.submodule {
freeformType = lib.types.attrsOf settingsFormat.type;
options = {
type = mkOption {
type = types.enum [ "hwmon" "atasmart" "tpacpi" "nvml" ];
type = lib.mkOption {
type = lib.types.enum [ "hwmon" "atasmart" "tpacpi" "nvml" ];
description = ''
The ${name} type, can be
`hwmon` for standard ${name}s,
Expand All @@ -41,8 +38,8 @@ let
`nvml` for the (proprietary) nVidia driver.
'';
};
query = mkOption {
type = types.str;
query = lib.mkOption {
type = lib.types.str;
description = ''
The query string used to match one or more ${name}s: can be
a fullpath to the temperature file (single ${name}) or a fullpath
Expand All @@ -54,8 +51,8 @@ let
:::
'';
};
indices = mkOption {
type = with types; nullOr (listOf ints.unsigned);
indices = lib.mkOption {
type = with lib.types; nullOr (listOf ints.unsigned);
default = null;
description = ''
A list of ${name}s to pick in case multiple ${name}s match the query.
Expand All @@ -65,9 +62,9 @@ let
:::
'';
};
} // optionalAttrs (name == "sensor") {
correction = mkOption {
type = with types; nullOr (listOf int);
} // lib.optionalAttrs (name == "sensor") {
correction = lib.mkOption {
type = with lib.types; nullOr (listOf int);
default = null;
description = ''
A list of values to be added to the temperature of each sensor,
Expand All @@ -79,7 +76,7 @@ let

# removes NixOS special and unused attributes
sensorToConf = { type, query, ... }@args:
(filterAttrs (k: v: v != null && !(elem k ["type" "query"])) args)
(lib.filterAttrs (k: v: v != null && !(lib.elem k ["type" "query"])) args)
// { "${type}" = query; };

syntaxNote = name: ''
Expand All @@ -103,8 +100,8 @@ in {

services.thinkfan = {

enable = mkOption {
type = types.bool;
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to enable thinkfan, a fan control program.
Expand All @@ -117,17 +114,17 @@ in {
relatedPackages = [ "thinkfan" ];
};

smartSupport = mkOption {
type = types.bool;
smartSupport = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to build thinkfan with S.M.A.R.T. support to read temperatures
directly from hard disks.
'';
};

sensors = mkOption {
type = types.listOf (sensorType "sensor");
sensors = lib.mkOption {
type = lib.types.listOf (sensorType "sensor");
default = [
{ type = "tpacpi";
query = "/proc/acpi/ibm/thermal";
Expand All @@ -140,8 +137,8 @@ in {
'';
};

fans = mkOption {
type = types.listOf (sensorType "fan");
fans = lib.mkOption {
type = lib.types.listOf (sensorType "fan");
default = [
{ type = "tpacpi";
query = "/proc/acpi/ibm/fan";
Expand All @@ -154,8 +151,8 @@ in {
'';
};

levels = mkOption {
type = types.listOf levelType;
levels = lib.mkOption {
type = lib.types.listOf levelType;
default = [
[0 0 55]
[1 48 60]
Expand All @@ -177,8 +174,8 @@ in {
'';
};

extraArgs = mkOption {
type = types.listOf types.str;
extraArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "-b" "0" ];
description = ''
Expand All @@ -187,8 +184,8 @@ in {
'';
};

settings = mkOption {
type = types.attrsOf settingsFormat.type;
settings = lib.mkOption {
type = lib.types.attrsOf settingsFormat.type;
default = { };
description = ''
Thinkfan settings. Use this option to configure thinkfan
Expand All @@ -203,11 +200,11 @@ in {

};

config = mkIf cfg.enable {
config = lib.mkIf cfg.enable {

environment.systemPackages = [ thinkfan ];

services.thinkfan.settings = mapAttrs (k: v: mkDefault v) {
services.thinkfan.settings = lib.mapAttrs (k: v: lib.mkDefault v) {
sensors = map sensorToConf cfg.sensors;
fans = map sensorToConf cfg.fans;
levels = cfg.levels;
Expand All @@ -216,7 +213,7 @@ in {
systemd.packages = [ thinkfan ];

systemd.services = {
thinkfan.environment.THINKFAN_ARGS = escapeShellArgs ([ "-c" configFile ] ++ cfg.extraArgs);
thinkfan.environment.THINKFAN_ARGS = lib.escapeShellArgs ([ "-c" configFile ] ++ cfg.extraArgs);
thinkfan.serviceConfig = {
Restart = "on-failure";
RestartSec = "30s";
Expand Down

0 comments on commit d9dc50d

Please sign in to comment.