From fb3c68a676643e60e396daf4076684e12d157677 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 13 Jun 2025 15:02:58 +0530 Subject: Syncthing + service router refactor --- configuration.nix | 38 ++++++---------------- flake.lock | 6 ++-- modules/hardware.nix | 7 ++++ modules/network/default.nix | 16 +++++++++ modules/network/service-router.service.nix | 52 ++++++++++++++++++++++++++++++ modules/service-router.service.nix | 49 ---------------------------- modules/storage/default.nix | 6 ++++ modules/storage/syncthing.nix | 44 +++++++++++++++++++++++++ settings.nix | 16 ++++++--- 9 files changed, 150 insertions(+), 84 deletions(-) create mode 100644 modules/network/service-router.service.nix delete mode 100644 modules/service-router.service.nix create mode 100644 modules/storage/default.nix create mode 100644 modules/storage/syncthing.nix diff --git a/configuration.nix b/configuration.nix index 048164f..ca5aaf8 100644 --- a/configuration.nix +++ b/configuration.nix @@ -1,13 +1,4 @@ -# Edit this configuration file to define what should be installed on -# your system. Help is available in the configuration.nix(5) man page -# and in the NixOS manual (accessible by running ‘nixos-help’). - -{ pkgs, lib, ... }: -let - settings = import ./settings.nix { inherit lib; }; - ports = settings.network.ports; - host = settings.network.host; -in +{ pkgs, ... }: { imports = [ ./modules/hardware.nix @@ -15,20 +6,9 @@ in ./modules/network ./modules/media ./modules/dashboard - ./modules/service-router.service.nix + ./modules/storage ]; - services.service-router = { - enable = true; - routes = { - "home.local" = { inherit host; port = ports.dashboard; }; - "sonarr.local" = { inherit host; port = ports.sonarr; }; - "radarr.local" = { inherit host; port = ports.radarr; }; - "prowlarr.local" = { inherit host; port = ports.prowlarr; }; - "jellyfin.local" = { inherit host; port = ports.jellyfin; }; - }; - }; - nixpkgs.config.allowUnfree = true; environment.systemPackages = with pkgs; [ @@ -38,15 +18,17 @@ in lf util-linux dig - ]; - systemd.extraConfig = ''DefaultLimitNOFILE=65536''; - systemd.user.extraConfig = ''DefaultLimitNOFILE=65536''; - boot.kernel.sysctl."fs.inotify.max_user_instances" = 8192; - security.pam.loginLimits = [ - { domain = "*"; type = "-"; item = "nofile"; value = "65536"; } + # Mom mode + st + ungoogled-chromium ]; + # Mom mode + services.xserver.enable = true; + services.xserver.windowManager.dwm.enable = true; + services.xserver.displayManager.startx.enable = true; + time.timeZone = "Asia/Kolkata"; i18n.defaultLocale = "en_GB.UTF-8"; diff --git a/flake.lock b/flake.lock index 6d6d850..f1c7fdc 100644 --- a/flake.lock +++ b/flake.lock @@ -53,11 +53,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1749143949, - "narHash": "sha256-QuUtALJpVrPnPeozlUG/y+oIMSLdptHxb3GK6cpSVhA=", + "lastModified": 1749285348, + "narHash": "sha256-frdhQvPbmDYaScPFiCnfdh3B/Vh81Uuoo0w5TkWmmjU=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "d3d2d80a2191a73d1e86456a751b83aa13085d7d", + "rev": "3e3afe5174c561dee0df6f2c2b2236990146329f", "type": "github" }, "original": { diff --git a/modules/hardware.nix b/modules/hardware.nix index 5a20353..7b11ca6 100644 --- a/modules/hardware.nix +++ b/modules/hardware.nix @@ -63,4 +63,11 @@ swapDevices = [{ device = "/dev/disk/by-label/swap"; }]; networking.useDHCP = lib.mkDefault true; + + systemd.extraConfig = ''DefaultLimitNOFILE=65536''; + systemd.user.extraConfig = ''DefaultLimitNOFILE=65536''; + boot.kernel.sysctl."fs.inotify.max_user_instances" = 8192; + security.pam.loginLimits = [ + { domain = "*"; type = "-"; item = "nofile"; value = "65536"; } + ]; } diff --git a/modules/network/default.nix b/modules/network/default.nix index ee3462c..6a40ef7 100644 --- a/modules/network/default.nix +++ b/modules/network/default.nix @@ -1,12 +1,28 @@ { lib, ... }: let settings = import ../../settings.nix { inherit lib; }; + ports = settings.network.ports; + host = settings.network.host; in { imports = [ ./ssh.nix + ./service-router.service.nix ]; + services.service-router = { + enable = true; + routes = { + "home.local" = { inherit host; port = ports.dashboard; }; + "sonarr.local" = { inherit host; port = ports.sonarr; }; + "radarr.local" = { inherit host; port = ports.radarr; }; + "prowlarr.local" = { inherit host; port = ports.prowlarr; }; + "jellyfin.local" = { inherit host; port = ports.jellyfin; }; + "syncthing.local" = { inherit host; port = ports.syncthing; }; + "lidarr.local" = { inherit host; port = ports.lidarr; }; + }; + }; + networking = { hostName = "bacchus"; diff --git a/modules/network/service-router.service.nix b/modules/network/service-router.service.nix new file mode 100644 index 0000000..9c4e6e2 --- /dev/null +++ b/modules/network/service-router.service.nix @@ -0,0 +1,52 @@ +{ config, lib, dns, ... }: +with lib; +let + cfg = config.services.service-router; + domainAZone = domain: record: { + A = [ record ]; + SOA = { + nameServer = "ns.${domain}."; + adminEmail = "dont@email.me"; + serial = 2019030800; + }; + NS = [ "ns.${domain}." ]; + }; +in { + options.services.service-router = { + enable = mkEnableOption "enable router"; + routes = mkOption { + type = types.attrsOf (types.submodule { options = { + port = mkOption { type = types.int; }; + host = mkOption { type = types.str; default = "127.0.0.1"; }; + protocol = mkOption { type = types.str; default = "http"; }; + nginx = mkOption { type = types.attrs; default = {}; }; + }; }); + default = {}; + }; + }; + + config = lib.mkIf cfg.enable { + services.nginx = { + enable = true; + recommendedOptimisation = true; + virtualHosts = lib.mapAttrs (_: val: { + locations."/" = if val.nginx == {} then { + proxyPass = "${val.protocol}://${val.host}:${toString val.port}"; + proxyWebsockets = true; + } else val.nginx; + }) cfg.routes; + }; + + services.nsd = { + enable = true; + interfaces = [ "0.0.0.0" ]; + zones = lib.mapAttrs (domain: val: { + data = dns.lib.toString domain (domainAZone domain val.host); + }) cfg.routes; + }; + networking.firewall.allowedTCPPorts = [ 53 ]; + networking.firewall.allowedUDPPorts = [ 53 ]; + + networking.hosts."127.0.0.1" = lib.mapAttrsToList (name: _: name) cfg.routes; + }; +} diff --git a/modules/service-router.service.nix b/modules/service-router.service.nix deleted file mode 100644 index 0b9a873..0000000 --- a/modules/service-router.service.nix +++ /dev/null @@ -1,49 +0,0 @@ -{ config, lib, dns, ... }: -with lib; -let - cfg = config.services.service-router; - domainAZone = domain: record: { - A = [ record ]; - SOA = { - nameServer = "ns.${domain}."; - adminEmail = "dont@email.me"; - serial = 2019030800; - }; - NS = [ "ns.${domain}." ]; - }; -in { - options.services.service-router = { - enable = mkEnableOption "enable router"; - routes = mkOption { - type = types.attrsOf (types.submodule { options = { - port = mkOption { type = types.int; }; - host = mkOption { type = types.str; default = "127.0.0.1"; }; - protocol = mkOption { type = types.str; default = "http"; }; - }; }); - default = {}; - }; - }; - - config = lib.mkIf cfg.enable { - services.nginx = { - enable = true; - virtualHosts = lib.mapAttrs (_: val: { - locations."/" = { - proxyPass = "${val.protocol}://${val.host}:${toString val.port}"; - }; - }) cfg.routes; - }; - - services.nsd = { - enable = true; - interfaces = [ "0.0.0.0" ]; - zones = lib.mapAttrs (domain: val: { - data = dns.lib.toString domain (domainAZone domain val.host); - }) cfg.routes; - }; - networking.firewall.allowedTCPPorts = [ 53 ]; - networking.firewall.allowedUDPPorts = [ 53 ]; - - networking.hosts."127.0.0.1" = lib.mapAttrsToList (name: _: name) cfg.routes; - }; -} diff --git a/modules/storage/default.nix b/modules/storage/default.nix new file mode 100644 index 0000000..22bd2ff --- /dev/null +++ b/modules/storage/default.nix @@ -0,0 +1,6 @@ +{ ... }: +{ + imports = [ + ./syncthing.nix + ]; +} diff --git a/modules/storage/syncthing.nix b/modules/storage/syncthing.nix new file mode 100644 index 0000000..5291182 --- /dev/null +++ b/modules/storage/syncthing.nix @@ -0,0 +1,44 @@ +{ lib, ... }: +let + settings = import ../../settings.nix { inherit lib; }; + group = "syncthing"; +in +{ + systemd.tmpfiles.rules = [ + "d ${settings.syncthing.baseDir} 0770 - ${group} - -" + "d ${settings.syncthing.photosDir} 0770 - ${group} - -" + ]; + users.groups.${group} = { }; + users.users.bacchus.extraGroups = [ group ]; + + networking.firewall.allowedTCPPorts = [ settings.network.ports.syncthing ]; + + services.syncthing = { + enable = true; + user = "bacchus"; + group = group; + dataDir = settings.syncthing.baseDir; + guiAddress = "0.0.0.0:${toString settings.network.ports.syncthing}"; + overrideFolders = true; + + settings = { + folders = { + artemis-photos = { + label = "Photos"; + path = settings.syncthing.photosDir; + }; + }; + + options.urAccepted = -1; + + extraOptions = { + gui = { + enabled = true; + theme = "black"; + user = settings.syncthing.username; + password = settings.syncthing.password; + }; + }; + }; + }; +} diff --git a/settings.nix b/settings.nix index 43a4804..f6c717b 100644 --- a/settings.nix +++ b/settings.nix @@ -8,18 +8,26 @@ in lib.recursiveUpdate privateSettings rec { ssh = 22; radarr = 7878; sonarr = 8989; + lidarr = 8686; prowlarr = 9696; jellyfin = 8096; transmissionRPC = 9091; + syncthing = 3141; }; exposeTransmissionRPC = false; }; - media = { + syncthing = { + baseDir = "/media/syncthing"; + photosDir = "${syncthing.baseDir}/photos"; + }; + + media = rec { baseDir = "/media"; - downloadsDir = "${media.baseDir}/_downloads"; - tvDir = "${media.baseDir}/tv"; - moviesDir = "${media.baseDir}/movies"; + downloadsDir = "${baseDir}/_downloads"; + tvDir = "${baseDir}/tv"; + moviesDir = "${baseDir}/movies"; + musicDir = "${baseDir}/music"; group = "multimedia"; }; } -- cgit v1.3.1