feat: add web host

This commit is contained in:
Katharina Heidenreich 2026-03-16 19:57:27 +01:00
parent df9367d12f
commit 155801244a
3 changed files with 63 additions and 1 deletions

34
data/web.nix Normal file
View file

@ -0,0 +1,34 @@
let
lib = import <nixpkgs/lib>;
mapFileNameToContent = fileName: {
status = 200;
contentType = "text/html";
content = builtins.readFile fileName;
};
findFiles = dir:
let content = builtins.readDir dir;
processEntry = name: type:
if type == "directory" then
findFiles (dir + "/${name}")
else if type == "regular" then
[ (dir + "/${name}") ]
else
[];
in
lib.concatLists (lib.mapAttrsToList processEntry content);
removePrefix = str: prefix: if builtins.substring 0 (builtins.stringLength prefix) str == prefix
then builtins.substring (builtins.stringLength prefix) (builtins.stringLength str - builtins.stringLength prefix) str
else str;
contentFn = basePath:
let
files = findFiles basePath;
baseLength = builtins.stringLength basePath;
files' = builtins.filter (file: builtins.match ".*\\.html$" file != null) files;
in builtins.listToAttrs (map (file: {
name = removePrefix file basePath;
value = mapFileNameToContent file;
}) files');
in
rec {
"nudelerde.de" = contentFn "/etc/nixos/data/web/";
}

11
data/web/index.html Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Hii :3</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body>
Hiiii :3
</body>
</html>

View file

@ -1,6 +1,7 @@
{ config, pkgs, lib, ... }: { config, pkgs, lib, ... }:
let let
network = import ../data/network.nix; network = import ../data/network.nix;
web = import ../data/web.nix;
virtualHostFn = name: service: let virtualHostFn = name: service: let
domain = if service ? domainOverride domain = if service ? domainOverride
then service.domainOverride then service.domainOverride
@ -54,6 +55,22 @@ let
} // serverAlias // sslConfig // externConnections // myExtraConfig; } // serverAlias // sslConfig // externConnections // myExtraConfig;
rproxyServices = builtins.mapAttrs (virtualHostFn) network.reverse_proxy; rproxyServices = builtins.mapAttrs (virtualHostFn) network.reverse_proxy;
serviceNamesMessage = builtins.toString (builtins.attrNames network.reverse_proxy); serviceNamesMessage = builtins.toString (builtins.attrNames network.reverse_proxy);
webHosts = lib.mapAttrs' (name: description: {
name = "${name}.web";
value = {
serverName = "${name}";
listen = [ {addr = "0.0.0.0"; port = 80;} {addr = "0.0.0.0"; port = 443; ssl = true;}];
locations = lib.mapAttrs' (endpointName: endpointValue: {
name = endpointName;
value = {
extraConfig = ''
default_type ${endpointValue.contentType};
return ${toString endpointValue.status} "${endpointValue.content}";
'';
};
}) description;
};
}) web;
fallback = { fallback = {
serverName = "_"; serverName = "_";
listen = [ {addr = "0.0.0.0"; port = 80;}]; listen = [ {addr = "0.0.0.0"; port = 80;}];
@ -77,7 +94,7 @@ in {
recommendedOptimisation = true; recommendedOptimisation = true;
recommendedGzipSettings = true; recommendedGzipSettings = true;
virtualHosts = rproxyServices // {fallback = fallback;}; virtualHosts = rproxyServices // webHosts // {fallback = fallback;};
}; };
networking.firewall.allowedTCPPorts = network.usedPorts; networking.firewall.allowedTCPPorts = network.usedPorts;