summary refs log tree commit diff stats
path: root/user/modules/tabnine.nix
blob: 3eee48341d106dc4fd3a2007ef2491a38b6e1ee7 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{ config
, lib
, pkgs
, ...
}:
with lib; let
  inherit (pkgs) stdenv;
  tomlFormat = pkgs.formats.toml { };
  cfg = config.programs.tabnine;
  lspConfigFile = config:
    pkgs.runCommand "TabNine.toml"
      {
        buildInputs = [ pkgs.remarshal ];
        preferLocalBuild = true;
        allowSubstitutes = false;
      }
      ''
        remarshal -if json -of toml \
        < ${pkgs.writeText "config.json" (builtins.toJSON cfg.lspConfig)} \
        | sed  -e 's/^\["\(.*\)"\]/[\1]/' \
        > $out
      '';
in
{
  options.programs.tabnine = {
    enable = mkEnableOption "TabNine, Smart Compose for code.";

    configDir = mkOption {
      type = types.str;
      default =
        if stdenv.isDarwin
        then "Library/Preferences/TabNine"
        else "${config.xdg.configHome}/TabNine";
      description = ''
        Location of TabNine configuration directory relative to \$HOME
      '';
    };

    config = mkOption {
      type = types.attrs;
      default = { };
      description = ''
        TabNine configuration written to
        <filename>\${configDir}/tabnine_config.json</filename>
      '';
    };

    registrationKey = mkOption {
      type = types.str;
      description = ''
        Registration key for TabNine.  For one-time-purchase users, this should be the e-mail address and base-64-encoded key joined together without spacing.
      '';
    };

    lspConfig = mkOption {
      type = tomlFormat.type;
      default = { };
      description = ''
        LSP Server configuration written to
        <filename>\${configDir}/TabNine.toml</filename>
        </para><para>
      '';
    };
  };

  config = mkIf cfg.enable {
    home.file."${cfg.configDir}/TabNine.toml" = {
      source = (tomlFormat.generate "TabNine.toml" cfg.lspConfig);
    };
    home.file."${cfg.configDir}/tabnine_config.json" = {
      source = pkgs.writeText "tabnine_config.json" (builtins.toJSON cfg.config);
    };
    home.file."${cfg.configDir}/registration_key" = {
      text = cfg.registrationKey;
    };

    programs.neovim.plugins = [
      pkgs.vimPlugins.coc-tabnine
    ];
    programs.neovim.coc.settings = {
      "tabnine.binary_path" = "${pkgs.tabnine}/bin/TabNine";
    };

    programs.emacs.extraPackages = epkgs: [
      epkgs.tabnine
    ];
    programs.emacs.extraConfig = ''
      (defun my/override-tabnine-exe-path (&rest args)
        "${pkgs.tabnine}/bin/TabNine")
      (with-eval-after-load 'tabnine
        (advice-add 'tabnine--executable-path :override #'my/override-tabnine-exe-path))
    '';
  };
}