aboutsummaryrefslogtreecommitdiffstats
path: root/modules/by-name/di/direnv/module.nix
blob: fc800f35d886c9e2315527cf186608ac13eaf61d (plain) (blame)
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
# nixos-config - My current NixOS configuration
#
# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of my nixos-config.
#
# You should have received a copy of the License along with this program.
# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
{
  config,
  lib,
  pkgs,
  sources,
  ...
}: let
  cfg = config.soispha.programs.direnv;
  direnvCfg = config.home-manager.users.soispha.programs.direnv;

  inherit (sources) devshells nixpkgs;

  pre-cache = pkgs.runCommand "pre-cache.sh" {} ''
    cat "${./pre-cache.sh}" > $out

    substituteInPlace $out \
      --replace-fail "%DEVSHELLS_REPO%" "${devshells}" \
      --replace-fail "%DEVSHELLS_REPO_REF%" "${devshells.sourceInfo.rev}.${builtins.readFile "${nixpkgs}/.git-revision"}"
  '';

  landrun-fish = pkgs.writeShellApplication {
    name = "landrun-fish";
    text = builtins.readFile ./landrun-fish.sh;
    inheritPath = true;
    runtimeInputs = [
      pkgs.landrun
    ];
  };

  direnvPkg = lib.getExe direnvCfg.package;
in {
  options.soispha.programs.direnv = {
    enable = lib.mkEnableOption "direnv";
  };

  config = lib.mkIf cfg.enable {
    soispha.programs.fish.integrations.direnv =
      # fish
      ''
        set --global SHOULD_SANDBOX_DIRENV

        function _direnv_export_eval
          ${direnvPkg} export fish | source
        end

        function _direnv_hook --on-variable PWD
          if not set --query IN_DIRENV_SANDBOX;
            and set --query SHOULD_SANDBOX_DIRENV;
            and ${direnvPkg} status | grep 'Found RC path' --quiet;
            and ${direnvPkg} status | grep 'No .envrc or .env loaded' --quiet

            # We found a RC path, but no RC file is loaded. So we can start our shell and
            # load it.
            ${lib.getExe landrun-fish} --init-command='_direnv_export_eval'

            # After the user exists (via Ctrl+D), they probably want to change the
            # directory.
            ll
          else
            _direnv_export_eval
          end
        end
      '';

    home-manager.users.soispha = {
      programs.direnv = {
        enable = true;

        package = pkgs.direnv-patched;

        nix-direnv = {
          enable = true;
          package = pkgs.nix-direnv.override {nix = config.nix.package;};
        };

        # Handled by myself (and the script is overridden)
        enableBashIntegration = true;
        enableZshIntegration = true;
        enableFishIntegration = false;

        config = {
          warn_timeout = 0;

          # NOTE: Something variable is undeclared in the my default loaded stuff :/. <2026-08-18>
          # strict_env = true;

          # disable_stdin = true;
        };
      };
      xdg.configFile = {
        "direnv/lib/hm-pre-cache.sh" = {
          source = pre-cache;
        };
      };
    };
  };
}