feat(ssh): add enable option

This commit is contained in:
2026-05-22 10:23:22 +02:00
parent a5adea70ea
commit 086e091add
8 changed files with 93 additions and 70 deletions

View File

@@ -59,6 +59,7 @@
shell.bash.aliases.lang-js = true; shell.bash.aliases.lang-js = true;
shell.bash.addBinToPath = true; shell.bash.addBinToPath = true;
audio.enable = true; audio.enable = true;
ssh.enable = true;
music.enable = true; music.enable = true;
terminal.enable = true; terminal.enable = true;
devenv.enable = true; devenv.enable = true;

View File

@@ -56,6 +56,7 @@
shell.bash.addBinToPath = true; shell.bash.addBinToPath = true;
my.yubikey.enable = true; my.yubikey.enable = true;
audio.enable = true; audio.enable = true;
ssh.enable = true;
music.enable = true; music.enable = true;
terminal.enable = true; terminal.enable = true;
devenv.enable = true; devenv.enable = true;

View File

@@ -1,10 +1,13 @@
{ {
myUtils, config,
lib, lib,
pkgs, pkgs,
myUtils,
... ...
}: }:
let let
cfg = config.ssh;
hostDir = ../../hosts; hostDir = ../../hosts;
hostNames = myUtils.dirNames hostDir; hostNames = myUtils.dirNames hostDir;
hostsWithKeys = lib.filter ( hostsWithKeys = lib.filter (
@@ -12,6 +15,9 @@ let
) hostNames; ) hostNames;
in in
{ {
options.ssh.enable = lib.mkEnableOption "ssh";
config = lib.mkIf cfg.enable {
home.packages = with pkgs; [ sshfs ]; home.packages = with pkgs; [ sshfs ];
programs.ssh = { programs.ssh = {
@@ -39,4 +45,5 @@ in
}; };
}; };
}; };
};
} }

View File

@@ -16,8 +16,8 @@ in
}; };
}; };
# auto generate authorized_keys from `authorizedHosts` config = lib.mkIf config.ssh.enable {
config.users.users.${username}.openssh.authorizedKeys.keys = users.users.${username}.openssh.authorizedKeys.keys =
lib.flatten ( lib.flatten (
map ( map (
hostname: hostname:
@@ -28,4 +28,5 @@ in
) ((builtins.filter (h: h != config.host.name) adminHosts) ++ config.ssh.authorizedHosts) ) ((builtins.filter (h: h != config.host.name) adminHosts) ++ config.ssh.authorizedHosts)
) )
++ lib.splitString "\n" (builtins.readFile ./ssh_bak.pub); ++ lib.splitString "\n" (builtins.readFile ./ssh_bak.pub);
};
} }

View File

@@ -1,10 +1,17 @@
{ lib, ... }: { lib, config, ... }:
let
cfg = config.ssh;
in
{ {
imports = [ ./hardened-openssh.nix ]; imports = [ ./hardened-openssh.nix ];
config.services.openssh = { options.ssh.enable = lib.mkEnableOption "SSH server";
config = lib.mkIf cfg.enable {
services.openssh = {
enable = lib.mkDefault true; enable = lib.mkDefault true;
harden = lib.mkDefault true; harden = lib.mkDefault true;
}; };
};
} }

View File

@@ -1,9 +1,10 @@
{ lib, config, ... }: { lib, config, ... }:
let let
inherit (config.host) username; inherit (config.host) username;
in in
{ {
# auto extract SSH keys config = lib.mkIf config.ssh.enable {
system.activationScripts.extractSshKeys = lib.stringAfter [ "etc" ] '' system.activationScripts.extractSshKeys = lib.stringAfter [ "etc" ] ''
HOST_KEY="/etc/ssh/ssh_host_ed25519_key.pub" HOST_KEY="/etc/ssh/ssh_host_ed25519_key.pub"
HOST_DIR="/home/${username}/nix/hosts/${config.networking.hostName}" HOST_DIR="/home/${username}/nix/hosts/${config.networking.hostName}"
@@ -29,4 +30,5 @@ in
chmod 644 "$HOST_DIR/ssh_user.pub" chmod 644 "$HOST_DIR/ssh_user.pub"
fi fi
''; '';
};
} }

View File

@@ -1,5 +1,7 @@
{ lib, config, ... }: { lib, config, ... }:
with lib; with lib;
let let
cfg = config.services.openssh; cfg = config.services.openssh;
in in
@@ -13,7 +15,7 @@ in
options.services.openssh.harden = mkEnableOption "harden ssh server configuration"; options.services.openssh.harden = mkEnableOption "harden ssh server configuration";
config = { config = {
networking.firewall.allowedTCPPorts = [ 22 ]; networking.firewall.allowedTCPPorts = lib.mkIf config.ssh.enable [ 22 ];
services.openssh.settings = optionalAttrs cfg.harden { services.openssh.settings = optionalAttrs cfg.harden {
PermitRootLogin = "no"; PermitRootLogin = "no";

View File

@@ -4,6 +4,7 @@
outputs, outputs,
... ...
}: }:
let let
hosts = lib.attrNames outputs.nixosConfigurations; hosts = lib.attrNames outputs.nixosConfigurations;
hostsWithKeys = lib.filter ( hostsWithKeys = lib.filter (
@@ -11,9 +12,10 @@ let
) hosts; ) hosts;
in in
{ {
# auto generate known_hosts for all hosts in flake config = lib.mkIf config.ssh.enable {
programs.ssh.knownHosts = lib.genAttrs hostsWithKeys (hostname: { programs.ssh.knownHosts = lib.genAttrs hostsWithKeys (hostname: {
publicKeyFile = ../../hosts/${hostname}/ssh_host.pub; publicKeyFile = ../../hosts/${hostname}/ssh_host.pub;
extraHostNames = lib.optional (hostname == config.networking.hostName) "localhost"; extraHostNames = lib.optional (hostname == config.networking.hostName) "localhost";
}); });
};
} }