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 ]; }; };}How it works
Section titled “How it works”The flakeModule calls new with a callback that:
- Creates
flake.aspectsas a user-facing option (type:aspectsType). - Sets
flake.modulesto the transposed + resolved output.
After evaluation, flake.modules.<class>.<aspect> contains fully-resolved Nix modules ready for use in nixosSystem, darwinSystem, evalModules, etc.
With flake-parts modules output
Section titled “With flake-parts modules output”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.
Using dependencies
Section titled “Using dependencies”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 ] }.