|
|
|
|
|
by Cyph0n
665 days ago
|
|
Great post! I (relatively) recently switched my primary home server over to NixOS and am now a huge fan of it as a distribution for self-hosting. Here is how setting this all up would like in NixOS (modulo some details & machine-specific configuration). It's <100 lines, can be executed/configured with a single CLI command (even from a different machine!), rolled back easily if things go wrong, and can be re-used on any NixOS machine :) {
networking = {
# Server hostname
hostName = "myserver";
# Firewall
firewall = {
enable = true;
allowedTCPPorts = [ 80 443 2222 ];
};
};
# Users
users.users = {
newuser = {
isNormalUser = true;
home = "/home/newuser";
hashedPassword = "my-hashed-pwd";
openssh.authorizedKeys.keys = [ "my-pub-key" ];
};
};
# SSH
services.openssh = {
enable = true;
ports = [ 2222 ];
settings = {
PermitRootLogin = "no";
PasswordAuthentication = false;
AllowUsers = [ "newuser" ];
};
extraConfig = ''
Protocol 2 # Use only SSH protocol version 2
MaxAuthTries 3 # Limit authentication attempts
ClientAliveInterval 300 # Client alive interval in seconds
ClientAliveCountMax 2 # Maximum client alive count
'';
};
services.fail2ban.enable = true;
# Nginx + SSL via LetsEncrypt
services.nginx = {
enable = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
recommendedTlsSettings = true;
virtualHosts = {
"example.com" = {
locations."/" = {
proxyPass = "http://localhost:8080";
proxyWebsockets = true;
};
forceSSL = true;
enableACME = true;
};
};
};
security.acme = {
acceptTerms = true;
defaults.email = "myemail@gmail.com";
certs."example.com" = {
dnsProvider = "cloudflare";
environmentFile = ./my-env-file;
};
};
# Logrotate
services.logrotate = {
enable = true;
configFile = pkgs.writeText "logrotate.conf" ''
/var/log/nginx/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
'';
};
# Bonus: auto-upgrade from GH repo
system.autoUpgrade = {
enable = true;
flake = "github:myuser/nixos-config";
flags = [
"-L" # print build logs
"--refresh" # do not use cached Flake
];
dates = "00:00";
allowReboot = true;
randomizedDelaySec = "45min";
};
}
|
|
Getting into it has a learning curve, but it's honestly so much easier in a lot of ways, too.