about summary refs log tree commit diff
path: root/nixos/kanata/configuration.nix
blob: c575e06ff819e00a3f2766f8778dac76d202f126 (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
{ 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 generatd 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;

  # NOTE: managed by modules.persistence
  # TODO: remove?
  # fileSystems."/persist".neededForBoot = true;
  #
  # services.openssh.hostKeys = [
  #   {
  #     path = "/persist/ssh/ssh_host_ed25519_key";
  #     type = "ed25519";
  #   }
  #   {
  #     path = "/persist/ssh/ssh_host_rsa_key";
  #     type = "rsa";
  #     bits = 4096;
  #   }
  # ];

  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 = { };

  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.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/tailscale" ];

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

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

      routes = {
        "dns.kusanari.network" = "http://localhost:4000";
        "metrics.kusanari.network" = "http://localhost:4001";
        "nitter.kusanari.network" = "http://localhost:4002";

        # Nginx pre-configured routes
        # NOTE: Routes with port 80 or 443 will NOT create corresponding nginx virtualHosts.
        "nextcloud.kusanari.network" = "http://localhost:80";
      };

      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 = [
            "nitter"
            "nextcloud"
            "jellyfin"
            "dns"
            "metrics"
          ];
        };
      };

      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;

    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;
    };
  };

  # 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?
}