Like mkShell, but less env clutter?

I like to use nix (and direnv) to make, in certain directories, certain programs available; this seems to be a relatively common use-case.

For example in one project I am contributing to I have a .envrc file with

use flake ../../spectec-nix

which points to a directory with a flake.nix file as follows:

  inputs.nixpkgs.url = "github:NixOS/nixpkgs";
  description = "Nix infrastructure for spectec";
  outputs = { self, nixpkgs }:
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
    in {
      devShells.${system}.default =
        pkgs.mkShell {
          packages = with pkgs; [

When I enter that directory, I suddenly have pinned versions of the Ocaml and Haskell toolchain available:

~ $ ghc
ghc: command not found
~ $ cd ~/build/spectec/spectec
direnv: loading ~/build/spectec/spectec/.envrc
direnv: using flake ../../spectec-nix
direnv: export +AR +AS +CC +CONFIG_SHELL +CXX +HOST_PATH +IN_NIX_SHELL +LD +NIX_BINTOOLS +NIX_BINTOOLS_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_BUILD_CORES +NIX_BUILD_TOP +NIX_CC +NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_CFLAGS_COMPILE +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_LDFLAGS +NIX_STORE +NM +OBJCOPY +OBJDUMP +OCAMLFIND_DESTDIR +OCAMLPATH +RANLIB +READELF +SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +TEMP +TEMPDIR +TMP +TMPDIR +__structuredAttrs +buildInputs +buildPhase +builder +cmakeFlags +configureFlags +depsBuildBuild +depsBuildBuildPropagated +depsBuildTarget +depsBuildTargetPropagated +depsHostHost +depsHostHostPropagated +depsTargetTarget +depsTargetTargetPropagated +doCheck +doInstallCheck +dontAddDisableDepTrack +mesonFlags +name +nativeBuildInputs +out +outputs +patches +phases +preferLocalBuild +propagatedBuildInputs +propagatedNativeBuildInputs +shell +shellHook +stdenv +strictDeps +system ~PATH ~XDG_DATA_DIRS
~/build/spectec/spectec $ ghc
ghc: no input files
Usage: For basic information, try the `--help' option.
~/build/spectec/spectec $ ghc --version
The Glorious Glasgow Haskell Compilation System, version 9.2.7

But note the long list of environment variables that are set! Some are certainly relevant: PATH first and foremost, but also OCAMLPATH and In_NIX_SHEL and maybe others.

On the other hand, most of them seem to be irrelevant for what I am doing. So I wonder if mkShell could be changed (or an alternative provided) to omit most of them, and only really set the stuff that’s necessary to run the programs mentioned in packages?

1 Like

mkNakedShell- GitHub - yusdacra/mk-naked-shell, shamelessly taken from numtide’s devshell.

1 Like

Good to see that someone else wanted that too. Should this be in nixpkgs?

Hopefully, this will no longer be an issue with __structuredAttrs. You might even try it today, though support by different builders might vary.

1 Like