2020-04-24 23:36:52 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
cfg = config.services.autorandr;
|
2022-03-30 09:31:56 +00:00
|
|
|
|
hookType = types.lines;
|
|
|
|
|
|
|
|
|
|
matrixOf = n: m: elemType:
|
|
|
|
|
mkOptionType rec {
|
|
|
|
|
name = "matrixOf";
|
|
|
|
|
description =
|
|
|
|
|
"${toString n}×${toString m} matrix of ${elemType.description}s";
|
|
|
|
|
check = xss:
|
|
|
|
|
let listOfSize = l: xs: isList xs && length xs == l;
|
|
|
|
|
in listOfSize n xss
|
|
|
|
|
&& all (xs: listOfSize m xs && all elemType.check xs) xss;
|
|
|
|
|
merge = mergeOneOption;
|
|
|
|
|
getSubOptions = prefix: elemType.getSubOptions (prefix ++ [ "*" "*" ]);
|
|
|
|
|
getSubModules = elemType.getSubModules;
|
|
|
|
|
substSubModules = mod: matrixOf n m (elemType.substSubModules mod);
|
|
|
|
|
functor = (defaultFunctor name) // { wrapped = elemType; };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
profileModule = types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
fingerprint = mkOption {
|
|
|
|
|
type = types.attrsOf types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
|
Output name to EDID mapping.
|
2022-08-12 12:06:08 +00:00
|
|
|
|
Use `autorandr --fingerprint` to get current setup values.
|
2022-03-30 09:31:56 +00:00
|
|
|
|
'';
|
|
|
|
|
default = { };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
|
type = types.attrsOf configModule;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Per output profile configuration.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = { };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
hooks = mkOption {
|
|
|
|
|
type = hooksModule;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Profile hook scripts.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = { };
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
configModule = types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
enable = mkOption {
|
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Whether to enable the output.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
crtc = mkOption {
|
|
|
|
|
type = types.nullOr types.ints.unsigned;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Output video display controller.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = null;
|
|
|
|
|
example = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
primary = mkOption {
|
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Whether output should be marked as primary";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
position = mkOption {
|
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Output position";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = "";
|
|
|
|
|
example = "5760x0";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mode = mkOption {
|
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Output resolution.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = "";
|
|
|
|
|
example = "3840x2160";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
rate = mkOption {
|
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Output framerate.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = "";
|
|
|
|
|
example = "60.00";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
gamma = mkOption {
|
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Output gamma configuration.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = "";
|
|
|
|
|
example = "1.0:0.909:0.833";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
rotate = mkOption {
|
|
|
|
|
type = types.nullOr (types.enum [ "normal" "left" "right" "inverted" ]);
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Output rotate configuration.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = null;
|
|
|
|
|
example = "left";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
transform = mkOption {
|
|
|
|
|
type = types.nullOr (matrixOf 3 3 types.float);
|
|
|
|
|
default = null;
|
|
|
|
|
example = literalExpression ''
|
|
|
|
|
[
|
|
|
|
|
[ 0.6 0.0 0.0 ]
|
|
|
|
|
[ 0.0 0.6 0.0 ]
|
|
|
|
|
[ 0.0 0.0 1.0 ]
|
|
|
|
|
]
|
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
|
Refer to
|
2022-08-21 13:32:41 +00:00
|
|
|
|
{manpage}`xrandr(1)`
|
2022-03-30 09:31:56 +00:00
|
|
|
|
for the documentation of the transform matrix.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dpi = mkOption {
|
|
|
|
|
type = types.nullOr types.ints.positive;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Output DPI configuration.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = null;
|
|
|
|
|
example = 96;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
scale = mkOption {
|
|
|
|
|
type = types.nullOr (types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
method = mkOption {
|
|
|
|
|
type = types.enum [ "factor" "pixel" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Output scaling method.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = "factor";
|
|
|
|
|
example = "pixel";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
x = mkOption {
|
|
|
|
|
type = types.either types.float types.ints.positive;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Horizontal scaling factor/pixels.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
y = mkOption {
|
|
|
|
|
type = types.either types.float types.ints.positive;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Vertical scaling factor/pixels.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
|
Output scale configuration.
|
2022-08-12 12:06:08 +00:00
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
|
Either configure by pixels or a scaling factor. When using pixel method the
|
2022-09-09 14:08:57 +00:00
|
|
|
|
{manpage}`xrandr(1)`
|
2022-03-30 09:31:56 +00:00
|
|
|
|
option
|
2022-09-09 14:08:57 +00:00
|
|
|
|
`--scale-from`
|
2022-03-30 09:31:56 +00:00
|
|
|
|
will be used; when using factor method the option
|
2022-09-09 14:08:57 +00:00
|
|
|
|
`--scale`
|
2022-03-30 09:31:56 +00:00
|
|
|
|
will be used.
|
2022-08-12 12:06:08 +00:00
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
|
This option is a shortcut version of the transform option and they are mutually
|
|
|
|
|
exclusive.
|
|
|
|
|
'';
|
|
|
|
|
default = null;
|
|
|
|
|
example = literalExpression ''
|
|
|
|
|
{
|
|
|
|
|
x = 1.25;
|
|
|
|
|
y = 1.25;
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
hooksModule = types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
postswitch = mkOption {
|
|
|
|
|
type = types.attrsOf hookType;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Postswitch hook executed after mode switch.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = { };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
preswitch = mkOption {
|
|
|
|
|
type = types.attrsOf hookType;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Preswitch hook executed before mode switch.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = { };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
predetect = mkOption {
|
|
|
|
|
type = types.attrsOf hookType;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2022-03-30 09:31:56 +00:00
|
|
|
|
Predetect hook executed before autorandr attempts to run xrandr.
|
|
|
|
|
'';
|
|
|
|
|
default = { };
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
hookToFile = folder: name: hook:
|
|
|
|
|
nameValuePair "xdg/autorandr/${folder}/${name}" {
|
|
|
|
|
source = "${pkgs.writeShellScriptBin "hook" hook}/bin/hook";
|
|
|
|
|
};
|
|
|
|
|
profileToFiles = name: profile:
|
|
|
|
|
with profile;
|
|
|
|
|
mkMerge ([
|
|
|
|
|
{
|
|
|
|
|
"xdg/autorandr/${name}/setup".text = concatStringsSep "\n"
|
|
|
|
|
(mapAttrsToList fingerprintToString fingerprint);
|
|
|
|
|
"xdg/autorandr/${name}/config".text =
|
|
|
|
|
concatStringsSep "\n" (mapAttrsToList configToString profile.config);
|
|
|
|
|
}
|
|
|
|
|
(mapAttrs' (hookToFile "${name}/postswitch.d") hooks.postswitch)
|
|
|
|
|
(mapAttrs' (hookToFile "${name}/preswitch.d") hooks.preswitch)
|
|
|
|
|
(mapAttrs' (hookToFile "${name}/predetect.d") hooks.predetect)
|
|
|
|
|
]);
|
|
|
|
|
fingerprintToString = name: edid: "${name} ${edid}";
|
|
|
|
|
configToString = name: config:
|
|
|
|
|
if config.enable then
|
|
|
|
|
concatStringsSep "\n" ([ "output ${name}" ]
|
|
|
|
|
++ optional (config.position != "") "pos ${config.position}"
|
|
|
|
|
++ optional (config.crtc != null) "crtc ${toString config.crtc}"
|
|
|
|
|
++ optional config.primary "primary"
|
|
|
|
|
++ optional (config.dpi != null) "dpi ${toString config.dpi}"
|
|
|
|
|
++ optional (config.gamma != "") "gamma ${config.gamma}"
|
|
|
|
|
++ optional (config.mode != "") "mode ${config.mode}"
|
|
|
|
|
++ optional (config.rate != "") "rate ${config.rate}"
|
|
|
|
|
++ optional (config.rotate != null) "rotate ${config.rotate}"
|
|
|
|
|
++ optional (config.transform != null) ("transform "
|
|
|
|
|
+ concatMapStringsSep "," toString (flatten config.transform))
|
|
|
|
|
++ optional (config.scale != null)
|
|
|
|
|
((if config.scale.method == "factor" then "scale" else "scale-from")
|
|
|
|
|
+ " ${toString config.scale.x}x${toString config.scale.y}"))
|
|
|
|
|
else ''
|
|
|
|
|
output ${name}
|
|
|
|
|
off
|
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
services.autorandr = {
|
2024-04-21 15:54:59 +00:00
|
|
|
|
enable = mkEnableOption "handling of hotplug and sleep events by autorandr";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
defaultTarget = mkOption {
|
|
|
|
|
default = "default";
|
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Fallback if no monitor layout can be detected. See the docs
|
|
|
|
|
(https://github.com/phillipberndt/autorandr/blob/v1.0/README.md#how-to-use)
|
|
|
|
|
for further reference.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2022-03-30 09:31:56 +00:00
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
|
ignoreLid = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Treat outputs as connected even if their lids are closed";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
matchEdid = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
|
|
|
|
description = "Match displays based on edid instead of name";
|
2023-01-20 10:41:00 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
|
hooks = mkOption {
|
|
|
|
|
type = hooksModule;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Global hook scripts";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = { };
|
2022-12-17 10:02:37 +00:00
|
|
|
|
example = literalExpression ''
|
2022-03-30 09:31:56 +00:00
|
|
|
|
{
|
|
|
|
|
postswitch = {
|
|
|
|
|
"notify-i3" = "''${pkgs.i3}/bin/i3-msg restart";
|
|
|
|
|
"change-background" = readFile ./change-background.sh;
|
|
|
|
|
"change-dpi" = '''
|
|
|
|
|
case "$AUTORANDR_CURRENT_PROFILE" in
|
|
|
|
|
default)
|
|
|
|
|
DPI=120
|
|
|
|
|
;;
|
|
|
|
|
home)
|
|
|
|
|
DPI=192
|
|
|
|
|
;;
|
|
|
|
|
work)
|
|
|
|
|
DPI=144
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo "Unknown profle: $AUTORANDR_CURRENT_PROFILE"
|
|
|
|
|
exit 1
|
|
|
|
|
esac
|
|
|
|
|
echo "Xft.dpi: $DPI" | ''${pkgs.xorg.xrdb}/bin/xrdb -merge
|
2022-12-17 10:02:37 +00:00
|
|
|
|
''';
|
2022-03-30 09:31:56 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
profiles = mkOption {
|
|
|
|
|
type = types.attrsOf profileModule;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Autorandr profiles specification.";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
default = { };
|
|
|
|
|
example = literalExpression ''
|
|
|
|
|
{
|
|
|
|
|
"work" = {
|
|
|
|
|
fingerprint = {
|
|
|
|
|
eDP1 = "<EDID>";
|
|
|
|
|
DP1 = "<EDID>";
|
|
|
|
|
};
|
|
|
|
|
config = {
|
|
|
|
|
eDP1.enable = false;
|
|
|
|
|
DP1 = {
|
|
|
|
|
enable = true;
|
|
|
|
|
crtc = 0;
|
|
|
|
|
primary = true;
|
|
|
|
|
position = "0x0";
|
|
|
|
|
mode = "3840x2160";
|
|
|
|
|
gamma = "1.0:0.909:0.833";
|
|
|
|
|
rate = "60.00";
|
|
|
|
|
rotate = "left";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
hooks.postswitch = readFile ./work-postswitch.sh;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
|
|
services.udev.packages = [ pkgs.autorandr ];
|
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
|
environment = {
|
|
|
|
|
systemPackages = [ pkgs.autorandr ];
|
|
|
|
|
etc = mkMerge ([
|
|
|
|
|
(mapAttrs' (hookToFile "postswitch.d") cfg.hooks.postswitch)
|
|
|
|
|
(mapAttrs' (hookToFile "preswitch.d") cfg.hooks.preswitch)
|
|
|
|
|
(mapAttrs' (hookToFile "predetect.d") cfg.hooks.predetect)
|
|
|
|
|
(mkMerge (mapAttrsToList profileToFiles cfg.profiles))
|
|
|
|
|
]);
|
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
systemd.services.autorandr = {
|
|
|
|
|
wantedBy = [ "sleep.target" ];
|
|
|
|
|
description = "Autorandr execution hook";
|
|
|
|
|
after = [ "sleep.target" ];
|
|
|
|
|
|
2020-11-03 02:18:15 +00:00
|
|
|
|
startLimitIntervalSec = 5;
|
|
|
|
|
startLimitBurst = 1;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
serviceConfig = {
|
2023-01-20 10:41:00 +00:00
|
|
|
|
ExecStart = ''
|
|
|
|
|
${pkgs.autorandr}/bin/autorandr \
|
|
|
|
|
--batch \
|
|
|
|
|
--change \
|
|
|
|
|
--default ${cfg.defaultTarget} \
|
2024-04-21 15:54:59 +00:00
|
|
|
|
${optionalString cfg.ignoreLid "--ignore-lid"} \
|
|
|
|
|
${optionalString cfg.matchEdid "--match-edid"}
|
2023-01-20 10:41:00 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Type = "oneshot";
|
|
|
|
|
RemainAfterExit = false;
|
2022-02-10 20:34:41 +00:00
|
|
|
|
KillMode = "process";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
|
meta.maintainers = with maintainers; [ alexnortung ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
}
|