2020-10-12 00:22:58 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
2024-06-04 18:23:39 +00:00
|
|
|
inherit (lib)
|
|
|
|
mkOption mkRenamedOptionModule mkRemovedOptionModule mkEnableOption types
|
|
|
|
mkPackageOption mkIf mkAfter getExe;
|
2020-10-12 00:22:58 +00:00
|
|
|
|
|
|
|
cfg = config.programs.direnv;
|
2021-07-02 22:36:30 +00:00
|
|
|
|
|
|
|
tomlFormat = pkgs.formats.toml { };
|
2020-10-12 00:22:58 +00:00
|
|
|
|
|
|
|
in {
|
2021-07-02 22:36:30 +00:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [
|
|
|
|
"programs"
|
|
|
|
"direnv"
|
|
|
|
"enableNixDirenvIntegration"
|
|
|
|
] [ "programs" "direnv" "nix-direnv" "enable" ])
|
2021-12-03 05:30:34 +00:00
|
|
|
(mkRemovedOptionModule [ "programs" "direnv" "nix-direnv" "enableFlakes" ]
|
|
|
|
"Flake support is now always enabled.")
|
2021-07-02 22:36:30 +00:00
|
|
|
];
|
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
meta.maintainers = [ lib.maintainers.rycee ];
|
2020-10-12 00:22:58 +00:00
|
|
|
|
|
|
|
options.programs.direnv = {
|
|
|
|
enable = mkEnableOption "direnv, the environment switcher";
|
|
|
|
|
2024-01-05 11:06:15 +00:00
|
|
|
package = mkPackageOption pkgs "direnv" { };
|
|
|
|
|
2020-10-12 00:22:58 +00:00
|
|
|
config = mkOption {
|
2021-07-02 22:36:30 +00:00
|
|
|
type = tomlFormat.type;
|
2020-10-12 00:22:58 +00:00
|
|
|
default = { };
|
|
|
|
description = ''
|
|
|
|
Configuration written to
|
2023-08-08 10:19:01 +00:00
|
|
|
{file}`$XDG_CONFIG_HOME/direnv/direnv.toml`.
|
|
|
|
|
2020-10-12 00:22:58 +00:00
|
|
|
See
|
2023-08-08 10:19:01 +00:00
|
|
|
{manpage}`direnv.toml(1)`.
|
2020-10-12 00:22:58 +00:00
|
|
|
for the full list of options.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
stdlib = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Custom stdlib written to
|
2023-08-08 10:19:01 +00:00
|
|
|
{file}`$XDG_CONFIG_HOME/direnv/direnvrc`.
|
2020-10-12 00:22:58 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2021-11-04 16:42:44 +00:00
|
|
|
readOnly = true;
|
2020-10-12 00:22:58 +00:00
|
|
|
description = ''
|
2021-11-04 16:42:44 +00:00
|
|
|
Whether to enable Fish integration. Note, enabling the direnv module
|
|
|
|
will always active its functionality for Fish since the direnv package
|
2023-01-10 09:35:00 +00:00
|
|
|
automatically gets loaded in Fish. If this is not the case try adding
|
2023-08-08 10:19:01 +00:00
|
|
|
```nix
|
2023-01-10 09:35:00 +00:00
|
|
|
environment.pathsToLink = [ "/share/fish" ];
|
2023-08-08 10:19:01 +00:00
|
|
|
```
|
2023-01-10 09:35:00 +00:00
|
|
|
to the system configuration.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableNushellIntegration = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Whether to enable Nushell integration.
|
2020-10-12 00:22:58 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-07-02 22:36:30 +00:00
|
|
|
nix-direnv = {
|
|
|
|
enable = mkEnableOption ''
|
2023-08-08 10:19:01 +00:00
|
|
|
[nix-direnv](https://github.com/nix-community/nix-direnv),
|
|
|
|
a fast, persistent use_nix implementation for direnv'';
|
2024-01-05 11:06:15 +00:00
|
|
|
|
|
|
|
package = mkPackageOption pkgs "nix-direnv" { };
|
2021-07-02 22:36:30 +00:00
|
|
|
};
|
|
|
|
|
2020-10-12 00:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2024-01-05 11:06:15 +00:00
|
|
|
home.packages = [ cfg.package ];
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2023-01-10 09:35:00 +00:00
|
|
|
xdg.configFile."direnv/direnv.toml" = mkIf (cfg.config != { }) {
|
2021-07-02 22:36:30 +00:00
|
|
|
source = tomlFormat.generate "direnv-config" cfg.config;
|
|
|
|
};
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2024-06-04 18:23:39 +00:00
|
|
|
xdg.configFile."direnv/lib/hm-nix-direnv.sh" = mkIf cfg.nix-direnv.enable {
|
|
|
|
source = "${cfg.nix-direnv.package}/share/nix-direnv/direnvrc";
|
|
|
|
};
|
|
|
|
|
|
|
|
xdg.configFile."direnv/direnvrc" =
|
|
|
|
lib.mkIf (cfg.stdlib != "") { text = cfg.stdlib; };
|
2020-10-12 00:22:58 +00:00
|
|
|
|
|
|
|
programs.bash.initExtra = mkIf cfg.enableBashIntegration (
|
|
|
|
# Using mkAfter to make it more likely to appear after other
|
|
|
|
# manipulations of the prompt.
|
|
|
|
mkAfter ''
|
2024-01-05 11:06:15 +00:00
|
|
|
eval "$(${getExe cfg.package} hook bash)"
|
2020-10-12 00:22:58 +00:00
|
|
|
'');
|
|
|
|
|
|
|
|
programs.zsh.initExtra = mkIf cfg.enableZshIntegration ''
|
2024-01-05 11:06:15 +00:00
|
|
|
eval "$(${getExe cfg.package} hook zsh)"
|
2020-10-12 00:22:58 +00:00
|
|
|
'';
|
2023-01-10 09:35:00 +00:00
|
|
|
|
|
|
|
programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration (
|
|
|
|
# Using mkAfter to make it more likely to appear after other
|
|
|
|
# manipulations of the prompt.
|
|
|
|
mkAfter ''
|
2024-01-05 11:06:15 +00:00
|
|
|
${getExe cfg.package} hook fish | source
|
2023-01-10 09:35:00 +00:00
|
|
|
'');
|
|
|
|
|
|
|
|
programs.nushell.extraConfig = mkIf cfg.enableNushellIntegration (
|
|
|
|
# Using mkAfter to make it more likely to appear after other
|
|
|
|
# manipulations of the prompt.
|
|
|
|
mkAfter ''
|
2024-01-05 11:06:15 +00:00
|
|
|
$env.config = ($env.config? | default {})
|
|
|
|
$env.config.hooks = ($env.config.hooks? | default {})
|
|
|
|
$env.config.hooks.pre_prompt = (
|
|
|
|
$env.config.hooks.pre_prompt?
|
|
|
|
| default []
|
|
|
|
| append {||
|
|
|
|
let direnv = (${getExe cfg.package} export json
|
|
|
|
| from json
|
|
|
|
| default {})
|
|
|
|
if ($direnv | is-empty) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$direnv
|
|
|
|
| items {|key, value|
|
|
|
|
{
|
|
|
|
key: $key
|
|
|
|
value: (do (
|
|
|
|
$env.ENV_CONVERSIONS?
|
|
|
|
| default {}
|
|
|
|
| get -i $key
|
|
|
|
| get -i from_string
|
|
|
|
| default {|x| $x}
|
|
|
|
) $value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| transpose -ird
|
|
|
|
| load-env
|
|
|
|
}
|
|
|
|
)
|
2023-01-10 09:35:00 +00:00
|
|
|
'');
|
2020-10-12 00:22:58 +00:00
|
|
|
};
|
|
|
|
}
|