Creating a Nix flake that combines the outputs of two mkDerivation calls into a single devShell requires creating a flake with the necessary inputs, defining each derivation, and then combining their outputs through buildInputs in the devShell attribute.
Let's walk through an example flake.nix where we create two simple derivations, one that provides a "hello" executable and another that provides a "world" executable. We would want both commands to be available in our development shell environment.
nix
{
description = "A devShell with combined mkDerivation outputs";
# Define the inputs for our flake
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
};
# Derivation for 'hello' executable
hello = pkgs.stdenv.mkDerivation {
name = "hello";
src = null;
installPhase = ''
mkdir -p $out/bin
echo '#!${pkgs.stdenv.shell}' > $out/bin/hello
echo 'echo Hello' >> $out/bin/hello
chmod +x $out/bin/hello
'';
};
# Derivation for 'world' executable
world = pkgs.stdenv.mkDerivation {
name = "world";
src = null;
installPhase = ''
mkdir -p $out/bin
echo '#!${pkgs.stdenv.shell}' > $out/bin/world
echo 'echo World' >> $out/bin/world
chmod +x $out/bin/world
'';
};
in
{
# Combine the outputs of our two derivations in a development shell
devShell = pkgs.mkShell {
buildInputs = [ hello world ];
};
});
}
In this flake.nix file, we use flake-utils to make our flake compatible with different systems. We define two mkDerivation calls, hello and world, in the outputs attribute. Each mkDerivation creates a simple shell script in $out/bin that acts as our executable. We then use pkgs.mkShell to create a devShell which includes both executables through buildInputs.
With these hello and world executables, when you enter the development shell environment with nix develop, you should be able to run hello and world commands and get "Hello" and "World" printed to the terminal respectively.
Make sure you have experimental features enabled in your Nix configuration to use nix develop. You can enable experimental features by adding the following line to your ~/.config/nix/nix.conf: