Skip to content

Commit 749da99

Browse files
committed
way-displays: support stateful configuration file
1 parent d23d20f commit 749da99

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

modules/services/way-displays.nix

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,40 @@ let
1414
mkEnableOption
1515
mkIf
1616
mkOption
17+
optionalAttrs
18+
optionalString
1719
types
1820
;
1921
mergeSets = sets: lists.fold attrsets.recursiveUpdate { } sets;
2022
cfg = config.services.way-displays;
23+
serviceName = "way-displays" + optionalString cfg.statefulConfiguration "@";
2124
yaml = pkgs.formats.yaml { };
2225
in
2326
{
24-
meta.maintainers = [ maintainers.jolars ];
27+
meta.maintainers = [
28+
maintainers.jolars
29+
maintainers.mightyiam
30+
];
2531

2632
options.services.way-displays = {
2733
enable = mkEnableOption "way-displays";
2834

2935
package = lib.mkPackageOption pkgs "way-displays" { };
3036

37+
statefulConfiguration = mkOption {
38+
type = types.bool;
39+
default = false;
40+
description = ''
41+
A configuration file is not generated.
42+
Allows way-displays to write its own configuration.
43+
44+
The service is _not_ started automatically.
45+
It can be started by running `systemctl --user start "way-displays@$XDG_VTNR".service`.
46+
'';
47+
};
48+
3149
settings = mkOption {
32-
type = yaml.type;
50+
type = types.nullOr yaml.type;
3351
default = { };
3452
example = literalExpression ''
3553
{
@@ -77,35 +95,49 @@ in
7795
config = mkIf cfg.enable {
7896
assertions = [
7997
(lib.hm.assertions.assertPlatform "services.way-displays" pkgs lib.platforms.linux)
98+
{
99+
assertion = cfg.statefulConfiguration -> cfg.settings == null;
100+
message = "way-displays: when `statefulConfiguration = true` then `settings` must be `null`";
101+
}
102+
{
103+
assertion = (!cfg.statefulConfiguration) -> cfg.settings != null;
104+
message = "way-displays: `settings` must be non-null unless `statefulConfiguration = true`";
105+
}
80106
];
81107

82-
xdg.configFile."way-displays/cfg.yaml".source =
83-
yaml.generate "way-displays-config.yaml"
84-
(mergeSets [
85-
{
86-
CALLBACK_CMD = "${pkgs.libnotify}/bin/notify-send \"way-displays \${CALLBACK_LEVEL}\" \"\${CALLBACK_MSG}\"";
87-
}
88-
cfg.settings
89-
]);
108+
home.packages = [ cfg.package ];
90109

91-
systemd.user.services.way-displays = {
92-
Unit = {
93-
Description = "Display configuration service";
94-
Documentation = "man:way-displays(1)";
95-
ConditionEnvironment = "WAYLAND_DISPLAY";
96-
PartOf = cfg.systemdTarget;
97-
Requires = cfg.systemdTarget;
98-
After = cfg.systemdTarget;
99-
};
110+
xdg.configFile."way-displays/cfg.yaml" = mkIf (!cfg.statefulConfiguration) {
111+
source = yaml.generate "way-displays-config.yaml" (mergeSets [
112+
{
113+
CALLBACK_CMD = "${pkgs.libnotify}/bin/notify-send \"way-displays \${CALLBACK_LEVEL}\" \"\${CALLBACK_MSG}\"";
114+
}
115+
cfg.settings
116+
]);
117+
};
118+
119+
systemd.user.services.${serviceName} = {
120+
Unit =
121+
{
122+
Description = "Display configuration service";
123+
Documentation = "man:way-displays(1)";
124+
ConditionEnvironment = "WAYLAND_DISPLAY";
125+
Requisite = cfg.systemdTarget;
126+
After = cfg.systemdTarget;
127+
}
128+
// optionalAttrs (!cfg.statefulConfiguration) {
129+
PartOf = cfg.systemdTarget;
130+
};
100131

101-
Install = {
132+
Install = mkIf (!cfg.statefulConfiguration) {
102133
WantedBy = [ cfg.systemdTarget ];
103134
};
104135

105136
Service = {
106137
Type = "simple";
107138
ExecStart = "${lib.getExe cfg.package}";
108139
Restart = "always";
140+
Environment = mkIf (cfg.statefulConfiguration) "XDG_VTNR=%i";
109141
};
110142
};
111143
};

0 commit comments

Comments
 (0)