about summary refs log tree commit diff
path: root/nixos/modules/programs/kubeswitch.nix
blob: 9348540022f23fcf902b24fb75c6fca64ba1cc9d (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
{
  config,
  pkgs,
  lib,
  ...
}:
let
  cfg = config.programs.kubeswitch;
in
{
  options = {
    programs.kubeswitch = {
      enable = lib.mkEnableOption "kubeswitch";

      commandName = lib.mkOption {
        type = lib.types.str;
        default = "kswitch";
        description = "The name of the command to use";
      };

      package = lib.mkOption {
        type = lib.types.package;
        default = pkgs.kubeswitch;
        defaultText = lib.literalExpression "pkgs.kubeswitch";
        description = "The package to install for kubeswitch";
      };
    };
  };

  config =
    let
      shell_files = pkgs.runCommand "kubeswitch-shell-files" {} ''
        mkdir -p $out/share
        for shell in bash zsh; do
          ${cfg.package}/bin/switcher init $shell | sed 's/switch(/${cfg.commandName}(/' > $out/share/${cfg.commandName}_init.$shell
          ${cfg.package}/bin/switcher --cmd ${cfg.commandName} completion $shell > $out/share/${cfg.commandName}_completion.$shell
        done
      '';
    in
    lib.mkIf cfg.enable {
      environment.systemPackages = [ cfg.package ];

      programs.bash.interactiveShellInit = ''
        source ${shell_files}/share/${cfg.commandName}_init.bash
        source ${shell_files}/share/${cfg.commandName}_completion.bash
      '';
      programs.zsh.interactiveShellInit = ''
        source ${shell_files}/share/${cfg.commandName}_init.zsh
        source ${shell_files}/share/${cfg.commandName}_completion.zsh
      '';
    };
}