fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Ramsesdirenv: add package options4f02e35f

master
4.1 KiB147 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.direnv;
8
9  tomlFormat = pkgs.formats.toml { };
10
11in {
12  imports = [
13    (mkRenamedOptionModule [
14      "programs"
15      "direnv"
16      "enableNixDirenvIntegration"
17    ] [ "programs" "direnv" "nix-direnv" "enable" ])
18    (mkRemovedOptionModule [ "programs" "direnv" "nix-direnv" "enableFlakes" ]
19      "Flake support is now always enabled.")
20  ];
21
22  meta.maintainers = [ maintainers.rycee ];
23
24  options.programs.direnv = {
25    enable = mkEnableOption "direnv, the environment switcher";
26
27    package = mkPackageOption pkgs "direnv" { };
28
29    config = mkOption {
30      type = tomlFormat.type;
31      default = { };
32      description = ''
33        Configuration written to
34        {file}`$XDG_CONFIG_HOME/direnv/direnv.toml`.
35
36        See
37        {manpage}`direnv.toml(1)`.
38        for the full list of options.
39      '';
40    };
41
42    stdlib = mkOption {
43      type = types.lines;
44      default = "";
45      description = ''
46        Custom stdlib written to
47        {file}`$XDG_CONFIG_HOME/direnv/direnvrc`.
48      '';
49    };
50
51    enableBashIntegration = mkOption {
52      default = true;
53      type = types.bool;
54      description = ''
55        Whether to enable Bash integration.
56      '';
57    };
58
59    enableZshIntegration = mkOption {
60      default = true;
61      type = types.bool;
62      description = ''
63        Whether to enable Zsh integration.
64      '';
65    };
66
67    enableFishIntegration = mkOption {
68      default = true;
69      type = types.bool;
70      readOnly = true;
71      description = ''
72        Whether to enable Fish integration. Note, enabling the direnv module
73        will always active its functionality for Fish since the direnv package
74        automatically gets loaded in Fish. If this is not the case try adding
75        ```nix
76          environment.pathsToLink = [ "/share/fish" ];
77        ```
78        to the system configuration.
79      '';
80    };
81
82    enableNushellIntegration = mkOption {
83      default = true;
84      type = types.bool;
85      description = ''
86        Whether to enable Nushell integration.
87      '';
88    };
89
90    nix-direnv = {
91      enable = mkEnableOption ''
92        [nix-direnv](https://github.com/nix-community/nix-direnv),
93        a fast, persistent use_nix implementation for direnv'';
94
95      package = mkPackageOption pkgs "nix-direnv" { };
96    };
97
98  };
99
100  config = mkIf cfg.enable {
101    home.packages = [ cfg.package ];
102
103    xdg.configFile."direnv/direnv.toml" = mkIf (cfg.config != { }) {
104      source = tomlFormat.generate "direnv-config" cfg.config;
105    };
106
107    xdg.configFile."direnv/direnvrc" = let
108      text = concatStringsSep "\n" (optional (cfg.stdlib != "") cfg.stdlib
109        ++ optional cfg.nix-direnv.enable
110        "source ${cfg.nix-direnv.package}/share/nix-direnv/direnvrc");
111    in mkIf (text != "") { inherit text; };
112
113    programs.bash.initExtra = mkIf cfg.enableBashIntegration (
114      # Using mkAfter to make it more likely to appear after other
115      # manipulations of the prompt.
116      mkAfter ''
117        eval "$(${cfg.package}/bin/direnv hook bash)"
118      '');
119
120    programs.zsh.initExtra = mkIf cfg.enableZshIntegration ''
121      eval "$(${cfg.package}/bin/direnv hook zsh)"
122    '';
123
124    programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration (
125      # Using mkAfter to make it more likely to appear after other
126      # manipulations of the prompt.
127      mkAfter ''
128        ${cfg.package}/bin/direnv hook fish | source
129      '');
130
131    programs.nushell.extraConfig = mkIf cfg.enableNushellIntegration (
132      # Using mkAfter to make it more likely to appear after other
133      # manipulations of the prompt.
134      mkAfter ''
135        $env.config = ($env | default {} config).config
136        $env.config = ($env.config | default {} hooks)
137        $env.config = ($env.config | update hooks ($env.config.hooks | default [] pre_prompt))
138        $env.config = ($env.config | update hooks.pre_prompt ($env.config.hooks.pre_prompt | append {
139          code: "
140            let direnv = (${cfg.package}/bin/direnv export json | from json)
141            let direnv = if not ($direnv | is-empty) { $direnv } else { {} }
142            $direnv | load-env
143            "
144        }))
145      '');
146  };
147}