about summary refs log tree commit diff
path: root/nixos/kanata/configuration.nix
blob: 0d8dc10f38db841764f83f8ddfd1082ceea29531 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
{ config, lib, input, pkgs, ... }:

let
  sefidelKeys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILN14b5Fu+StHeMXq4ClyLG4G+/vCAfS7adxceEFria/ openpgp:0x1D5BCD11" ];
  maintainerKeys = [ ] ++ sefidelKeys;

  poorObfuscation = y: x: "${x}@${y}";
in
{
  imports = [ ];

  deployment = {
    targetHost = "kanata.bee-polaris.ts.net";
    targetUser = "root";
  };

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

  boot.supportedFilesystems = [ "zfs" ];
  networking.hostId = "31cc5527";

  networking.hostName = "kanata";

  # Erase your darlings
  boot.initrd.postDeviceCommands = lib.mkAfter ''
    zfs rollback -r rpool/local/root@blank
  '';

  boot.kernelModules = [ "r8169" ];
  boot.initrd.kernelModules = [ "r8169" ];

  boot.initrd.network.enable = true;
  boot.initrd.network.ssh = {
    enable = true;

    # Using the same port as the actual SSH daemon will cause the clients to
    # throw errors related to host key mismatch.
    port = 2222;

    hostKeys = [
      # XXX: This has to be manually generated during NixOS install.
      # The files are then copied to initrd secrets during activation.
      "/persist/initrd/ssh_host_rsa_key"
      "/persist/initrd/ssh_host_ed25519_key"
    ];

    authorizedKeys = maintainerKeys;
  };

  boot.initrd.network.postCommands = ''
    cat <<EOF > /root/.profile
    if pgrep -x "zfs" > /dev/null
    then
      zfs load-key -a
      killall zfs
    else
      echo "ZFS is not running -- this could be a sign of failure."
    fi
    EOF
  '';

  modules.tailscale-initrd = {
    enable = true;
    # XXX: This has to be manually generated during NixOS install.
    # The files are then copied to initrd secrets during activation.
    tailscaleStatePath = "/persist/initrd/tailscale-initrd.state";
  };

  services.openssh.enable = true;
  users.users.root.openssh.authorizedKeys.keys = maintainerKeys;

  sops.defaultSopsFile = ./secrets/secrets.yaml;

  powerManagement.cpuFreqGovernor = "ondemand";

  sops.secrets.zfs-smol-key = { };
  sops.secrets.nextcloud-admin-pass = { owner = "nextcloud"; };
  sops.secrets.acme-credentials = { owner = "acme"; };
  sops.secrets.grafana-admin-pass = { owner = "grafana"; };
  sops.secrets.cf-kusanari-kanata-credentials = { owner = "cloudflared"; };
  sops.secrets.nitter-account-jsonl = { };
  # sops.secrets.interlink-private-key = { };
  sops.secrets.interlink-wg-config = { };
  sops.secrets.proton-private-key = { };
  sops.secrets.attic-credentials = { };
  # TODO: insecure?
  sops.secrets.invidious-hmac = { mode = "0444"; };
  sops.secrets.transmission-extra-config = { owner = "transmission"; };
  sops.secrets.paperless-superuser-password = { owner = "paperless"; };

  boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
  boot.kernel.sysctl."net.ipv6.conf.all.forwarding" = 1;

  networking.firewall.enable = true;

  networking.nat = {
    enable = true;
    internalInterfaces = [ "ve-+" ];
    externalInterface = "enp3s0";
    # Lazy IPv6 connectivity for the container
    enableIPv6 = true;
  };

  services.nscd = {
    enable = true;
    config = ''
      # We basically use nscd as a proxy for forwarding nss requests to appropriate
      # nss modules, as we run nscd with LD_LIBRARY_PATH set to the directory
      # containing all such modules
      # Note that we can not use `enable-cache no` As this will actually cause nscd
      # to just reject the nss requests it receives, which then causes glibc to
      # fallback to trying to handle the request by itself. Which won't work as glibc
      # is not aware of the path in which the nss modules live.  As a workaround, we
      # have `enable-cache yes` with an explicit ttl of 0
      server-user             nscd

      enable-cache            passwd          yes
      positive-time-to-live   passwd          0
      negative-time-to-live   passwd          0
      shared                  passwd          yes

      enable-cache            group           yes
      positive-time-to-live   group           0
      negative-time-to-live   group           0
      shared                  group           yes

      enable-cache            netgroup        yes
      positive-time-to-live   netgroup        0
      negative-time-to-live   netgroup        0
      shared                  netgroup        yes

      enable-cache            hosts           yes
      positive-time-to-live   hosts           3600 # Invidious spams DNS queries to PubSubHubBub
      negative-time-to-live   hosts           0
      shared                  hosts           yes

      enable-cache            services        yes
      positive-time-to-live   services        0
      negative-time-to-live   services        0
      shared                  services        yes
    '';
  };

  services.tailscale = {
    enable = true;
    useRoutingFeatures = "both";
    openFirewall = true;
  };

  services.nginx.enable = true;
  services.cloudflared = {
    enable = true;

    tunnels."bf6dcc14-d315-41c7-b798-3fe0e0e968eb" = {
      default = "http_status:404";
    };
  };

  networking.firewall.allowedTCPPorts = [ 80 443 ];
  modules = {
    persistence.directories = [
      "/var/lib/nixos"
      "/var/lib/tailscale"
      "/var/lib/nixos-containers"
    ];

    persistence = {
      enable = true;
      storagePath = "/persist";
      setupSshHostKeys = true;
    };

    # NOTE: This module only populates route entries,
    # each service needs to be enabled individually.
    expose = {
      enable = true;

      routes = {
        "dns.kusanari.network".to = "http://localhost:4000";
        "metrics.kusanari.network".to = "http://localhost:4001";
        "nitter.kusanari.network".to = "http://localhost:4002";
        "invidious.kusanari.network".to = "http://localhost:4003";
        "hydra.kusanari.network".to = "http://localhost:4004";
        "cache.kusanari.network".to = "http://localhost:4005";
        "torrent.kusanari.network".to = "http://localhost:4006";
        "paperless.kusanari.network".to = "http://localhost:4007";
        "change.labs.kusanari.network".to = "http://localhost:4901";

        # Nginx pre-configured routes
        "nextcloud.kusanari.network" = {
          to = "http://localhost:80";
          configureNginx = false;
        };
      };

      ssl = {
        enable = true;
        acmeHost = "kusanari.network";
      };

      tailscaleIp = "100.93.1.1";
      # kusanari-kanata @ core
      cloudflareUUID = "bf6dcc14-d315-41c7-b798-3fe0e0e968eb";
      secrets.cloudflare-credentials = config.sops.secrets.cf-kusanari-kanata-credentials.path;
    };

    services.nginx.enable = true;

    services.acme = {
      enable = true;
      email = poorObfuscation "sefidel.net" "postmaster";

      certs = {
        "kusanari.network" = {
          subDomains = [
            "*.labs"
            "cache"
            "dns"
            "hydra"
            "invidious"
            "jellyfin"
            "metrics"
            "nextcloud"
            "nitter"
            "paperless"
            "torrent"
          ];
        };
      };

      secrets.acme-credentials = config.sops.secrets.acme-credentials.path;
    };

    services.metrics = {
      enable = true;
      realHost = "metrics.kusanari.network";
      secrets.adminPassword = config.sops.secrets.grafana-admin-pass.path;
    };

    services.postgresql.enable = true;

    services.blocky = {
      enable = true;

      realHost = "dns.kusanari.network";
    };

    services.nextcloud = rec {
      enable = true;

      ssl = {
        enable = true;
        acmeHost = domain;
      };

      domain = "kusanari.network";
      realHost = "nextcloud.kusanari.network";
      secrets.admin-pass = config.sops.secrets.nextcloud-admin-pass.path;
    };

    services.nitter = {
      enable = true;

      title = "Kusanari Nitter";

      domain = "kusanari.network";
      realHost = "nitter.kusanari.network";
      secrets.nitter-guest-accounts = config.sops.secrets.nitter-account-jsonl.path;
    };

    services.minecraft-server = {
      enable = true;

      package = pkgs.papermcServers.papermc-1_21;
      eula = true;
      bedrockSupport = true;

      dataDir = "/smol/sandbox/minecraft-server";
      openFirewall = true;
    };

    services.invidious = {
      enable = true;

      domain = "kusanari.network";
      realHost = "invidious.kusanari.network";
      secrets.invidious-hmac-key = config.sops.secrets.invidious-hmac.path;
    };

    services.hydra = {
      enable = true;

      baseURL = "https://hydra.kusanari.network";
    };

    services.atticd = {
      enable = true;

      hosts = [ "cache.kusanari.network" ];
      baseURL = "https://cache.kusanari.network/";

      storagePath = "/smol/archive/attic";

      watchStore = true;

      secrets.attic-credentials = config.sops.secrets.attic-credentials.path;
    };

    services.transmission = {
      enable = true;
      home = "/smol/sandbox/torrent";

      secrets.transmission-extra-config = config.sops.secrets.transmission-extra-config.path;
    };

    services.paperless = {
      enable = true;

      realHost = "paperless.kusanari.network";
      secrets.paperless-superuser-password = config.sops.secrets.paperless-superuser-password.path;
    };
  };

  containers.v-interlink = {
    autoStart = true;
    enableTun = true;
    # Tailscale authkeys expire after 90 days, which means if a system
    # restarts, there's a high chance that the key will be invalid.
    # Therefore, we use classic authentication with non-ephemeral storage.
    ephemeral = false;
    privateNetwork = true;
    hostAddress = "172.16.1.1";
    localAddress = "172.16.1.2";
    # bindMounts."/run/secrets/interlink-private-key".hostPath = config.sops.secrets.interlink-private-key.path;
    bindMounts."/run/secrets/interlink-wg-config".hostPath = config.sops.secrets.interlink-wg-config.path;
    config = { config, pkgs, lib, ... }: {
      services.tailscale = {
        enable = true;
        useRoutingFeatures = "both";
        extraUpFlags = [
          "--advertise-exit-node=true"
        ];
      };

      networking.firewall.allowedUDPPorts = [ 51820 ];

      networking.wg-quick.interfaces.wg0 = {
        autostart = true;

        configFile = "/run/secrets/interlink-wg-config";

        # address = [ "***.***.***.***/32" ];
        # listenPort = 51820;
        #
        # mtu = 1350;
        #
        # privateKeyFile = "/run/secrets/interlink-private-key";
        #
        # peers = [{
        #   publicKey = "*******************************************";
        #   # Exclude 100.64.0.0/10
        #   allowedIPs = [
        #     "0.0.0.0/0"
        #     # "0.0.0.0/2"
        #     # "64.0.0.0/3"
        #     # "96.0.0.0/6"
        #     # "100.0.0.0/10"
        #     # "100.128.0.0/9"
        #     # "101.0.0.0/8"
        #     # "102.0.0.0/7"
        #     # "104.0.0.0/5"
        #     # "112.0.0.0/4"
        #     # "128.0.0.0/1"
        #   ];
        #   persistentKeepalive = 120;
        #   endpoint = "***.***.***.***:51840";
        # }];
      };


      system.stateVersion = "24.05";
    };
  };

  networking.firewall.allowedUDPPorts = [ 51820 ];

  containers.v-proton-jp43 = {
    autoStart = true;
    enableTun = true;
    # Tailscale authkeys expire after 90 days, which means if a system
    # restarts, there's a high chance that the key will be invalid.
    # Therefore, we use classic authentication with non-ephemeral storage.
    ephemeral = false;
    privateNetwork = true;
    hostAddress = "172.16.1.3";
    localAddress = "172.16.1.4";
    bindMounts."/run/secrets/proton-private-key".hostPath = config.sops.secrets.proton-private-key.path;
    config = { config, pkgs, lib, ... }: {
      services.tailscale = {
        enable = true;
        useRoutingFeatures = "both";
        extraUpFlags = [
          "--advertise-exit-node=true"
        ];
      };

      networking.firewall.allowedUDPPorts = [ 51820 ];

      networking.wg-quick.interfaces.wg0 = {
        autostart = true;

        address = [ "10.2.0.2/32" ];
        listenPort = 51820;

        privateKeyFile = "/run/secrets/proton-private-key";

        peers = [{
          publicKey = "7FslkahrdLwGbv4QSX5Cft5CtQLmBUlpWC382SSF7Hw=";
          # Exclude 100.64.0.0/10
          allowedIPs = [
            "0.0.0.0/0"
            # "0.0.0.0/2"
            # "64.0.0.0/3"
            # "96.0.0.0/6"
            # "100.0.0.0/10"
            # "100.128.0.0/9"
            # "101.0.0.0/8"
            # "102.0.0.0/7"
            # "104.0.0.0/5"
            # "112.0.0.0/4"
            # "128.0.0.0/1"
          ];
          endpoint = "103.125.235.19:51820";
        }];
      };

      system.stateVersion = "24.05";
    };
  };

  # This option defines the first version of NixOS you have installed on this particular machine,
  # and is used to maintain compatibility with application data (e.g. databases) created on older NixOS versions.
  #
  # Most users should NEVER change this value after the initial install, for any reason,
  # even if you've upgraded your system to a new NixOS release.
  #
  # This value does NOT affect the Nixpkgs version your packages and OS are pulled from,
  # so changing it will NOT upgrade your system - see https://nixos.org/manual/nixos/stable/#sec-upgrading for how
  # to actually do that.
  #
  # This value being lower than the current NixOS release does NOT mean your system is
  # out of date, out of support, or vulnerable.
  #
  # Do NOT change this value unless you have manually inspected all the changes it would make to your configuration,
  # and migrated your data accordingly.
  #
  # For more information, see `man configuration.nix` or https://nixos.org/manual/nixos/stable/options#opt-system.stateVersion .
  system.stateVersion = "24.05"; # Did you read the comment?
}