From 7e351d93114c87946ec8e3e466fd741fc3ffef75 Mon Sep 17 00:00:00 2001 From: ooks-io Date: Mon, 22 Apr 2024 19:05:33 +1200 Subject: [PATCH] feat(ooksphone): add ssh configuration to ooksphone --- home/user/ooks/ooksphone/default.nix | 1 + system/hosts/ooksphone/modules/default.nix | 2 +- system/hosts/ooksphone/modules/openssh.nix | 37 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 system/hosts/ooksphone/modules/openssh.nix diff --git a/home/user/ooks/ooksphone/default.nix b/home/user/ooks/ooksphone/default.nix index b53ebb6..e3beff0 100644 --- a/home/user/ooks/ooksphone/default.nix +++ b/home/user/ooks/ooksphone/default.nix @@ -23,6 +23,7 @@ home.packages = with pkgs; [ pfetch lazygit + openssh ]; programs = { ssh.enable = true; diff --git a/system/hosts/ooksphone/modules/default.nix b/system/hosts/ooksphone/modules/default.nix index 73027cc..ddcf533 100644 --- a/system/hosts/ooksphone/modules/default.nix +++ b/system/hosts/ooksphone/modules/default.nix @@ -1,6 +1,6 @@ { imports = [ ./theme.nix - # ./ssh.nix + # ./openssh.nix ]; } diff --git a/system/hosts/ooksphone/modules/openssh.nix b/system/hosts/ooksphone/modules/openssh.nix new file mode 100644 index 0000000..753c409 --- /dev/null +++ b/system/hosts/ooksphone/modules/openssh.nix @@ -0,0 +1,37 @@ +{ pkgs, config, ... }: + +let + sshdTmpDirectory = "${config.user.home}/sshd-tmp"; + sshdDirectory = "${config.user.home}/sshd"; + pathToPubKey = "..."; + port = 8022; +in + +{ + build.activation.sshd = '' + $DRY_RUN_CMD mkdir $VERBOSE_ARG --parents "${config.user.home}/.ssh" + $DRY_RUN_CMD cat ${pathToPubKey} > "${config.user.home}/.ssh/authorized_keys" + + if [[ ! -d "${sshdDirectory}" ]]; then + $DRY_RUN_CMD rm $VERBOSE_ARG --recursive --force "${sshdTmpDirectory}" + $DRY_RUN_CMD mkdir $VERBOSE_ARG --parents "${sshdTmpDirectory}" + + $VERBOSE_ECHO "Generating host keys..." + $DRY_RUN_CMD ${pkgs.openssh}/bin/ssh-keygen -t rsa -b 4096 -f "${sshdTmpDirectory}/ssh_host_rsa_key" -N "" + + $VERBOSE_ECHO "Writing sshd_config..." + $DRY_RUN_CMD echo -e "HostKey ${sshdDirectory}/ssh_host_rsa_key\nPort ${toString port}\n" > "${sshdTmpDirectory}/sshd_config" + + $DRY_RUN_CMD mv $VERBOSE_ARG "${sshdTmpDirectory}" "${sshdDirectory}" + fi + ''; + + environment.packages = [ + (pkgs.writeScriptBin "sshd-start" '' + #!${pkgs.runtimeShell} + + echo "Starting sshd in non-daemonized way on port ${toString port}" + ${pkgs.openssh}/bin/sshd -f "${sshdDirectory}/sshd_config" -D + '') + ]; +}