summary refs log tree commit diff
path: root/nixos/modules/programs/yabar.nix
blob: a01083c3ace980f97eb9663362ad2476977eeee8 (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{ lib, pkgs, config, ... }:

with lib;

let
  cfg = config.programs.yabar;

  mapExtra = v: lib.concatStringsSep "\n" (mapAttrsToList (
    key: val: "${key} = ${if (isString val) then "\"${val}\"" else "${builtins.toString val}"};"
  ) v);

  listKeys = r: concatStringsSep "," (map (n: "\"${n}\"") (attrNames r));

  configFile = let
    bars = mapAttrsToList (
      name: cfg: ''
        ${name}: {
          font: "${cfg.font}";
          position: "${cfg.position}";

          ${mapExtra cfg.extra}

          block-list: [${listKeys cfg.indicators}]

          ${concatStringsSep "\n" (mapAttrsToList (
            name: cfg: ''
              ${name}: {
                exec: "${cfg.exec}";
                align: "${cfg.align}";
                ${mapExtra cfg.extra}
              };
            ''
          ) cfg.indicators)}
        };
      ''
    ) cfg.bars;
  in pkgs.writeText "yabar.conf" ''
    bar-list = [${listKeys cfg.bars}];
    ${concatStringsSep "\n" bars}
  '';
in
  {
    options.programs.yabar = {
      enable = mkEnableOption "yabar";

      package = mkOption {
        default = pkgs.yabar;
        example = literalExample "pkgs.yabar-unstable";
        type = types.package;

        description = ''
          The package which contains the `yabar` binary.

          Nixpkgs provides the `yabar` and `yabar-unstable`
          derivations since 18.03, so it's possible to choose.
        '';
      };

      bars = mkOption {
        default = {};
        type = types.attrsOf(types.submodule {
          options = {
            font = mkOption {
              default = "sans bold 9";
              example = "Droid Sans, FontAwesome Bold 9";
              type = types.string;

              description = ''
                The font that will be used to draw the status bar.
              '';
            };

            position = mkOption {
              default = "top";
              example = "bottom";
              type = types.enum [ "top" "bottom" ];

              description = ''
                The position where the bar will be rendered.
              '';
            };

            extra = mkOption {
              default = {};
              type = types.attrsOf types.string;

              description = ''
                An attribute set which contains further attributes of a bar.
              '';
            };

            indicators = mkOption {
              default = {};
              type = types.attrsOf(types.submodule {
                options.exec = mkOption {
                  example = "YABAR_DATE";
                  type = types.string;
                  description = ''
                     The type of the indicator to be executed.
                  '';
                };

                options.align = mkOption {
                  default = "left";
                  example = "right";
                  type = types.enum [ "left" "center" "right" ];

                  description = ''
                    Whether to align the indicator at the left or right of the bar.
                  '';
                };

                options.extra = mkOption {
                  default = {};
                  type = types.attrsOf (types.either types.string types.int);

                  description = ''
                    An attribute set which contains further attributes of a indicator.
                  '';
                };
              });

              description = ''
                Indicators that should be rendered by yabar.
              '';
            };
          };
        });

        description = ''
          List of bars that should be rendered by yabar.
        '';
      };
    };

    config = mkIf cfg.enable {
      systemd.user.services.yabar = {
        description = "yabar service";
        wantedBy = [ "graphical-session.target" ];
        partOf = [ "graphical-session.target" ];

        script = ''
          ${cfg.package}/bin/yabar -c ${configFile}
        '';

        serviceConfig.Restart = "always";
      };
    };
  }