]> Git — Sourcephile - sourcephile-nix.git/blob - servers/losurdo/networking.nix
nix: add module security.pass
[sourcephile-nix.git] / servers / losurdo / networking.nix
1 { pkgs, lib, config, nodes, ... }:
2 with builtins;
3 let
4 inherit (builtins.extraBuiltins) pass-to-file;
5 inherit (config) networking users;
6 lanIPv4 = "192.168.1.215";
7 lanNet = "192.168.1.0/24";
8 lanIPv4Gateway = "192.168.1.1";
9 in
10 {
11 imports = [
12 networking/nftables.nix
13 ];
14 boot.initrd.network = {
15 enable = true;
16 ssh = {
17 enable = true;
18 # To prevent ssh from freaking out because a different host key is used,
19 # a different port for dropbear is useful
20 # (assuming the same host has also a normal sshd running)
21 port = 2222;
22 # The initrd needs a cleartext key and is built on the host,
23 # hence this key needs to be cleartext on the host.
24 # Moreover building the initrd means that the key will go into the Nix store,
25 # of the host, then of the target on deployment,
26 # because GRUB does not support boot.initrd.secrets
27 # (only systemd-boot does, but sticking to GRUB is more reassuring).
28 # In any case, the initrd is sent to a non-encrypted /boot partition
29 # to be able to start unattended, hence the key will be available
30 # to anyone who has physically access to the disk where /boot is.
31 hostKeys = [
32 (pass-to-file "servers/losurdo/ssh/ecdsa.key"
33 (../../../sec + "/tmp/losurdo.ecdsa.key"))
34 ];
35
36 authorizedKeys = users.users.root.openssh.authorizedKeys.keys;
37 };
38 # This will automatically load the zfs password prompt on login
39 # and kill the other prompt so boot can continue
40 # The pkill zfs kills the zfs load-key from the console
41 # allowing the boot to continue.
42 postCommands = ''
43 echo >>/root/.profile "zfs load-key -a && pkill zfs"
44 '';
45 };
46
47 /* WARNING: using ipconfig (the ip= kernel parameter) IS NOT RELIABLE:
48 a 91.216.110.35/32 becomes a 91.216.110.35/8
49 boot.kernelParams = map
50 (ip: "ip=${ip.clientIP}:${ip.serverIP}:${ip.gatewayIP}:${ip.netmask}:${ip.hostname}:${ip.device}:${ip.autoconf}")
51 [ { clientIP = netIPv4; serverIP = "";
52 gatewayIP = networking.defaultGateway.address;
53 netmask = "255.255.255.255";
54 hostname = ""; device = networking.defaultGateway.interface;
55 autoconf = "off";
56 }
57 { clientIP = lanIPv4; serverIP = "";
58 gatewayIP = "";
59 netmask = "255.255.255.0";
60 hostname = ""; device = "enp2s0";
61 autoconf = "off";
62 }
63 ];
64 */
65 /* DIY network config, but a right one */
66 boot.initrd.preLVMCommands = ''
67 set -x
68
69 # IPv4 lan
70 ip link set enp5s0 up
71 ip address add ${lanIPv4}/32 dev enp5s0
72 ip route add ${lanIPv4Gateway} dev enp5s0
73 ip route add ${lanNet} dev enp5s0 src ${lanIPv4} proto kernel
74 # NOTE: ${lanIPv4}/24 would not work with initrd's ip, hence ${lanNet}
75 ip route add default via ${lanIPv4Gateway} dev enp5s0
76
77 # IPv6 net
78 #ip -6 address add ''${lanIPv6} dev enp5s0
79 #ip -6 route add ''${lanIPv6Gateway} dev enp5s0
80 #ip -6 route add default via ''${lanIPv6Gateway} dev enp5s0
81
82 ip -4 address
83 ip -4 route
84 #ip -6 address
85 #ip -6 route
86
87 set +x
88
89 # Since boot.initrd.network's preLVMCommands won't set hasNetwork=1
90 # we have to run the postCommands ourselves.
91 ${config.boot.initrd.network.postCommands}
92 '';
93 # Workaround https://github.com/NixOS/nixpkgs/issues/56822
94 #boot.initrd.kernelModules = [ "ipv6" ];
95
96 # Useless without an out-of-band access, and unsecure
97 # (though / may still be encrypted at this point).
98 # boot.kernelParams = [ "boot.shell_on_fail" ];
99
100 # Disable IPv6 entirely until it's available
101 boot.kernel.sysctl = {
102 "net.ipv6.conf.enp5s0.disable_ipv6" = 1;
103 };
104
105 networking = rec {
106 hostName = "losurdo";
107 domainBase = "sourcephile";
108 domain = "${domainBase}.fr";
109
110 useDHCP = false;
111 defaultGateway = {
112 address = lanIPv4Gateway;
113 interface = "enp5s0";
114 };
115 /*
116 defaultGateway6 = {
117 address = lanIPv6Gateway;
118 interface = "enp5s0";
119 };
120 */
121 #nameservers = [ ];
122 nftables.ruleset = ''
123 add rule inet filter input iifname "enp5s0" goto net2fw
124 add rule inet filter output oifname "enp5s0" goto fw2net
125 add rule inet filter fw2net ip daddr ${lanNet} counter accept comment "LAN"
126 add rule inet filter fw2net ip daddr 224.0.0.0/4 udp dport 1900 counter accept comment "UPnP"
127 '';
128 interfaces.enp5s0 = {
129 useDHCP = false;
130 ipv4.addresses = [ { address = lanIPv4; prefixLength = 24; } ];
131 ipv4.routes = [ { address = networking.defaultGateway.address; prefixLength = 32; } ];
132
133 /*
134 ipv6.addresses = [ { address = lanIPv6; prefixLength = 64; }
135 { address = "fe80::1"; prefixLength = 10; }
136 ];
137 ipv6.routes = [ { address = networking.defaultGateway6.address; prefixLength = 64; } ];
138 */
139 };
140 interfaces.wlp4s0 = {
141 useDHCP = false;
142 };
143 };
144 }