Recommended way for extending/joining/merging a derivation and a `shell.nix` file

Let’s say I have a pretty little derivation in default.nix ;

{ pkgs ? import <nixpkgs>{} } :

pkgs.stdenv.mkDerivation rec {
  name = "xmonad-config";
  src = ./.;
  buildInputs = [ pkgs.ghc ];
  installPhase = "cp ./xmonad.hs $out";

And then I want to a shell.nix file, that contains all of the default.nix goodies, but also some new features. What would I need to do to accomplish that? What way would you recommend?

The following code won’t work, but might help to give an understanding of what I want to do:

with import <nixpkgs> {};

  default = pkgs.callPackage ./default.nix {};
  stdenv.mkDerivation rec {
    shellHook = ''
      echo 'Welcome to ${}!';
      echo 'Just look at all of the incredible buildInputs in default.nix we have:';
      echo $buildInputs;

Should I use overlays somehow? If so, how?

1 Like

I once came up with this structure (, I still use it as it helps me generate a Nixpkgs compliant derivation, override dependencies, and incorporate the overlay into configuariton.nix or nixops expressions as needed.

Thank you! Looks very interesting and I might start using that structure as well

But all of the files had examples except for shell.nix. How would you write your shell.nix if you need to inherit everything from default.nix and just add, let’s say a new buildInput item?

I’m a bit new to nix so I’m not sure what’s best practice or even how you could do that

nix-shell will use default.nix if shell.nix is not present. If you want to add something on top of default.nix for development purposes, there are two approaches:

  1. override the default.nix derivation
  2. create an environment using buildEnv

example of #2:

  nixpkgs = import ./nixpkgs.nix;
  pkgs = import nixpkgs {
    config = {};
    overlays = [
      (import ./overlay.nix)

in pkgs.buildEnv {
name = "myEnv";
paths = [ pkgs.mySpecialPackage pkgs.someOtherDeveloptmentToolForNixShellOnly ];

Oh, I see, pkgs.buildEnv looks like what I need! I’ll try it out, thanks a lot!