Skip to content

Commit 1d120e7

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

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

modules/services/way-displays.nix

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,41 @@ 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+
Enables a stateful setup.
42+
A configuration file is not generated.
43+
Allows way-displays to write its own configuration.
44+
45+
The service is _not_ started automatically.
46+
It can be started by running `systemctl --user start "way-displays@$XDG_VTNR".service`.
47+
'';
48+
};
49+
3150
settings = mkOption {
32-
type = yaml.type;
51+
type = types.nullOr yaml.type;
3352
default = { };
3453
example = literalExpression ''
3554
{
@@ -77,35 +96,47 @@ in
7796
config = mkIf cfg.enable {
7897
assertions = [
7998
(lib.hm.assertions.assertPlatform "services.way-displays" pkgs lib.platforms.linux)
99+
{
100+
assertion = cfg.statefulConfiguration -> cfg.settings == null;
101+
message = "way-displays: when `statefulConfiguration = true` then `settings` must be `null`";
102+
}
103+
{
104+
assertion = (!cfg.statefulConfiguration) -> cfg.settings != null;
105+
message = "way-displays: `settings` must be non-null unless `statefulConfiguration = true`";
106+
}
80107
];
81108

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-
]);
109+
xdg.configFile."way-displays/cfg.yaml".source = mkIf (!cfg.statefulConfiguration) (
110+
yaml.generate "way-displays-config.yaml" (mergeSets [
111+
{
112+
CALLBACK_CMD = "${pkgs.libnotify}/bin/notify-send \"way-displays \${CALLBACK_LEVEL}\" \"\${CALLBACK_MSG}\"";
113+
}
114+
cfg.settings
115+
])
116+
);
90117

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-
};
118+
systemd.user.services.${serviceName} = {
119+
Unit =
120+
{
121+
Description = "Display configuration service";
122+
Documentation = "man:way-displays(1)";
123+
ConditionEnvironment = "WAYLAND_DISPLAY";
124+
}
125+
// optionalAttrs (!cfg.statefulConfiguration) {
126+
PartOf = cfg.systemdTarget;
127+
Requires = cfg.systemdTarget;
128+
After = cfg.systemdTarget;
129+
};
100130

101-
Install = {
131+
Install = mkIf (!cfg.statefulConfiguration) {
102132
WantedBy = [ cfg.systemdTarget ];
103133
};
104134

105135
Service = {
106136
Type = "simple";
107-
ExecStart = "${lib.getExe cfg.package}";
137+
ExecStart = lib.getExe cfg.package;
108138
Restart = "always";
139+
Environment = mkIf (cfg.statefulConfiguration) "XDG_VTNR=%i";
109140
};
110141
};
111142
};

0 commit comments

Comments
 (0)