Creating a Nix flake that combines the outputs of two mkDerivation calls into a single devShell involves a few steps. The devShell is an environment intended for development purposes and can include various dependencies, tools, and environment variables. Here, we will set up build dependencies from two separate derivations and include them in a development shell for our project.
Here's how you can set up such a flake.nix:
{
description = "A flake with combined devShell outputs";
# Define the inputs for the flake, such as Nixpkgs.
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = { self, nixpkgs }: {
# Define the Nix packages for the current system
packages = forAllSystems (system:
let
pkgs = import nixpkgs {
inherit system;
};
# Define our first derivation
derivationA = pkgs.stdenv.mkDerivation {
name = "exampleA";
# Build dependencies, install phases and other attributes
buildInputs = [ pkgs.curl ];
};
# Define our second derivation
derivationB = pkgs.stdenv.mkDerivation {
name = "exampleB";
# Build dependencies, install phases and other attributes
buildInputs = [ pkgs.jq ];
};
in
{
# Combine outputs from two derivations
combined = pkgs.stdenv.mkDerivation rec {
name = "combined-${version}";
version = "1.0";
buildInputs = [
derivationA
derivationB
] ++ (with pkgs; [
# Any extra packages needed for development can be added here
]);
installPhase = ''
mkdir -p $out/bin
cp -r ${derivationA}/bin/* $out/bin/ || true
cp -r ${derivationB}/bin/* $out/bin/ || true
'';
};
}
);
# Define a dev shell for the current system
devShell = forAllSystems (system:
let
pkgs = import nixpkgs {
inherit system;
};
in
pkgs.mkShell {
buildInputs = [
(self.packages.${system}.combined)
];
# Set environment variables and other shell settings
shellHook = ''
echo "Welcome to the devShell!"
'';
}
);
};
}
In this example, we define two separate derivations, derivationA and derivationB, which simply include curl and jq as dependencies. In a real-world scenario, you'd replace these placeholders with the actual build instructions for your projects.
The combined derivation aggregates the binaries from derivationA and derivationB into its output.
Finally, in the devShell section, we create a development shell using mkShell, adding the combined derivation to buildInputs. This makes the outputs of both derivationA and derivationB available in the shell environment.
The shellHook is used to specify commands that will be run when entering the shell; for example, it prints a welcome message in this case.
Please adjust the paths and commands to match the actual structure of your derived outputs as needed. Also, the above example may not work directly since cp is used to copy binaries, but mkDerivation does not necessarily produce binaries in the bin directory. You'll need to modify the installPhase to copy the actual results of your build processes.
Finally, to use the development shell defined in this flake, you would run:
nix develop
This would open a shell with the combined outputs of derivationA and derivationB.