aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/direnv.nix
blob: 1d1374b8e260de36b8817381184889aa4c884705 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.programs.direnv;
  configFile = config:
    pkgs.runCommand "config.toml" {
      buildInputs = [ pkgs.remarshal ];
      preferLocalBuild = true;
      allowSubstitutes = false;
    } ''
      remarshal -if json -of toml \
        < ${pkgs.writeText "config.json" (builtins.toJSON config)} \
        > $out
    '';

in {
  meta.maintainers = [ maintainers.rycee ];

  options.programs.direnv = {
    enable = mkEnableOption "direnv, the environment switcher";

    config = mkOption {
      type = types.attrs;
      default = { };
      description = ''
        Configuration written to
        <filename>~/.config/direnv/config.toml</filename>.
        </para><para>
        See
        <citerefentry>
          <refentrytitle>direnv.toml</refentrytitle>
          <manvolnum>1</manvolnum>
        </citerefentry>.
        for the full list of options.
      '';
    };

    stdlib = mkOption {
      type = types.lines;
      default = "";
      description = ''
        Custom stdlib written to
        <filename>~/.config/direnv/direnvrc</filename>.
      '';
    };

    enableBashIntegration = mkOption {
      default = true;
      type = types.bool;
      description = ''
        Whether to enable Bash integration.
      '';
    };

    enableZshIntegration = mkOption {
      default = true;
      type = types.bool;
      description = ''
        Whether to enable Zsh integration.
      '';
    };

    enableFishIntegration = mkOption {
      default = true;
      type = types.bool;
      description = ''
        Whether to enable Fish integration.
      '';
    };

    enableNixDirenvIntegration = mkEnableOption ''
      <link
          xlink:href="https://github.com/nix-community/nix-direnv">nix-direnv</link>,
          a fast, persistent use_nix implementation for direnv'';
  };

  config = mkIf cfg.enable {
    home.packages = [ pkgs.direnv ];

    xdg.configFile."direnv/config.toml" =
      mkIf (cfg.config != { }) { source = configFile cfg.config; };

    xdg.configFile."direnv/direnvrc" = let
      text = concatStringsSep "\n" (optional (cfg.stdlib != "") cfg.stdlib
        ++ optional cfg.enableNixDirenvIntegration
        "source ${pkgs.nix-direnv}/share/nix-direnv/direnvrc");
    in mkIf (text != "") { inherit text; };

    programs.bash.initExtra = mkIf cfg.enableBashIntegration (
      # Using mkAfter to make it more likely to appear after other
      # manipulations of the prompt.
      mkAfter ''
        eval "$(${pkgs.direnv}/bin/direnv hook bash)"
      '');

    programs.zsh.initExtra = mkIf cfg.enableZshIntegration ''
      eval "$(${pkgs.direnv}/bin/direnv hook zsh)"
    '';

    programs.fish.shellInit = mkIf cfg.enableFishIntegration ''
      eval (${pkgs.direnv}/bin/direnv hook fish)
    '';
  };
}