@@ -0,0 +1,149 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.boot.initrd.network;
7+
8+in
9+{
10+
11+ options = {
12+
13+ boot.initrd.network.enable = mkOption {
14+ type = types.bool;
15+ default = false;
16+ description = ''
17+ Add network connectivity support to initrd.
18+
19+ Network options are configured via <literal>ip</literal> kernel
20+ option, according to the kernel documentation.
21+ '';
22+ };
23+
24+ boot.initrd.network.ssh.enable = mkOption {
25+ type = types.bool;
26+ default = false;
27+ description = ''
28+ Start SSH service during initrd boot. It can be used to debug failing
29+ boot on a remote server, enter pasphrase for an encrypted partition etc.
30+ Service is killed when stage-1 boot is finished.
31+ '';
32+ };
33+
34+ boot.initrd.network.ssh.port = mkOption {
35+ type = types.int;
36+ default = 22;
37+ description = ''
38+ Port on which SSH initrd service should listen.
39+ '';
40+ };
41+
42+ boot.initrd.network.ssh.shell = mkOption {
43+ type = types.str;
44+ default = "/bin/ash";
45+ description = ''
46+ Login shell of the remote user. Can be used to limit actions user can do.
47+ '';
48+ };
49+
50+ boot.initrd.network.ssh.hostRSAKey = mkOption {
51+ type = types.nullOr types.path;
52+ default = null;
53+ description = ''
54+ RSA SSH private key file in the Dropbear format.
55+
56+ WARNING: This key is contained insecurely in the global Nix store. Do NOT
57+ use your regular SSH host private keys for this purpose or you'll expose
58+ them to regular users!
59+ '';
60+ };
61+
62+ boot.initrd.network.ssh.hostDSSKey = mkOption {
63+ type = types.nullOr types.path;
64+ default = null;
65+ description = ''
66+ DSS SSH private key file in the Dropbear format.
67+
68+ WARNING: This key is contained insecurely in the global Nix store. Do NOT
69+ use your regular SSH host private keys for this purpose or you'll expose
70+ them to regular users!
71+ '';
72+ };
73+
74+ boot.initrd.network.ssh.hostECDSAKey = mkOption {
75+ type = types.nullOr types.path;
76+ default = null;
77+ description = ''
78+ ECDSA SSH private key file in the Dropbear format.
79+
80+ WARNING: This key is contained insecurely in the global Nix store. Do NOT
81+ use your regular SSH host private keys for this purpose or you'll expose
82+ them to regular users!
83+ '';
84+ };
85+
86+ boot.initrd.network.ssh.authorizedKeys = mkOption {
87+ type = types.listOf types.str;
88+ default = config.users.extraUsers.root.openssh.authorizedKeys.keys;
89+ description = ''
90+ Authorized keys for the root user on initrd.
91+ '';
92+ };
93+
94+ };
95+
96+ config = mkIf cfg.enable {
97+
98+ boot.initrd.kernelModules = [ "af_packet" ];
99+
100+ boot.initrd.extraUtilsCommands = ''
101+ copy_bin_and_libs ${pkgs.mkinitcpio-nfs-utils}/bin/ipconfig
102+ '' + optionalString cfg.ssh.enable ''
103+ copy_bin_and_libs ${pkgs.dropbear}/bin/dropbear
104+
105+ cp -pv ${pkgs.glibc}/lib/libnss_files.so.* $out/lib
106+ '';
107+
108+ boot.initrd.extraUtilsCommandsTest = optionalString cfg.ssh.enable ''
109+ $out/bin/dropbear -V
110+ '';
111+
112+ boot.initrd.postEarlyDeviceCommands = ''
113+ # Search for interface definitions in command line
114+ for o in $(cat /proc/cmdline); do
115+ case $o in
116+ ip=*)
117+ ipconfig $o && hasNetwork=1
118+ ;;
119+ esac
120+ done
121+ '' + optionalString cfg.ssh.enable ''
122+ if [ -n "$hasNetwork" ]; then
123+ mkdir /dev/pts
124+ mount -t devpts devpts /dev/pts
125+
126+ mkdir -p /etc
127+ echo 'root:x:0:0:root:/root:${cfg.ssh.shell}' > /etc/passwd
128+ echo '${cfg.ssh.shell}' > /etc/shells
129+ echo 'passwd: files' > /etc/nsswitch.conf
130+
131+ mkdir -p /var/log
132+ touch /var/log/lastlog
133+
134+ mkdir -p /etc/dropbear
135+ ${optionalString (cfg.ssh.hostRSAKey != null) "ln -s ${cfg.ssh.hostRSAKey} /etc/dropbear/dropbear_rsa_host_key"}
136+ ${optionalString (cfg.ssh.hostDSSKey != null) "ln -s ${cfg.ssh.hostDSSKey} /etc/dropbear/dropbear_dss_host_key"}
137+ ${optionalString (cfg.ssh.hostECDSAKey != null) "ln -s ${cfg.ssh.hostECDSAKey} /etc/dropbear/dropbear_ecdsa_host_key"}
138+
139+ mkdir -p /root/.ssh
140+ ${concatStrings (map (key: ''
141+ echo -n ${escapeShellArg key} >> /root/.ssh/authorized_keys
142+ '') cfg.ssh.authorizedKeys)}
143+
144+ dropbear -s -j -k -E -m -p ${toString cfg.ssh.port}
145+ fi
146+ '';
147+
148+ };
149+}