{
  config,
  pkgs,
  lib,
  theme,
  ...
}: {
  imports = [../common.nix ./kitty.nix ./polybar.nix];

  home.packages = with pkgs; [bemenu transmission xwinwrap xclip];

  xsession.windowManager.i3 = {
    enable = true;
    # Workaround for hiding the titlebar
    # Ref: https://github.com/i3/i3/issues/5149
    extraConfig = ''
      for_window [all] default_border pixel 2
      for_window [all] border pixel 2
    '';
    config = {
      terminal = "alacritty";
      defaultWorkspace = "workspace number 1";
      modifier = "Mod4";
      startup = [
        # Hack around the fact that I'm not using a DM
        # TODO: Have systemd manage those
        {command = "dunst";}
        {command = "flameshot";}
        {command = "picom --config ${config.xdg.configFile."picom/picom.conf".source}";}
        {command = "xwinwrap -b -s -fs -st -sp -nf -ov -d 'desktop' -- mpv --scale=nearest --loop-file=inf -wid WID --panscan=1 --no-audio --no-osc --no-osd-bar ${../wall.gif}";}
        {command = "polybar main";}
        {command = "polybar second";}
      ];
      # TODO: Switch to rofi eventually
      menu = lib.concatStringsSep " " [
        "bemenu-run"
        "--ignorecase"
        "--wrap"
        # "--fn 'Cozette 11'"
        "--fn 'Iosevka 11'"
        "--center"
        "--list 10"
        "--width-factor 0.4"
        "--line-height 22"
        "--nb '${theme.dark.black}'" # Normal background
        "--nf '${theme.bright.black}'" # Normal foreground
        "--hb '${theme.dark.black}'" # Highlight background
        "--hf '${theme.bright.white}'" # Highlight foreground
        "--tb '${theme.dark.black}'" # Title background
        "--tf '${theme.bright.blue}'" # Title foreground
        "--fb '${theme.dark.black}'" # Filter background
        "--ff '${theme.bright.white}'" # Filter foreground
        "--prompt 'Run:'"
      ];
      gaps = {
        smartGaps = true;
        smartBorders = "no_gaps";
        inner = 10;
      };
      workspaceAutoBackAndForth = true;
      workspaceOutputAssign =
        builtins.genList
        (idx: {
          workspace = toString (idx + 1);
          output = "DP-0";
        })
        5
        ++ builtins.genList
        (idx: {
          workspace = toString (
            if idx == 4
            then 0
            else idx + 6
          );
          output = "eDP-1-1";
        })
        5;
      bars = [];

      colors = {
        focused = rec {
          border = theme.bright.white;
          background = theme.dark.black;
          text = theme.bright.white;
          indicator = theme.bright.white;
          childBorder = border;
        };
        focusedInactive = rec {
          border = theme.bright.black;
          background = theme.dark.black;
          text = theme.bright.white;
          indicator = theme.bright.white;
          childBorder = border;
        };
        unfocused = rec {
          border = theme.dark.black;
          background = theme.dark.black;
          text = theme.bright.white;
          indicator = theme.bright.white;
          childBorder = border;
        };
        urgent = rec {
          border = theme.bright.red;
          background = theme.dark.black;
          text = theme.bright.white;
          indicator = theme.bright.white;
          childBorder = border;
        };
      };

      keybindings = let
        cfg = config.xsession.windowManager.i3.config;
        mod = cfg.modifier;

        bindings = with builtins; let
          bindStr = bind: modifier: "${mod}${lib.optionalString (modifier != "") ("+" + modifier)}+${bind}";
          bindToActions = actions: bind: let
            bs = bindStr bind;
          in
            if isString actions
            then [
              {
                name = bs "";
                value = actions;
              }
            ]
            else
              builtins.map (modifier: {
                name = bs modifier;
                value = builtins.getAttr modifier actions;
              }) (builtins.attrNames actions);

          binds = {
            "Return" = "exec ${cfg.terminal}";

            "d" = {
              "" = "exec ${cfg.menu}";
              "Shift" = "kill";
            };

            "Shift+s" = "exec flameshot gui";
            "Shift+0" = "exec i3-nagbar -t warning -m 'Do you want to exit i3?' -b 'Yes' 'i3-msg exit'";

            "v" = "split v";
            "b" = "split h";

            # Movement
            "h,Left" = {
              "" = "focus left";
              "Shift" = "move left";
            };
            "l,Right" = {
              "" = "focus right";
              "Shift" = "move right";
            };
            "j,Down" = {
              "" = "focus down";
              "Shift" = "move down";
            };
            "k,Up" = {
              "" = "focus up";
              "Shift" = "move up";
            };

            "space" = {
              "" = "focus mode_toggle";
              "Shift" = "floating toggle";
            };

            "f" = "fullscreen toggle";

            # Workspaces
            "1" = {
              "" = "workspace number 1";
              "Shift" = "move container to workspace number 1";
            };
            "2" = {
              "" = "workspace number 2";
              "Shift" = "move container to workspace number 2";
            };
            "3" = {
              "" = "workspace number 3";
              "Shift" = "move container to workspace number 3";
            };
            "4" = {
              "" = "workspace number 4";
              "Shift" = "move container to workspace number 4";
            };
            "5" = {
              "" = "workspace number 5";
              "Shift" = "move container to workspace number 5";
            };
            "q" = {
              "" = "workspace number 6";
              "Shift" = "move container to workspace number 6";
            };
            "w" = {
              "" = "workspace number 7";
              "Shift" = "move container to workspace number 7";
            };
            "e" = {
              "" = "workspace number 8";
              "Shift" = "move container to workspace number 8";
            };
            "r" = {
              "" = "workspace number 9";
              "Shift" = "move container to workspace number 9";
            };
            "t" = {
              "" = "workspace number 0";
              "Shift" = "move container to workspace number 0";
            };
          };
        in
          listToAttrs (concatMap (bind: concatMap (bindToActions (getAttr bind binds)) (lib.splitString "," bind)) (attrNames binds));
      in
        lib.mkForce bindings;
    };
  };

  services.picom.enable = true;

  # TODO: use autorandr instead of hardcoded `xrandr`
  home.file.".xinitrc".text = ''
    xrandr --setprovideroutputsource modesetting NVIDIA-0
    # xrandr --auto --output DP-0 --primary --output eDP-1-1 --mode 1920x1080 --pos 3440x237
    xrandr --auto --output DP-0 --primary --output eDP-1-1 --mode 1920x1080 --left-of DP-0
    exec i3
  '';

  programs.helix.settings.keys.normal = {
    space.c = ":pipe /home/niko/code/color-show/target/debug/cs";
  };
}