nix-stuff/roles/server/ssh.nix
2024-12-05 09:16:25 +01:00

83 lines
2 KiB
Nix
Executable file

{
flakeSelf,
config,
pkgs,
lib,
...
}: let
hostSecrets = "${flakeSelf.inputs.secrets}/" + config.networking.hostName + "/";
hostKeyPath = "/etc/ssh/everest_host_key";
notify =
pkgs.writers.writePython3 "send-discord-login-notification" {
libraries = [pkgs.python3Packages.requests];
} ''
import requests
import os
if os.environ["PAM_TYPE"] != "open_session":
raise SystemExit
print("User logging in")
secretPath = "${config.age.secrets.discord-webhook.path}"
webhookUrl: str
with open(secretPath) as file:
webhookUrl = file.read().strip()
user = os.environ["PAM_USER"]
rhost = os.environ["PAM_RHOST"]
data = {
"username": "SSH Login",
"content": user + " logged in from " + rhost
}
result = requests.post(webhookUrl, json=data)
'';
in {
age.secrets = {
everest-host-key = {
file = hostSecrets + "host-private-key.age";
path = hostKeyPath;
mode = "0400";
};
"everest-host-key.pub" = {
file = hostSecrets + "host-public-key.age";
path = hostKeyPath + ".pub";
};
discord-webhook = {
file = hostSecrets + "discord-webhook.age";
};
};
users.users.toast.openssh.authorizedKeys.keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEas0RvecJpUA1rG87fHunxDb4O0Q7bQG7X8h3hZnrsE Everest access key"
];
services.openssh = {
enable = true;
settings = {
UseDns = true;
PermitRootLogin = "no";
PasswordAuthentication = false;
};
# The forgejo module is fucky so I can't set this with the nixos option
# https://github.com/NixOS/nixpkgs/issues/306205
extraConfig = ''
AcceptEnv COLORTERM
'';
hostKeys = [
{
path = hostKeyPath;
type = "ed25519";
comment = "Everest host key";
}
];
startWhenNeeded = true;
};
security.pam.services.sshd.text = lib.mkDefault (lib.mkAfter ''
session optional pam_exec.so debug stdout ${notify}
'');
}