Skip to content

Instantly share code, notes, and snippets.

@adisbladis
Created July 17, 2019 11:53
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save adisbladis/2a44cded73e048458a815b5822eea195 to your computer and use it in GitHub Desktop.
Save adisbladis/2a44cded73e048458a815b5822eea195 to your computer and use it in GitHub Desktop.
Merge multilple shell.nix files using mkShell
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [
pkgs.hello
];
shellHook = ''
echo env1
'';
}
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [
pkgs.vim
];
shellHook = ''
echo env2
'';
}
let
pkgs = import <nixpkgs> {};
lib = pkgs.lib;
env1 = import ./env1-shell.nix { inherit pkgs; };
env2 = import ./env2-shell.nix { inherit pkgs; };
mergeEnvs = envs: pkgs.mkShell (builtins.foldl' (a: v: {
buildInputs = a.buildInputs ++ v.buildInputs;
nativeBuildInputs = a.nativeBuildInputs ++ v.nativeBuildInputs;
propagatedBuildInputs = a.propagatedBuildInputs ++ v.propagatedBuildInputs;
propagatedNativeBuildInputs = a.propagatedNativeBuildInputs ++ v.propagatedNativeBuildInputs;
shellHook = a.shellHook + "\n" + v.shellHook;
}) (pkgs.mkShell {}) envs);
in mergeEnvs [ env1 env2 ]
@chaoky
Copy link

chaoky commented Mar 27, 2024

if you only care about inputs and shellHook

pkgs.mkShell {
  name = "my shell";
  inputsFrom = [ env1 env2 ];
  buildInputs = with pkgs; [ foo ];
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment