My direnv flake.nix is overriding my home.nix shell settings somehow

My home.nix has a programs.zsh section that looks like this:

  programs.zsh = {
    enable = true;
    autocd = true;
    enableAutosuggestions = true;
    enableCompletion = true;
    shellAliases = {
      ls = "ls -alG";
      vim = "nvim";
      htop = "btop";
      tmux = "zellij";
    };

    initExtra = ''
      PS1='%(?.%F{green}>.%F{red}?%?)%f %B%F{100}%~%f%b %# ';
      CLICOLOR=1;
      PATH=/Users/mmai/scripts:$PATH;
      NIXPKGS_ALLOW_UNFREE=1;
      setopt rmstarsilent;
    '';
  };

This gives me highlighted directory colours when doing “ls”. But when I run the flake below via direnv use flake the directory highlighting is gone in my console:

{
  description = "R development environment";

  # Flake inputs
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs"; # also valid: "nixpkgs"
  };

  # Flake outputs
  outputs = { self, nixpkgs }:
    let
      # Systems supported
      allSystems = [
        "x86_64-linux" # 64-bit Intel/AMD Linux
        "aarch64-linux" # 64-bit ARM Linux
        "x86_64-darwin" # 64-bit Intel macOS
        "aarch64-darwin" # 64-bit ARM macOS
      ];

      # Helper to provide system-specific attributes
      forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
        pkgs = import nixpkgs { inherit system; };
      });
    in
    {
      # Development environment output
      devShells = forAllSystems ({ pkgs }: {
        default = pkgs.mkShell {
          # The Nix packages provided in the environment
          packages = with pkgs; [
            R
            rPackages.xts
            rPackages.glmnet
            rPackages.ggplot2
            rPackages.reshape2
            rPackages.lattice
            rPackages.quantreg
            rPackages.gridBase
            radianWrapper
            rPackages.ggplot2
            rPackages.xts
            rPackages.ggsci
            rPackages.ragg
            rPackages.png
            rPackages.viridis
            rPackages.forcats
            rPackages.ggridges
            rPackages.scales
            rPackages.RColorBrewer
            rPackages.cowplot
            rPackages.magick
            rPackages.RPostgreSQL
            rPackages.DBI
            rPackages.devtools
            rPackages.tidyverse
          ];
        };
      });
    };
}

Shouldn’t it be inheriting my home.nix environment automatically? Or do I need to add something to this flake?

I had an issue with the same symptoms and came across this post, so here is my solution.

The problem is not that the environment is not inherited, indeed:

~ $ echo $CLICOLOR
1
~ $ cd dev/ffmpeg
direnv: loading ~/Dev/ffmpeg/.envrc
~/dev/ffmpeg $ echo $CLICOLOR
1

Rather the issue is that entering the dev shell changes which ls executable is in use, from one supplied by the system to one supplied by Nix:

~ $ which ls
/bin/ls
~ $ cd dev/ffmpeg
direnv: loading ~/Dev/ffmpeg/.envrc
~/dev/ffmpeg $ which ls
/nix/store/27hlrqpjy40ifmmy3rw2fbi5b57l8cmw-coreutils-9.5/bin/ls

The version of ls supplied by Nix had slightly different semantics to the one supplied by the system in my case. Specifically, the version supplied by Nix requires the --color=auto flag in order to print colourful output. I fixed the issue, then, by aliasing ls to ls --color=auto.

1 Like