2019-07-04 16:34:49 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.clight;
|
|
|
|
|
|
|
|
toConf =
|
|
|
|
v:
|
|
|
|
if builtins.isFloat v then
|
|
|
|
toString v
|
|
|
|
else if isInt v then
|
|
|
|
toString v
|
|
|
|
else if isBool v then
|
|
|
|
boolToString v
|
|
|
|
else if isString v then
|
|
|
|
''"${escape [ ''"'' ] v}"''
|
|
|
|
else if isList v then
|
|
|
|
"[ " + concatMapStringsSep ", " toConf v + " ]"
|
2020-09-26 15:28:54 +00:00
|
|
|
else if isAttrs v then
|
|
|
|
"\n{\n" + convertAttrs v + "\n}"
|
2019-07-04 16:34:49 +00:00
|
|
|
else
|
|
|
|
abort "clight.toConf: unexpected type (v = ${v})";
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2020-09-26 15:28:54 +00:00
|
|
|
getSep = v: if isAttrs v then ":" else "=";
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2020-09-26 15:28:54 +00:00
|
|
|
convertAttrs =
|
|
|
|
attrs:
|
|
|
|
concatStringsSep "\n" (
|
|
|
|
mapAttrsToList (name: value: "${toString name} ${getSep value} ${toConf value};") attrs
|
|
|
|
);
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2020-09-26 15:28:54 +00:00
|
|
|
clightConf = pkgs.writeText "clight.conf" (
|
|
|
|
convertAttrs (filterAttrs (_: value: value != null) cfg.settings)
|
|
|
|
);
|
2019-07-04 16:34:49 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.clight = {
|
2022-11-09 21:45:00 +00:00
|
|
|
enable = mkEnableOption "clight";
|
2019-07-04 16:34:49 +00:00
|
|
|
|
|
|
|
temperature = {
|
|
|
|
day = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 5500;
|
|
|
|
description = ''
|
|
|
|
Colour temperature to use during the day, between
|
|
|
|
`1000` and `25000` K.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
night = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 3700;
|
|
|
|
description = ''
|
|
|
|
Colour temperature to use at night, between
|
|
|
|
`1000` and `25000` K.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
settings =
|
|
|
|
let
|
2020-09-26 15:28:54 +00:00
|
|
|
validConfigTypes =
|
|
|
|
with types;
|
|
|
|
oneOf [
|
|
|
|
int
|
|
|
|
str
|
|
|
|
bool
|
|
|
|
float
|
|
|
|
];
|
|
|
|
collectionTypes =
|
|
|
|
with types;
|
|
|
|
oneOf [
|
|
|
|
validConfigTypes
|
|
|
|
(listOf validConfigTypes)
|
2024-12-10 19:26:33 +00:00
|
|
|
];
|
2019-07-04 16:34:49 +00:00
|
|
|
in
|
|
|
|
mkOption {
|
2020-09-26 15:28:54 +00:00
|
|
|
type = with types; attrsOf (nullOr (either collectionTypes (attrsOf collectionTypes)));
|
2019-07-04 16:34:49 +00:00
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
captures = 20;
|
|
|
|
gamma_long_transition = true;
|
|
|
|
ac_capture_timeouts = [
|
|
|
|
120
|
|
|
|
300
|
|
|
|
60
|
|
|
|
];
|
|
|
|
};
|
|
|
|
description = ''
|
|
|
|
Additional configuration to extend clight.conf. See
|
|
|
|
<https://github.com/FedeDP/Clight/blob/master/Extra/clight.conf> for a
|
|
|
|
sample configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2020-04-03 01:33:02 +00:00
|
|
|
assertions =
|
|
|
|
let
|
|
|
|
inRange =
|
|
|
|
v: l: r:
|
|
|
|
v >= l && v <= r;
|
|
|
|
in
|
|
|
|
[
|
|
|
|
{
|
|
|
|
assertion =
|
|
|
|
config.location.provider == "manual"
|
|
|
|
-> inRange config.location.latitude (-90) 90 && inRange config.location.longitude (-180) 180;
|
|
|
|
message = "You must specify a valid latitude and longitude if manually providing location";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2019-07-04 16:34:49 +00:00
|
|
|
boot.kernelModules = [ "i2c_dev" ];
|
|
|
|
environment.systemPackages = with pkgs; [
|
|
|
|
clight
|
|
|
|
clightd
|
|
|
|
];
|
|
|
|
services.dbus.packages = with pkgs; [
|
|
|
|
clight
|
|
|
|
clightd
|
|
|
|
];
|
|
|
|
services.upower.enable = true;
|
|
|
|
|
|
|
|
services.clight.settings =
|
|
|
|
{
|
2020-09-26 15:28:54 +00:00
|
|
|
gamma.temp =
|
|
|
|
with cfg.temperature;
|
|
|
|
mkDefault [
|
|
|
|
day
|
|
|
|
night
|
|
|
|
];
|
2019-07-04 16:34:49 +00:00
|
|
|
}
|
|
|
|
// (optionalAttrs (config.location.provider == "manual") {
|
2020-09-26 15:28:54 +00:00
|
|
|
daytime.latitude = mkDefault config.location.latitude;
|
|
|
|
daytime.longitude = mkDefault config.location.longitude;
|
2019-07-04 16:34:49 +00:00
|
|
|
});
|
|
|
|
|
2019-08-13 21:52:01 +00:00
|
|
|
services.geoclue2.appConfig.clightc = {
|
2019-07-04 16:34:49 +00:00
|
|
|
isAllowed = true;
|
|
|
|
isSystem = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.clightd = {
|
|
|
|
requires = [ "polkit.service" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
description = "Bus service to manage various screen related properties (gamma, dpms, backlight)";
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "dbus";
|
|
|
|
BusName = "org.clightd.clightd";
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = 5;
|
|
|
|
ExecStart = ''
|
|
|
|
${pkgs.clightd}/bin/clightd
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.user.services.clight = {
|
|
|
|
after = [
|
|
|
|
"upower.service"
|
|
|
|
"clightd.service"
|
|
|
|
];
|
|
|
|
wants = [
|
|
|
|
"upower.service"
|
|
|
|
"clightd.service"
|
|
|
|
];
|
|
|
|
partOf = [ "graphical-session.target" ];
|
|
|
|
wantedBy = [ "graphical-session.target" ];
|
|
|
|
|
|
|
|
description = "C daemon to adjust screen brightness to match ambient brightness, as computed capturing frames from webcam";
|
|
|
|
serviceConfig = {
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = 5;
|
|
|
|
ExecStart = ''
|
|
|
|
${pkgs.clight}/bin/clight --conf-file ${clightConf}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|