nixos-server/host.nix

127 lines
2.7 KiB
Nix
Raw Normal View History

2024-05-24 11:13:36 +02:00
state-version: { pkgs, ... }:
2024-05-24 22:30:04 +02:00
let services = with builtins;
let services_no_ip =
map (s: import (./services + "/${s}") { inherit pkgs; })
(filter (s: ! isNull (match ".*\.nix" s))
(attrNames (readDir ./services)));
in genList (i: elemAt services_no_ip i // { ip = "10.10.0.${toString (i+2)}"; }) (length services_no_ip);
2024-05-23 12:08:06 +02:00
lib = import ./lib.nix;
2024-05-24 22:30:04 +02:00
secrets = import ./secrets/secrets.nix;
hostIp = "10.10.0.1";
2024-05-23 10:59:42 +02:00
in
2024-05-23 12:08:06 +02:00
2024-05-24 22:30:04 +02:00
{
containers =
lib.foldMap ({ name, config, ip, ports, ... }:
2024-05-23 10:59:42 +02:00
{
2024-05-24 22:30:04 +02:00
${name} = {
autoStart = true;
ephemeral = true;
privateNetwork = true;
hostAddress = hostIp;
localAddress = ip;
config = config // {
boot.isContainer = true;
networking = {
hostName = "${name}";
hosts = lib.foldMap ({ name, ip, ...}:
{ "${ip}" = [ "${name}.containers" "${name}" ]; }
) services;
firewall.enable = true;
firewall.allowedTCPPorts = ports.tcp;
firewall.allowedUDPPorts = ports.udp;
};
system.stateVersion = state-version;
};
2024-05-24 11:13:36 +02:00
};
2024-05-23 12:08:06 +02:00
}
2024-05-24 22:30:04 +02:00
) services;
}
2024-05-23 12:08:06 +02:00
//
2024-05-23 10:59:42 +02:00
2024-05-23 12:08:06 +02:00
{
2024-05-23 10:59:42 +02:00
# [NGINX]
services.nginx = {
enable = true;
recommendedProxySettings = true;
2024-05-23 12:08:06 +02:00
virtualHosts =
2024-05-24 22:30:04 +02:00
lib.foldMap ({ ports, hosts, ip, ... }:
2024-05-23 12:08:06 +02:00
lib.foldMap (host:
2024-05-24 22:30:04 +02:00
if (builtins.isNull ports.http)
then {}
else {
"${host}" = {
locations."/".proxyPass =
"http://${ip}:${builtins.toString ports.http}";
};
}
2024-05-23 12:08:06 +02:00
) hosts
) services;
2024-05-23 10:59:42 +02:00
};
2024-05-24 22:30:04 +02:00
# [SSHD]
services.openssh = {
enable = true;
settings = {
PermitRootLogin = "no";
PasswordAuthentication = false;
};
};
2024-05-23 10:59:42 +02:00
# [NETWORK]
2024-05-24 22:30:04 +02:00
networking = {
hostName = "cafe";
firewall.allowedTCPPorts = [ 22 80 443 ];
firewall.allowedUDPPorts = [ ];
nat = {
enable = true;
internalInterfaces = ["ve-+"];
externalInterface = "lo";
};
};
# [USER]
users.users.admin = {
isNormalUser = true;
group = "admin";
extraGroups = [ "wheel" ];
hashedPassword = pkgs.lib.removeSuffix "\n"
(builtins.readFile ./secrets/admin_password);
openssh.authorizedKeys.keyFiles = [ ./secrets/id_ed25519.pub ];
};
2024-05-23 11:21:24 +02:00
users.groups.admin = {};
2024-05-23 10:59:42 +02:00
2024-05-24 22:30:04 +02:00
# [SOFTWARE]
programs.bash.interactiveShellInit = ''
set -o vi
'';
# [NIX]
nix = {
settings = {
experimental-features = [ "nix-command" "flakes" ];
auto-optimise-store = true;
};
gc = {
automatic = true;
dates = "monthly";
options = "--delete-older-than 30d";
};
};
security.sudo.enable = false;
security.doas.enable = true;
2024-05-24 11:13:36 +02:00
system.stateVersion = state-version;
2024-05-23 10:59:42 +02:00
}