Skip to content

nix-darwin machines

Many options mean the same on NixOS and nix-darwin: networking.hostName, environment.systemPackages, nix.settings. Write them once. A host forwards its os sink to nixos, and a mac forwards it to darwin.

modules/platforms.nix
# Two kinds of machine, one `os` sink: an aspect writes `os` once, and each platform forwards it.
{ yen, inputs, ... }:
{
yen.kinds.fleet = {
spawns = {
host = _: yen.host;
mac = _: yen.mac;
};
sinks.out.reduce = yen.lib.reduce.merge;
};
yen.kinds.host.sinks = {
os.nixos =
{ os, ... }:
{
imports = os;
};
nixos.out =
{ host, nixos, ... }:
{
nixosConfigurations.${host.name} = inputs.nixpkgs.lib.nixosSystem { modules = nixos; };
};
};
yen.kinds.mac.sinks = {
os.darwin =
{ os, ... }:
{
imports = os;
};
darwin.out =
{ mac, darwin, ... }:
{
darwinConfigurations.${mac.name} = inputs.nix-darwin.lib.darwinSystem { modules = darwin; };
};
};
yen.fleet.main = { };
yen.flake = { out, ... }: out;
}
modules/shared.nix
# Written once, for every machine: options that NixOS and nix-darwin share.
{ yen, ... }:
{
yen.aspects.shared = {
at = [
yen.kinds.host
yen.kinds.mac
];
os =
{ scope, pkgs, ... }:
{
networking.hostName = scope.name;
environment.systemPackages = [ pkgs.git ];
nix.settings.experimental-features = [
"nix-command"
"flakes"
];
};
};
}
modules/machines.nix
{ yen, ... }:
{
yen.host.server = { };
yen.mac.studio = { };
yen.aspects.server = {
at = yen.host.server;
nixos = {
boot.loader.grub.device = "nodev";
fileSystems."/" = {
device = "/dev/sda1";
fsType = "ext4";
};
nixpkgs.hostPlatform = "x86_64-linux";
system.stateVersion = "25.05";
};
};
yen.aspects.studio = {
at = yen.mac.studio;
darwin = {
nixpkgs.hostPlatform = "aarch64-darwin";
system.stateVersion = 6;
};
};
}

The routes: shared gives os at each machine, and each os sink feeds its platform’s sink:

flowchart BT
  n6c4c773b6d["fleet main"]
  n04467a35ae["host server"]
  n061ed499d1["mac studio"]
  n04467a35ae -->|out| n6c4c773b6d
  n061ed499d1 -->|out| n6c4c773b6d
Terminal window
sudo nixos-rebuild switch --flake .#server
sudo darwin-rebuild switch --flake .#studio
Contribute Community Sponsor