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

#1

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> {};

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

Should I use overlays somehow? If so, how?

0 Likes

#2

I once came up with this structure (https://www.reddit.com/r/NixOS/comments/8tkllx/standard_project_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.

0 Likes

#3

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

0 Likes

#4

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 ];
}
0 Likes

#5

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

0 Likes