nixos/hosts/common/optional/nixos-containers/semitamaps.nix

127 lines
3.2 KiB
Nix
Raw Normal View History

{
pkgs,
lib,
2025-02-08 14:15:45 +00:00
configVars,
...
}: let
containerName = "semitamaps";
pubKeys = lib.filesystem.listFilesRecursive ../../users/keys;
hostAddress = configVars.networking.addresses.semitamaps.hostAddress;
localAddress = configVars.networking.addresses.semitamaps.localAddress;
2025-02-14 19:14:32 +00:00
workingDirectory = "/var/www/semitamaps";
in {
2025-02-08 14:15:45 +00:00
systemd.tmpfiles.rules = [
2025-02-14 19:14:32 +00:00
"d /var/run/sockets 0660 www-data www-data -"
2025-02-08 14:15:45 +00:00
];
networking = {
nat = {
enable = true;
internalInterfaces = ["ve-+"];
externalInterface = "enp1s0";
};
};
environment.persistence."/persist" = {
hideMounts = true;
directories = [
"/var/lib/nixos-containers/${containerName}"
];
};
2025-02-22 08:32:20 +00:00
imports = [
../nginx/semitamaps.nix
];
containers."${containerName}" = {
autoStart = true;
privateNetwork = true;
hostAddress = hostAddress;
localAddress = localAddress;
nixpkgs = pkgs.path;
2025-02-08 14:15:45 +00:00
bindMounts = {
"/etc/ssh/ssh_host_ed25519_key" = {
hostPath = "/etc/ssh/ssh_host_ed25519_key";
isReadOnly = true;
};
"/var/run/sockets" = {
hostPath = "/var/run/sockets";
isReadOnly = false;
};
};
config = {
pkgs,
lib,
...
}: {
networking = {
firewall = {
enable = true;
rejectPackets = true;
allowedTCPPorts = [
2025-02-08 14:15:45 +00:00
80
443
];
};
useHostResolvConf = lib.mkForce false;
};
2025-02-14 19:14:32 +00:00
systemd.tmpfiles.rules = [
"d ${workingDirectory} 0750 www-data www-data"
"d ${workingDirectory}/.venv 0750 www-data www-data"
"d ${workingDirectory}/public/uploads 0775 www-data www-data"
];
services.resolved.enable = true;
environment.systemPackages = [
pkgs.vim
pkgs.git
];
services.openssh = {
enable = true;
settings.PasswordAuthentication = false;
};
2025-02-14 19:14:32 +00:00
systemd.services.semitamaps = {
2025-02-08 14:15:45 +00:00
wantedBy = ["multi-user.target"];
after = ["network.target"];
2025-02-14 19:14:32 +00:00
description = "Deploys and serves semitamaps";
2025-02-08 14:15:45 +00:00
environment = {
};
serviceConfig = {
2025-02-14 19:14:32 +00:00
WorkingDirectory = "${workingDirectory}";
ExecStartPre = pkgs.writeShellScript "semitamaps-prestart" ''
2025-02-08 14:15:45 +00:00
set -e
GITCMD="${pkgs.openssh}/bin/ssh -i /etc/ssh/ssh_host_ed25519_key"
2025-02-14 19:14:32 +00:00
if [ ! -d ${workingDirectory}/.git ]; then
2025-02-08 14:15:45 +00:00
export GIT_SSH_COMMAND=$GITCMD
2025-02-14 19:14:32 +00:00
${pkgs.git}/bin/git clone git@git.bitlab21.com:sam/semitamaps.com.git ${workingDirectory}
2025-02-08 14:15:45 +00:00
fi
${pkgs.poetry}/bin/poetry install
'';
2025-02-14 19:14:32 +00:00
ExecStart = pkgs.writeShellScript "semitamaps-start" ''
.venv/bin/python .venv/bin/uvicorn --workers 4 --uds /var/run/sockets/semitamaps.sock app:app
2025-02-08 14:15:45 +00:00
'';
Restart = "on-failure";
};
};
programs.ssh.knownHosts = {
"git.bitlab21.com".publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIALNd2BGf64heYjWT9yt0fVmngepiHRIMsL7au/MRteg";
};
users.users = {
root = {
openssh.authorizedKeys.keys = lib.lists.forEach pubKeys (key: builtins.readFile key);
};
};
system.stateVersion = "24.05";
};
};
}