Creating a Nix flake that combines the outputs of two derivations into a single devShell involves a few key steps. First, you need to define the two derivations, then create a development shell that includes both of them.
Below is an example flake.nix that does just that. Assume that packageA and packageB are two hypothetical packages that we want to include in our development shell:
{
description = "A devShell combining two derivations";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }: {
# Define our NixOS system configuration (optional, you might not need this)
nixosConfigurations.example = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [];
};
# Define the derivations
packages.x86_64-linux.packageA = nixpkgs.legacyPackages.x86_64-linux.stdenv.mkDerivation {
name = "packageA";
# ... configuration for packageA ...
buildInputs = [ nixpkgs.legacyPackages.x86_64-linux.git ];
};
packages.x86_64-linux.packageB = nixpkgs.legacyPackages.x86_64-linux.stdenv.mkDerivation {
name = "packageB";
# ... configuration for packageB ...
buildInputs = [ nixpkgs.legacyPackages.x86_64-linux.htop ];
};
# Define the development shell that combines them
devShell.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.mkShell {
buildInputs = with nixpkgs.legacyPackages.x86_64-linux; [
# Include packageA and packageB in the development shell
(self.packages.x86_64-linux.packageA)
(self.packages.x86_64-linux.packageB)
];
# Optional: include other packages in the shell
propagatedBuildInputs = with nixpkgs.legacyPackages.x86_64-linux; [
vim
gcc
];
shellHook = ''
echo "Welcome to the dev shell with packageA and packageB!"
'';
};
};
}
Explanation of key parts of flake.nix:
inputs: Specifies the sources for the flake’s dependencies.
outputs: The main function that defines all the artifacts produced by the flake.
packages: Defines Nix derivations that result in build outputs, which are the actual package artifacts.
devShell: Defines a development shell environment for the x86_64-linux system that includes packageA and packageB by referencing the derivation definitions from packages.
To use the devShell, you would run the following command:
nix develop
This will drop you into a shell that has packageA, packageB, as well as any additional tools (like vim and gcc as specified) available in your environment.
Please note that the specific details for your packageA and packageB (... configuration for packageA ... and ... configuration for packageB ...) will depend on the actual packages you're trying to include. You'll need to replace these placeholders with real configuration options, such as source, build commands, dependencies, etc.