Files
homelab-config/homelab-services/nimh-static/flake.nix
T

104 lines
3.2 KiB
Nix
Raw Normal View History

2026-05-26 23:45:58 -05:00
{
description = "Flake that configures a static site hosted in a k3s pod";
inputs = {
2026-06-20 16:34:54 -05:00
nixpkgs.url = "github:NixOS/nixpkgs/nixos-26.05";
2026-05-26 23:45:58 -05:00
};
2026-05-27 00:46:48 -05:00
outputs =
2026-06-20 16:34:54 -05:00
{ ... }:
2026-05-27 00:46:48 -05:00
{
nixosModules.nimh-static =
{
config,
lib,
pkgs,
...
}:
let
2026-05-31 21:47:46 -05:00
k3sDir = ./k3s;
2026-05-31 21:57:04 -05:00
siteDir = ./site;
2026-05-27 00:46:48 -05:00
opts = config.services.nimh-static;
in
{
options.services.nimh-static = {
enable = lib.mkEnableOption "serve nimh via k3s pod.";
2026-05-26 23:45:58 -05:00
2026-05-27 00:46:48 -05:00
default-nginx = {
enable = lib.mkEnableOption "Enable nginx reverse proxy.";
hostname = lib.mkOption {
type = lib.types.str;
default = "localhost";
description = "Hostname for reverse proxy";
};
2026-05-26 23:45:58 -05:00
};
};
2026-05-31 22:21:24 -05:00
config = lib.mkIf opts.enable {
2026-05-27 00:46:48 -05:00
systemd.services.nimh-static = {
description = "oneshot apply service to k3s";
2026-05-27 00:53:45 -05:00
after = [ "k3s.service" ];
2026-05-27 00:46:48 -05:00
wantedBy = [ "multi-user.target" ];
2026-05-26 23:45:58 -05:00
2026-05-27 00:46:48 -05:00
serviceConfig = {
Type = "oneshot";
2026-05-31 21:31:37 -05:00
ExecStart = pkgs.writeShellScript "start-nimh-static" ''
2026-05-31 21:39:10 -05:00
echo "Creating temp dir"
2026-05-31 21:47:46 -05:00
kubernetes_config=$(mktemp -d)
2026-05-31 21:39:10 -05:00
echo "Generating templated files"
2026-05-27 00:46:48 -05:00
gomplate=${pkgs.gomplate}/bin/gomplate
2026-05-31 22:03:05 -05:00
$gomplate --input-dir=${k3sDir} --output-dir=$kubernetes_config -d site=file://${siteDir}/index.html?type=text/plain
2026-05-31 21:39:10 -05:00
echo "Applying k3s config"
2026-05-27 00:46:48 -05:00
kubectl=${pkgs.kubectl}/bin/kubectl
2026-05-31 22:32:20 -05:00
$kubectl --kubeconfig=/etc/rancher/k3s/k3s.yaml apply -k $kubernetes_config
2026-05-27 00:46:48 -05:00
'';
2026-05-26 23:45:58 -05:00
2026-05-31 22:19:56 -05:00
User = "root";
Group = "root";
2026-05-26 23:45:58 -05:00
};
};
2026-05-27 00:46:48 -05:00
2026-05-27 00:52:31 -05:00
services.nginx = lib.mkIf opts.default-nginx.enable {
2026-05-27 00:46:48 -05:00
enable = true;
virtualHosts.${opts.default-nginx.hostname} = {
forceSSL = true;
# Parse TLD from hostname to use wildcard cert (just takes last two elements separated by a period)
useACMEHost =
let
b = builtins;
s = lib.strings;
fl = s.splitString "." "${opts.default-nginx.hostname}";
in
b.concatStringsSep "." [
(b.elemAt fl (b.length fl - 2))
(b.elemAt fl (b.length fl - 1))
];
locations."/" = {
2026-05-31 22:58:12 -05:00
proxyPass = "http://127.0.0.1:30080";
2026-05-31 23:26:46 -05:00
extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
'';
2026-05-27 00:46:48 -05:00
};
};
};
2026-05-27 00:55:09 -05:00
networking.firewall.allowedTCPPorts = lib.mkIf opts.default-nginx.enable [
2026-05-27 00:46:48 -05:00
80
443
];
2026-05-26 23:45:58 -05:00
};
};
2026-05-27 00:46:48 -05:00
};
2026-05-26 23:45:58 -05:00
}