Skip to content

From configuration.nix

You have one machine and one configuration.nix. This page moves it to yen. The result builds the same system: the docs check that both give one toplevel drvPath.

before/configuration.nix
# /etc/nixos/configuration.nix, as nixos-generate-config leaves it, plus a few edits.
{ pkgs, ... }:
{
imports = [ ./hardware-configuration.nix ];
boot.loader.systemd-boot.enable = true;
networking.hostName = "laptop";
time.timeZone = "Europe/Berlin";
i18n.defaultLocale = "en_US.UTF-8";
nix.settings.experimental-features = [
"nix-command"
"flakes"
];
services.openssh.enable = true;
users.users.alice = {
isNormalUser = true;
extraGroups = [ "wheel" ];
};
environment.systemPackages = with pkgs; [
git
vim
];
system.stateVersion = "25.05";
}

A configuration.nix mixes three things: data (the host name, the users), features (ssh, tools), and generated hardware. yen keeps them apart:

In configuration.nixIn yen
networking.hostName = "laptop"the instance yen.host.laptop: the name is data
users.users.alice = …the instance yen.user.alice, and one account aspect for every user
services.openssh.enable = truean aspect, active at every host
imports = [ ./hardware-configuration.nix ]machines/<name>/hardware-configuration.nix, found by name
  1. Get the template, and copy your two files over the ones in machines/laptop/:

    Terminal window
    nix flake init -t github:vic/yen#from-configuration-nix
  2. Declare the fleet. A host builds NixOS from its nixos sink. A user lives under the hosts it logs in to.

    modules/fleet.nix
    # The fleet spawns hosts and the users that log in to them.
    {
    yen,
    lib,
    inputs,
    ...
    }:
    {
    yen.kinds.fleet = {
    spawns.host = _: yen.host;
    sinks.out.reduce = yen.lib.reduce.merge;
    };
    yen.kinds.host = {
    spawns.user = s: lib.filterAttrs (_: u: lib.any (yen.lib.same s) u.hosts) yen.user;
    sinks.nixos.out =
    { host, nixos, ... }:
    {
    nixosConfigurations.${host.name} = inputs.nixpkgs.lib.nixosSystem { modules = nixos; };
    };
    };
    yen.kinds.user.fields = {
    hosts = yen.lib.is lib.isList;
    groups = yen.lib.opt (yen.lib.is lib.isList) [ ];
    };
    yen.fleet.main = { };
    yen.flake = { out, ... }: out;
    }
  3. Declare your machine and your people as data:

    modules/laptop.nix
    # The machine and its people: data, not configuration.
    { yen, ... }:
    {
    yen.host.laptop = { };
    yen.user.alice = {
    hosts = [ yen.host.laptop ];
    groups = [ "wheel" ];
    };
    }
  4. Move each part of configuration.nix to an aspect. Each one is active at every host:

    modules/features/machine.nix
    # What every host gets: its generated hardware file, its name, and the boot and locale basics.
    { yen, ... }:
    {
    yen.aspects.machine = {
    at = yen.kinds.host;
    nixos =
    { host, ... }:
    {
    imports = [ (../../machines + "/${host.name}/hardware-configuration.nix") ];
    networking.hostName = host.name;
    boot.loader.systemd-boot.enable = true;
    time.timeZone = "Europe/Berlin";
    i18n.defaultLocale = "en_US.UTF-8";
    nix.settings.experimental-features = [
    "nix-command"
    "flakes"
    ];
    system.stateVersion = "25.05";
    };
    };
    }
    modules/features/ssh.nix
    { yen, ... }:
    {
    yen.aspects.ssh = {
    at = yen.kinds.host;
    nixos.services.openssh.enable = true;
    };
    }
    modules/features/tools.nix
    # `pkgs` stays open: NixOS fills it.
    { yen, ... }:
    {
    yen.aspects.tools = {
    at = yen.kinds.host;
    nixos =
    { pkgs, ... }:
    {
    environment.systemPackages = with pkgs; [
    git
    vim
    ];
    };
    };
    }
  5. Give accounts from user data. A user has no nixos sink, so the account goes up to the host:

    modules/features/accounts.nix
    # Each user gives its account. A user has no `nixos` sink, so the account goes up to the host.
    { yen, ... }:
    {
    yen.aspects.account = {
    at = yen.kinds.user;
    nixos =
    { user, ... }:
    {
    users.users.${user.name} = {
    isNormalUser = true;
    extraGroups = user.groups;
    };
    };
    };
    }
  6. Switch:

    Terminal window
    sudo nixos-rebuild switch --flake .#laptop

Add yen.host.desktop = { };, its machines/desktop/hardware-configuration.nix, and yen.host.desktop to the hosts of each user. Every aspect above is active there too. Continue with From a flake.

Contribute Community Sponsor