kanshi: update configuration to better match upstream

This commit is contained in:
Vincent Gatine 2024-05-09 18:04:48 +02:00 committed by Robert Helgesson
parent f61917cbaa
commit 4855bfb6ce
No known key found for this signature in database
GPG key ID: 96E745BD17AA17ED
5 changed files with 231 additions and 36 deletions

View file

@ -6,6 +6,48 @@ let
cfg = config.services.kanshi; cfg = config.services.kanshi;
directivesTag = types.attrTag {
profile = mkOption {
type = profileModule;
description = ''
profile attribute set.
'';
};
output = mkOption {
type = outputModule;
description = ''
output attribute set.
'';
};
include = mkOption {
type = types.str;
description = ''
Include as another file from _path_.
Expands shell syntax (see *wordexp*(3) for details).
'';
};
};
tagToStr = x:
if x ? profile then
profileStr x.profile
else if x ? output then
outputStr x.output
else if x ? include then
''include "${x.include}"''
else
throw "Unknown tags ${attrNames x}";
directivesStr = ''
${concatStringsSep "\n" (map tagToStr cfg.settings)}
'';
oldDirectivesStr = ''
${concatStringsSep "\n"
(mapAttrsToList (n: v: profileStr (v // { name = n; })) cfg.profiles)}
${cfg.extraConfig}
'';
outputModule = types.submodule { outputModule = types.submodule {
options = { options = {
@ -113,6 +155,14 @@ let
''; '';
}; };
name = mkOption {
type = types.str;
default = "";
description = ''
Profile name
'';
};
exec = mkOption { exec = mkOption {
type = with types; coercedTo str singleton (listOf str); type = with types; coercedTo str singleton (listOf str);
default = [ ]; default = [ ];
@ -127,15 +177,14 @@ let
}; };
}; };
profileStr = name: profileStr = { outputs, exec, ... }@args: ''
{ outputs, exec, ... }: '' profile ${args.name or ""} {
profile ${name} { ${
${ concatStringsSep "\n "
concatStringsSep "\n " (map outputStr outputs ++ map (cmd: "exec ${cmd}") exec)
(map outputStr outputs ++ map (cmd: "exec ${cmd}") exec)
}
} }
''; }
'';
in { in {
meta.maintainers = [ hm.maintainers.nurelin ]; meta.maintainers = [ hm.maintainers.nurelin ];
@ -157,7 +206,7 @@ in {
type = types.attrsOf profileModule; type = types.attrsOf profileModule;
default = { }; default = { };
description = '' description = ''
List of profiles. Attribute set of profiles.
''; '';
example = literalExpression '' example = literalExpression ''
undocked = { undocked = {
@ -190,6 +239,39 @@ in {
''; '';
}; };
settings = mkOption {
type = types.listOf directivesTag;
default = [ ];
description = ''
Ordered list of directives.
See kanshi(5) for informations.
'';
example = literalExpression ''
{ include = "path/to/included/files"; }
{ output.criteria = "eDP-1";
output.scale = 2;
}
{ profile.name = "undocked";
profile.outputs = [
{
criteria = "eDP-1";
}
];
}
{ profile.name = "docked";
profile.outputs = [
{
criteria = "eDP-1";
}
{
criteria = "Some Company ASDF 4242";
transform = "90";
}
];
}
'';
};
systemdTarget = mkOption { systemdTarget = mkOption {
type = types.str; type = types.str;
default = "sway-session.target"; default = "sway-session.target";
@ -199,33 +281,56 @@ in {
}; };
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable (mkMerge [
assertions = [ {
(lib.hm.assertions.assertPlatform "services.kanshi" pkgs assertions = [
lib.platforms.linux) (lib.hm.assertions.assertPlatform "services.kanshi" pkgs
]; lib.platforms.linux)
{
assertion = (cfg.profiles == { } && cfg.extraConfig == "")
|| (length cfg.settings) == 0;
message =
"Cannot mix kanshi.settings with kanshi.profiles or kanshi.extraConfig";
}
];
}
xdg.configFile."kanshi/config".text = '' (mkIf (cfg.profiles != { }) {
${concatStringsSep "\n" (mapAttrsToList profileStr cfg.profiles)} warnings = [
${cfg.extraConfig} "kanshi.profiles option is deprecated. Use kanshi.settings instead."
''; ];
})
systemd.user.services.kanshi = { (mkIf (cfg.extraConfig != "") {
Unit = { warnings = [
Description = "Dynamic output configuration"; "kanshi.extraConfig option is deprecated. Use kanshi.settings instead."
Documentation = "man:kanshi(1)"; ];
PartOf = cfg.systemdTarget; })
Requires = cfg.systemdTarget;
After = cfg.systemdTarget; {
xdg.configFile."kanshi/config".text =
if cfg.profiles == { } && cfg.extraConfig == "" then
directivesStr
else
oldDirectivesStr;
systemd.user.services.kanshi = {
Unit = {
Description = "Dynamic output configuration";
Documentation = "man:kanshi(1)";
PartOf = cfg.systemdTarget;
Requires = cfg.systemdTarget;
After = cfg.systemdTarget;
};
Service = {
Type = "simple";
ExecStart = "${cfg.package}/bin/kanshi";
Restart = "always";
};
Install = { WantedBy = [ cfg.systemdTarget ]; };
}; };
}
Service = { ]);
Type = "simple";
ExecStart = "${cfg.package}/bin/kanshi";
Restart = "always";
};
Install = { WantedBy = [ cfg.systemdTarget ]; };
};
};
} }

View file

@ -47,6 +47,11 @@
''; '';
}; };
test.asserts.warnings.expected = [
"kanshi.profiles option is deprecated. Use kanshi.settings instead."
"kanshi.extraConfig option is deprecated. Use kanshi.settings instead."
];
nmt.script = '' nmt.script = ''
serviceFile=home-files/.config/systemd/user/kanshi.service serviceFile=home-files/.config/systemd/user/kanshi.service
assertFileExists $serviceFile assertFileExists $serviceFile

View file

@ -1 +1,4 @@
{ kanshi-basic-configuration = ./basic-configuration.nix; } {
kanshi-basic-configuration = ./basic-configuration.nix;
kanshi-new-configuration = ./new-configuration.nix;
}

View file

@ -0,0 +1,19 @@
include "path/to/included/file"
output "*" enable
profile nomad {
output "eDP-1" enable
}
profile desktop {
output "eDP-1" disable
output "Iiyama North America PLE2483H-DP" enable position 0,0
output "Iiyama North America PLE2483H-DP 1158765348486" enable mode 1920x1080 position 1920,0 scale 2.100000 transform flipped-270
exec echo "1 two 3"
exec echo "4 five 6"
}
profile {
output "LVDS-1" enable
exec echo "7 eight 9"
}

View file

@ -0,0 +1,63 @@
{ config, pkgs, ... }: {
config = {
services.kanshi = {
enable = true;
package = config.lib.test.mkStubPackage { };
settings = [
{ include = "path/to/included/file"; }
{
output = {
criteria = "*";
status = "enable";
};
}
{
profile.name = "nomad";
profile.outputs = [{
criteria = "eDP-1";
status = "enable";
}];
}
{
profile.name = "desktop";
profile.exec = [ ''echo "1 two 3"'' ''echo "4 five 6"'' ];
profile.outputs = [
{
criteria = "eDP-1";
status = "disable";
}
{
criteria = "Iiyama North America PLE2483H-DP";
status = "enable";
position = "0,0";
}
{
criteria = "Iiyama North America PLE2483H-DP 1158765348486";
status = "enable";
position = "1920,0";
scale = 2.1;
mode = "1920x1080";
transform = "flipped-270";
}
];
}
{
profile.outputs = [{
criteria = "LVDS-1";
status = "enable";
}];
profile.exec = ''echo "7 eight 9"'';
}
];
};
nmt.script = ''
serviceFile=home-files/.config/systemd/user/kanshi.service
assertFileExists $serviceFile
assertFileExists home-files/.config/kanshi/config
assertFileContent home-files/.config/kanshi/config \
${./new-configuration.conf}
'';
};
}