From 318bc0754ed6370cfcae13183a7f13f7aa4bc73f Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Mon, 1 Jun 2020 09:56:48 +0200 Subject: [PATCH] direnv: add support for nix-direnv PR #1297 (cherry picked from commit b33802ca7f0675fa8f64dd0f322ab7a57e45ba6a) --- modules/programs/direnv.nix | 12 ++++++++-- tests/modules/programs/direnv/default.nix | 2 ++ tests/modules/programs/direnv/nix-direnv.nix | 18 +++++++++++++++ .../programs/direnv/stdlib-and-nix-direnv.nix | 23 +++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/modules/programs/direnv/nix-direnv.nix create mode 100644 tests/modules/programs/direnv/stdlib-and-nix-direnv.nix diff --git a/modules/programs/direnv.nix b/modules/programs/direnv.nix index de57727f..1d1374b8 100644 --- a/modules/programs/direnv.nix +++ b/modules/programs/direnv.nix @@ -70,6 +70,11 @@ in { Whether to enable Fish integration. ''; }; + + enableNixDirenvIntegration = mkEnableOption '' + nix-direnv, + a fast, persistent use_nix implementation for direnv''; }; config = mkIf cfg.enable { @@ -78,8 +83,11 @@ in { xdg.configFile."direnv/config.toml" = mkIf (cfg.config != { }) { source = configFile cfg.config; }; - xdg.configFile."direnv/direnvrc" = - mkIf (cfg.stdlib != "") { text = cfg.stdlib; }; + 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 diff --git a/tests/modules/programs/direnv/default.nix b/tests/modules/programs/direnv/default.nix index 82ad15ff..3efad2b6 100644 --- a/tests/modules/programs/direnv/default.nix +++ b/tests/modules/programs/direnv/default.nix @@ -1,4 +1,6 @@ { direnv-bash = ./bash.nix; + direnv-nix-direnv = ./nix-direnv.nix; direnv-stdlib = ./stdlib.nix; + direnv-stdlib-and-nix-direnv = ./stdlib-and-nix-direnv.nix; } diff --git a/tests/modules/programs/direnv/nix-direnv.nix b/tests/modules/programs/direnv/nix-direnv.nix new file mode 100644 index 00000000..57b3907d --- /dev/null +++ b/tests/modules/programs/direnv/nix-direnv.nix @@ -0,0 +1,18 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + config = { + programs.bash.enable = true; + programs.direnv.enable = true; + programs.direnv.enableNixDirenvIntegration = true; + + nmt.script = '' + assertFileExists home-files/.bashrc + assertFileRegex \ + home-files/.config/direnv/direnvrc \ + 'source /nix/store/.*nix-direnv.*/share/nix-direnv/direnvrc' + ''; + }; +} diff --git a/tests/modules/programs/direnv/stdlib-and-nix-direnv.nix b/tests/modules/programs/direnv/stdlib-and-nix-direnv.nix new file mode 100644 index 00000000..1dc22431 --- /dev/null +++ b/tests/modules/programs/direnv/stdlib-and-nix-direnv.nix @@ -0,0 +1,23 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let expectedContent = "something important"; +in { + config = { + programs.bash.enable = true; + programs.direnv.enable = true; + programs.direnv.enableNixDirenvIntegration = true; + programs.direnv.stdlib = expectedContent; + + nmt.script = '' + assertFileExists home-files/.bashrc + assertFileRegex \ + home-files/.config/direnv/direnvrc \ + 'source /nix/store/.*nix-direnv.*/share/nix-direnv/direnvrc' + assertFileRegex \ + home-files/.config/direnv/direnvrc \ + '${expectedContent}' + ''; + }; +}