summaryrefslogtreecommitdiff
path: root/hosts/vidhar/default.nix
blob: 1d8f4c3cd3bb71196bdc312bc20f5b5cc67d7140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
{ hostName, flake, config, pkgs, lib, ... }:
{
  imports = with flake.nixosModules.systemProfiles; [
    ./zfs.nix ./dsl.nix
    initrd-all-crypto-modules default-locale openssh rebuild-machines
    build-server
    initrd-ssh
  ];

  config = {
    nixpkgs = {
      system = "x86_64-linux";
    };

    networking.hostId = "1e7ddd78";
    environment.etc."machine-id".text = "1e7ddd784c525bba2a03d7c160c5da4e";

    boot = {
      loader.grub = {
        enable = true;
        version = 2;
        device = "/dev/disk/by-id/ata-SuperMicro_SSD_SMC0515D95019BDF4083";
      };

      kernelPackages = pkgs.linuxPackages_latest;
      kernelModules = [ "kvm-intel" ];

      kernelParams = [
        "ip=10.141.0.1:::255.255.255.0::eno1:static"
      ];

      tmpOnTmpfs = true;

      initrd = {
        supportedFilesystems = [ "zfs" ];
        availableKernelModules = [ "ehci_pci" "ahci" "nvme" "isci" "xhci_pci" "usb_storage" "usbhid" "sd_mod" "sr_mod" "drbg" "rtsx_pci_sdmmc" "libsas" "scsi_transport_sas" "e1000e" ];
        kernelModules = [ "dm-raid" "dm-integrity" "dm-snapshot" "dm-thin-pool" ];

        luks.devices = {
          nvm0.device = "/dev/disk/by-label/${hostName}-nvm0";
          nvm1.device = "/dev/disk/by-label/${hostName}-nvm1";
          
          hdd0.device = "/dev/disk/by-label/${hostName}-hdd0";
          hdd1.device = "/dev/disk/by-label/${hostName}-hdd1";
          hdd2.device = "/dev/disk/by-label/${hostName}-hdd2";
          hdd3.device = "/dev/disk/by-label/${hostName}-hdd3";
          hdd4.device = "/dev/disk/by-label/${hostName}-hdd4";
          hdd5.device = "/dev/disk/by-label/${hostName}-hdd5";
        };

        network.flushBeforeStage2 = false;
      };
      
      supportedFilesystems = [ "zfs" ];
      zfs = {
        enableUnstable = true;
      };
    };

    fileSystems = {
      "/" = {
        fsType = "tmpfs";
        options = [ "mode=0755" ];
      };
    };

    networking = {
      hostName = "vidhar";
      domain = "yggdrasil";
      search = [ "yggdrasil" ];

      useDHCP = false;
      useNetworkd = true;

      interfaces."eno1" = {
        ipv4.addresses = [
          { address = "10.141.0.1"; prefixLength = 24; }
        ];
      };

      firewall = {
        enable = true;
        allowPing = true;
        allowedTCPPorts = [
          22 # ssh
        ];
        allowedUDPPorts = [
          51820 # wireguard
        ];
        allowedUDPPortRanges = [
          { from = 60000; to = 61000; } # mosh
        ];
        extraCommands = ''
          ip46tables -D FORWARD -j nixos-fw-forward 2>/dev/null || true
          ip46tables -F nixos-fw-forward 2>/dev/null || true
          ip46tables -X nixos-fw-forward 2>/dev/null || true
          ip46tables -N nixos-fw-forward

          ip46tables -A nixos-fw-forward -i eno1 -j ACCEPT
          ip46tables -A nixos-fw-forward -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
          ip6tables -A nixos-fw-forward -p icmpv6 --icmpv6-type redirect -j nixos-fw-log-refuse
          ip6tables -A nixos-fw-forward -p icmpv6 --icmpv6-type 139 -j nixos-fw-log-refuse
          ip6tables -A nixos-fw-forward -p icmpv6 -j ACCEPT

          ip46tables -A nixos-fw-forward -j nixos-fw-log-refuse
          ip46tables -A FORWARD -j nixos-fw-forward


          ip46tables -t nat -D POSTROUTING -j nixos-fw-postrouting 2>/dev/null || true
          ip46tables -t nat -F nixos-fw-postrouting 2>/dev/null || true
          ip46tables -t nat -X nixos-fw-postrouting 2>/dev/null || true
          ip46tables -t nat -N nixos-fw-postrouting

          iptables -t nat -A nixos-fw-postrouting -o dsl -j MASQUERADE

          ip46tables -t nat -A POSTROUTING -j nixos-fw-postrouting


          ip46tables -t mangle -D POSTROUTING -j nixos-fw-postrouting 2>/dev/null || true
          ip46tables -t mangle -F nixos-fw-postrouting 2>/dev/null || true
          ip46tables -t mangle -X nixos-fw-postrouting 2>/dev/null || true

          ip46tables -t mangle -N nixos-fw-postrouting
          ip46tables -t mangle -A nixos-fw-postrouting -o dsl -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

          ip46tables -t mangle -A POSTROUTING -j nixos-fw-postrouting
        '';
      };
    };

    services.dhcpd4 = {
      enable = true;
      interfaces = [ "eno1" ];
      extraConfig = ''
        subnet 10.141.0.0 netmask 255.255.255.0 {
          range 10.141.0.128 10.141.0.254;
          option domain-name-servers 1.1.1.1, 1.0.0.1;
          option broadcast-address 10.141.0.255;
          option routers 10.141.0.1;
          option domain-name "asgard.yggdrasil";
        }
      '';
    };
    services.corerad = {
      enable = true;
      settings = {
        interfaces = [
          { name = config.networking.pppInterface;
            monitor = true;
            verbose = true;
          }
          { name = "eno1";
            advertise = true;
            verbose = true;
            prefix = [{ prefix = "::/64"; }];
            route = [{ prefix = "::/0"; }];
          }
        ];
      };
    };
    boot.kernel.sysctl = {
      "net.ipv6.conf.all.forwarding" = true;
      "net.ipv6.conf.default.forwarding" = true;
      "net.ipv4.conf.all.forwarding" = true;
      "net.ipv4.conf.default.forwarding" = true;
    };
    systemd.network.networks = {
      "eno2" = {
        matchConfig.Name = "eno2";
        networkConfig.LinkLocalAddressing = "no";
      };
      "telekom" = {
        matchConfig.Name = "telekom";
        networkConfig.LinkLocalAddressing = "no";
      };
    };
    systemd.services."dhcpcd-telekom" = {
      wantedBy = [ "multi-user.target" "network-online.target" ];
      bindsTo = [ "pppd-telekom.service" ];
      after = [ "pppd-telekom.service" ];
      wants = [ "network.target" ];
      before = [ "network-online.target" ];

      path = with pkgs; [ dhcpcd nettools openresolv ];
      unitConfig.ConditionCapability = "CAP_NET_ADMIN";

      stopIfChanged = false;

      serviceConfig = let
        dhcpcdConf = pkgs.writeText "dhcpcd.conf" ''
          duid
          vendorclassid
          ipv6only

          nooption domain_name_servers, domain_name, domain_search
          option classless_static_routes
          option interface_mtu

          option host_name
          option rapid_commit
          require dhcp_server_identifier
          slaac private

          noipv6rs                 # disable routing solicitation
          allowinterfaces dsl
          interface dsl
            ipv6rs                 # enable routing solicitation for WAN adapter
            ia_na 1                # request an IPv6 address
            ia_pd 1 eno1/0         # request a PD and assign it to the LAN

          waitip 6
        '';
      in {
        Type = "forking";
        PIDFile = "/run/dhcpcd.pid";
        ExecStart = "@${pkgs.dhcpcd}/sbin/dhcpcd dhcpcd --config ${dhcpcdConf}";
        ExecReload = "${pkgs.dhcpcd}/sbin/dhcpcd --rebind";
        Restart = "always";
      };
    };
    
    services.timesyncd.enable = false;
    services.chrony = {
      enable = true;
      servers = [];
      extraConfig = ''
        pool time.cloudflare.com iburst nts
        pool nts.ntp.se iburst nts
        server nts.sth1.ntp.se iburst nts
        server nts.sth2.ntp.se iburst nts
        server ptbtime1.ptb.de iburst nts
        server ptbtime2.ptb.de iburst nts
        server ptbtime3.ptb.de iburst nts

        makestep 0.1 3

        cmdport 0
      '';
    };

    services.openssh = {
      enable = true;
      passwordAuthentication = false;
      challengeResponseAuthentication = false;
      extraConfig = ''
        AllowGroups ssh
      '';
    };
    users.groups."ssh" = {
      members = ["root"];
    };

    security.sudo.extraConfig = ''
      Defaults lecture = never
    '';

    nix.gc = {
      automatic = true;
      options = "--delete-older-than 30d";
    };

    powerManagement = {
      enable = true;

      cpuFreqGovernor = "schedutil";
    };
  };
}