|
14 | 14 | mkEnableOption
|
15 | 15 | mkIf
|
16 | 16 | mkOption
|
| 17 | + optionalAttrs |
| 18 | + optionalString |
17 | 19 | types
|
18 | 20 | ;
|
19 | 21 | mergeSets = sets: lists.fold attrsets.recursiveUpdate { } sets;
|
20 | 22 | cfg = config.services.way-displays;
|
| 23 | + serviceName = "way-displays" + optionalString cfg.statefulConfiguration "@"; |
21 | 24 | yaml = pkgs.formats.yaml { };
|
22 | 25 | in
|
23 | 26 | {
|
24 |
| - meta.maintainers = [ maintainers.jolars ]; |
| 27 | + meta.maintainers = [ |
| 28 | + maintainers.jolars |
| 29 | + maintainers.mightyiam |
| 30 | + ]; |
25 | 31 |
|
26 | 32 | options.services.way-displays = {
|
27 | 33 | enable = mkEnableOption "way-displays";
|
28 | 34 |
|
29 | 35 | package = lib.mkPackageOption pkgs "way-displays" { };
|
30 | 36 |
|
| 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 | + |
31 | 49 | settings = mkOption {
|
32 |
| - type = yaml.type; |
| 50 | + type = types.nullOr yaml.type; |
33 | 51 | default = { };
|
34 | 52 | example = literalExpression ''
|
35 | 53 | {
|
|
77 | 95 | config = mkIf cfg.enable {
|
78 | 96 | assertions = [
|
79 | 97 | (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 | + } |
80 | 106 | ];
|
81 | 107 |
|
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 ]; |
90 | 109 |
|
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 | + }; |
100 | 131 |
|
101 |
| - Install = { |
| 132 | + Install = mkIf (!cfg.statefulConfiguration) { |
102 | 133 | WantedBy = [ cfg.systemdTarget ];
|
103 | 134 | };
|
104 | 135 |
|
105 | 136 | Service = {
|
106 | 137 | Type = "simple";
|
107 | 138 | ExecStart = "${lib.getExe cfg.package}";
|
108 | 139 | Restart = "always";
|
| 140 | + Environment = mkIf (cfg.statefulConfiguration) "XDG_VTNR=%i"; |
109 | 141 | };
|
110 | 142 | };
|
111 | 143 | };
|
|
0 commit comments