aboutsummaryrefslogtreecommitdiffstats
path: root/modules/by-name/di/direnv/module.nix
blob: 93e29f512b3120aa8064d18b0aefd2223233f7d2 (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
# 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
    ];
  };
in {
  options.soispha.programs.direnv = {
    enable = lib.mkEnableOption "direnv";
  };

  config = lib.mkIf cfg.enable {
    soispha.programs.fish.integrations.direnv =
      # fish
      ''
        function __direnv_export_eval
          ${lib.getExe direnvCfg.package} export fish | source;
        end

        if test -z "$NO_DIRENV_LOAD"
          function _direnv_hook
            if test -f ".env" -o -f ".envrc" -o -f ".nenvrc"
              ${landrun-fish} --interactive --command='__direnv_export_eval'
            end
          end

          function __direnv_on_prompt --on-event fish_prompt;
            _direnv_hook
          end

          function _direnv_on_preexec --on-event fish_preexec;
            _direnv_hook
          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;
        };
      };
    };
  };
}