{
  pkgs,
  lib,
  config,
  ...
}:
{
  imports = [ ./bar ];

  environment.systemPackages = with pkgs; [
    rmenu
    grim
    slurp
    brightnessctl
    wl-clipboard
    swaymux
    qt6ct
    swaynotificationcenter
    searchclip
    ydotool
    networkmanagerapplet
  ];

  grimmShared.sway = {
    enable = true;
    config =
      let
        inherit (lib) getExe getExe' concatLines;
        inherit (pkgs)
          rmenu
          xdg-terminal-exec
          slurp
          swaymux
          grim
          brightnessctl
          searchclip
          ranger
          deskwhich
          ;
      in
      {
        definitions = {
          mod = "Mod4";
          left = "h";
          down = "j";
          up = "k";
          right = "l";
          menu = "${getExe rmenu} -r drun | xargs swaymsg exec --";
          menu_run = "${getExe rmenu} -r run | xargs swaymsg exec --";

          primecol = "#8800FF";
          accentcol = "#5700a0";
          splitcol = "#69507f";
          actsplitcol = "#cd97fc";
          darkcol = "#202020";
          urgentcol = "#9e3c3c";
          realwhite = "#C7D3E3";
        };
        keybinds =
          {
            "$mod+d" = "exec $menu";
            "$mod+Shift+d" = "exec $menu_run";
            "$mod+Shift+s" = ''exec ${getExe grim} -g "$(${getExe slurp} -d)" - | wl-copy'';
            "$mod+Shift+Return" = "exec ${getExe xdg-terminal-exec} xonsh";
            "$mod+Return" = "exec ${getExe xdg-terminal-exec}";
            "$mod+Shift+q" = "kill";
            "$mod+Shift+c" = "reload";
            "$mod+Shift+e" =
              "exec swaynag -t warning -m 'You pressed the exit shortcut. Do you really want to exit sway? This will end your Wayland session.' -B 'Yes, exit sway' 'swaymsg exit'";

            # Move your focus around
            "$mod+$left" = "focus left";
            "$mod+$down" = "focus down";
            "$mod+$up" = "focus up";
            "$mod+$right" = "focus right";
            # Or use $mod+[up|down|left|right]
            "$mod+Left" = "focus left";
            "$mod+Down" = "focus down";
            "$mod+Up" = "focus up";
            "$mod+Right" = "focus right";

            # Move the focused window with the same, but add Shift
            "$mod+Shift+$left" = "move left";
            "$mod+Shift+$down" = "move down";
            "$mod+Shift+$up" = "move up";
            "$mod+Shift+$right" = "move right";
            # Ditto, with arrow keys
            "$mod+Shift+Left" = "move left";
            "$mod+Shift+Down" = "move down";
            "$mod+Shift+Up" = "move up";
            "$mod+Shift+Right" = "move right";

            # Layout stuff:
            #
            # You can "split" the current object of your focus with
            # $mod+b or $mod+v, for horizontal and vertical splits
            # respectively.
            "$mod+b" = "splith";
            "$mod+v" = "splitv";

            # Switch the current container between different layout styles
            "$mod+s" = "layout stacking";
            "$mod+w" = "layout tabbed";
            "$mod+e" = "layout toggle split";

            # Make the current focus fullscreen
            "$mod+f" = "fullscreen";

            # Toggle the current focus between tiling and floating mode
            "$mod+Shift+space" = "floating toggle";

            # Swap focus between the tiling area and the floating area
            "$mod+space" = "focus mode_toggle";

            # Move focus to the parent container
            "$mod+a" = "focus parent";

            "$mod+Shift+minus" = "move scratchpad";
            "$mod+minus" = "scratchpad show";

            "$mod+r" = "mode \"resize\"";

            XF86AudioRaiseVolume = "exec pactl set-sink-volume @DEFAULT_SINK@ +5%";
            XF86AudioLowerVolume = "exec pactl set-sink-volume @DEFAULT_SINK@ -5%";
            "Shift+XF86AudioLowerVolume" = "exec pactl set-source-volume @DEFAULT_SOURCE@ -5%";
            "Shift+XF86AudioRaiseVolume" = "exec pactl set-source-volume @DEFAULT_SOURCE@ +5%";
            XF86AudioMute = "exec pactl set-sink-mute @DEFAULT_SINK@ toggle";
            XF86AudioPlay = "exec playerctl play-pause";
            XF86AudioNext = "exec playerctl next";
            XF86AudioPrev = "exec playerctl previous";
            "$mod+c" = "exec ${getExe swaymux}";
            XF86MonBrightnessUp = "exec ${getExe brightnessctl} s 10+%";
            XF86MonBrightnessDown = "exec ${getExe brightnessctl} s 10-%";
            XF86Explorer = "exec ${getExe xdg-terminal-exec} ${getExe ranger}";
            XF86Search = "exec ${getExe searchclip}";
            XF86HomePage =
              let
                open = pkgs.writeShellScriptBin "open_or_switch_browser" ''
                  browser=$(xdg-settings get default-web-browser | sed "s/\.desktop//")
                  swaymsg [app_id="$browser"] focus || ${getExe deskwhich} $browser | xargs gio launch
                '';
              in
              "exec ${getExe open}";
            XF86Tools =
              let
                open = pkgs.writeShellScriptBin "open_or_switch_spotify" ''
                  # FIXME: spotify is being weird
                  while IFS= read -r pid; do
                    swaymsg [pid=$pid] focus && exit 0
                  done <<< $(pgrep spotify -u "$(whoami)")
                  ${getExe deskwhich} spotify | xargs gio launch
                '';
              in
              "exec ${getExe open}"; # for some reason tools = audio media on my keyboard??
            XF86Mail =
              let
                open = pkgs.writeShellScriptBin "open_or_switch_mail" ''
                  desk=$(xdg-settings get default-url-scheme-handler mailto | sed "s/\.desktop//")
                  swaymsg [app_id="$desk"] focus || ${getExe deskwhich} $desk | xargs gio launch
                '';
              in
              "exec ${getExe open}";
            # XF86Bluetooth = "exec blueman-manager";
          }
          // (
            let
              inherit (builtins) toString;
            in
            lib.mergeAttrsList (
              map (n: {
                "$mod+${toString n}" = "workspace number ${toString n}";
                "$mod+Shift+${toString n}" = "move container to workspace number ${toString n}";
              }) (lib.range 0 9)
            )
          );
        autolaunch =
          let
            aw-modules = with pkgs; [
              aw-server-rust
              aw-watcher-window-wayland
            ];
            aw-modules-list = lib.concatStringsSep "," (map (p: p.meta.mainProgram) aw-modules);
            aw-bundle = (
              pkgs.writeShellScriptBin "aw-bundle" ''
                export RUST_BACKTRACE=full
                export PATH=$PATH:${lib.makeBinPath (aw-modules ++ [ pkgs.coreutils-full ])}
                ${getExe' pkgs.coreutils-full "sleep"} 5
                ${getExe pkgs.aw-qt} --autostart-modules ${aw-modules-list}
              ''
            );
          in
          [
            (getExe' pkgs.dbus "dbus-update-activation-environment")
            (getExe' pkgs.xdg-user-dirs "xdg-user-dirs-update")
            ''${getExe' pkgs.coreutils-full "sleep"} 3 && ${getExe' pkgs.blueman "blueman-applet"}''
            (getExe' pkgs.lxqt.lxqt-policykit "lxqt-policykit-agent")
            (getExe' config.hardware.opentabletdriver.package "otd-daemon")
            pkgs.swaynotificationcenter
            pkgs.networkmanagerapplet
            #            aw-bundle
            # (pkgs.writeShellScriptBin "rmenu-cache-clear" "rm -r $HOME/.cache/rmenu") # invalidate rmenu cache on sway restart
          ];
        extraConfig = ''
          output * bg ${./wallpapers/switzerland.jpg} fill

          floating_modifier $mod normal

          input type:keyboard xkb_numlock enabled
          include /etc/sway/config.d/*

          # Borders, gaps, titlebars, behavior
          default_border pixel 3
          default_floating_border pixel 3
          gaps inner 5
          titlebar_padding 5 5

          #5Smart things
          smart_gaps on
          hide_edge_borders --i3 smart


          input * {
            xkb_layout "de"
            dwt disabled
          }

          for_window [app_id="swaymux"] floating enable
          for_window [app_id="rmenu"] floating enable
          # for_window [app_id="firefox.*" title="Picture-in-Picture"] floating enable
          for_window [app_id="lxqt-policykit-agent"] floating enable
          for_window [title="OpenSSH Authentication Passphrase request"] floating enable
        '';

        modes.resize.keybinds = {
          "$left" = "resize shrink width 10px";
          "$down" = "resize grow height 10px";
          "$up" = "resize shrink height 10px";
          "$right" = "resize grow width 10px";

          Left = "resize shrink width 10px";
          Down = "resize grow height 10px";
          Up = "resize shrink height 10px";
          Right = "resize grow width 10px";

          Return = "mode \"default\"";
          Escape = "mode \"default\"";
        };
      };
  };
}