This commit is contained in:
Joakim Repomaa
2025-02-06 16:22:34 +02:00
committed by Joakim Repomaa
parent 983e313e11
commit 7a24ac5fe6
21 changed files with 233 additions and 193 deletions

View File

@@ -3,18 +3,6 @@
# https://search.nixos.org/options and in the NixOS manual (`nixos-help`). # https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
{ lib, pkgs, config, ... }: { lib, pkgs, config, ... }:
let
nginxVhost = options: {
http2 = true;
forceSSL = true;
enableACME = true;
acmeRoot = null;
} // options;
nginxProxy = options: {
proxyWebsockets = true;
} // options;
in
{ {
boot.loader.grub.enable = true; boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/sda"; boot.loader.grub.device = "/dev/sda";
@@ -72,13 +60,17 @@ in
}; };
}; };
modules.firewall.rules = { modules.firewall = {
koti = [ "dhcp" "dns" "ssh" ]; enable = true;
interfaces = {
koti = [ "dhcp" "dns" "ssh" "web" ];
gast = [ "dhcp" "dns" ]; gast = [ "dhcp" "dns" ];
iot = [ "dhcp" "dns" ]; iot = [ "dhcp" "dns" ];
cfg = [ "dhcp" "dns" ]; cfg = [ "dhcp" "dns" ];
"tailscale*" = [ "ssh" "web" ]; "tailscale*" = [ "ssh" "web" ];
}; };
allInterfaces = [ ];
};
systemd.network = { systemd.network = {
enable = true; enable = true;
@@ -228,31 +220,12 @@ in
}; };
}; };
services.nginx.virtualHosts."koti.repomaa.com" = nginxVhost { modules.webserver = {
extraConfig = ''
proxy_buffering off;
'';
locations."/" = nginxProxy {
proxyPass = "http://127.0.0.1:8123";
};
};
services.nginx = {
enable = true; enable = true;
recommendedProxySettings = true; acme.dnsChallenge = true;
recommendedTlsSettings = true; vHosts."koti.repomaa.com" = {
recommendedBrotliSettings = true; proxyBuffering = false;
recommendedGzipSettings = true; locations."/".proxy = "http://127.0.0.1:8123";
recommendedZstdSettings = true;
recommendedOptimisation = true;
};
security.acme = {
acceptTerms = true;
defaults = {
dnsProvider = "hetzner";
environmentFile = "/var/secrets/lego";
email = "admin@j.repomaa.com";
}; };
}; };

View File

@@ -6,7 +6,7 @@ in
imports = [ imports = [
./hardware-configuration.nix ./hardware-configuration.nix
./configuration.nix ./configuration.nix
./services.nix ./services
gtrackmap.nixosModules.x86_64-linux.default gtrackmap.nixosModules.x86_64-linux.default
]; ];
} }

View File

@@ -1,16 +0,0 @@
{ config, ... }:
let
fqdn = "trackmap.freun.dev";
in
{
services.gtrackmap = {
enable = true;
port = 3200;
};
services.caddy.virtualHosts = {
"${fqdn}".extraConfig = ''
reverse_proxy localhost:${toString config.services.gtrackmap.port}
'';
};
}

View File

@@ -1,21 +0,0 @@
{ ... }:
let
fqdn = "ci.freun.dev";
port = 3400;
in
{
services.hydra = {
enable = true;
hydraURL = "https://${fqdn}";
notificationSender = "Hydra <noreply@freun.dev>";
buildMachinesFiles = [];
useSubstitutes = true;
inherit port;
};
services.caddy.virtualHosts = {
"${fqdn}".extraConfig = ''
reverse_proxy localhost:${builtins.toString port}
'';
};
}

View File

@@ -1,16 +0,0 @@
{ ... }:
let
fqdn = "stream.freun.dev";
in
{
services.owncast = {
enable = true;
openFirewall = true;
};
services.caddy.virtualHosts = {
"${fqdn}".extraConfig = ''
reverse_proxy localhost:8080
'';
};
}

View File

@@ -1,13 +1,5 @@
{ pkgs, ... }: { pkgs, ... }:
{ {
networking.firewall.allowedTCPPorts = [ 80 443 ];
services.caddy = {
enable = true;
enableReload = true;
email = "admin@pimeys.pm";
};
services.postgresql = { services.postgresql = {
enable = true; enable = true;
package = pkgs.postgresql_17; package = pkgs.postgresql_17;
@@ -30,6 +22,8 @@
interfaces.podman1.allowedUDPPorts = [ 53 ]; interfaces.podman1.allowedUDPPorts = [ 53 ];
}; };
modules.webserver.enable = true;
imports = [ imports = [
./vaultwarden.nix ./vaultwarden.nix
./immich.nix ./immich.nix

View File

@@ -1,6 +1,7 @@
{ config, ... }: { config, lib, ... }:
let let
fqdn = "social.freun.dev"; domain = "freun.dev";
fqdn = "social.${domain}";
port = 3500; port = 3500;
in in
{ {
@@ -26,17 +27,21 @@ in
setupPostgresqlDB = true; setupPostgresqlDB = true;
}; };
services.caddy.virtualHosts = { modules.webserver.vHosts = {
"freun.dev".extraConfig = '' ${domain}.locations = lib.listToAttrs (
redir /.well-known/host-meta* https://${fqdn}{uri} permanent lib.map
redir /.well-known/webfinger* https://${fqdn}{uri} permanent (path: {
redir /.well-known/nodeinfo* https://${fqdn}{uri} permanent name = "/.well-known/${path}";
value.extraConfig = ''
rewrite ^.*$ https://${fqdn}/.well-known/${path} permanent;
''; '';
}) [
"host-meta"
"webfinger"
"nodeinfo"
]
);
"${fqdn}".extraConfig = '' "${fqdn}".locations."/".proxy = "http://localhost:${toString port}";
reverse_proxy localhost:${toString config.services.gotosocial.settings.port} {
flush_interval -1
}
'';
}; };
} }

View File

@@ -43,11 +43,7 @@ in
]; ];
}; };
services.caddy.virtualHosts = { modules.webserver.vHosts.${fqdn}.locations."/".proxy = "http://localhost:${toString port}";
"${fqdn}".extraConfig = ''
reverse_proxy localhost:${builtins.toString port}
'';
};
services.postgresql = { services.postgresql = {
ensureDatabases = [ "grafana" ]; ensureDatabases = [ "grafana" ];
@@ -57,4 +53,3 @@ in
}]; }];
}; };
} }

View File

@@ -0,0 +1,13 @@
{ ... }:
let
fqdn = "trackmap.freun.dev";
port = 3200;
in
{
services.gtrackmap = {
enable = true;
inherit port;
};
modules.webserver.vHosts.${fqdn}.locations."/".proxy = "http://localhost:${toString port}";
}

View File

@@ -1,6 +1,9 @@
{ pkgs, lib, ... }: { pkgs, lib, ... }:
let let
fqdn = "img.freun.dev";
port = 2283;
immich_version = "v1.125.2"; immich_version = "v1.125.2";
storage_dir = "/mnt/storage/syncthing"; storage_dir = "/mnt/storage/syncthing";
immich_data_dir = "/mnt/storage/immich"; immich_data_dir = "/mnt/storage/immich";
@@ -115,7 +118,7 @@ in
"${storage_dir}:${storage_dir}:ro" "${storage_dir}:${storage_dir}:ro"
]; ];
ports = [ ports = [
"2283:2283/tcp" "${toString port}:2283/tcp"
]; ];
dependsOn = [ dependsOn = [
"immich_postgres" "immich_postgres"
@@ -164,11 +167,7 @@ in
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
}; };
services.caddy.virtualHosts = { modules.webserver.vHosts.${fqdn}.locations."/".proxy = "http://localhost:${toString port}";
"img.freun.dev".extraConfig = ''
reverse_proxy localhost:2283
'';
};
fileSystems."${immich_data_dir}" = { fileSystems."${immich_data_dir}" = {
device = "//u407959.your-storagebox.de/backup/immich"; device = "//u407959.your-storagebox.de/backup/immich";

View File

@@ -1,11 +1,14 @@
{ ... }: { ... }:
let let
fqdn = "vid.freun.dev"; fqdn = "vid.freun.dev";
port = 3000;
in in
{ {
services.invidious = { services.invidious = {
enable = true; enable = true;
domain = fqdn; domain = fqdn;
address = "127.0.0.1";
inherit port;
settings = { settings = {
external_port = 443; external_port = 443;
@@ -16,10 +19,5 @@ in
}; };
}; };
services.caddy.virtualHosts = { modules.webserver.vHosts.${fqdn}.locations."/".proxy = "http://localhost:${toString port}";
"${fqdn}".extraConfig = ''
reverse_proxy localhost:3000
'';
};
} }

View File

@@ -0,0 +1,14 @@
{ ... }:
let
fqdn = "stream.freun.dev";
port = 8080;
in
{
services.owncast = {
enable = true;
openFirewall = true;
inherit port;
};
modules.webserver.vHosts.${fqdn}.locations."/".proxy = "http://localhost:${toString port}";
}

View File

@@ -0,0 +1,33 @@
{ pkgs, config, lib, ... }:
let
fqdn = "sync.freun.dev";
port = 8384;
storage_dir = "/mnt/storage/syncthing";
in
{
services.syncthing = {
enable = true;
dataDir = "/mnt/storage/syncthing";
openDefaultPorts = true;
guiAddress = "127.0.0.1:${toString port}";
};
modules.webserver.vHosts.${fqdn}.locations."/".proxy = "http://localhost:${toString port}";
fileSystems."${storage_dir}" = {
device = "//u407959.your-storagebox.de/backup/syncthing";
fsType = "cifs";
options =
let
# this line prevents hanging on network split
automount_opts = "x-systemd.automount,auto,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";
uid = builtins.toString config.users.users.syncthing.uid;
gid = builtins.toString config.users.groups.syncthing.gid;
in
[ "${automount_opts},credentials=/var/secrets/smb-storage,uid=${uid},gid=${gid}" ];
};
environment.systemPackages = [ pkgs.cifs-utils ];
}

View File

@@ -1,11 +1,14 @@
{ ... }: { lib, ... }:
let
fqdn = "pw.freun.dev";
in
{ {
services.vaultwarden = { services.vaultwarden = {
enable = true; enable = true;
dbBackend = "postgresql"; dbBackend = "postgresql";
environmentFile = "/var/secrets/vaultwarden.env"; environmentFile = "/var/secrets/vaultwarden.env";
config = { config = {
DOMAIN = "https://pw.freun.dev"; DOMAIN = "https://${fqdn}";
DATABASE_URL = "postgres://%2Fvar%2Frun%2Fpostgresql/vaultwarden"; DATABASE_URL = "postgres://%2Fvar%2Frun%2Fpostgresql/vaultwarden";
WEBSOCKET_ENABLED = true; WEBSOCKET_ENABLED = true;
WEBSOCKET_ADDRESS = "127.0.0.1"; WEBSOCKET_ADDRESS = "127.0.0.1";
@@ -23,13 +26,9 @@
}; };
}; };
services.caddy.virtualHosts = { modules.webserver.vHosts.${fqdn}.locations = {
"pw.freun.dev".extraConfig = '' "/".proxy = "http://localhost:8000";
reverse_proxy /notifications/hub localhost:3012 "/notifications/hub".proxy = "http://localhost:3012";
reverse_proxy localhost:8000 {
header_up X-Real-IP {remote_host}
}
'';
}; };
services.postgresql = { services.postgresql = {

View File

@@ -31,9 +31,5 @@ in
}; };
}; };
services.caddy.virtualHosts = { modules.webserver.vHosts.${fqdn}.locations."/".proxy = "http://localhost:${toString port}";
"${fqdn}".extraConfig = ''
reverse_proxy localhost:${toString port}
'';
};
} }

View File

@@ -1,33 +0,0 @@
{ pkgs, config, lib, ... }:
let
storage_dir = "/mnt/storage/syncthing";
in
{
services.syncthing = {
enable = true;
dataDir = "/mnt/storage/syncthing";
openDefaultPorts = true;
};
services.caddy.virtualHosts = {
"sync.freun.dev".extraConfig = ''
reverse_proxy localhost:8384 {
header_up Host {upstream_hostport}
}
'';
};
fileSystems."${storage_dir}" = {
device = "//u407959.your-storagebox.de/backup/syncthing";
fsType = "cifs";
options = let
# this line prevents hanging on network split
automount_opts = "x-systemd.automount,auto,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";
uid = builtins.toString config.users.users.syncthing.uid;
gid = builtins.toString config.users.groups.syncthing.gid;
in ["${automount_opts},credentials=/var/secrets/smb-storage,uid=${uid},gid=${gid}"];
};
environment.systemPackages = [ pkgs.cifs-utils ];
}

View File

@@ -3,5 +3,6 @@
imports = [ imports = [
./vlans.nix ./vlans.nix
./firewall.nix ./firewall.nix
./webserver.nix
]; ];
} }

View File

@@ -16,18 +16,25 @@ let
{ allowedUDPPorts = [ ]; allowedTCPPorts = [ ]; } { allowedUDPPorts = [ ]; allowedTCPPorts = [ ]; }
enabledServices; enabledServices;
cfg = config.modules.firewall.rules; cfg = config.modules.firewall;
in in
{ {
options.modules.firewall.rules = lib.mkOption { options.modules.firewall = {
enable = lib.mkEnableOption "Enable the firewall";
interfaces = lib.mkOption {
type = lib.types.attrsOf (lib.types.listOf (lib.types.enum (lib.attrNames services))); type = lib.types.attrsOf (lib.types.listOf (lib.types.enum (lib.attrNames services)));
default = { }; default = { };
}; };
allInterfaces = lib.mkOption {
config = lib.mkIf (lib.length (lib.attrNames cfg) > 0) { type = lib.types.listOf (lib.types.enum (lib.attrNames services));
networking.firewall = { default = [ ];
enable = lib.mkDefault true;
interfaces = lib.mapAttrs (_: enabledServices: rulesForServices enabledServices) cfg;
}; };
}; };
config = lib.mkIf cfg.enable {
networking.firewall = {
enable = true;
interfaces = lib.mapAttrs (_: enabledServices: rulesForServices enabledServices) cfg.interfaces;
} // rulesForServices cfg.allInterfaces;
};
} }

99
modules/webserver.nix Normal file
View File

@@ -0,0 +1,99 @@
{ lib, config, ... }:
let
cfg = config.modules.webserver;
nginxVhost = options: {
http2 = true;
forceSSL = true;
enableACME = true;
acmeRoot = null;
} // options;
nginxProxy = options: {
proxyWebsockets = true;
} // options;
types = {
location = lib.types.submodule {
options = {
proxy = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};
extraConfig = lib.mkOption {
type = lib.types.str;
default = "";
};
};
};
vhost = lib.types.submodule {
options = {
proxyBuffering = lib.mkOption {
type = lib.types.bool;
default = true;
};
locations = lib.mkOption {
type = lib.types.attrsOf types.location;
};
};
};
};
in
{
options.modules.webserver = {
enable = lib.mkEnableOption "Enable nginx";
acme = {
dnsChallenge = lib.mkEnableOption "Enable DNS challenge";
dnsProvider = lib.mkOption {
type = lib.types.str;
default = "hetzner";
};
environmentFile = lib.mkOption {
type = lib.types.str;
default = "/var/secrets/lego";
};
};
vHosts = lib.mkOption {
type = lib.types.attrsOf types.vhost;
default = { };
};
};
config = lib.mkIf cfg.enable {
services.nginx = {
enable = lib.mkDefault true;
recommendedProxySettings = true;
recommendedTlsSettings = true;
recommendedBrotliSettings = true;
recommendedGzipSettings = true;
recommendedZstdSettings = true;
recommendedOptimisation = true;
virtualHosts = lib.mapAttrs
(_: { proxyBuffering, locations }: nginxVhost {
extraConfig = lib.mkIf (!proxyBuffering) ''
proxy_buffering off;
'';
locations = lib.mapAttrs
(_: { proxy, extraConfig }: lib.mergeAttrsList [
{ inherit extraConfig; }
(if (lib.isString proxy) then (nginxProxy { proxyPass = proxy; }) else { })
])
locations;
})
cfg.vHosts;
};
security.acme = {
acceptTerms = true;
defaults = {
email = "admin@j.repomaa.com";
dnsProvider = lib.mkIf cfg.acme.dnsChallenge cfg.acme.dnsProvider;
webroot = lib.mkIf (!cfg.acme.dnsChallenge) "/var/lib/acme/";
};
};
modules.firewall.allInterfaces = lib.mkDefault [ "web" ];
};
}