From 63d6e2de24aabbcbefbb3edd6087733cd4cabeda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Tri=C3=B1anes?= Date: Sun, 17 Dec 2023 13:45:58 +0100 Subject: [PATCH] direnv: Apply nushell env transformations In nushell, the ENV_CONVERSIONS environment variable is used to transform the defined variables from a string to a nushell value (PATH to a list being one of the most common uses). This commit applies user-defined conversions to direnv-loaded variables. This fixes binary autocompletion not being triggered for newly added paths and makes direnv work consistently with nushell --- modules/programs/direnv.nix | 25 ++++++++++++++--------- tests/modules/programs/direnv/nushell.nix | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/modules/programs/direnv.nix b/modules/programs/direnv.nix index 74072589a..308a7ef3d 100644 --- a/modules/programs/direnv.nix +++ b/modules/programs/direnv.nix @@ -132,16 +132,21 @@ in { # Using mkAfter to make it more likely to appear after other # manipulations of the prompt. mkAfter '' - $env.config = ($env | default {} config).config - $env.config = ($env.config | default {} hooks) - $env.config = ($env.config | update hooks ($env.config.hooks | default [] pre_prompt)) - $env.config = ($env.config | update hooks.pre_prompt ($env.config.hooks.pre_prompt | append { - code: " - let direnv = (${cfg.package}/bin/direnv export json | from json) - let direnv = if not ($direnv | is-empty) { $direnv } else { {} } - $direnv | load-env - " - })) + $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 = (${cfg.package}/bin/direnv 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 + }) ''); }; } diff --git a/tests/modules/programs/direnv/nushell.nix b/tests/modules/programs/direnv/nushell.nix index 3041770c1..c202a6f80 100644 --- a/tests/modules/programs/direnv/nushell.nix +++ b/tests/modules/programs/direnv/nushell.nix @@ -14,6 +14,6 @@ in '' assertFileExists "${configFile}" assertFileRegex "${configFile}" \ - 'let direnv = (/nix/store/.*direnv.*/bin/direnv export json | from json)' + 'let direnv = (/nix/store/.*direnv.*/bin/direnv export json \| from json | default {})' ''; }