Building part of the flake

I have a nixos system that uses flakes. The flake I have is spread across multiple files, but if combined the relevant structure would be something like:


  outputs = inputs: {
    nixosConfigurations = {
      hostX = nixpkgs.lib.nixosSystem {
        environment.systemPackages = {
             my-python-packages = python-packages: with python-packages;
               	 some-python-package = python-packages.callPackage (
               	   {buildPythonPackage, ...}: buildPythonPackage { ... }
               in [some-python-package];
             python-with-my-packages = python3.withPackages my-python-packages;
           in python-with-my-packages;

I hope I did not make any typos, but you should get the gist.

My question is - right now I’m testing this by doing:

# nixos-rebuild switch --flake .#hostX

which basically rebuilds the whole system. That has two drawbacks:

  • It adds entries into the list of nixos generations
  • It’s building much more than it should

for situations where I just want to add and tweak a single package’s build process.

Is there a way for me to build only this particular part of the flake (without necessarily deploying it where it should normally go)?


Without a detailed analysis, I see two options:

  • Develop your package in a separate flake and use it as an input to your NixOS system configuration flake.
  • Expose your package as a separate flake output. For example:
outputs = inputs: {
   packages."<system>"."<name>" = derivation;

Then you can build your package separately using nix build .#<name>.