Skip to content

Reusable Libraries vs Personal Fleets

Most people want to share their best NixOS and Home Manager modules across work, personal machines, and teammates — without leaking their usernames, hostnames, or SSH keys, and without forcing everyone to fork the repo.

In Yen, splitting a reusable library (./yen-modules) from a personal fleet (./modules) takes zero custom option boilerplate:

  • Library files declare default aspects (with at), opt-in capabilities (without at), typed settings, and reusable kits.
  • Any downstream flake imports [ inputs.my-lib.yenModules ./modules ], declares its own hosts and users, opts in with use, tunes parameters via settings, and opts out of defaults with drop or drop.subtree.

1. The reusable library (zero personal data)

Section titled “1. The reusable library (zero personal data)”
yen-modules/defaults.nix
{
yen,
std,
types,
...
}:
{
imports = [ std.modules.all ];
# Reusable library modules contain ZERO personal hostnames, usernames, or SSH keys.
# 1. Default baseline feature (consumers can opt out per host with `drop`).
yen.aspects.lib-ssh = {
at = yen.kinds.host;
settings.port = types.opt types.port 22;
nixos =
{ settings, ... }:
{
services.openssh = {
enable = true;
ports = [ settings.port ];
};
};
};
# 2. Opt-in capability feature (no `at`: activates only where a consumer adds `use`).
yen.aspects.lib-dev-rust = {
hm.programs.cargo.enable = true;
nixos.environment.variables.CARGO_INCREMENTAL = "1";
};
# 3. Baseline shell config consumers can drop or extend.
yen.aspects.lib-shell-defaults = {
at = yen.kinds.user;
hm.programs.zsh.enable = true;
};
}

Export that folder directly from your library’s flake.nix:

outputs = { ... }: {
yenModules = ./yen-modules;
};

2. A downstream fleet consuming it with zero forks

Section titled “2. A downstream fleet consuming it with zero forks”

Here a consumer brings their own users (alice and bob) and host (workstation). Alice opts into lib-dev-rust, Bob drops lib-shell-defaults, and workstation tunes lib-ssh to port 2222:

modules/fleet.nix
{ yen, ... }:
{
# Consumer's personal fleet: imports the shared library and adds only local hosts & choices.
yen.user.alice = {
email = "alice@example.org";
# Opt in to the library's Rust capability.
use = [ yen.aspects.lib-dev-rust ];
};
yen.user.bob = {
email = "bob@example.org";
# Opt out of the library's default zsh feature without forking the library.
drop = [ yen.aspects.lib-shell-defaults ];
};
yen.host.workstation = {
addr = "198.51.100.10";
users = [
yen.user.alice
yen.user.bob
];
# Tune the library's SSH port for this host only.
use = [ (yen.aspects.lib-ssh { port = 2222; }) ];
};
}

Inspect the evaluated NixOS and Home Manager outputs:

nix eval .#nixos + .#hm
{
hm = {
workstation = {
alice = {
programs = {
cargo = {
enable = true;
};
zsh = {
enable = true;
};
};
};
};
};
nixos = {
workstation = {
environment = {
variables = {
CARGO_INCREMENTAL = "1";
};
};
services = {
openssh = {
enable = true;
ports = [ 2222 ];
};
};
};
};
}
  • No mkEnableOption boilerplate: an aspect without at is automatically opt-in via use = [ yen.aspects.<name> ].
  • Clean opt-out: drop = [ yen.aspects.<name> ] disables a default on one host or user, while drop.subtree = [ ... ] disables it across a host and all its users.
  • Zero leaks: test your library standalone in CI with a synthetic test user to guarantee no personal hostnames or secrets ever creep into ./yen-modules.

Next, drive Linux, macOS, Kubernetes, and Terraform from one inventory in NixOS, macOS, Kubernetes & Terraform.

ContributeCommunitySponsor