From 56bb9289f6032916f7c0be21764da751ba6f08fd Mon Sep 17 00:00:00 2001 From: xeals Date: Wed, 27 Sep 2023 14:12:26 +1000 Subject: [PATCH] modules/betanin: init --- modules/default.nix | 1 + modules/services/web-apps/betanin.nix | 114 ++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 modules/services/web-apps/betanin.nix diff --git a/modules/default.nix b/modules/default.nix index 905a61d..892719b 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -2,6 +2,7 @@ amdgpu-common = ./services/hardware/amdgpu-common.nix; amdgpu-fan = ./services/hardware/amdgpu-fan.nix; amdgpu-pwm = ./services/hardware/amdgpu-pwm.nix; + betanin = ./services/web-apps/betanin.nix; dunst = ./services/x11/dunst.nix; radeon-profile-daemon = ./services/hardware/radeon-profile-daemon.nix; } diff --git a/modules/services/web-apps/betanin.nix b/modules/services/web-apps/betanin.nix new file mode 100644 index 0000000..662adc5 --- /dev/null +++ b/modules/services/web-apps/betanin.nix @@ -0,0 +1,114 @@ +{ config, lib, pkgs, ... }: + +let + inherit (lib) mkIf mkOption optionalAttrs types; + cfg = config.services.betanin; + + yaml = pkgs.formats.yaml { }; + configFile = + if (cfg.beetsConfigFile != null) + then cfg.beetsConfigFile + else yaml.generate "beets-config.yaml" cfg.beetsConfig; + + defaultUser = "betanin"; + defaultGroup = "betanin"; +in +{ + options = { + services.betanin = { + enable = lib.mkEnableOption "betanin"; + + package = mkOption { + description = "Package containing betanin program."; + type = types.package; + default = pkgs.betanin or (import ../../.. { inherit pkgs; }).betanin; + }; + + openFirewall = mkOption { + description = "Open ports in the firewall for the server."; + type = types.bool; + default = false; + }; + + port = mkOption { + description = "Port to access betanin on."; + type = types.port; + default = 9393; + }; + + user = mkOption { + description = "User that the betanin program should run under."; + type = types.str; + default = defaultUser; + }; + + group = mkOption { + description = "Group that the betanin program should run under."; + type = types.str; + default = defaultGroup; + }; + + dataDir = mkOption { + description = "Directory to store application data."; + type = types.str; + default = "/var/lib/betanin"; + }; + + beetsConfig = mkOption { + description = "beets configuration."; + type = yaml.type; + default = { }; + }; + + beetsConfigFile = mkOption { + description = "beets configuration file."; + type = nullOr (either str path); + default = null; + }; + }; + }; + + config = mkIf cfg.enable { + assertions = [{ + assertion = (cfg.beetsConfig == { }) != (cfg.beetsConfigFile == null); + message = "either services.betanin.beetsConfig or services.betanin.beetsConfigFile is required"; + }]; + + networking.firewall = mkIf cfg.openFirewall { + allowedTCPPorts = [ cfg.port ]; + }; + + systemd.services.betanin = { + description = "Betanin service"; + wantedBy = [ "multi-user.target" ]; + after = [ "networking.target" ]; + environment = { + HOME = cfg.dataDir; + }; + serviceConfig = lib.mkMerge [ + { + User = cfg.user; + Group = cfg.group; + ExecStart = "${cfg.package}/bin/betanin --port ${cfg.port}"; + PrivateTmp = true; + Restart = "always"; + WorkingDirectory = cfg.dataDir; + } + (mkIf (cfg.dataDir == "/var/lib/betanin") { + StateDirectory = "betanin"; + }) + ]; + }; + + users.users = optionalAttrs (cfg.user == defaultUser) { + ${cfg.user} = { + isSystemUser = true; + group = cfg.group; + }; + }; + + users.groups = optionalAttrs (cfg.group == defaultGroup) { + ${cfg.group} = { }; + }; + }; +}