added ssh-bootstrap
This commit is contained in:
54
modules/core/ssh-bootstrap.nix
Normal file
54
modules/core/ssh-bootstrap.nix
Normal file
@@ -0,0 +1,54 @@
|
||||
{ config, pkgs, lib, ... }:
|
||||
|
||||
let
|
||||
userName = "free";
|
||||
keyPath = "/home/${userName}/.ssh/factory_login";
|
||||
in
|
||||
{
|
||||
# Ensure SSH dirs exist with correct perms
|
||||
systemd.tmpfiles.rules = [
|
||||
"d /root/.ssh 0700 root root -"
|
||||
"d /home/${userName}/.ssh 0700 ${userName} users -"
|
||||
];
|
||||
|
||||
# Generate keypair if missing (runs once)
|
||||
systemd.services.factory-ssh-keygen = {
|
||||
description = "Generate factory SSH key for ${userName} if missing";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
};
|
||||
path = [ pkgs.openssh pkgs.coreutils ];
|
||||
script = ''
|
||||
if [ ! -f "${keyPath}" ]; then
|
||||
ssh-keygen -q -N "gosovransystems" -t ed25519 -f "${keyPath}"
|
||||
chown ${userName}:users "${keyPath}" "${keyPath}.pub"
|
||||
chmod 600 "${keyPath}"
|
||||
chmod 644 "${keyPath}.pub"
|
||||
fi
|
||||
'';
|
||||
};
|
||||
|
||||
# Pull the public key into root's authorized_keys once it exists
|
||||
systemd.services.factory-ssh-authorize = {
|
||||
description = "Authorize factory SSH key for root";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "factory-ssh-keygen.service" ];
|
||||
requires = [ "factory-ssh-keygen.service" ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
};
|
||||
path = [ pkgs.coreutils ];
|
||||
script = ''
|
||||
if [ -f "${keyPath}.pub" ]; then
|
||||
PUB=$(cat "${keyPath}.pub")
|
||||
mkdir -p /root/.ssh
|
||||
touch /root/.ssh/authorized_keys
|
||||
grep -qxF "$PUB" /root/.ssh/authorized_keys || echo "$PUB" >> /root/.ssh/authorized_keys
|
||||
chmod 600 /root/.ssh/authorized_keys
|
||||
fi
|
||||
'';
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user