fork/home-manager

git clone https://git.julia.blue/fork/home-manager

André Silvahyprland: add tray.target09587fbb

master
8.5 KiB267 linesraw
1{ config, lib, pkgs, ... }:
2let
3
4  cfg = config.wayland.windowManager.hyprland;
5
6  variables = builtins.concatStringsSep " " cfg.systemd.variables;
7  extraCommands = builtins.concatStringsSep " "
8    (map (f: "&& ${f}") cfg.systemd.extraCommands);
9  systemdActivation = ''
10    exec-once = ${pkgs.dbus}/bin/dbus-update-activation-environment --systemd ${variables} ${extraCommands}
11  '';
12
13in {
14  meta.maintainers = [ lib.maintainers.fufexan ];
15
16  # A few option removals and renames to aid those migrating from the upstream
17  # module.
18  imports = [
19    (lib.mkRemovedOptionModule # \
20      [ "wayland" "windowManager" "hyprland" "disableAutoreload" ]
21      "Autoreloading now always happens")
22
23    (lib.mkRemovedOptionModule # \
24      [ "wayland" "windowManager" "hyprland" "recommendedEnvironment" ]
25      "Recommended environment variables are now always set")
26
27    (lib.mkRemovedOptionModule # \
28      [ "wayland" "windowManager" "hyprland" "xwayland" "hidpi" ]
29      "HiDPI patches are deprecated. Refer to https://wiki.hyprland.org/Configuring/XWayland")
30
31    (lib.mkRenamedOptionModule # \
32      [ "wayland" "windowManager" "hyprland" "nvidiaPatches" ] # \
33      [ "wayland" "windowManager" "hyprland" "enableNvidiaPatches" ])
34
35    (lib.mkRenamedOptionModule # \
36      [ "wayland" "windowManager" "hyprland" "systemdIntegration" ] # \
37      [ "wayland" "windowManager" "hyprland" "systemd" "enable" ])
38  ];
39
40  options.wayland.windowManager.hyprland = {
41    enable = lib.mkEnableOption "Hyprland wayland compositor";
42
43    package = lib.mkPackageOption pkgs "hyprland" { };
44
45    finalPackage = lib.mkOption {
46      type = lib.types.package;
47      readOnly = true;
48      default = cfg.package.override {
49        enableXWayland = cfg.xwayland.enable;
50        enableNvidiaPatches = cfg.enableNvidiaPatches;
51      };
52      defaultText = lib.literalMD
53        "`wayland.windowManager.hyprland.package` with applied configuration";
54      description = ''
55        The Hyprland package after applying configuration.
56      '';
57    };
58
59    plugins = lib.mkOption {
60      type = with lib.types; listOf (either package path);
61      default = [ ];
62      description = ''
63        List of Hyprland plugins to use. Can either be packages or
64        absolute plugin paths.
65      '';
66    };
67
68    systemd = {
69      enable = lib.mkEnableOption null // {
70        default = true;
71        description = ''
72          Whether to enable {file}`hyprland-session.target` on
73          hyprland startup. This links to `graphical-session.target`.
74          Some important environment variables will be imported to systemd
75          and D-Bus user environment before reaching the target, including
76          - `DISPLAY`
77          - `HYPRLAND_INSTANCE_SIGNATURE`
78          - `WAYLAND_DISPLAY`
79          - `XDG_CURRENT_DESKTOP`
80        '';
81      };
82
83      variables = lib.mkOption {
84        type = with lib.types; listOf str;
85        default = [
86          "DISPLAY"
87          "HYPRLAND_INSTANCE_SIGNATURE"
88          "WAYLAND_DISPLAY"
89          "XDG_CURRENT_DESKTOP"
90        ];
91        example = [ "-all" ];
92        description = ''
93          Environment variables to be imported in the systemd & D-Bus user
94          environment.
95        '';
96      };
97
98      extraCommands = lib.mkOption {
99        type = with lib.types; listOf str;
100        default = [
101          "systemctl --user stop hyprland-session.target"
102          "systemctl --user start hyprland-session.target"
103        ];
104        description = "Extra commands to be run after D-Bus activation.";
105      };
106    };
107
108    xwayland.enable = lib.mkEnableOption "XWayland" // { default = true; };
109
110    enableNvidiaPatches =
111      lib.mkEnableOption "patching wlroots for better Nvidia support";
112
113    settings = lib.mkOption {
114      type = with lib.types;
115        let
116          valueType = nullOr (oneOf [
117            bool
118            int
119            float
120            str
121            path
122            (attrsOf valueType)
123            (listOf valueType)
124          ]) // {
125            description = "Hyprland configuration value";
126          };
127        in valueType;
128      default = { };
129      description = ''
130        Hyprland configuration written in Nix. Entries with the same key
131        should be written as lists. Variables' and colors' names should be
132        quoted. See <https://wiki.hyprland.org> for more examples.
133
134        ::: {.note}
135        Use the [](#opt-wayland.windowManager.hyprland.plugins) option to
136        declare plugins.
137        :::
138
139      '';
140      example = lib.literalExpression ''
141        {
142          decoration = {
143            shadow_offset = "0 5";
144            "col.shadow" = "rgba(00000099)";
145          };
146
147          "$mod" = "SUPER";
148
149          bindm = [
150            # mouse movements
151            "$mod, mouse:272, movewindow"
152            "$mod, mouse:273, resizewindow"
153            "$mod ALT, mouse:272, resizewindow"
154          ];
155        }
156      '';
157    };
158
159    extraConfig = lib.mkOption {
160      type = lib.types.lines;
161      default = "";
162      example = ''
163        # window resize
164        bind = $mod, S, submap, resize
165
166        submap = resize
167        binde = , right, resizeactive, 10 0
168        binde = , left, resizeactive, -10 0
169        binde = , up, resizeactive, 0 -10
170        binde = , down, resizeactive, 0 10
171        bind = , escape, submap, reset
172        submap = reset
173      '';
174      description = ''
175        Extra configuration lines to add to `~/.config/hypr/hyprland.conf`.
176      '';
177    };
178  };
179
180  config = lib.mkIf cfg.enable {
181    assertions = [
182      (lib.hm.assertions.assertPlatform "wayland.windowManager.hyprland" pkgs
183        lib.platforms.linux)
184    ];
185
186    warnings = let
187      inconsistent = (cfg.systemd.enable || cfg.plugins != [ ])
188        && cfg.extraConfig == "" && cfg.settings == { };
189      warning =
190        "You have enabled hyprland.systemd.enable or listed plugins in hyprland.plugins but do not have any configuration in hyprland.settings or hyprland.extraConfig. This is almost certainly a mistake.";
191    in lib.optional inconsistent warning;
192
193    home.packages = lib.optional (cfg.package != null) cfg.finalPackage;
194
195    xdg.configFile."hypr/hyprland.conf" = let
196      combinedSettings = cfg.settings // {
197        plugin = let
198          mkEntry = entry:
199            if lib.types.package.check entry then
200              "${entry}/lib/lib${entry.pname}.so"
201            else
202              entry;
203        in map mkEntry cfg.plugins;
204      };
205
206      shouldGenerate = cfg.systemd.enable || cfg.extraConfig != ""
207        || combinedSettings != { };
208
209      toHyprconf = with lib;
210        attrs: indentLevel:
211        let
212          indent = concatStrings (replicate indentLevel "  ");
213
214          mkSection = n: attrs: ''
215            ${indent}${n} {
216            ${toHyprconf attrs (indentLevel + 1)}${indent}}
217          '';
218          sections = filterAttrs (n: v: isAttrs v) attrs;
219
220          mkFields = generators.toKeyValue {
221            listsAsDuplicateKeys = true;
222            inherit indent;
223          };
224          allFields = filterAttrs (n: v: !(isAttrs v)) attrs;
225          importantFields =
226            filterAttrs (n: _: (hasPrefix "$" n) || (hasPrefix "bezier" n))
227            allFields;
228          fields = builtins.removeAttrs allFields
229            (mapAttrsToList (n: _: n) importantFields);
230        in mkFields importantFields
231        + concatStringsSep "\n" (mapAttrsToList mkSection sections)
232        + mkFields fields;
233    in lib.mkIf shouldGenerate {
234      text = lib.optionalString cfg.systemd.enable systemdActivation
235        + lib.optionalString (combinedSettings != { })
236        (toHyprconf combinedSettings 0)
237        + lib.optionalString (cfg.extraConfig != "") cfg.extraConfig;
238
239      onChange = lib.mkIf (cfg.package != null) ''
240        (  # execute in subshell so that `shopt` won't affect other scripts
241          shopt -s nullglob  # so that nothing is done if /tmp/hypr/ does not exist or is empty
242          for instance in /tmp/hypr/*; do
243            HYPRLAND_INSTANCE_SIGNATURE=''${instance##*/} ${cfg.finalPackage}/bin/hyprctl reload config-only \
244              || true  # ignore dead instance(s)
245          done
246        )
247      '';
248    };
249
250    systemd.user.targets.hyprland-session = lib.mkIf cfg.systemd.enable {
251      Unit = {
252        Description = "Hyprland compositor session";
253        Documentation = [ "man:systemd.special(7)" ];
254        BindsTo = [ "graphical-session.target" ];
255        Wants = [ "graphical-session-pre.target" ];
256        After = [ "graphical-session-pre.target" ];
257      };
258    };
259
260    systemd.user.targets.tray = {
261      Unit = {
262        Description = "Home Manager System Tray";
263        Requires = [ "graphical-session-pre.target" ];
264      };
265    };
266  };
267}