2016-02-02 16:25:18 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.boot.initrd.network.ssh;
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
boot.initrd.network.ssh.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Start SSH service during initrd boot. It can be used to debug failing
|
|
|
|
boot on a remote server, enter pasphrase for an encrypted partition etc.
|
|
|
|
Service is killed when stage-1 boot is finished.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.initrd.network.ssh.port = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 22;
|
|
|
|
description = ''
|
|
|
|
Port on which SSH initrd service should listen.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.initrd.network.ssh.shell = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/bin/ash";
|
|
|
|
description = ''
|
|
|
|
Login shell of the remote user. Can be used to limit actions user can do.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.initrd.network.ssh.hostRSAKey = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
RSA SSH private key file in the Dropbear format.
|
|
|
|
|
2017-04-02 19:35:45 +00:00
|
|
|
WARNING: Unless your bootloader supports initrd secrets, this key is
|
|
|
|
contained insecurely in the global Nix store. Do NOT use your regular
|
|
|
|
SSH host private keys for this purpose or you'll expose them to
|
|
|
|
regular users!
|
2016-02-02 16:25:18 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.initrd.network.ssh.hostDSSKey = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
DSS SSH private key file in the Dropbear format.
|
|
|
|
|
2017-04-02 19:35:45 +00:00
|
|
|
WARNING: Unless your bootloader supports initrd secrets, this key is
|
|
|
|
contained insecurely in the global Nix store. Do NOT use your regular
|
|
|
|
SSH host private keys for this purpose or you'll expose them to
|
|
|
|
regular users!
|
2016-02-02 16:25:18 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.initrd.network.ssh.hostECDSAKey = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
ECDSA SSH private key file in the Dropbear format.
|
|
|
|
|
2017-04-02 19:35:45 +00:00
|
|
|
WARNING: Unless your bootloader supports initrd secrets, this key is
|
|
|
|
contained insecurely in the global Nix store. Do NOT use your regular
|
|
|
|
SSH host private keys for this purpose or you'll expose them to
|
|
|
|
regular users!
|
2016-02-02 16:25:18 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.initrd.network.ssh.authorizedKeys = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = config.users.extraUsers.root.openssh.authorizedKeys.keys;
|
|
|
|
description = ''
|
|
|
|
Authorized keys for the root user on initrd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-02-03 10:37:19 +00:00
|
|
|
config = mkIf (config.boot.initrd.network.enable && cfg.enable) {
|
2016-09-20 09:04:48 +00:00
|
|
|
assertions = [
|
|
|
|
{ assertion = cfg.authorizedKeys != [];
|
|
|
|
message = "You should specify at least one authorized key for initrd SSH";
|
|
|
|
}
|
|
|
|
];
|
2016-02-02 16:25:18 +00:00
|
|
|
|
|
|
|
boot.initrd.extraUtilsCommands = ''
|
|
|
|
copy_bin_and_libs ${pkgs.dropbear}/bin/dropbear
|
2016-05-07 12:38:34 +00:00
|
|
|
cp -pv ${pkgs.glibc.out}/lib/libnss_files.so.* $out/lib
|
2016-02-02 16:25:18 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
boot.initrd.extraUtilsCommandsTest = ''
|
|
|
|
$out/bin/dropbear -V
|
|
|
|
'';
|
|
|
|
|
|
|
|
boot.initrd.network.postCommands = ''
|
2016-02-03 13:37:10 +00:00
|
|
|
echo '${cfg.shell}' > /etc/shells
|
|
|
|
echo 'root:x:0:0:root:/root:${cfg.shell}' > /etc/passwd
|
|
|
|
echo 'passwd: files' > /etc/nsswitch.conf
|
2016-02-02 16:25:18 +00:00
|
|
|
|
2016-02-03 13:37:10 +00:00
|
|
|
mkdir -p /var/log
|
|
|
|
touch /var/log/lastlog
|
2016-02-02 16:25:18 +00:00
|
|
|
|
2016-02-03 13:37:10 +00:00
|
|
|
mkdir -p /etc/dropbear
|
2016-02-02 16:25:18 +00:00
|
|
|
|
2016-02-03 13:37:10 +00:00
|
|
|
mkdir -p /root/.ssh
|
|
|
|
${concatStrings (map (key: ''
|
2016-11-16 14:17:42 +00:00
|
|
|
echo ${escapeShellArg key} >> /root/.ssh/authorized_keys
|
2016-02-03 13:37:10 +00:00
|
|
|
'') cfg.authorizedKeys)}
|
2016-02-02 16:25:18 +00:00
|
|
|
|
2018-03-02 02:21:27 +00:00
|
|
|
dropbear -s -j -k -E -p ${toString cfg.port} ${optionalString (cfg.hostRSAKey == null && cfg.hostDSSKey == null && cfg.hostECDSAKey == null) "-R"}
|
2016-02-02 16:25:18 +00:00
|
|
|
'';
|
|
|
|
|
2017-04-02 19:35:45 +00:00
|
|
|
boot.initrd.secrets =
|
|
|
|
(optionalAttrs (cfg.hostRSAKey != null) { "/etc/dropbear/dropbear_rsa_host_key" = cfg.hostRSAKey; }) //
|
|
|
|
(optionalAttrs (cfg.hostDSSKey != null) { "/etc/dropbear/dropbear_dss_host_key" = cfg.hostDSSKey; }) //
|
|
|
|
(optionalAttrs (cfg.hostECDSAKey != null) { "/etc/dropbear/dropbear_ecdsa_host_key" = cfg.hostECDSAKey; });
|
|
|
|
|
2016-02-02 16:25:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|