Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/direnv: init #192667

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions nixos/modules/programs/direnv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{ lib, config, pkgs, ... }: {

options.programs.direnv.enable = lib.mkOption {
type = lib.types.bool;
default = false;
example = true;
description = ''
Whether to enable direnv integration. Takes care of both installation and
setting up the sourcing of the shell. Additionally enables nix-direnv
integration. Note that you need to logout and login for this change to apply.
Comment on lines +8 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should mention the nix setting changes.

'';
};

config = lib.mkIf config.programs.direnv.enable {

environment.systemPackages = with pkgs; [
direnv
nix-direnv
];

environment.pathsToLink = [
"/share/direnv"
];

environment.sessionVariables.DIRENV_CONFIG = "/run/current-system/sw/share/direnv";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned: This disallows user customization entirely, I think.
Either we need a change to direnv to address this or we need a change here to give us the ability for the user to specify additional contents.


programs.bash.interactiveShellInit = ''
eval "$(direnv hook bash)"
'';

programs.zsh.interactiveShellInit = ''
eval "$(direnv hook zsh)"
'';

programs.fish.interactiveShellInit = ''
direnv hook fish | source
'';

# nix options for derivations to persist garbage collection
nix.settings.keep-outputs = true;
ck3d marked this conversation as resolved.
Show resolved Hide resolved
nix.settings.keep-derivations = true;
};

}
3 changes: 3 additions & 0 deletions pkgs/tools/misc/nix-direnv/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ stdenv.mkDerivation rec {
installPhase = ''
runHook preInstall
install -m500 -D direnvrc $out/share/nix-direnv/direnvrc

# Allows NixOS to set DIRENV_CONFIG to /run/current-system/sw/share/direnv and have direnv load this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, the module will also have to handle user settings since DIRENV_CONFIG is also used to locate the direnv.toml.

Alternatively, it could make sense to change direnv so it also loads all the libraries in
share/direnv/lib/*.sh, following the XDG spec?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that, given that direnv already reads ${XDG_CONFIG_HOME:-$HOME/.config}/direnv/lib/*.sh it makes perfect sense for it to read the equivalent system directory, does it not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think so too, I quote the XDG Base Directory Specification:

$XDG_CONFIG_DIRS defines the preference-ordered set of base directories to search for configuration files in addition to the $XDG_CONFIG_HOME base directory. The directories in $XDG_CONFIG_DIRS should be seperated with a colon ':'.

If $XDG_CONFIG_DIRS is either not set or empty, a value equal to /etc/xdg should be used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a direnv PR for this: direnv/direnv#990

install -m500 -D direnvrc $out/share/direnv/lib/nix-direnv.sh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this not be a symlink?

runHook postInstall
'';

Expand Down