From e92cfa3b234a91fee8bea179f7e06f8a9016b7bd Mon Sep 17 00:00:00 2001
From: Oliver Davies <oliver@oliverdavies.dev>
Date: Fri, 22 Mar 2024 23:38:40 +0000
Subject: [PATCH] Switch to wayland and sway

---
 lib/nixos/configuration.nix                  |  35 ++--
 lib/nixos/default.nix                        |   2 +
 lib/nixos/home-manager/desktop.nix           |   5 +-
 lib/nixos/home-manager/modules/autorandr.nix |  52 ------
 lib/nixos/home-manager/modules/i3.nix        | 116 ------------
 lib/nixos/home-manager/modules/kanshi.nix    |  39 ++++
 lib/nixos/home-manager/modules/rofi.nix      |   1 -
 lib/nixos/home-manager/modules/sway.nix      | 184 +++++++++++++++++++
 lib/nixos/modules/sway.nix                   |  31 ++++
 9 files changed, 270 insertions(+), 195 deletions(-)
 delete mode 100644 lib/nixos/home-manager/modules/autorandr.nix
 delete mode 100644 lib/nixos/home-manager/modules/i3.nix
 create mode 100644 lib/nixos/home-manager/modules/kanshi.nix
 delete mode 100644 lib/nixos/home-manager/modules/rofi.nix
 create mode 100644 lib/nixos/home-manager/modules/sway.nix
 create mode 100644 lib/nixos/modules/sway.nix

diff --git a/lib/nixos/configuration.nix b/lib/nixos/configuration.nix
index 088a7f6a..409ca1f5 100644
--- a/lib/nixos/configuration.nix
+++ b/lib/nixos/configuration.nix
@@ -67,16 +67,6 @@ in {
       layout = "gb";
       variant = "";
     };
-
-    displayManager = {
-      defaultSession = "none+i3";
-      lightdm.enable = true;
-    };
-
-    windowManager.i3 = {
-      enable = true;
-      extraPackages = with pkgs; [ i3status i3lock i3blocks ];
-    };
   };
 
   # Configure console keymap
@@ -89,20 +79,19 @@ in {
 
   # Enable sound with pipewire.
   sound.enable = true;
-  hardware.pulseaudio.enable = true;
+  hardware.pulseaudio.enable = false;
   security.rtkit.enable = true;
-  # services.pipewire = {
-  #   enable = true;
-  #   alsa.enable = true;
-  #   alsa.support32Bit = true;
-  #   pulse.enable = true;
-  #   # If you want to use JACK applications, uncomment this
-  #   #jack.enable = true;
-  #
-  #   # use the example session manager (no others are packaged yet so this is enabled by default,
-  #   # no need to redefine it in your config for now)
-  #   #media-session.enable = true;
-  # };
+
+  services.pipewire = {
+    enable = true;
+
+    alsa = {
+      enable = true;
+      support32Bit = true;
+    };
+
+    pulse.enable = true;
+  };
 
   # Enable touchpad support (enabled default in most desktopManager).
   # services.xserver.libinput.enable = true;
diff --git a/lib/nixos/default.nix b/lib/nixos/default.nix
index 55283815..890a3dab 100644
--- a/lib/nixos/default.nix
+++ b/lib/nixos/default.nix
@@ -16,6 +16,8 @@ in inputs.nixpkgs.lib.nixosSystem {
       };
     }
 
+    (import ./modules/sway.nix { inherit inputs username; })
+
     configuration
     hardwareConfiguration
   ];
diff --git a/lib/nixos/home-manager/desktop.nix b/lib/nixos/home-manager/desktop.nix
index ee9b642f..d90d6a1d 100644
--- a/lib/nixos/home-manager/desktop.nix
+++ b/lib/nixos/home-manager/desktop.nix
@@ -1,13 +1,12 @@
 { config, inputs, pkgs, username, }: {
   imports = [
     ./modules/alacritty.nix
-    ./modules/autorandr.nix
     ./modules/copyq.nix
     ./modules/dunst.nix
     ./modules/espanso.nix
     ./modules/feh.nix
     ./modules/flameshot.nix
-    ./modules/i3.nix
-    ./modules/rofi.nix
+    ./modules/kanshi.nix
+    ./modules/sway.nix
   ];
 }
diff --git a/lib/nixos/home-manager/modules/autorandr.nix b/lib/nixos/home-manager/modules/autorandr.nix
deleted file mode 100644
index a057098b..00000000
--- a/lib/nixos/home-manager/modules/autorandr.nix
+++ /dev/null
@@ -1,52 +0,0 @@
-{ username, ... }: {
-  services.autorandr.enable = true;
-
-  xdg.configFile."autorandr/postswitch" = {
-    executable = true;
-    text = ''
-      #!/usr/bin/env bash
-
-      feh --randomize --bg-scale /home/${username}/Pictures/Wallpaper/*;
-    '';
-  };
-
-  programs.autorandr = {
-    enable = true;
-
-    profiles = let
-      fingerprint =
-        "00ffffffffffff000daef21400000000161c0104a51f117802ee95a3544c99260f505400000001010101010101010101010101010101363680a0703820402e1e240035ad10000018000000fe004e3134304843472d4751320a20000000fe00434d4e0a202020202020202020000000fe004e3134304843472d4751320a2000bb";
-      name = "eDP-1";
-    in {
-      default = {
-        fingerprint = { "${name}" = fingerprint; };
-
-        config = {
-          "${name}" = {
-            enable = true;
-            mode = "1920x1080";
-          };
-        };
-      };
-
-      desktop = {
-        config = {
-          "${name}".enable = false;
-
-          "HDMI-1" = {
-            enable = true;
-            mode = "2560x1440";
-            primary = true;
-            rate = "59.95";
-          };
-        };
-
-        fingerprint = {
-          "${name}" = fingerprint;
-          "HDMI-1" =
-            "00ffffffffffff004c2d1710424e58432b1f0103803f24782ac8b5ad50449e250f5054bfef80714f810081c081809500a9c0b300010108e80030f2705a80b0588a0078682100001e000000fd00324b1e873c000a202020202020000000fc004c5532385235350a2020202020000000ff0048345a524130303132380a20200183020335f04961120313041f10605f2309070783010000e305c0006b030c002000b83c2000200167d85dc401788003e20f81e3060501023a801871382d40582c450078682100001e023a80d072382d40102c458078682100001e04740030f2705a80b0588a0078682100001e565e00a0a0a029503020350078682100001a000049";
-        };
-      };
-    };
-  };
-}
diff --git a/lib/nixos/home-manager/modules/i3.nix b/lib/nixos/home-manager/modules/i3.nix
deleted file mode 100644
index 78b767c6..00000000
--- a/lib/nixos/home-manager/modules/i3.nix
+++ /dev/null
@@ -1,116 +0,0 @@
-{ config, inputs, pkgs, self, username, ... }:
-let
-  theme = import "${self}/lib/theme" { inherit pkgs; };
-
-  modifier = "Mod4";
-in {
-  xsession.windowManager.i3 = {
-    enable = true;
-
-    config = {
-      assigns = {
-        "7" = [{ class = "vlc"; }];
-        "8" = [{ class = "0ad"; }];
-        "9" = [ { class = "Slack"; } { class = "discord"; } ];
-      };
-
-      defaultWorkspace = "workspace number 1";
-
-      focus.followMouse = false;
-
-      modifier = modifier;
-
-      keybindings = inputs.nixpkgs.lib.mkOptionDefault {
-        "${modifier}+d" = "exec ${pkgs.rofi}/bin/rofi -show drun";
-
-        "${modifier}+Shift+b" = "exec ${pkgs.firefox}/bin/firefox";
-        "${modifier}+Shift+f" = "exec ${pkgs.xfce.thunar}/bin/thunar";
-
-        # Change focus.
-        "${modifier}+h" = "focus left";
-        "${modifier}+j" = "focus down";
-        "${modifier}+k" = "focus up";
-        "${modifier}+l" = "focus right";
-
-        # Move focused window.
-        "${modifier}+Shift+h" = "move left";
-        "${modifier}+Shift+j" = "move down";
-        "${modifier}+Shift+k" = "move up";
-        "${modifier}+Shift+l" = "move right";
-
-        "${modifier}+Shift+s" = "exec ${pkgs.flameshot}/bin/flameshot gui";
-        "${modifier}+Shift+p" = "exec ${pkgs.autorandr}/bin/autorandr --change";
-        "${modifier}+Shift+y" = "exec ${pkgs.copyq}/bin/copyq toggle";
-
-        "XF86AudioRaiseVolume" = "exec pamixer -ui 2 && pamixer --get-volume";
-        "XF86AudioLowerVolume" = "exec pamixer -ud 2 && pamixer --get-volume";
-        "XF86AudioMute" = ''
-          exec pamixer --toggle-mute && ( [ "$(pamixer --get-mute)" = "true" ] && echo 0'';
-
-        "XF86MonBrightnessDown" =
-          "exec brightnessctl set 5%- | sed -En 's/.*(([0-9]+)%).*/1/p'";
-        "XF86MonBrightnessUp" =
-          "exec brightnessctl set +5% | sed -En 's/.*(([0-9]+)%).*/1/p'";
-      };
-
-      terminal = "alacritty";
-
-      window = {
-        border = 0;
-        hideEdgeBorders = "none";
-      };
-    };
-
-    extraConfig = ''
-      set $laptop eDP-1
-      bindswitch --reload --locked lid:on output $laptop disable
-      bindswitch --reload --locked lid:off output $laptop enable
-
-      exec_always --no-startup-id autotiling
-
-      exec --no-startup-id ${pkgs.feh}/bin/feh --randomize --bg-scale /home/${username}/Pictures/Wallpaper/*;
-
-      default_border none
-      default_floating_border none
-      smart_borders on
-      smart_gaps on
-
-      for_window [class="zoom"] floating enable
-      # xprop | grep -i class
-    '';
-
-    config = {
-      bars = [{
-        position = "bottom";
-        statusCommand = "${pkgs.i3status}/bin/i3status";
-      }];
-
-      fonts.names = [ "${theme.fonts.monospace.name}" ];
-
-      gaps = {
-        smartBorders = "on";
-        smartGaps = true;
-      };
-    };
-  };
-
-  programs.i3status = {
-    enable = true;
-
-    general = {
-      colors = true;
-
-      color_bad = "#f7768e";
-      color_degraded = "#ff9e64";
-      color_good = "#c0caf5";
-    };
-
-    modules = {
-      cpu_temperature.enable = false;
-      ipv6.enable = false;
-      load.enable = false;
-    };
-  };
-
-  home.packages = with pkgs; [ autotiling ];
-}
diff --git a/lib/nixos/home-manager/modules/kanshi.nix b/lib/nixos/home-manager/modules/kanshi.nix
new file mode 100644
index 00000000..424bc6d5
--- /dev/null
+++ b/lib/nixos/home-manager/modules/kanshi.nix
@@ -0,0 +1,39 @@
+{
+  systemd.user.services.kanshi = {
+    serviceConfig = {
+      StartLimitBurst = 5;
+      StartLimitIntervalSec = 30;
+    };
+  };
+
+  services.kanshi = {
+    enable = true;
+
+    # swaymsg -t get_outputs
+    profiles = {
+      undocked = {
+        outputs = [{
+          criteria = "eDP-1";
+          status = "enable";
+          mode = "1920x1080";
+          position = "0,0";
+        }];
+      };
+
+      docked = {
+        outputs = [
+          {
+            criteria = "eDP-1";
+            status = "disable";
+          }
+
+          {
+            criteria = "Samsung Electric Company LU28R55 H4ZRA00128";
+            mode = "2560x1440";
+            position = "0,0";
+          }
+        ];
+      };
+    };
+  };
+}
diff --git a/lib/nixos/home-manager/modules/rofi.nix b/lib/nixos/home-manager/modules/rofi.nix
deleted file mode 100644
index dd34f5e9..00000000
--- a/lib/nixos/home-manager/modules/rofi.nix
+++ /dev/null
@@ -1 +0,0 @@
-{ programs.rofi.enable = true; }
diff --git a/lib/nixos/home-manager/modules/sway.nix b/lib/nixos/home-manager/modules/sway.nix
new file mode 100644
index 00000000..e89a570e
--- /dev/null
+++ b/lib/nixos/home-manager/modules/sway.nix
@@ -0,0 +1,184 @@
+{ config, inputs, pkgs, ... }:
+
+{
+  # services.swayidle = {
+  #   enable = true;
+  #
+  #   events = [
+  #     {
+  #       event = "before-sleep";
+  #       command = "${pkgs.swaylock}/bin/swaylock --daemonize --image ~/.config/wallpape
+  #       /wallpaper.jpg";
+  #     }
+  #   ];
+  # };
+
+  # services.swayidle.timeouts = [
+  #   {
+  #     timeout = 300;
+  #     command = "${pkgs.swaylock}/bin/swaylock --daemonize --image ~/.config/wallpaper/wallpaper.jpg";
+  #   }
+  #
+  #   {
+  #     timeout = 600;
+  #     command = "swaymsg 'output * dpms off'";
+  #     resumeCommand = "swaymsg 'output * dpms on'";
+  #   }
+  # ];
+
+  # programs.swaylock = {
+  #   enable = true;
+  #
+  #   settings = {
+  #     daemonize = true;
+  #     image = "~/.config/wallpaper/wallpaper.jpg";
+  #   };
+  # };
+
+  programs.i3status-rust = {
+    enable = true;
+
+    bars = {
+      default = {
+        blocks = [
+          {
+            block = "net";
+            format = " $ip ";
+          }
+          {
+            block = "battery";
+            format = " $icon $percentage $time ";
+          }
+          { block = "cpu"; }
+          {
+            block = "memory";
+            format = " $icon $mem_total_used_percents.eng(w:2) ";
+            format_alt = " $icon_swap $swap_used_percents.eng(w:2) ";
+          }
+          {
+            block = "disk_space";
+            path = "/";
+            info_type = "available";
+            alert_unit = "GB";
+            interval = 20;
+            warning = 20.0;
+            alert = 10.0;
+            format = " $icon $available.eng(w:2) ";
+          }
+          {
+            block = "time";
+            interval = 1;
+            format = " $timestamp.datetime(f:'%F %T') ";
+          }
+        ];
+
+        settings = {
+          theme = {
+            theme = "plain";
+            overrides = {
+              critical_bg = "#222222";
+              good_bg = "#222222";
+              good_fg = "#ffffff";
+              idle_bg = "#222222";
+              idle_fg = "#ffffff";
+              info_bg = "#222222";
+              info_fg = "#ffffff";
+              separator_bg = "#222222";
+              warning_bg = "#222222";
+              warning_fg = "#ffffff";
+            };
+          };
+        };
+      };
+    };
+  };
+
+  wayland.windowManager.sway = {
+    enable = true;
+    xwayland = true;
+
+    config = {
+      assigns = {
+        "1" = [{ app_id = "firefox"; }];
+        "2" = [{ app_id = "Alacritty"; }];
+        "3" = [{ class = "vlc"; }];
+        "9" = [ { class = "Slack"; } { class = "discord"; } ];
+        # Get app_id with swaymsg -t get_tree
+      };
+
+      bars = [{
+        colors.background = "#222222";
+
+        fonts = {
+          names = [ "JetBrainsMono Nerd Font Mono" ];
+          size = 12.0;
+        };
+
+        statusCommand =
+          "i3status-rs ~/.config/i3status-rust/config-default.toml";
+        trayPadding = 5;
+      }];
+
+      gaps = {
+        inner = 0;
+        smartBorders = "on";
+        # smartGaps = true;
+      };
+
+      input = {
+        "*" = { xkb_layout = "gb"; };
+
+        "type:touchpad" = {
+          dwt = "enabled";
+          tap = "enabled";
+        };
+      };
+
+      keybindings =
+        let modifier = config.wayland.windowManager.sway.config.modifier;
+        in inputs.nixpkgs.lib.mkOptionDefault {
+          "${modifier}+Escape" = "exec swaylock --daemonize";
+          "${modifier}+Shift+b" = "exec ${pkgs.firefox}/bin/firefox";
+          "${modifier}+Shift+s" = "exec ${pkgs.flameshot}/bin/flameshot gui";
+          "${modifier}+Shift+y" = "exec ${pkgs.copyq}/bin/copyq toggle";
+        };
+
+      menu = "wofi --show run";
+      modifier = "Mod4";
+
+      output = {
+        # "*" = {
+        #   bg = "~/.config/wallpaper/wallpaper.jpg fill";
+        # };
+
+        eDP-1 = { scale = "1.0"; };
+      };
+
+      terminal = "alacritty";
+
+      window.border = 3;
+    };
+
+    extraConfig = ''
+      set $laptop eDP-1
+      bindswitch --reload --locked lid:on output $laptop disable
+      bindswitch --reload --locked lid:off output $laptop enable
+
+      set $WOBSOCK $XDG_RUNTIME_DIR/wob.sock
+      exec rm -f $WOBSOCK && mkfifo $WOBSOCK && tail -f $WOBSOCK | wob
+
+      bindsym XF86AudioRaiseVolume exec pamixer -ui 2 && pamixer --get-volume > $WOBSOCK
+      bindsym XF86AudioLowerVolume exec pamixer -ud 2 && pamixer --get-volume > $WOBSOCK
+      bindsym XF86AudioMute exec pamixer --toggle-mute && ( [ "$(pamixer --get-mute)" = "true" ] && echo 0 > $WOBSOCK ) || pamixer --get-volume > $WOBSOCK
+
+      # TODO: light?
+      bindsym XF86MonBrightnessDown exec brightnessctl set 5%- | sed -En 's/.*\(([0-9]+)%\).*/\1/p' > $WOBSOCK
+      bindsym XF86MonBrightnessUp exec brightnessctl set +5% | sed -En 's/.*\(([0-9]+)%\).*/\1/p' > $WOBSOCK
+
+      for_window [class="vlc"] inhibit_idle fullscreen
+
+      for_window [app_id="flameshot"] floating enable
+      for_window [class="zoom"] floating enable
+    '';
+  };
+}
diff --git a/lib/nixos/modules/sway.nix b/lib/nixos/modules/sway.nix
new file mode 100644
index 00000000..84e6bed9
--- /dev/null
+++ b/lib/nixos/modules/sway.nix
@@ -0,0 +1,31 @@
+{ inputs, username, ... }:
+{ pkgs, ... }: {
+  services = {
+    xserver = {
+      displayManager = {
+        defaultSession = "sway";
+        sddm.enable = true;
+      };
+    };
+  };
+
+  programs.sway.enable = true;
+
+  xdg.portal = {
+    enable = true;
+    extraPortals = with pkgs; [ xdg-desktop-portal-gtk xdg-desktop-portal-wlr ];
+  };
+
+  home-manager.users.${username} = { pkgs, ... }: {
+    home.sessionVariables = {
+      MOZ_ENABLE_WAYLAND = "1";
+      MOZ_USE_XINPUT2 = "1";
+      QT_QPA_PLATFORM = "wayland";
+      SDL_VIDEODRIVER = "wayland";
+      XDG_CURRENT_DESKTOP = "sway";
+      XDG_SESSION_TYPE = "wayland";
+    };
+
+    home.packages = with pkgs; [ i3status wl-clipboard wofi ];
+  };
+}