Serge Bazanski | 2e632b9 | 2023-06-20 01:10:05 +0200 | [diff] [blame] | 1 | { config, lib, pkgs, ... }: |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 2 | |
| 3 | let |
| 4 | hw = builtins.fromJSON (builtins.readFile ./hw.json); |
| 5 | ssh-keys = { |
| 6 | vuko = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFhaCaC/CVYv6hphqmEdKaPrIn+Q946+myvL9SSnzFZk vuko@eagle"; |
| 7 | informatic = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDoKB2p/gFaKthQNXeQvSLzhOlLSq3vjVL3AEOBTMXGH informatic@atuin"; |
Serge Bazanski | 2e632b9 | 2023-06-20 01:10:05 +0200 | [diff] [blame] | 8 | q3k = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIG599UildOrAq+LIOQjKqtGMwjgjIxozI1jtQQRKHtCP q3k@mimeomia"; |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 9 | }; |
| 10 | networks = { |
| 11 | lan = { |
| 12 | description = "LAN"; |
| 13 | hw_addr = "4c:52:62:ba:a9:78"; |
vuko | aa7303e | 2022-12-19 23:56:03 +0100 | [diff] [blame] | 14 | ipv4 = "10.8.1.16"; |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 15 | #ipv6 = "2a0d:eb00:4242::1"; |
| 16 | }; |
| 17 | }; |
| 18 | |
| 19 | system-vim = pkgs.vim_configurable.customize { |
| 20 | name = "vim"; |
| 21 | vimrcConfig.packages.myplugins = with pkgs.vimPlugins; { |
| 22 | start = [ vim-nix vim-lastplace ]; |
| 23 | opt = []; |
| 24 | }; |
| 25 | vimrcConfig.customRC = '' |
| 26 | set nocompatible |
| 27 | ''; |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | in { |
| 32 | imports = |
| 33 | [ |
| 34 | ./hardware-configuration.nix |
vuko | f0ab6c4 | 2022-12-19 23:57:03 +0100 | [diff] [blame] | 35 | ./spejsiot-api.nix |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 36 | ]; |
| 37 | |
| 38 | boot.loader.systemd-boot.enable = true; |
| 39 | boot.loader.efi.canTouchEfiVariables = true; |
| 40 | |
| 41 | time.timeZone = "Europe/Warsaw"; |
| 42 | |
| 43 | fileSystems."/" = { |
| 44 | device = "/dev/disk/by-partuuid/${hw.rootUUID}"; |
| 45 | fsType = "ext4"; |
| 46 | }; |
| 47 | |
| 48 | networking.hostName = "newsound"; |
| 49 | networking.domain = "waw.hackerspace.pl"; |
| 50 | networking.useDHCP = false; |
| 51 | |
| 52 | networking.defaultGateway = { |
| 53 | address = "10.8.1.2"; |
| 54 | interface = "lan"; |
| 55 | }; |
| 56 | |
| 57 | networking.interfaces = { |
| 58 | lan = { |
| 59 | ipv4.addresses = [ |
| 60 | { |
| 61 | address = networks.lan.ipv4; |
| 62 | prefixLength = 16; |
| 63 | } |
| 64 | ]; |
| 65 | }; |
| 66 | }; |
| 67 | |
| 68 | networking.nameservers = ["10.8.1.2"]; |
| 69 | |
| 70 | services.acpid.enable = true; |
| 71 | |
vuko | 142c8e6 | 2022-12-20 00:00:59 +0100 | [diff] [blame] | 72 | # nixos mosquitto service adds psk_file to its namespace mounts. Using separate service and directory other than |
| 73 | # /run/mosuitto/ seems like most reliable. |
| 74 | systemd.services."mosquitto-secrets" = pkgs.lib.mkIf config.services.mosquitto.enable ( |
| 75 | let |
| 76 | user = config.systemd.services.mosquitto.serviceConfig.User; |
| 77 | in { |
| 78 | description = "Mosquitto secrets"; |
| 79 | wantedBy = [ "multi-user.target" ]; |
| 80 | wants = [ "mosquitto.service" ]; |
| 81 | before = [ "mosquitto.service" ]; |
| 82 | |
| 83 | serviceConfig.Type = "oneshot"; |
| 84 | serviceConfig.RemainAfterExit = "yes"; |
| 85 | serviceConfig.ExecStart = [ |
| 86 | ''${pkgs.coreutils}/bin/install "--owner=${user}" --mode=500 --directory /run/mosquitto-secrets'' |
| 87 | ''${pkgs.coreutils}/bin/install "--owner=${user}" /root/secrets/mosquitto-pwfile /run/mosquitto-secrets/pwfile'' |
| 88 | ]; |
| 89 | serviceConfig.ExecStop = [ |
| 90 | ''${pkgs.coreutils}/bin/rm -rf /run/mosquitto-secrets'' |
| 91 | ]; |
| 92 | } |
| 93 | ); |
| 94 | |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 95 | services.mosquitto.enable = true; |
| 96 | services.mosquitto.listeners = [ |
| 97 | { |
| 98 | settings.allow_anonymous = true; |
vuko | 142c8e6 | 2022-12-20 00:00:59 +0100 | [diff] [blame] | 99 | settings.psk_file = "/run/mosquitto-secrets/pwfile"; |
| 100 | acl = [ |
| 101 | "topic read $SYS/#" |
| 102 | "topic #" |
| 103 | ]; |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 104 | } |
| 105 | ]; |
vuko | 142c8e6 | 2022-12-20 00:00:59 +0100 | [diff] [blame] | 106 | services.mosquitto.logType = ["all"]; |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 107 | |
| 108 | services.home-assistant = { |
| 109 | enable = true; |
| 110 | config = import ./home-assistant.nix; |
| 111 | |
| 112 | # TODO if some components / packages are not needed |
| 113 | extraComponents = [ |
| 114 | "default_config" |
| 115 | "mqtt" |
| 116 | "met" |
| 117 | "media_player" |
| 118 | "light" |
| 119 | "frontend" |
| 120 | "cast" |
| 121 | "spotify" |
| 122 | ]; |
| 123 | extraPackages = ps: [ |
| 124 | ps.aiohttp-cors |
| 125 | ps.pillow |
| 126 | ps.sqlalchemy |
| 127 | ps.websockets |
| 128 | ps.fnvhash |
| 129 | ps.hass-nabucasa |
| 130 | ps.pymetno |
| 131 | ps.radios |
vuko | 4fe6e02 | 2022-12-19 23:55:31 +0100 | [diff] [blame] | 132 | ps.pyipp |
| 133 | ps.brother |
| 134 | ps.wled |
| 135 | ps.securetar |
| 136 | ps.numpy |
| 137 | ps.pyatv |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 138 | ]; |
| 139 | }; |
| 140 | |
| 141 | sound.enable = true; |
| 142 | |
| 143 | # TODO create config that setups volume, default output etc. |
| 144 | hardware.pulseaudio = { |
| 145 | enable = true; |
| 146 | systemWide = true; |
| 147 | zeroconf.publish.enable = true; |
| 148 | |
| 149 | tcp.enable = true; |
| 150 | tcp.anonymousClients.allowAll = true; |
| 151 | }; |
| 152 | |
| 153 | services.nginx = { |
| 154 | enable = true; |
| 155 | virtualHosts = { |
| 156 | "iot.waw.hackerspace.pl" = { |
| 157 | serverAliases = ["default_server"]; |
| 158 | listen = [ |
| 159 | { |
| 160 | addr = networks.lan.ipv4; |
| 161 | port = 80; |
| 162 | ssl = false; |
| 163 | } |
| 164 | ]; |
| 165 | locations."/" = { |
| 166 | extraConfig = '' |
| 167 | proxy_set_header Upgrade $http_upgrade; |
| 168 | proxy_set_header Connection $http_connection; |
| 169 | proxy_set_header Host $host; |
| 170 | proxy_set_header X-Real-IP $remote_addr; |
| 171 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 172 | proxy_set_header X-Forwarded-Host $host:$server_port; |
| 173 | proxy_set_header X-Forwarded-Server $host; |
| 174 | proxy_set_header X-Forwarded-Proto $scheme; |
| 175 | ''; |
| 176 | proxyPass = "http://localhost:8123"; |
| 177 | }; |
| 178 | }; |
vuko | f0ab6c4 | 2022-12-19 23:57:03 +0100 | [diff] [blame] | 179 | "spejsiot.waw.hackerspace.pl" = { |
| 180 | serverAliases = ["default_server"]; |
| 181 | listen = [ |
| 182 | { |
| 183 | addr = networks.lan.ipv4; |
| 184 | port = 80; |
| 185 | ssl = false; |
| 186 | } |
| 187 | ]; |
| 188 | locations."/" = { |
| 189 | # TODO copied from iot, are all headers needed? |
| 190 | extraConfig = '' |
| 191 | proxy_set_header Upgrade $http_upgrade; |
| 192 | proxy_set_header Connection $http_connection; |
| 193 | proxy_set_header Host $host; |
| 194 | proxy_set_header X-Real-IP $remote_addr; |
| 195 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 196 | proxy_set_header X-Forwarded-Host $host:$server_port; |
| 197 | proxy_set_header X-Forwarded-Server $host; |
| 198 | proxy_set_header X-Forwarded-Proto $scheme; |
| 199 | ''; |
| 200 | proxyPass = "http://localhost:5100"; |
| 201 | }; |
| 202 | }; |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 203 | }; |
| 204 | }; |
| 205 | |
| 206 | |
| 207 | systemd.network.links = builtins.listToAttrs (map ( |
| 208 | name: { name = "10-link-${name}"; value = { |
| 209 | enable = true; |
| 210 | matchConfig = { |
| 211 | MACAddress = networks."${name}".hw_addr; |
| 212 | }; |
| 213 | linkConfig = { |
| 214 | Name = "${name}"; |
| 215 | }; |
| 216 | }; } |
| 217 | ) (builtins.filter (name: builtins.hasAttr "hw_addr" networks."${name}") (builtins.attrNames networks))); |
| 218 | |
| 219 | networking.firewall = { |
| 220 | enable = true; |
| 221 | allowedTCPPorts = [ |
| 222 | 22 # ssh |
| 223 | 80 # nginx http |
| 224 | 1883 # mqtt (mosquitto) |
vuko | 6204ccd | 2022-12-20 00:00:32 +0100 | [diff] [blame] | 225 | 8883 # tls mqtt (mosquitto) |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 226 | 4713 # pulseaudo |
| 227 | ]; |
| 228 | }; |
| 229 | |
| 230 | services.openssh = { |
| 231 | enable = true; |
| 232 | passwordAuthentication = false; |
| 233 | }; |
| 234 | |
| 235 | # TODO extract ssh keys synchronization from customs and add it here |
Serge Bazanski | 2e632b9 | 2023-06-20 01:10:05 +0200 | [diff] [blame] | 236 | users.users.root.openssh.authorizedKeys.keys = lib.mapAttrsToList (_: v: v) ssh-keys; |
vuko | deeeff8 | 2022-10-02 23:12:29 +0200 | [diff] [blame] | 237 | |
| 238 | environment.systemPackages = with pkgs; [ |
| 239 | system-vim tcpdump htop nmon tmux git file procps parted dmidecode ack utillinux |
| 240 | nmap mosh ncdu tree lz4 bind neovim hdparm usbutils |
| 241 | ]; |
| 242 | |
| 243 | programs.mtr.enable = true; |
| 244 | |
| 245 | environment.variables = { |
| 246 | EDITOR = "vim"; |
| 247 | }; |
| 248 | |
| 249 | #environment.extraInit = '' |
| 250 | # export NIX_PATH="nixpkgs=${config.channel-sources.nixpkgs}"; |
| 251 | #''; |
| 252 | |
| 253 | environment.etc."inputrc" = { |
| 254 | text = pkgs.lib.mkDefault( pkgs.lib.mkAfter '' |
| 255 | set colored-stats on |
| 256 | set show-all-if-ambiguous on |
| 257 | set completion-ignore-case on |
| 258 | |
| 259 | # arrow up |
| 260 | "\e[A": history-search-backward |
| 261 | # arrow down |
| 262 | "\e[B": history-search-forward |
| 263 | |
| 264 | "\e[5~": history-search-backward |
| 265 | "\e[6~": history-search-forward |
| 266 | ''); |
| 267 | }; |
| 268 | |
| 269 | system.stateVersion = "22.05"; |
| 270 | |
| 271 | |
| 272 | boot.vesa = false; |
| 273 | boot.loader.grub.splashImage = null; |
| 274 | } |
| 275 | |