aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/services/window-managers/bspwm/default.nix
blob: 9ea5adbc88055d524c35828e6c7ea7cdd2f306e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.xsession.windowManager.bspwm;
  bspwm = cfg.package;

  camelToSnake = s:
    builtins.replaceStrings lib.upperChars (map (c: "_${c}") lib.lowerChars) s;

  formatConfig = n: v:
    let
      formatList = x:
        if isList x then
          throw "can not convert 2-dimensional lists to bspwm format"
        else
          formatValue x;

      formatValue = v:
        if isBool v then
          (if v then "true" else "false")
        else if isList v then
          concatMapStringsSep ", " formatList v
        else if isString v then
          "${lib.strings.escapeShellArg v}"
        else
          toString v;
    in "bspc config ${n} ${formatValue v}";

  formatMonitors = n: v: "bspc monitor ${n} -d ${concatStringsSep " " v}";

  formatRules = target: directiveOptions:
    let
      formatDirective = n: v:
        if isBool v then
          (if v then "${camelToSnake n}=on" else "${camelToSnake n}=off")
        else if (n == "desktop" || n == "node") then
          "${camelToSnake n}='${v}'"
        else
          "${camelToSnake n}=${lib.strings.escapeShellArg v}";

      directives =
        filterAttrs (n: v: v != null && !(lib.strings.hasPrefix "_" n))
        directiveOptions;
      directivesStr = builtins.concatStringsSep " "
        (mapAttrsToList formatDirective directives);
    in "bspc rule -a ${target} ${directivesStr}";

  formatStartupPrograms = map (s: "${s} &");

in {
  options = import ./options.nix {
    inherit pkgs;
    inherit lib;
  };

  config = mkIf cfg.enable {
    home.packages = [ bspwm ];
    xsession.windowManager.command = let
      configFile = pkgs.writeShellScript "bspwmrc" (concatStringsSep "\n"
        ((mapAttrsToList formatMonitors cfg.monitors)
          ++ (mapAttrsToList formatConfig cfg.settings)
          ++ (mapAttrsToList formatRules cfg.rules) ++ [''
            # java gui fixes
            export _JAVA_AWT_WM_NONREPARENTING=1
            bspc rule -a sun-awt-X11-XDialogPeer state=floating
          ''] ++ [ cfg.extraConfig ]
          ++ (formatStartupPrograms cfg.startupPrograms)));
      configCmdOpt = optionalString (cfg.settings != null) "-c ${configFile}";
    in "${cfg.package}/bin/bspwm ${configCmdOpt}";
  };
}