1 { config, lib, pkgs, ... }:
7 cfg = config.boot.initrd.network.ssh;
8 shell = if cfg.shell == null then "/bin/ash" else cfg.shell;
9 inherit (config.programs.ssh) package;
11 enabled = let initrd = config.boot.initrd; in (initrd.network.enable || initrd.systemd.network.enable) && cfg.enable;
17 options.boot.initrd.network.ssh = {
21 description = lib.mdDoc ''
22 Start SSH service during initrd boot. It can be used to debug failing
23 boot on a remote server, enter pasphrase for an encrypted partition etc.
24 Service is killed when stage-1 boot is finished.
26 The sshd configuration is largely inherited from
27 {option}`services.openssh`.
34 description = lib.mdDoc ''
35 Port on which SSH initrd service should listen.
40 type = types.nullOr types.str;
42 defaultText = ''"/bin/ash"'';
43 description = lib.mdDoc ''
44 Login shell of the remote user. Can be used to limit actions user can do.
49 type = types.listOf (types.either types.str types.path);
52 "/etc/secrets/initrd/ssh_host_rsa_key"
53 "/etc/secrets/initrd/ssh_host_ed25519_key"
55 description = lib.mdDoc ''
56 Specify SSH host keys to import into the initrd.
59 {manpage}`ssh-keygen(1)`
63 ssh-keygen -t rsa -N "" -f /etc/secrets/initrd/ssh_host_rsa_key
64 ssh-keygen -t ed25519 -N "" -f /etc/secrets/initrd/ssh_host_ed25519_key
68 Unless your bootloader supports initrd secrets, these keys
69 are stored insecurely in the global Nix store. Do NOT use
70 your regular SSH host private keys for this purpose or
71 you'll expose them to regular users!
73 Additionally, even if your initrd supports secrets, if
74 you're using initrd SSH to unlock an encrypted disk then
75 using your regular host keys exposes the private keys on
76 your unencrypted boot partition.
81 ignoreEmptyHostKeys = mkOption {
84 description = lib.mdDoc ''
85 Allow leaving {option}`config.boot.initrd.network.ssh` empty,
86 to deploy ssh host keys out of band.
90 authorizedKeys = mkOption {
91 type = types.listOf types.str;
92 default = config.users.users.root.openssh.authorizedKeys.keys;
93 defaultText = literalExpression "config.users.users.root.openssh.authorizedKeys.keys";
94 description = lib.mdDoc ''
95 Authorized keys for the root user on initrd.
99 extraConfig = mkOption {
102 description = lib.mdDoc "Verbatim contents of {file}`sshd_config`.";
107 map (opt: mkRemovedOptionModule ([ "boot" "initrd" "network" "ssh" ] ++ [ opt ]) ''
108 The initrd SSH functionality now uses OpenSSH rather than Dropbear.
110 If you want to keep your existing initrd SSH host keys, convert them with
111 $ dropbearconvert dropbear openssh dropbear_host_$type_key ssh_host_$type_key
112 and then set options.boot.initrd.network.ssh.hostKeys.
113 '') [ "hostRSAKey" "hostDSSKey" "hostECDSAKey" ];
116 # Nix complains if you include a store hash in initrd path names, so
117 # as an awful hack we drop the first character of the hash.
118 initrdKeyPath = path: if isString path
120 else let name = builtins.baseNameOf path; in
121 builtins.unsafeDiscardStringContext ("/etc/ssh/" +
122 substring 1 (stringLength name) name);
124 sshdCfg = config.services.openssh;
128 Port ${toString cfg.port}
130 PasswordAuthentication no
131 AuthorizedKeysFile %h/.ssh/authorized_keys %h/.ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u
132 ChallengeResponseAuthentication no
134 ${flip concatMapStrings cfg.hostKeys (path: ''
135 HostKey ${initrdKeyPath path}
138 KexAlgorithms ${concatStringsSep "," sshdCfg.settings.KexAlgorithms}
139 Ciphers ${concatStringsSep "," sshdCfg.settings.Ciphers}
140 MACs ${concatStringsSep "," sshdCfg.settings.Macs}
142 LogLevel ${sshdCfg.settings.LogLevel}
144 ${if sshdCfg.settings.UseDns then ''
155 assertion = cfg.authorizedKeys != [];
156 message = "You should specify at least one authorized key for initrd SSH";
160 assertion = (cfg.hostKeys != []) || cfg.ignoreEmptyHostKeys;
162 You must now pre-generate the host keys for initrd SSH.
163 See the boot.initrd.network.ssh.hostKeys documentation
169 warnings = lib.optional (config.boot.initrd.systemd.enable && cfg.shell != null) ''
170 Please set 'boot.initrd.systemd.users.root.shell' instead of 'boot.initrd.network.ssh.shell'
173 boot.initrd.extraUtilsCommands = mkIf (!config.boot.initrd.systemd.enable) ''
174 copy_bin_and_libs ${package}/bin/sshd
175 cp -pv ${pkgs.glibc.out}/lib/libnss_files.so.* $out/lib
178 boot.initrd.extraUtilsCommandsTest = mkIf (!config.boot.initrd.systemd.enable) ''
179 # sshd requires a host key to check config, so we pass in the test's
180 tmpkey="$(mktemp initrd-ssh-testkey.XXXXXXXXXX)"
181 cp "${../../../tests/initrd-network-ssh/ssh_host_ed25519_key}" "$tmpkey"
182 # keys from Nix store are world-readable, which sshd doesn't like
184 echo -n ${escapeShellArg sshdConfig} |
185 $out/bin/sshd -t -f /dev/stdin \
190 boot.initrd.network.postCommands = mkIf (!config.boot.initrd.systemd.enable) ''
191 echo '${shell}' > /etc/shells
192 echo 'root:x:0:0:root:/root:${shell}' > /etc/passwd
193 echo 'sshd:x:1:1:sshd:/var/empty:/bin/nologin' >> /etc/passwd
194 echo 'passwd: files' > /etc/nsswitch.conf
196 mkdir -p /var/log /var/empty
197 touch /var/log/lastlog
200 echo -n ${escapeShellArg sshdConfig} > /etc/ssh/sshd_config
202 echo "export PATH=$PATH" >> /etc/profile
203 echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH" >> /etc/profile
206 ${concatStrings (map (key: ''
207 echo ${escapeShellArg key} >> /root/.ssh/authorized_keys
208 '') cfg.authorizedKeys)}
210 ${flip concatMapStrings cfg.hostKeys (path: ''
211 # keys from Nix store are world-readable, which sshd doesn't like
212 chmod 0600 "${initrdKeyPath path}"
218 boot.initrd.postMountCommands = mkIf (!config.boot.initrd.systemd.enable) ''
219 # Stop sshd cleanly before stage 2.
221 # If you want to keep it around to debug post-mount SSH issues,
222 # run `touch /.keep_sshd` (either from an SSH session or in
223 # another initrd hook like preDeviceCommands).
224 if ! [ -e /.keep_sshd ]; then
229 boot.initrd.secrets = listToAttrs
230 (map (path: nameValuePair (initrdKeyPath path) path) cfg.hostKeys);
232 # Systemd initrd stuff
233 boot.initrd.systemd = mkIf config.boot.initrd.systemd.enable {
234 users.sshd = { uid = 1; group = "sshd"; };
235 groups.sshd = { gid = 1; };
237 users.root.shell = mkIf (config.boot.initrd.network.ssh.shell != null) config.boot.initrd.network.ssh.shell;
239 contents."/etc/ssh/authorized_keys.d/root".text =
240 concatStringsSep "\n" config.boot.initrd.network.ssh.authorizedKeys;
241 contents."/etc/ssh/sshd_config".text = sshdConfig;
242 storePaths = ["${package}/bin/sshd"];
245 description = "SSH Daemon";
246 wantedBy = ["initrd.target"];
247 after = ["network.target" "initrd-nixos-copy-secrets.service"];
249 # Keys from Nix store are world-readable, which sshd doesn't
250 # like. If this were a real nix store and not the initrd, we
251 # neither would nor could do this
252 preStart = flip concatMapStrings cfg.hostKeys (path: ''
253 /bin/chmod 0600 "${initrdKeyPath path}"
255 unitConfig.DefaultDependencies = false;
257 ExecStart = "${package}/bin/sshd -D -f /etc/ssh/sshd_config";
259 KillMode = "process";
260 Restart = "on-failure";