aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/readline.nix
blob: 2f79df6e103713b26935453a139ec356d1485429 (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
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.programs.readline;

  mkSetVariableStr = n: v:
    let
      mkValueStr = v:
        if v == true then
          "on"
        else if v == false then
          "off"
        else if isInt v then
          toString v
        else if isString v then
          v
        else
          abort ("values ${toPretty v} is of unsupported type");
    in "set ${n} ${mkValueStr v}";

  mkBindingStr = k: v: ''"${k}": ${v}'';

in {
  options.programs.readline = {
    enable = mkEnableOption "readline";

    bindings = mkOption {
      default = { };
      type = types.attrsOf types.str;
      example = literalExample ''
        { "\\C-h" = "backward-kill-word"; }
      '';
      description = "Readline bindings.";
    };

    variables = mkOption {
      type = with types; attrsOf (either str (either int bool));
      default = { };
      example = { expand-tilde = true; };
      description = ''
        Readline customization variable assignments.
      '';
    };

    includeSystemConfig = mkOption {
      type = types.bool;
      default = true;
      description = "Whether to include the system-wide configuration.";
    };

    extraConfig = mkOption {
      type = types.lines;
      default = "";
      description = ''
        Configuration lines appended unchanged to the end of the
        <filename>~/.inputrc</filename> file.
      '';
    };
  };

  config = mkIf cfg.enable {
    home.file.".inputrc".text = let
      configStr = concatStringsSep "\n"
        (optional cfg.includeSystemConfig "$include /etc/inputrc"
          ++ mapAttrsToList mkSetVariableStr cfg.variables
          ++ mapAttrsToList mkBindingStr cfg.bindings);
    in ''
      # Generated by Home Manager.

      ${configStr}
      ${cfg.extraConfig}
    '';
  };
}