Skip to content

Custom Nix Classes

import { Aside } from ‘@astrojs/starlight/components’;

Den natively supports nixos, darwin, and homeManager classes. But you can create your own classes that forward their settings into any target submodule. This is exactly how Home-Manager integration works internally.

den._.forward creates an aspect that takes configs from a source class and inserts them into a target class at a specified path.

Forward a custom class into NixOS top-level:

{ den, lib, ... }:
let
forwarded = { class, aspect-chain }:
den._.forward {
each = lib.singleton class;
fromClass = _: "custom";
intoClass = _: "nixos";
intoPath = _: [ ];
fromAspect = _: lib.head aspect-chain;
};
in {
den.aspects.igloo = {
includes = [ forwarded ];
custom.networking.hostName = "from-custom-class";
};
}

Insert configs into a nested submodule:

den.aspects.igloo = {
includes = [ forwarded ];
nixos.imports = [
{ options.fwd-box = lib.mkOption {
type = lib.types.submoduleWith { modules = [ myModule ]; };
}; }
];
src.items = [ "from-src-class" ];
};

With intoPath = _: [ "fwd-box" ], the src class configs merge into nixos.fwd-box.

Den’s Home-Manager integration is built on forward:

den._.forward {
each = lib.singleton true;
fromClass = _: "homeManager";
intoClass = _: host.class;
intoPath = _: [ "home-manager" "users" user.userName ];
fromAspect = _: userAspect;
}

This takes all homeManager class configs from user aspects and inserts them into home-manager.users.<name> on the host’s OS configuration.

  • User environments: Forward a user class into users.users.<name>
  • Containerization: Forward a container class into systemd-nspawn configs
  • VM configs: Forward a vm class into microvm or QEMU settings
  • Custom tools: Forward into any Nix-configurable system (NixVim, etc.)

The forward function parameters:

ParameterDescription
eachList of items to iterate over
fromClassSource class name (string)
intoClassTarget class name (string)
intoPathAttribute path in target (list of strings)
fromAspectSource aspect to read from