feat: introduce guests as nested-container definitions
Add a third auto-loaded kind beside the host and the module: the guest, a reusable definition under guests/ that a host enables like a module and that realizes its interior as a systemd-nspawn nested container. Split the shared base config so a guest can stand on it. base.nix now holds the substrate both bases share — the primary user, home-manager, and the unstable/stable overlays. system.nix keeps the host-only machinery, and a new guest.nix is the slim guest-base: it imports the full modules tree, pins the interior release, and auto-enables the toolkit bundle and SSH so any guest is workable on sight. Give modules.ssh a guest flavor. A host restores its host keys from secrets as before, while a guest sets hostKeys.restore = false, names no sops files, and self-generates a host key, so it holds no age key of its own. The lib grows a guest helper that declares the guests.<path> namespace with an enable and a backend field. Only the container backend is built; microvm is a reserved value that trips a clear build-time assertion rather than silently building nothing. A sample guest exercises the whole path, and neogaia enables it, so the guest interior builds through the existing nix flake check seam.
This commit was merged in pull request #28.
This commit is contained in:
102
modules/ssh.nix
102
modules/ssh.nix
@@ -53,11 +53,25 @@ in
|
||||
'';
|
||||
};
|
||||
|
||||
hostKeys.restore = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = true;
|
||||
description = ''
|
||||
Restore the host keys from secrets rather than letting the daemon
|
||||
generate its own.
|
||||
|
||||
A machine with its own identity keeps its fingerprint across a reimage
|
||||
by restoring committed keys. A guest carries no host identity, so it
|
||||
turns this off and presents a self-generated key instead.
|
||||
'';
|
||||
};
|
||||
|
||||
hostKeys.sopsFile = lib.mkOption {
|
||||
type = lib.types.path;
|
||||
type = lib.types.nullOr lib.types.path;
|
||||
default = null;
|
||||
description = ''
|
||||
Encrypted file holding this host's SSH host private keys, one entry per
|
||||
key type, named `ssh-host-<type>-key`.
|
||||
key type, named `ssh-host-<type>-key`. Required when `restore` is on.
|
||||
|
||||
These are the keys the daemon presents to identify itself to connecting
|
||||
clients, not keys used to authenticate anyone to a remote server.
|
||||
@@ -81,10 +95,12 @@ in
|
||||
};
|
||||
|
||||
userKey.sopsFile = lib.mkOption {
|
||||
type = lib.types.path;
|
||||
type = lib.types.nullOr lib.types.path;
|
||||
default = null;
|
||||
description = ''
|
||||
Encrypted file holding this machine's SSH client private key, under the
|
||||
entry `ssh-user-ed25519-key`.
|
||||
entry `ssh-user-ed25519-key`. Left unset on a machine that authenticates
|
||||
to no remote server, such as a guest.
|
||||
|
||||
This is the key the primary user offers to authenticate to a remote
|
||||
server, not a key the daemon presents to identify this machine.
|
||||
@@ -94,45 +110,61 @@ in
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
services.openssh.enable = true;
|
||||
config = lib.mkIf cfg.enable (
|
||||
lib.mkMerge [
|
||||
{
|
||||
services.openssh.enable = true;
|
||||
|
||||
sops.secrets =
|
||||
# The daemon reads its host keys once at startup, so a re-key has to
|
||||
# restart it to take effect.
|
||||
lib.genAttrs (map hostKeySecret cfg.hostKeys.types) (_: {
|
||||
inherit (cfg.hostKeys) sopsFile;
|
||||
mode = "0400";
|
||||
restartUnits = [ "sshd.service" ];
|
||||
# The primary user is the only account reachable over SSH.
|
||||
users.users.${user}.openssh.authorizedKeys.keys = cfg.authorizedKeys;
|
||||
}
|
||||
|
||||
# A machine with its own identity restores its host keys from secrets.
|
||||
(lib.mkIf cfg.hostKeys.restore {
|
||||
assertions = [
|
||||
{
|
||||
assertion = cfg.hostKeys.sopsFile != null;
|
||||
message = "modules.ssh.hostKeys.restore requires modules.ssh.hostKeys.sopsFile to name the encrypted host keys.";
|
||||
}
|
||||
];
|
||||
|
||||
# The daemon reads its host keys once at startup, so a re-key has to
|
||||
# restart it to take effect.
|
||||
sops.secrets = lib.genAttrs (map hostKeySecret cfg.hostKeys.types) (_: {
|
||||
inherit (cfg.hostKeys) sopsFile;
|
||||
mode = "0400";
|
||||
restartUnits = [ "sshd.service" ];
|
||||
});
|
||||
|
||||
# An empty list is what stops the daemon generating keys of its own.
|
||||
services.openssh.hostKeys = [ ];
|
||||
services.openssh.extraConfig = lib.concatMapStrings (
|
||||
type: "HostKey ${config.sops.secrets.${hostKeySecret type}.path}\n"
|
||||
) cfg.hostKeys.types;
|
||||
})
|
||||
// {
|
||||
|
||||
# The client key the primary user offers to remote servers, present only on
|
||||
# a machine that has one.
|
||||
(lib.mkIf (cfg.userKey.sopsFile != null) {
|
||||
# The primary user is the only account that authenticates with this key,
|
||||
# and the mode admits no other.
|
||||
# The client rereads it per connection, so no unit restarts on a re-key.
|
||||
${userKeySecret} = {
|
||||
sops.secrets.${userKeySecret} = {
|
||||
inherit (cfg.userKey) sopsFile;
|
||||
mode = "0400";
|
||||
owner = user;
|
||||
};
|
||||
};
|
||||
|
||||
# An empty list is what stops the daemon generating keys of its own.
|
||||
services.openssh.hostKeys = [ ];
|
||||
services.openssh.extraConfig = lib.concatMapStrings (
|
||||
type: "HostKey ${config.sops.secrets.${hostKeySecret type}.path}\n"
|
||||
) cfg.hostKeys.types;
|
||||
|
||||
# The primary user is the only account reachable over SSH.
|
||||
users.users.${user}.openssh.authorizedKeys.keys = cfg.authorizedKeys;
|
||||
|
||||
# The client reads the decrypted key where it is written, so no copy of it
|
||||
# lives in the user's home to drift from the secret.
|
||||
# Declaring no defaults of home-manager's own leaves every other directive
|
||||
# at the one OpenSSH itself ships.
|
||||
home-manager.users.${user}.programs.ssh = {
|
||||
enable = true;
|
||||
enableDefaultConfig = false;
|
||||
settings."*".IdentityFile = config.sops.secrets.${userKeySecret}.path;
|
||||
};
|
||||
};
|
||||
# The client reads the decrypted key where it is written, so no copy of it
|
||||
# lives in the user's home to drift from the secret.
|
||||
# Declaring no defaults of home-manager's own leaves every other directive
|
||||
# at the one OpenSSH itself ships.
|
||||
home-manager.users.${user}.programs.ssh = {
|
||||
enable = true;
|
||||
enableDefaultConfig = false;
|
||||
settings."*".IdentityFile = config.sops.secrets.${userKeySecret}.path;
|
||||
};
|
||||
})
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user