Skip to content

With flake-parts

Add flake-aspects to your flake inputs and import its flakeModule:

{
inputs.flake-aspects.url = "github:vic/flake-aspects";
outputs = { flake-parts, flake-aspects, nixpkgs, ... }@inputs:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [ flake-aspects.flakeModule ];
flake.aspects = {
my-desktop = {
nixos = { pkgs, ... }: { environment.systemPackages = [ pkgs.firefox ]; };
darwin = { pkgs, ... }: { environment.systemPackages = [ pkgs.firefox ]; };
};
my-server = {
nixos = { services.nginx.enable = true; };
};
};
flake.nixosConfigurations.workstation = nixpkgs.lib.nixosSystem {
modules = [
inputs.self.modules.nixos.my-desktop
];
};
};
}

The flakeModule calls new with a callback that:

  1. Creates flake.aspects as a user-facing option (type: aspectsType).
  2. Sets flake.modules to the transposed + resolved output.

After evaluation, flake.modules.<class>.<aspect> contains fully-resolved Nix modules ready for use in nixosSystem, darwinSystem, evalModules, etc.

If you also import flake-parts.flakeModules.modules, the resolved modules are exposed as inputs.self.modules:

imports = [
flake-aspects.flakeModule
flake-parts.flakeModules.modules # exposes flake.modules as output
];

Then in your system configs: inputs.self.modules.nixos.my-desktop.

Aspects can reference each other via the aspects fixpoint argument:

flake.aspects = { aspects, ... }: {
base-tools = {
nixos = { pkgs, ... }: { environment.systemPackages = with pkgs; [ git curl ]; };
};
workstation = {
includes = [ aspects.base-tools ];
nixos = { pkgs, ... }: { environment.systemPackages = [ pkgs.vscode ]; };
};
};

Resolving workstation for "nixos" produces { imports = [ workstation-nixos, base-tools-nixos ] }.

Contribute Community Sponsor