Creating a Nix flake that combines two mkShell environments into a single devShell is quite straightforward. You need to aggregate the buildInputs and other environment attributes from both mkShell environments. Here's an example of how to do this:
{
description = "Combine two mkShell environments into a single devShell";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
overlays = [];
system = system;
};
# Define the first mkShell
shellA = pkgs.mkShell {
buildInputs = [ pkgs.git pkgs.gcc ];
};
# Define the second mkShell
shellB = pkgs.mkShell {
buildInputs = [ pkgs.python3 pkgs.nodejs ];
};
# Combine the buildInputs of both mkShell environments
combinedDevShell = pkgs.mkShell {
buildInputs = shellA.buildInputs ++ shellB.buildInputs;
# You can also merge other attributes like shellHook, etc., if needed.
};
in
{
# Provide the combined dev shell for each system
devShell = combinedDevShell;
}
);
}
In this flake.nix file, we're taking the following steps:
-
Define the inputs for our flake, specifically nixpkgs and flake-utils. The former is the Nix package repository, and the latter is a library to help with flake creation.
-
Use flake-utils's eachDefaultSystem function to define the flake for multiple systems (e.g., x86_64-linux, aarch64-linux, etc.).
-
Import the nixpkgs to access the package collection, defining the target system.
-
Define two separate mkShell environments, shellA and shellB, with their respective buildInputs. These represent two different development shell environments that you want to combine.
-
Create another mkShell, combinedDevShell, which merges the buildInputs from both shellA and shellB. If needed, you can also merge other attributes like shellHook by concatenating their scripts or sequences of commands.
-
The resulting combinedDevShell is set as the devShell output for the flake, meaning it will be used as the development environment when you run nix develop.
To use the combined development shell environment, make sure you have Nix with flakes support installed and run the following command in the directory containing your flake.nix file:
nix develop
This will drop you into a shell that has both sets of packages available from shellA and shellB.