Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document module options #142

Open
github-actions bot opened this issue Aug 17, 2024 · 0 comments
Open

document module options #142

github-actions bot opened this issue Aug 17, 2024 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Aug 17, 2024

  • kalman
  • devices.events

# TODO: document module options

{ config, lib, myLib, pkgs, ... }:

with lib;
with myLib;

let
  pidFile = "/var/run/illuminanced.pid";
  cfg = config.services.illuminanced;
  # TODO: work on my fork to allow tweaking the keycode to switch modes
  illuminancedPkg = pkgs.rustPlatform.buildRustPackage rec {
    pname = "illuminanced";
    version = "0.0.1";
    doCheck = false;
    cargoHash = "sha256-uhQ7B/mki4xouftz2vBzF/DeMql/9sEkfBMVEHG2adE=";
    src = pkgs.fetchFromGitHub {
      owner = "pdalpra";
      repo = pname;
      rev = pkgRev;
      hash = "sha256-PQHGKz/2UxCf8FosuxmL7DL9Z+H9nzXHdyn+73gWw1I=";
    };
  };
  levels = mergeAll (lists.imap0
    (i: v: {
      "illuminance_${toString i}" = v.illuminance;
      "light_${toString i}" = v.light;
    })
    cfg.brightness.levels);
  configFile = (pkgs.formats.toml { }).generate "illuminance.toml" {
    daemonize = {
      log_to = cfg.log.file;
      log_level = cfg.log.level;
      pid_file = pidFile;
    };
    general = {
      light_steps = 10;
      min_backlight = cfg.brightness.min;
      step_barrier = 0.1;
      enable_max_brightness_mode = cfg.brightness.enableMaxBrightnessMode;

      check_period_in_seconds = cfg.interval;
      backlight_file = cfg.devices.current;
      max_backlight_file = cfg.devices.max;
      illuminance_file = cfg.devices.illuminance;
      event_device_mask = cfg.devices.events.mask;
      event_device_name = cfg.devices.events.name;
    };
    kalman = {
      inherit (cfg.kalman) q r covariance;
    };
    light = {
      points_count = builtins.length cfg.brightness.levels;
    } // levels;
  };
in
{
  # TODO: document module options
  # - kalman
  # - devices.events
  options.services.illuminanced = with types; {
    enable = mkEnableOption "Enable illuminanced";
    package = mkOption {
      type = package;
      description = "The package used to install illuminanced";
      default = illuminancedPkg;
    };
    log = {
      file = mkOption {
        type = string;
        default = "syslog";
        description = "'syslog' or path to a file";
      };
      level = mkOption {
        type = enum [ "OFF" "ERROR" "WARN" "INFO" "DEBUG" "TRACE" ];
        default = "ERROR";
        description = "Log level to use";
      };
    };
    interval = mkOption {
      type = int;
      default = 1;
      description = "Interval for checking the ambient light sensor";
    };
    kalman = {
      q = mkOption {
        type = int;
        default = 1;
        description = "TODO documentation ";
      };
      r = mkOption {
        type = int;
        default = 20;
        description = "TODO documentation";
      };
      covariance = mkOption {
        type = int;
        default = 10;
        description = "TODO documentation";
      };
    };
    devices = {
      current = mkOption {
        type = string;
        default = "/sys/class/backlight/amdgpu_bl1/brightness";
        description = "Path to device file with the current brightness level";
      };
      max = mkOption {
        type = string;
        default = "/sys/class/backlight/amdgpu_bl1/max_brightness";
        description = "Path to device file with the max brightness level";
      };
      illuminance = mkOption {
        type = string;
        default = "/sys/bus/iio/devices/iio:device0/in_illuminance_raw";
        description = "Path to device file with the detected illuminance";
      };
      events = {
        mask = mkOption {
          type = string;
          default = "/dev/input/event*";
          description = "TODO documentation";
        };
        name = mkOption {
          type = string;
          default = "Framework Laptop 16 Keyboard Module - ISO Keyboard";
          description = "TODO documentation";
        };
      };
    };
    brightness = {
      enableMaxBrightnessMode = mkEnableOption "Enable max brightness mode";
      min = mkOption {
        type = int;
        default = 30;
        description = "Minimum backlight level";
      };

      levels = mkOption {
        type = listOf (submodule {
          options = {
            illuminance = mkOption {
              type = int;
              description = "Illuminance level triggering the brightness change";
            };
            light = mkOption {
              type = int;
              description = "number of light steps matching to use for this level";
            };
          };
        });
        description = "Steps for increasing brightness based on illuminance";
        default = [
          { illuminance = 0; light = 0; }
          { illuminance = 20; light = 1; }
          { illuminance = 300; light = 3; }
          { illuminance = 700; light = 4; }
          { illuminance = 1100; light = 5; }
          { illuminance = 7100; light = 10; }
        ];
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.illuminanced = {
      enable = true;
      wantedBy = [ "multi-user.target" ];
      description = "Automatic brightness manager";
      serviceConfig = {
        Type = "forking";
        ExecStart = "${illuminancedPkg}/bin/illuminanced -c ${configFile}";
        PIDFile = pidFile;
        Restart = "on-failure";
      };
    };
  };
}
@github-actions github-actions bot added the todo label Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants