Files
nix/modules/server/default.nix

110 lines
2.5 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
2024-01-25 00:26:41 -03:00
let
2024-03-03 11:32:07 -03:00
cfg = config.server;
server = pkgs.writeShellScriptBin "server" ''
gum="${pkgs.gum}/bin/gum";
flakeDir="${toString cfg.flakeDir}";
2024-01-25 00:26:41 -03:00
command="$1";
if [[ "$command" == "build" ]]; then
shift 1;
sudo nixos-rebuild switch --flake "$flakeDir" "$@"
2024-01-25 00:26:41 -03:00
fi
2024-01-25 14:00:33 -03:00
${if cfg.forgejo.cliAlias then ''
if [[ "$command" == "forgejo" ]]; then
shift 1;
sudo --user=${cfg.forgejo.user} ${cfg.forgejo.package}/bin/gitea --work-path ${cfg.forgejo.data.root} "$@"
fi
'' else ""}
2024-01-25 00:26:41 -03:00
'';
in
{
imports = [
2024-01-27 12:03:36 -03:00
./adguard.nix
./caddy.nix
./forgejo.nix
2024-03-03 11:06:37 -03:00
./jellyfin.nix
./jellyseerr.nix
2024-01-28 21:32:46 -03:00
./network.nix
2024-02-12 14:58:40 -03:00
./nextcloud.nix
2024-03-03 11:06:37 -03:00
./photoprism.nix
2024-01-28 21:28:28 -03:00
./tailscale.nix
2024-01-25 00:26:41 -03:00
];
2024-03-03 11:32:07 -03:00
options.server = with lib; with lib.types; {
2024-01-25 00:26:41 -03:00
enable = mkEnableOption "";
2024-01-28 21:33:05 -03:00
name = mkOption {
type = str;
default = "server";
2024-01-28 21:33:05 -03:00
};
2024-01-25 00:26:41 -03:00
flakeDir = mkOption {
type = str;
};
storage = mkOption {
type = path;
default = /data + "/${cfg.name}";
2024-01-25 00:26:41 -03:00
description = "The Homelab central storage path";
};
domain = mkOption {
type = either str path;
2024-01-28 21:33:05 -03:00
default = "${cfg.name}.local";
};
2024-01-28 21:32:46 -03:00
localIp = mkOption {
type = str;
};
2024-01-28 21:32:46 -03:00
ip = mkOption {
type = str;
2024-01-28 21:32:46 -03:00
default = cfg.localIp;
};
handleDomains = mkOption {
type = bool;
default = true;
};
2024-01-25 00:26:41 -03:00
};
config = lib.mkIf cfg.enable {
environment.systemPackages = [
server
2024-01-25 00:26:41 -03:00
];
networking.firewall.allowedTCPPorts = lib.mkIf cfg.handleDomains [ 80 433 ];
systemd.services."tailscaled" = lib.mkIf cfg.handleDomains {
serviceConfig = {
Environment = [ "TS_PERMIT_CERT_UID=caddy" ];
};
};
2024-03-03 11:32:07 -03:00
server = with lib; mkIf cfg.handleDomains {
adguard = {
enable = true;
settings.dns.rewrites = (if hasPrefix "*." cfg.domain then {
"${cfg.domain}" = cfg.ip;
} else {
"${cfg.domain}" = cfg.ip;
"${"*." + cfg.domain}" = cfg.ip;
});
};
caddy =
let
homelabServices = (lib.filterAttrs (n: v: builtins.isAttrs v && v?domain) cfg);
in
with lib;
mkIf cfg.handleDomains {
enable = true;
settings.virtualHosts = mapAttrs'
(name: value: nameValuePair (value.domain) ({
extraConfig = ''
reverse_proxy ${cfg.localIp}:${toString value.port}
'';
}))
homelabServices;
};
};
2024-01-25 00:26:41 -03:00
};
}