From 60b7339ce2762529fb417b51f1d0eb0b42360bf8 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Wed, 24 Aug 2022 23:54:30 +0300 Subject: [PATCH] nix/wm: add polybar instead of xmobar --- nix/home.nix | 11 +- nix/wm/bar/default.nix | 1 + nix/wm/bar/polybar/default.nix | 109 ++++++++++++++++++ nix/wm/default.nix | 4 +- nix/wm/xmonad/config.hs | 71 +++++++++--- nix/wm/xmonad/default.nix | 4 + programs/xmobar/src/xmobar.hs | 4 +- .../scripts => scripts}/exchangerate.sh | 0 .../xmobar/scripts => scripts}/get_volume.sh | 0 9 files changed, 179 insertions(+), 25 deletions(-) create mode 100644 nix/wm/bar/default.nix create mode 100644 nix/wm/bar/polybar/default.nix rename {programs/xmobar/scripts => scripts}/exchangerate.sh (100%) rename {programs/xmobar/scripts => scripts}/get_volume.sh (100%) diff --git a/nix/home.nix b/nix/home.nix index b3b390b..8f34713 100644 --- a/nix/home.nix +++ b/nix/home.nix @@ -42,7 +42,10 @@ in ]; # window manager - wm.xmonad.enable = true; + wm = { + xmonad.enable = true; + bar.polybar.enable = true; + }; # shell shell = { @@ -109,9 +112,9 @@ in }; }; - xsession.initExtra = " - xrandr --output HDMI-A-0 --rotate right --left-of DisplayPort-1 - "; + xsession.initExtra = '' + xrandr --output HDMI-A-0 --rotate right --left-of DisplayPort-1 + ''; xdg = { enable = true; diff --git a/nix/wm/bar/default.nix b/nix/wm/bar/default.nix new file mode 100644 index 0000000..f20a3e5 --- /dev/null +++ b/nix/wm/bar/default.nix @@ -0,0 +1 @@ +[ ./polybar ] diff --git a/nix/wm/bar/polybar/default.nix b/nix/wm/bar/polybar/default.nix new file mode 100644 index 0000000..a23036c --- /dev/null +++ b/nix/wm/bar/polybar/default.nix @@ -0,0 +1,109 @@ +{ lib, config, pkgs, ...}: + +with lib; + +let + cfg = config.wm.bar.polybar; + + colors = { + orange = "#ee9a00"; + }; +in +{ + options.wm.bar.polybar = { + enable = mkOption { + type = types.bool; + default = false; + description = "Enable polybar status bar"; + }; + }; + + config = mkIf cfg.enable { + services.polybar = { + enable = true; + script = "polybar &"; + settings = { + "bar/main" = { + monitor = "DisplayPort-1"; + width = "100%"; + height = "16px"; + font = [ + "Fira Code:size=9:antialias=true" + "Fira Code:bold:size=0:antialias=true" + ]; + radius = 0; + modules = { + left = "xmonad"; + center = "date wifi"; + right = "lang time"; + }; + }; + + "module/date" = { + type = "internal/date"; + interval = 10; + date = "%a %d %b %Y"; + label = "%date%"; + format.padding = 1; + }; + + "module/time" = { + type = "internal/date"; + time = "%H:%M:%S"; + label = { + text = "%time%"; + font = 2; + foreground = colors.orange; + }; + }; + + "module/lang" = { + type = "internal/xkeyboard"; + format = { + text = ""; + padding = 1; + }; + }; + + # TODO: fix amixer + "module/volume" = { + type = "custom/script"; + exec = "~/scripts/get_volume.sh"; + interval = 1; + format = { + prefix = "Vol: "; + padding = 1; + }; + }; + + "module/wifi" = { + type = "internal/network"; + interval = 3; + interface = { + text = "wlxd03745e1e87b"; + type = "wireless"; + }; + label.connected = "%essid% %signal%"; + format.connected = { + prefix = "| "; + suffix = "%"; + }; + }; + + "module/xmonad" = mkIf config.wm.xmonad.enable { + type = "custom/script"; + exec = "${pkgs.xmonad-log}/bin/xmonad-log"; + tail = true; + }; + + "global/wm" = { + margin = { + bottom = 0; + top = 0; + }; + + }; + }; + }; + }; +} diff --git a/nix/wm/default.nix b/nix/wm/default.nix index ce6e841..a675759 100644 --- a/nix/wm/default.nix +++ b/nix/wm/default.nix @@ -1,3 +1 @@ -[ - ./xmonad -] +[ ./xmonad ] ++ (import ./bar) diff --git a/nix/wm/xmonad/config.hs b/nix/wm/xmonad/config.hs index bee59c6..eae3161 100644 --- a/nix/wm/xmonad/config.hs +++ b/nix/wm/xmonad/config.hs @@ -29,6 +29,11 @@ import XMonad.Util.Run import qualified Data.Map as M import qualified XMonad.StackSet as W +-- Imports for Polybar +import qualified Codec.Binary.UTF8.String as UTF8 +import qualified DBus as D +import qualified DBus.Client as D + -- The preferred terminal program, which is used in a binding below and by -- certain contrib modules. -- @@ -62,27 +67,19 @@ myModMask = mod1Mask -- -- > workspaces = ["web", "irc", "code" ] ++ map show [4..9] -- -myWorkspaces = ["1:web", "2:code", "3:code", "4:code"] <> map show [5..6] <> ["7:fin", "8:cfg", "9:chat"] +myWorkspaces = ["web", "dev", "dev2", "dev3", "fin", "sys", "com"] -- Border colors for unfocused and focused windows, respectively. -- myNormalBorderColor = "#444" myFocusedBorderColor = "#f00" ------------------------------------------------------------------------- --- Now run xmonad with all the defaults we set up. +main :: IO () +main = mkDbusClient >>= main' --- Run xmonad with the settings you specify. No need to modify this. --- -main = xmonad . ewmhFullscreen . ewmh . xmobarProp $ defaults --- A structure containing your configuration settings, overriding --- fields in the default config. Any you don't override, will --- use the defaults defined in xmonad/XMonad/Config.hs --- --- No need to modify this. --- -defaults = def +main' :: D.Client -> IO () +main' dbus = xmonad . docks . ewmhFullscreen . ewmh $ def -- simple stuff { terminal = myTerminal , focusFollowsMouse = myFocusFollowsMouse @@ -99,10 +96,52 @@ defaults = def , layoutHook = myLayout , manageHook = myManageHook , handleEventHook = myEventHook - , logHook = myLogHook + , logHook = myPolybarLogHook dbus , startupHook = myStartupHook } + +------------------------------------------------------------------------ +-- Polybar settings (needs DBus client). +-- +mkDbusClient :: IO D.Client +mkDbusClient = do + dbus <- D.connectSession + D.requestName dbus (D.busName_ "org.xmonad.log") opts + return dbus + where + opts = [D.nameAllowReplacement, D.nameReplaceExisting, D.nameDoNotQueue] + +-- Emit a DBus signal on log updates +dbusOutput :: D.Client -> String -> IO () +dbusOutput dbus str = + let opath = D.objectPath_ "/org/xmonad/Log" + iname = D.interfaceName_ "org.xmonad.Log" + mname = D.memberName_ "Update" + signal = D.signal opath iname mname + body = [D.toVariant $ UTF8.decodeString str] + in D.emit dbus $ signal { D.signalBody = body } + +polybarHook :: D.Client -> PP +polybarHook dbus = + let wrapper c = wrap ("%{F" <> c <> "}") "%{F-}" + + blue = "#2E9AFE" + gray = "#7F7F7F" + orange = "#ea4300" + purple = "#9058c7" + red = "#722222" + in def { ppOutput = dbusOutput dbus + , ppCurrent = wrapper blue + , ppVisible = wrapper purple + , ppUrgent = wrapper orange + , ppHidden = wrapper gray + , ppHiddenNoWindows = wrapper red + , ppTitle = wrapper purple . shorten 90 + } + +myPolybarLogHook dbus = myLogHook <+> dynamicLogWithPP (polybarHook dbus) + ------------------------------------------------------------------------ -- Layouts: @@ -175,8 +214,8 @@ myManageHook = composeAll , className =? "Gnome-calculator" --> doFloat , className =? "Gnome-font-viewer" --> doFloat , className =? "Org.gnome.Nautilus" --> doFloat - , resource =? "telegram-desktop" --> doShift "9:chat" - , className =? "Thunderbird" --> doShift "9:chat" + , resource =? "telegram-desktop" --> doShift "com" + , className =? "Thunderbird" --> doShift "com" -- my libs , resource =? "hwt" --> doFloat ] diff --git a/nix/wm/xmonad/default.nix b/nix/wm/xmonad/default.nix index 8808390..8b86bae 100644 --- a/nix/wm/xmonad/default.nix +++ b/nix/wm/xmonad/default.nix @@ -28,6 +28,10 @@ in enable = true; enableContribAndExtras = true; config = ./config.hs; + extraPackages = mkIf config.wm.bar.polybar.enable (hp: [ + hp.dbus + hp.monad-logger + ]); }; }; }; diff --git a/programs/xmobar/src/xmobar.hs b/programs/xmobar/src/xmobar.hs index aab1d1f..e0b1395 100644 --- a/programs/xmobar/src/xmobar.hs +++ b/programs/xmobar/src/xmobar.hs @@ -83,8 +83,8 @@ myCommands = , "--normal" , colorYellow ] (10 `seconds`) - , Run $ ComX "/bin/bash" ["-c", "~/.config/xmobar/scripts/get_volume.sh"] "N/A" "volume" 3 - , Run $ ComX "/bin/bash" ["-c", "~/.config/xmobar/scripts/exchangerate.sh"] "N/A" "usdrub" (60 `seconds`) + , Run $ ComX "/bin/bash" ["-c", "~/scripts/get_volume.sh"] "N/A" "volume" 3 + , Run $ ComX "/bin/bash" ["-c", "~/scripts/exchangerate.sh"] "N/A" "usdrub" (60 `seconds`) , Run XMonadLog ] where diff --git a/programs/xmobar/scripts/exchangerate.sh b/scripts/exchangerate.sh similarity index 100% rename from programs/xmobar/scripts/exchangerate.sh rename to scripts/exchangerate.sh diff --git a/programs/xmobar/scripts/get_volume.sh b/scripts/get_volume.sh similarity index 100% rename from programs/xmobar/scripts/get_volume.sh rename to scripts/get_volume.sh