nixos-config/system/server/initssh.nix

101 lines
2.5 KiB
Nix
Raw Normal View History

2019-10-24 02:20:38 +02:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.configuration.init-ssh;
in {
options.configuration.init-ssh = {
enable = mkOption {
default = "disable";
type = with types; enum [ "disable" "prepare" "enabled" ];
};
2019-12-20 05:54:26 +01:00
kernelModules = mkOption { type = with types; listOf str; };
2019-10-24 02:20:38 +02:00
port = mkOption {
default = 23;
type = with types; int;
};
authorizedKeys = mkOption {
type = with types; listOf str;
2019-12-20 05:54:26 +01:00
default = config.users.users.root.openssh.authorizedKeys.keys
++ (map (keyFile: lib.fileContents keyFile)
config.users.users.root.openssh.authorizedKeys.keyFiles);
2019-10-24 02:20:38 +02:00
};
hostECDSAKey = mkOption {
default = null;
type = with types; nullOr path;
description = ''
you only need one host key
nix-shell -p dropbear --run "dropbearkey -t ecdsa -f ./host_ecdsa_key"
'';
};
};
config = mkMerge [
(mkIf (cfg.enable != "disable") {
services.tor = {
enable = true;
client.enable = true;
2019-12-20 05:54:26 +01:00
hiddenServices.bootup.map = [{ port = 23; }];
2019-10-24 02:20:38 +02:00
};
})
(mkIf (cfg.enable == "enabled") {
# tor setup
2019-12-20 05:54:26 +01:00
boot.initrd.secrets = {
2019-10-24 02:20:38 +02:00
"/etc/tor/onion/bootup" = /var/lib/tor/onion/bootup;
};
boot.initrd.extraUtilsCommands = ''
copy_bin_and_libs ${pkgs.tor}/bin/tor
'';
2020-05-16 14:49:55 +02:00
boot.initrd.network.postCommands = let
torRc = (pkgs.writeText "tor.rc" ''
DataDirectory /etc/tor
SOCKSPort 127.0.0.1:9050 IsolateDestAddr
SOCKSPort 127.0.0.1:9063
HiddenServiceDir /etc/tor/onion/bootup
HiddenServicePort ${toString cfg.port} 127.0.0.1:${toString cfg.port}
'');
in ''
2019-12-20 05:54:26 +01:00
echo "tor: preparing onion folder"
# have to do this otherwise tor does not want to start
chmod -R 700 /etc/tor
2020-05-20 00:31:13 +02:00
echo "make sure localhost is up"
ip a a 127.0.0.1/8 dev lo
2020-05-20 00:59:58 +02:00
# ifconfig lo up
ip link set lo up
2020-05-20 00:31:13 +02:00
2019-12-20 05:54:26 +01:00
echo "tor: starting tor"
2020-05-16 14:49:55 +02:00
tor -f ${torRc} --verify-config
tor -f ${torRc} &
2019-12-20 05:54:26 +01:00
'';
# ssh setup
# todo add the ssh host fingerprint to your trusted stuff
# todo set ssh host key here
boot.initrd.network.enable = true;
boot.initrd.network.ssh = {
enable = true;
authorizedKeys = cfg.authorizedKeys;
port = cfg.port;
};
boot.initrd.availableKernelModules = cfg.kernelModules;
boot.initrd.network.ssh.hostECDSAKey = cfg.hostECDSAKey;
2019-10-24 02:20:38 +02:00
})
];
}