fixed ssh key generation

This commit is contained in:
Leyla Becker 2025-01-04 23:29:29 -06:00
parent caa08f1c18
commit 0c5b21d60a
2 changed files with 58 additions and 52 deletions

View file

@ -1,6 +1,7 @@
{ {
pkgs, pkgs,
osConfig, osConfig,
config,
... ...
}: { }: {
imports = [ imports = [
@ -106,9 +107,18 @@
}; };
bash.enable = true; bash.enable = true;
openssh.authorizedKeys = [ openssh = {
authorizedKeys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHeItmt8TRW43uNcOC+eIurYC7Eunc0V3LGocQqLaYj leyla@horizon" "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHeItmt8TRW43uNcOC+eIurYC7Eunc0V3LGocQqLaYj leyla@horizon"
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIILimFIW2exEH/Xo7LtXkqgE04qusvnPNpPWSCeNrFkP leyla@defiant"
]; ];
hostKeys = [
{
type = "ed25519";
path = "${config.home.username}_${osConfig.networking.hostName}_ed25519";
}
];
};
}; };
dconf = { dconf = {

View file

@ -12,23 +12,18 @@
}; };
hostKeys = lib.mkOption { hostKeys = lib.mkOption {
type = lib.types.listOf lib.types.attrs; type = lib.types.listOf lib.types.attrs;
default = [ default = [];
{
type = "ed25519";
path = ".ssh/${config.home.username}_${osConfig.networking.hostName}_ed25519";
}
];
example = [ example = [
{ {
type = "rsa"; type = "rsa";
bits = 4096; bits = 4096;
path = ".ssh/${config.home.username}_${osConfig.networking.hostName}_rsa"; path = "${config.home.username}_${osConfig.networking.hostName}_rsa";
rounds = 100; rounds = 100;
openSSHFormat = true; openSSHFormat = true;
} }
{ {
type = "ed25519"; type = "ed25519";
path = ".ssh/${config.home.username}_${osConfig.networking.hostName}_ed25519"; path = "${config.home.username}_${osConfig.networking.hostName}_ed25519";
rounds = 100; rounds = 100;
comment = "key comment"; comment = "key comment";
} }
@ -43,53 +38,54 @@
}; };
config = lib.mkMerge [ config = lib.mkMerge [
{ (
systemd.user.services."${config.home.username}-ssh-keygen" = { lib.mkIf ((builtins.length config.programs.openssh.hostKeys) != 0) {
Unit = { services.ssh-agent.enable = true;
description = "Generate SSH keys for user"; programs.ssh = {
enable = true;
addKeysToAgent = "yes";
}; };
Install = {
wantedBy = ["sshd.target" "multi-user.target" "default.target"];
};
Service = {
ExecStart = "${
pkgs.writeShellScript "ssh-keygen"
''
# Make sure we don't write to stdout, since in case of
# socket activation, it goes to the remote side (#19589).
exec >&2
${lib.flip lib.concatMapStrings config.programs.openssh.hostKeys (k: let systemd.user.services = builtins.listToAttrs (
path = "${config.home.homeDirectory}/${k.path}"; builtins.map (hostKey:
in '' lib.attrsets.nameValuePair "ssh-gen-keys-${hostKey.path}" {
Install = {
WantedBy = ["default.target"];
};
Service = let
path = "${config.home.homeDirectory}/.ssh/${hostKey.path}";
in {
Restart = "always";
Type = "simple";
ExecStart = "${
pkgs.writeShellScript "ssh-gen-keys" ''
if ! [ -s "${path}" ]; then if ! [ -s "${path}" ]; then
if ! [ -h "${path}" ]; then if ! [ -h "${path}" ]; then
rm -f "${path}" rm -f "${path}"
fi fi
mkdir -p "$(dirname '${path}')" mkdir -p "$(dirname '${path}')"
chmod 0755 "$(dirname '${path}')" chmod 0755 "$(dirname '${path}')"
ssh-keygen \ ${pkgs.openssh}/bin/ssh-keygen \
-t "${k.type}" \ -t "${hostKey.type}" \
${lib.optionalString (k ? bits) "-b ${toString k.bits}"} \ ${lib.optionalString (hostKey ? bits) "-b ${toString hostKey.bits}"} \
${lib.optionalString (k ? rounds) "-a ${toString k.rounds}"} \ ${lib.optionalString (hostKey ? rounds) "-a ${toString hostKey.rounds}"} \
${lib.optionalString (k ? comment) "-C '${k.comment}'"} \ ${lib.optionalString (hostKey ? comment) "-C '${hostKey.comment}'"} \
${lib.optionalString (k ? openSSHFormat && k.openSSHFormat) "-o"} \ ${lib.optionalString (hostKey ? openSSHFormat && hostKey.openSSHFormat) "-o"} \
-f "${path}" \ -f "${path}" \
-N "" -N ""
fi fi
'')}
'' ''
}"; }";
KillMode = "process";
Restart = "always";
Type = "simple";
};
}; };
})
config.programs.openssh.hostKeys
);
} }
)
(lib.mkIf osConfig.host.impermanence.enable { (lib.mkIf osConfig.host.impermanence.enable {
home.persistence."/persist${config.home.homeDirectory}" = { home.persistence."/persist${config.home.homeDirectory}" = {
files = lib.lists.flatten ( files = lib.lists.flatten (
builtins.map (hostKey: [hostKey.path "${hostKey.path}.pub"]) config.programs.openssh.hostKeys builtins.map (hostKey: [".ssh/${hostKey.path}" ".ssh/${hostKey.path}.pub"]) config.programs.openssh.hostKeys
); );
}; };
}) })