nix-config/config/c3d2.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

283 lines
7.8 KiB
Nix
Raw Normal View History

# This module sets configuration for all NixOS machines defined in this flake
2022-06-16 23:17:50 +02:00
{ zentralwerk, hostRegistry, config, options, lib, pkgs, ... }:
let
hqPrefix64 = lib.removeSuffix "::" (builtins.head (
builtins.split "/" zentralwerk.lib.config.site.net.c3d2.subnets6.dn42
));
# Generate a deterministic IPv6 address for a 64 bit prefix
# and seed string. Prefix must not contain trailing ':'.
toIpv6Address = prefix64: seed:
with builtins;
let
digest = builtins.hashString "sha256" seed;
hextets = map (i: substring (4 * i) 4 digest) [ 0 1 2 3 ];
in concatStringsSep ":" ([ prefix64 ] ++ hextets);
# Generate a deterministic public IPv6 addresses
# for the HQ networking using a seed string.
toHqPrivateAddress = toIpv6Address hqPrefix64;
in {
imports = [
./stats.nix
2021-09-27 22:40:27 +02:00
./audio-server
2022-06-23 22:11:13 +02:00
./logging.nix
];
config = let
cfg = config.c3d2;
2021-02-24 11:52:19 +01:00
mkIfIsInHq = x: lib.mkIf cfg.isInHq (lib.mkDefault x);
in {
# Configuration specific to this machine
assertions = [
{
assertion = cfg.isInHq -> (config.users.users.root.password == null);
2020-01-26 17:36:24 +01:00
message = "Root passwords not allowed in HQ";
}
{
assertion = cfg.hq.enableBinaryCache -> cfg.mergeHostsFile;
message = "mergeHostsFile must be enabled for enableBinaryCache";
}
{
assertion = cfg.hq.enableMpdProxy -> cfg.mergeHostsFile;
message = "mergeHostsFile must be enabled for enableMpdProxy";
}
{
2022-01-16 13:26:37 +01:00
assertion = cfg.isInHq -> builtins.hasAttr config.networking.hostName cfg.hosts;
message = "${config.networking.hostName} is not registered in ${
toString ../host-registry.nix
}";
}
2022-01-16 13:26:37 +01:00
( # Check for host registry address collisions
let
getAddrHosts = key:
builtins.foldl' (result: host:
2022-01-16 13:26:37 +01:00
if cfg.hosts.${host}.${key} != null
then let
2022-01-16 13:26:37 +01:00
addr = cfg.hosts."${host}"."${key}";
in if result ? "${addr}"
then result // {
2021-10-31 19:00:03 +01:00
"${addr}" = result."${addr}" ++ [ host ];
}
else result // {
"${addr}" = [ host ];
}
else result
2022-01-16 13:26:37 +01:00
) {} (builtins.attrNames cfg.hosts);
dupHosts =
builtins.concatMap (hosts:
if builtins.length hosts == 1
then []
else hosts
) (
builtins.attrValues (
getAddrHosts "ip4" // getAddrHosts "ip6"
)
);
in {
assertion = dupHosts == [];
message = "Hosts have duplicate addresses: ${
lib.concatStringsSep " " dupHosts
}";
})
];
2022-09-01 23:56:29 +02:00
boot.cleanTmpDir = true;
2022-01-16 00:09:17 +01:00
c3d2.allUsersCanSshRoot = lib.mkDefault true;
2022-06-13 20:14:52 +02:00
i18n = {
defaultLocale = "en_US.UTF-8";
supportedLocales = [
"en_US.UTF-8/UTF-8"
"de_DE.UTF-8/UTF-8"
"C.UTF-8/UTF-8"
];
};
networking.defaultGateway = lib.mkIf (!config.networking.useNetworkd) (
mkIfIsInHq "172.22.99.4"
);
networking.domain = mkIfIsInHq "hq.c3d2.de";
systemd.network.networks =
if cfg.hq.interface != null && config.networking.useNetworkd
then {
"40-eth0".routes = [ {
routeConfig = {
Gateway = "172.22.99.4";
GatewayOnLink = true;
};
} ];
} else {};
networking.interfaces =
/* (if cfg.hq.externalInterface == null then
{ }
else {
"${cfg.hq.externalInterface}" = {
ipv6.addresses = [{
address = toHqPublicAddress config.networking.hostName;
prefixLength = 64;
}];
};
}) //
*/
2021-10-31 19:00:03 +01:00
if cfg.hq.interface == null then
{ }
else {
"${cfg.hq.interface}" = {
ipv6.addresses = [{
address = toHqPrivateAddress config.networking.hostName;
prefixLength = 64;
2020-10-26 16:00:08 +01:00
}];
};
2021-10-31 19:00:03 +01:00
};
2022-06-16 23:17:50 +02:00
networking.nameservers = with hostRegistry.hosts.dnscache; [
ip4
ip6
"9.9.9.9"
];
networking.useHostResolvConf = lib.mkIf (!config.services.resolved.enable) true;
environment.etc."resolv.conf" = lib.mkIf (!config.services.resolved.enable) {
text = lib.concatMapStrings (ns: ''
nameserver ${ns}
'') config.networking.nameservers;
};
2022-06-16 23:17:50 +02:00
nix = {
settings = {
auto-optimise-store = true;
trusted-public-keys = lib.mkIf (config.networking.hostName != "hydra") [
(builtins.readFile ../hosts/hydra/cache-pub.key)
];
substituters = lib.mkIf (config.networking.hostName != "hydra") (
lib.mkBefore [ "https://nix-serve.hq.c3d2.de" ]
);
};
gc = {
automatic = true;
2022-06-09 02:54:51 +02:00
dates = "06:00";
2022-06-13 15:48:05 +02:00
options = "--delete-older-than 21d";
2022-06-09 02:54:51 +02:00
randomizedDelaySec = "6h";
};
registry.c3d2 = {
from = {
id = "c3d2";
type = "indirect";
};
to = {
type = "git";
2021-11-07 16:07:51 +01:00
url = "https://gitea.c3d2.de/C3D2/nix-config.git";
};
};
2022-06-13 20:26:47 +02:00
extraOptions = ''
2022-06-13 20:40:03 +02:00
experimental-features = nix-command flakes
2022-06-13 20:26:47 +02:00
builders-use-substitutes = true
'';
};
services.openssh = {
2022-06-17 23:44:45 +02:00
# Required for deployment
enable = true;
permitRootLogin = "prohibit-password";
};
2021-10-02 19:41:34 +02:00
sops.age.sshKeyPaths = lib.mkDefault [ "/etc/ssh/ssh_host_ed25519_key" ];
environment = {
systemPackages = with pkgs; [
# Network fetchers
curl wget git
# System monitors
htop iotop bmon
2022-06-24 01:02:28 +02:00
ripgrep
# Terminal managers
tmux screen
# Editors
vim
# Pipeview
pv
# Network debugging
tcpdump ethtool mtr
];
variables = {
TERM = "xterm-256color";
};
2022-06-13 20:38:20 +02:00
# breaks various package builds
noXlibs = lib.mkForce false;
};
2021-10-03 00:22:01 +02:00
programs = {
ssh.knownHosts = with builtins;
let
intersectKeys = intersectAttrs {
publicKey = null;
publicKeyFile = null;
};
list = map (name:
let
2022-01-16 13:26:37 +01:00
host = getAttr name cfg.hosts;
2021-10-03 00:22:01 +02:00
sshAttrs = intersectKeys host;
in if sshAttrs == { } then
null
else {
inherit name;
value = let
2022-01-16 13:26:37 +01:00
ip6 = if host.ip6 != null then
2021-10-03 00:22:01 +02:00
host.ip6
else
toHqPrivateAddress name;
in {
publicKey = null;
publicKeyFile = null;
hostNames = [ ip6 "${name}.hq.c3d2.de" "${name}.hq" name ];
} // sshAttrs;
2022-01-16 13:26:37 +01:00
}) (builtins.attrNames cfg.hosts);
keyedHosts = filter (x: x.value.publicKey != null || x.value.publicKeyFile != null) list;
2021-10-03 00:22:01 +02:00
in listToAttrs keyedHosts;
vim.defaultEditor = true;
};
2019-12-03 19:56:26 +01:00
services.nginx = lib.mkIf config.services.nginx.enable {
recommendedGzipSettings = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
recommendedTlsSettings = true;
};
2021-02-22 12:31:58 +01:00
time.timeZone = lib.mkDefault "Europe/Berlin";
# Reboot on hang
systemd.watchdog = lib.mkIf (!config.boot.isContainer) {
runtimeTime = "15s";
rebootTime = "15s";
};
2021-09-27 22:27:36 +02:00
# Defaults for LetsEncrypt
security.acme =
if options.security.acme ? defaults
then {
acceptTerms = true;
# NixOS>=22.05
defaults = {
email = cfg.acmeEmail;
# letsencrypt staging server with way higher rate limits
# server = "https://acme-staging-v02.api.letsencrypt.org/directory";
};
}
else {
acceptTerms = true;
# TODO: NixOS<=21.05
email = cfg.acmeEmail;
};
2022-06-18 00:32:23 +02:00
zramSwap.enable = true;
};
}