(sharing-dependencies)=
When packaging software in default.nix, you'll want a development environment in shell.nix to enter conveniently with nix-shell or automatically with direnv.
How to share the package's dependencies in default.nix with the development environment in shell.nix?
Use the inputsFrom attribute to pkgs.mkShellNoCC:
# default.nix
let
pkgs = import <nixpkgs> {};
myPackage = pkgs.callPackage ./package.nix {};
in
{
inherit myPackage;
shell = pkgs.mkShellNoCC {
inputsFrom = [ myPackage ];
};
}Import the shell attribute in shell.nix:
# shell.nix
(import ./.).shellAssume your package is defined in package.nix:
# package.nix
{ cowsay, runCommand }:
runCommand "cowsay-output" { buildInputs = [ cowsay ]; } ''
cowsay Hello, Nix! > $out
''In this example, cowsay is declared as a build-time dependency using buildInputs.
Further assume your project is defined in default.nix:
# default.nix
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11";
pkgs = import nixpkgs { config = {}; overlays = []; };
in
{
myPackage = pkgs.callPackage ./package.nix {};
}Add an attribute to default.nix specifying an environment:
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11";
pkgs = import nixpkgs { config = {}; overlays = []; };
in
{
myPackage = pkgs.callPackage ./package.nix {};
+ shell = pkgs.mkShellNoCC {
+ };
}Move the myPackage attribute into the let binding to be able to re-use it.
Then take the package's dependencies into the environment with inputsFrom:
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11";
pkgs = import nixpkgs { config = {}; overlays = []; };
+ myPackage = pkgs.callPackage ./package.nix {};
in
{
- myPackage = pkgs.callPackage ./package.nix {};
+ inherit myPackage;
shell = pkgs.mkShellNoCC {
+ inputsFrom = [ myPackage ];
};
}Finally, import the shell attribute in shell.nix:
# shell.nix
(import ./.).shellCheck the development environment, it contains the build-time dependency cowsay:
$ nix-shell --pure
[nix-shell]$ cowsay shell.nix