summaryrefslogtreecommitdiff
path: root/users/kassouni/palette/default.nix
blob: 7e9eec42361a73aaa98bdb60392a4f0ff5869845 (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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.kassouni.palette;

  # Every sibling .nix file is an ordinary home-manager module that appends to
  # kassouni.palette.commands, so dropping a file in here adds an entry.
  commandModules = map (file: ./. + "/${file}") (
    lib.attrNames (
      lib.filterAttrs (name: type: type == "regular" && name != "default.nix" && lib.hasSuffix ".nix" name)
        (builtins.readDir ./.)
    )
  );
in
{
  imports = commandModules;

  options.kassouni.palette.commands = lib.mkOption {
    type = lib.types.listOf (
      lib.types.submodule {
        options = {
          name = lib.mkOption {
            type = lib.types.str;
            description = "Label shown in the palette menu.";
          };
          icon = lib.mkOption {
            type = lib.types.str;
            description = "Nerd Font glyph shown before the label.";
          };
          exe = lib.mkOption {
            type = lib.types.package;
            description = "Executable to run when the entry is selected.";
          };
        };
      }
    );
    default = [ ];
    description = ''
      Entries shown in the rofi command palette. Menu order follows module
      import order, which is alphabetical by filename.
    '';
  };

  config.home.packages = [
    (pkgs.writers.writeBashBin "open-palette" ''
      set -eu
      exes=(${lib.concatMapStringsSep " " (c: "${c.exe}") cfg.commands})
      idx=$(${pkgs.rofi}/bin/rofi -dmenu -i -format i -p palette <<EOF
${lib.concatMapStrings (c: "${c.icon} ${c.name}\n") cfg.commands}EOF
      )
      exec "''${exes[$idx]}"
    '')
  ];
}