ctucx.git: nixfiles

ctucx' nixfiles

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 
{ inputs, config, lib, pkgs, ... }:

{

  deployment.targetHost = config.networking.secondaryIP4;

  #this enables the following services: restic-server
  deployment.tags       = [ "resticServer" ];

  imports = [
    ./hardware-configuration.nix
    ./impermanence.nix

    # syncthing (and it's backup)
    ./syncthing.nix

    # fedi server
    ./gotosocial.nix

    ./smarthome

    ./scanner-sftp.nix

    ./websites
  ];

  dns.zones."ctu.cx".subdomains."${config.networking.hostName}.home"  = lib.mkIf config.networking.usePBBUplink (pkgs.dns.lib.combinators.host config.networking.primaryIP4 config.networking.primaryIP);
  dns.zones."ctu.cx".subdomains."${config.networking.hostName}".CNAME = lib.mkIf config.networking.usePBBUplink [ "${config.networking.hostName}.home" ];
  dns.zones."ctu.cx".subdomains."home".CNAME                          = lib.mkIf config.networking.usePBBUplink [ "${config.networking.hostName}.home" ];

  age.secrets = {
    restic-server-briefkasten.file = ../../secrets/restic-server/briefkasten.age;
    restic-server-wanderduene.file = ../../secrets/restic-server/wanderduene.age;

    wireguard-privkey.file         = ./. + "/../../secrets/${config.networking.hostName}/wireguard-privkey.age";
  };

  boot = {

    kernel.sysctl = {
      "net.ipv6.conf.all.forwarding"    = true;
      "net.ipv4.conf.all.forwarding"    = true;
      "net.ipv6.conf.enp1s0.forwarding" = lib.mkIf config.networking.usePBBUplink 0;
      "net.ipv6.conf.enp1s0.autoconf"   = lib.mkIf config.networking.usePBBUplink 0;
      "net.ipv6.conf.enp1s0.accept_ra"  = lib.mkIf config.networking.usePBBUplink 0;
    };

    kernelModules = [ "intel_rapl_common" ];

    # seems to make realtek ethernet faster?
    kernelParams = [
      "pcie_aspm=off"
    ];

    loader = {
      systemd-boot.enable = true;
      efi.canTouchEfiVariables = true;
    };

    initrd.network = {
      enable = true;
      ssh    = {
        enable         = true;
        port           = 22;
        hostKeys       = [ /etc/ssh/ssh_host_rsa_key ];
        authorizedKeys = with lib; concatLists (mapAttrsToList (name: user: if elem "wheel" user.extraGroups then user.openssh.authorizedKeys.keys else []) config.users.users);
      };

      postCommands = ''
        echo 'cryptsetup-askpass' >> /root/.profile

      '' + lib.optionalString config.networking.usePBBUplink ''
        sysctl -w net.ipv6.conf.enp1s0.autoconf=0
        sysctl -w net.ipv6.conf.enp1s0.accept_ra=0
      '' + ''

        ip link set dev enp1s0 up

        ip addr add ${config.networking.primaryIP4}/28 dev enp1s0
        ip addr add ${config.networking.secondaryIP4}/8 dev enp1s0
        ip route add default via 195.39.246.41 dev enp1s0 onlink

      '' + lib.optionalString config.networking.usePBBUplink ''
        ip addr add ${config.networking.primaryIP}/128 dev enp1s0
        ip route add default via 2a0f:4ac0:acab::1 dev enp1s0 onlink
      '';
    };

  };

  nix.optimise.automatic = false;
  nix.gc.automatic       = false;

  systemd.network.networks = {
    "40-enp1s0".networkConfig.IPv6AcceptRA = lib.mkIf config.networking.usePBBUplink false;
  };

  services.logind.extraConfig = ''
    # don’t shutdown when power button is short-pressed
    HandlePowerKey=ignore
  '';

  services.udev.extraRules = ''
    ACTION=="add", SUBSYSTEM=="net", ENV{INTERFACE}=="iphone", RUN+="${pkgs.systemd}/bin/networkctl up bruplink",
  '';

  systemd.network.links."10-iphone" = {
    matchConfig.PermanentMACAddress = "aa:ab:b5:18:95:d9";
    linkConfig.Name = "iphone";
  };

  networking = {
    useNetworkd  = true;
    usePBBUplink = true;

    primaryIP    = "2a0f:4ac0:acab::45";
    primaryIP4   = "195.39.246.45";
    secondaryIP4 = "10.0.0.45";

    domain       = "home.ctu.cx";

    nameservers  = [ "195.39.246.41" "2a0f:4ac0:acab::1" ];

    defaultGateway.address   = "195.39.246.41";
    defaultGateway.interface = "enp1s0";

    defaultGateway6 = lib.mkIf config.networking.usePBBUplink{
      address   = "2a0f:4ac0:acab::1";
      interface = "enp1s0";
    };

    vlans.vlan10 = {
      id        = 10;
      interface = "enp1s0";
    };

    bridges.bruplink = {
      interfaces = [ "vlan10" "iphone" ];
    };

    interfaces.enp1s0 = {
      ipv4.addresses = [
        (lib.mkIf config.networking.usePBBUplink {
          address = config.networking.primaryIP4;
          prefixLength = 28;
        })
        {
          address = config.networking.secondaryIP4;
          prefixLength = 8;
        }
      ];

      ipv6.addresses = lib.mkIf config.networking.usePBBUplink [{
        address      = config.networking.primaryIP;
        prefixLength = 62;
      }];
    };

    wireguard = {
      enable = true;

      interfaces.wg-wanderduene = {
        listenPort             = 51820;
        privateKeyFile         = config.age.secrets.wireguard-privkey.path;
        generatePrivateKeyFile = true;
        postSetup              = "ip link set dev wg-wanderduene mtu 1500";
        ips                    = [ "172.17.0.2/24" ];

        peers = [
          {
            persistentKeepalive = 10;
            endpoint            = "46.38.253.139:51821";
            allowedIPs          = [ "172.17.0.0/24" ];
            publicKey           = "hOUeP8RFchzJXyy8DceTFKN9f1VHi9GzZQii0dX2zww=";
          }
        ];
      };
    };

    firewall.allowedTCPPorts = [ 5201 ];
    firewall.allowedUDPPorts = [ 5201 51820 ];
    firewall.trustedInterfaces = [ "wg-wanderduene" "vlan10" "bruplink" ];
    firewall.extraCommands = ''
      iptables  -A nixos-fw -p tcp -s 10.0.0.0/8          -j nixos-fw-accept
      iptables  -A nixos-fw -p udp -s 10.0.0.0/8          -j nixos-fw-accept
      iptables  -A nixos-fw -p tcp -s 195.39.246.32/28    -j nixos-fw-accept
      iptables  -A nixos-fw -p udp -s 195.39.246.32/28    -j nixos-fw-accept
      ip6tables -A nixos-fw -p tcp -s 2a0f:4ac0:acab::/48 -j nixos-fw-accept
      ip6tables -A nixos-fw -p udp -s 2a0f:4ac0:acab::/48 -j nixos-fw-accept
    '';
  };

  services = {
    usbmuxd.enable = true;
    email-notify.enable = true;
    nginx.virtualHosts."${config.networking.fqdn}" = {
      enableACME = lib.mkIf (config.networking.usePBBUplink == false) false;
      forceSSL   = lib.mkIf (config.networking.usePBBUplink == false) false;
      kTLS       = lib.mkIf (config.networking.usePBBUplink == false) false;
    };
    nginx.virtualHosts."restic.${config.networking.hostName}.ctu.cx" = {
      enableACME = lib.mkIf (config.networking.usePBBUplink == false) false;
      forceSSL   = lib.mkIf (config.networking.usePBBUplink == false) false;
      kTLS       = lib.mkIf (config.networking.usePBBUplink == false) false;
    };
  };

  ctucxConfig = {
    programs.yt-dlp.enable   = true;
    programs.ocrmypdf.enable = true;
  };

  system.stateVersion = "22.11"; # Did you read the comment?
  home-manager.users.leah.home.stateVersion = "22.11";

}