1
0

default.nix 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. { inputs, outputs, pkgs, lib, config, ... }:
  2. {
  3. imports = [
  4. inputs.nixos-facter-modules.nixosModules.facter
  5. { config.facter.reportPath = ./facter.json; }
  6. outputs.modules.global.nix-config
  7. inputs.impermanence.nixosModules.impermanence
  8. inputs.disko.nixosModules.disko
  9. ./disko.nix
  10. # ./secrets.nix
  11. # ./services
  12. ];
  13. # System identification
  14. networking.hostName = "odin";
  15. networking.useDHCP = lib.mkDefault true;
  16. # Boot configuration
  17. boot = {
  18. # Use systemd-boot for UEFI systems
  19. loader = {
  20. systemd-boot.enable = true;
  21. efi.canTouchEfiVariables = true;
  22. grub.devices = [ config.disko.devices.disk.main.device ];
  23. timeout = 3;
  24. };
  25. # Kernel parameters for server workload
  26. kernelParams = [ "rootflags=compress=zstd:1,noatime" ];
  27. kernelModules = [ "nct6775" ];
  28. # Enable KSM for memory efficiency with containers
  29. kernel.sysctl = {
  30. "kernel.sysrq" = 1;
  31. "vm.swappiness" = 10;
  32. "net.core.default_qdisc" = "cake";
  33. };
  34. # Impermanence: reset root on boot
  35. initrd.postDeviceCommands = lib.mkAfter ''
  36. DEVICE=${config.disko.devices.disk.main.device}-part2
  37. mkdir -p /mnt
  38. mount -o subvol=/ $DEVICE /mnt
  39. # Create a directory for old roots if it doesn't exist
  40. mkdir -p /mnt/old-roots
  41. # Move current root to old-roots with timestamp if it exists
  42. if [[ -e /mnt/@root && ! -e /mnt/@root-blank ]]; then
  43. timestamp=$(date --date="@$(stat -c %Y /mnt/@root)" "+%Y-%m-%d_%H:%M:%S")
  44. mv /mnt/@root "/mnt/old-roots/@root-$timestamp"
  45. fi
  46. # Function to recursively delete subvolumes
  47. delete_subvolume_recursively() {
  48. for i in $(btrfs subvolume list -o "$1" | cut -f 9- -d ' '); do
  49. delete_subvolume_recursively "/mnt/$i"
  50. done
  51. btrfs subvolume delete "$1"
  52. }
  53. # Delete old roots older than 30 days
  54. for i in $(find /mnt/old-roots/ -maxdepth 1 -mtime +30); do
  55. delete_subvolume_recursively "$i"
  56. done
  57. # Create fresh root from blank if needed or create blank if it doesn't exist
  58. if [[ -e /mnt/@root-blank ]]; then
  59. btrfs subvolume delete /mnt/@root || true
  60. btrfs subvolume snapshot /mnt/@root-blank /mnt/@root
  61. else
  62. btrfs subvolume create /mnt/@root-blank
  63. btrfs subvolume create /mnt/@root
  64. fi
  65. umount /mnt
  66. '';
  67. };
  68. hardware = {
  69. fancontrol = {
  70. enable = true;
  71. config = ''
  72. INTERVAL=10
  73. DEVPATH=hwmon1=devices/pci0000:00/0000:00:02.2/0000:04:00.0/nvme/nvme0 hwmon2=devices/platform/nct6775.656
  74. DEVNAME=hwmon1=nvme hwmon2=nct6798
  75. FCTEMPS=hwmon2/pwm7=hwmon1/temp1_input hwmon2/pwm2=hwmon1/temp1_input
  76. FCFANS=hwmon2/pwm7=hwmon2/fan7_input hwmon2/pwm2=hwmon2/fan2_input
  77. MINTEMP=hwmon2/pwm7=30 hwmon2/pwm2=30
  78. MAXTEMP=hwmon2/pwm7=60 hwmon2/pwm2=60
  79. MINSTART=hwmon2/pwm7=95 hwmon2/pwm2=150
  80. MINSTOP=hwmon2/pwm7=75 hwmon2/pwm2=0
  81. '';
  82. };
  83. };
  84. # Services configuration
  85. services = {
  86. # Drive power management and fan control
  87. hddfancontrol = {
  88. enable = true;
  89. settings = {
  90. harddrives =
  91. let
  92. devices = config.disko.devices.disk;
  93. in
  94. {
  95. disks = [
  96. devices.storage1.device
  97. devices.storage2.device
  98. devices.storage3.device
  99. ];
  100. pwmPaths = [
  101. "/sys/class/hwmon/hwmon2/pwm1:20:0"
  102. "/sys/class/hwmon/hwmon2/pwm4:80:60"
  103. ];
  104. logVerbosity = "DEBUG";
  105. extraArgs = [
  106. "--min-fan-speed-prct=0"
  107. ];
  108. };
  109. };
  110. };
  111. # SSH configuration is managed in secrets.nix
  112. openssh = {
  113. enable = true;
  114. openFirewall = true;
  115. settings = {
  116. PasswordAuthentication = false;
  117. PermitRootLogin = "no";
  118. X11Forwarding = false;
  119. };
  120. # hostKeys = [ ];
  121. };
  122. # System monitoring
  123. # smartd = {
  124. # enable = true;
  125. # autodetect = true;
  126. # notifications.mail.enable = false; # Configure if you want email alerts
  127. # };
  128. # # Time synchronization
  129. # timesyncd.enable = true;
  130. #
  131. # # Btrfs maintenance
  132. # btrfs.autoScrub = {
  133. # enable = true;
  134. # interval = "monthly";
  135. # fileSystems = [ "/" ];
  136. # };
  137. # Drive spin-down management
  138. # hdparm.devices = [
  139. # {
  140. # device = "/dev/disk/by-id/ata-ST8000VN002-2ZM188_WPV023WG";
  141. # spindownTime = 120; # 10 minutes
  142. # apmLevel = 127;
  143. # }
  144. # {
  145. # device = "/dev/disk/by-id/ata-ST8000VN002-2ZM188_WPV07RMA";
  146. # spindownTime = 120;
  147. # apmLevel = 127;
  148. # }
  149. # {
  150. # device = "/dev/disk/by-id/ata-ST8000VN002-2ZM188_WPV020CG";
  151. # spindownTime = 120;
  152. # apmLevel = 127;
  153. # }
  154. # ];
  155. };
  156. # # Automatic garbage collection
  157. # nix = {
  158. # gc = {
  159. # automatic = true;
  160. # dates = "weekly";
  161. # options = "--delete-older-than 30d";
  162. # };
  163. # optimise.automatic = true;
  164. # };
  165. # # Container runtime
  166. # virtualisation = {
  167. # docker = {
  168. # enable = true;
  169. # storageDriver = "btrfs";
  170. # autoPrune = {
  171. # enable = true;
  172. # dates = "weekly";
  173. # flags = [ "--all" "--force" "--volumes" ];
  174. # };
  175. # };
  176. # };
  177. # System packages
  178. environment.systemPackages = with pkgs; [
  179. # System utilities
  180. htop
  181. btop
  182. iotop
  183. lsof
  184. pciutils
  185. usbutils
  186. # Network tools
  187. curl
  188. wget
  189. rsync
  190. # File system tools
  191. btrfs-progs
  192. xfsprogs
  193. smartmontools
  194. hdparm
  195. # # Container tools
  196. # docker-compose
  197. # Storage management
  198. snapraid
  199. mergerfs
  200. # Monitoring
  201. lm_sensors
  202. nvme-cli
  203. ];
  204. # User configuration
  205. users = {
  206. mutableUsers = false; # Declarative user management
  207. users = {
  208. # Main user account
  209. thomas = {
  210. isNormalUser = true;
  211. extraGroups = [ "wheel" "users" ];
  212. hashedPassword = "$6$jO/t4PtMb4Ky.goy$2diW2qZjswUAVAzRQqOJ7wfGwD9QInJtUfQYEOOp8hkdhAy6wcccYfIG.gEQniStx7ZkxADNQxQ7pyfUiOqll.";
  213. openssh.authorizedKeys.keys = [
  214. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC5o7LT5wPYWgI8Mvr6RKOv+BcsbQgU7PCw2hheVu17alwF1uFUsAYV5BVQu+uv9uEm/UDsCNhfM6TwI0A1prdmtBz4pKiwXbj7fcdp6DcVOgTsPfawbXEpivtJvlhEatyTsR26MjHKnqpT0BxPvj6Ug6pvRkCYW5d2bWXiY9murmAX6Q5kSyNunkB8PdRTH+S47f7eOdCJY63VBOkkiG8M7XyPwFCDTYiHhbMZcejIdY9mB6kYnMQVRHDznQWiQxrcaE1fD/TY3db9GDcOVoo2aDBOZX7WT2+me67sU8dEK9+nSyhWDzBbEs8knu87ZlKPFwhl4slenRniKhbf22OpicXArtEcjEj0GyDJH5e+ZCIQ4eSQanA7TxnKFlDuaf+Qqx55UT+ya4vJJeik7nkzbRHaE9IoWhhiOaOnaN6kHIxuxB6z7EL3Gk7f78+I/qBaj5df6fgnXM3JBXKa5bRH2wqoSetJAo6EGpEgmU2huB1ktiGlO7BlF5XwSw6cb/KT7NSIXhncgLkCzsDVXxecVQv1FnPISBcp3+ti01ADVf2trgpPDbNTWV40Rgiefie0o2fc6KWAFfum1j5N3WWU+XVVmRjDmKKHiEJBLNKDAe0rQf+tryPW4c5GIN7aFoB+8dYFAuUyLd7Fu3vhZdmcckN5ryHunEc0dKPIiuoVZw=="
  215. ];
  216. };
  217. };
  218. };
  219. # Persistent directories for impermanence
  220. fileSystems."/persist".neededForBoot = true;
  221. environment.persistence."/persist" = {
  222. hideMounts = true;
  223. directories = [
  224. "/etc/nixos"
  225. "/etc/ssh"
  226. "/var/lib/nixos"
  227. "/var/lib/systemd"
  228. "/srv"
  229. ];
  230. files = [
  231. "/etc/machine-id"
  232. ];
  233. users.thomas = {
  234. directories = [
  235. ".ssh"
  236. ".config"
  237. ];
  238. };
  239. };
  240. # System state version
  241. system.stateVersion = "25.05";
  242. }