nixos-config/lib/fudo/hosts.nix

128 lines
3.8 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
mapOptional = f: val: if (val != null) then (f val) else null;
2021-10-04 13:23:40 -07:00
host = import ../types/host.nix { inherit lib; };
hostname = config.instance.hostname;
2021-11-05 07:06:08 -07:00
generate-string-hash = name: str: let
string-hash-pkg = pkgs.stdenv.mkDerivation {
name = "${name}-string-hash";
phases = "installPhase";
buildInputs = [ pkgs.openssl ];
installPhase = "openssl passwd -6 ${str} > $out";
};
in string-hash-pkg;
in {
2021-03-01 14:43:27 -08:00
options.fudo.hosts = with types;
mkOption {
2021-10-04 13:23:40 -07:00
type = attrsOf (submodule host.hostOpts);
2021-03-01 14:43:27 -08:00
description = "Host configurations for all hosts known to the system.";
default = { };
};
config = let
hostname = config.instance.hostname;
host-cfg = config.fudo.hosts.${hostname};
site-name = host-cfg.site;
2021-02-25 12:45:50 -08:00
site = config.fudo.sites.${site-name};
domain-name = host-cfg.domain;
2021-03-10 17:23:45 -08:00
domain = config.fudo.domains.${domain-name};
2021-04-10 13:25:43 -07:00
has-build-servers = (length (attrNames site.build-servers)) > 0;
has-build-keys = (length host-cfg.build-pubkeys) > 0;
in {
2021-11-13 10:30:58 -08:00
security.sudo.extraConfig = ''
# I get it, I get it
Defaults lecture = never
'';
networking = {
hostName = config.instance.hostname;
2021-04-12 14:41:08 -07:00
domain = domain-name;
nameservers = site.nameservers;
2021-03-01 16:08:14 -08:00
# This will cause a loop on the gateway itself
#defaultGateway = site.gateway-v4;
#defaultGateway6 = site.gateway-v6;
2021-11-05 07:06:08 -07:00
firewall = mkIf ((length host-cfg.external-interfaces) > 0) {
enable = true;
allowedTCPPorts = [ 22 2112 ]; # Make sure _at least_ SSH is allowed
trustedInterfaces = let
all-interfaces = attrNames config.networking.interfaces;
in subtractLists host-cfg.external-interfaces all-interfaces;
};
2021-09-30 08:40:47 -07:00
hostId = mkIf (host-cfg.machine-id != null)
(substring 0 8 host-cfg.machine-id);
};
environment = {
etc = {
# NixOS generates a stupid hosts file, just force it
hosts = let
host-entries = mapAttrsToList
(ip: hostnames: "${ip} ${concatStringsSep " " hostnames}")
config.fudo.system.hostfile-entries;
in mkForce {
text = ''
2021-11-13 10:30:58 -08:00
127.0.0.1 ${hostname}.${domain-name} ${hostname} localhost
127.0.0.2 ${hostname} localhost
::1 ${hostname}.${domain-name} ${hostname} localhost
${concatStringsSep "\n" host-entries}
'';
user = "root";
group = "root";
mode = "0444";
};
2021-09-30 08:40:47 -07:00
machine-id = mkIf (host-cfg.machine-id != null) {
text = host-cfg.machine-id;
user = "root";
group = "root";
mode = "0444";
};
2021-10-12 14:06:34 -07:00
current-system-packages.text = with builtins; let
packages = map (p: "${p.name}")
config.environment.systemPackages;
sorted-unique = sort lessThan (unique packages);
in concatStringsSep "\n" sorted-unique;
build-timestamp.text = toString config.instance.build-timestamp;
2021-11-05 07:06:08 -07:00
build-seed-hash.source =
generate-string-hash "build-seed" config.instance.build-seed;
};
systemPackages = with pkgs;
mkIf (host-cfg.docker-server) [ docker nix-prefetch-docker ];
};
2021-04-03 10:15:10 -07:00
time.timeZone = site.timezone;
krb5.libdefaults.default_realm = domain.gssapi-realm;
2021-11-05 07:06:08 -07:00
services = {
cron.mailto = domain.admin-email;
fail2ban.ignoreIP = config.instance.local-networks;
};
2021-02-25 12:45:50 -08:00
virtualisation.docker = mkIf (host-cfg.docker-server) {
enable = true;
enableOnBoot = true;
2021-02-25 12:45:50 -08:00
autoPrune.enable = true;
};
2021-03-17 12:45:40 -07:00
2021-08-18 10:00:18 -07:00
programs.adb.enable = host-cfg.android-dev;
users.groups.adbusers = mkIf host-cfg.android-dev {
2021-10-01 16:05:56 -07:00
members = config.instance.local-admins;
2021-08-18 10:00:18 -07:00
};
boot.tmpOnTmpfs = host-cfg.tmp-on-tmpfs;
};
}