From 8a47e4758e71b5bab65ca8630e5e22b81889899d Mon Sep 17 00:00:00 2001 From: oluceps Date: Wed, 6 Sep 2023 21:21:31 +0000 Subject: [PATCH] Add daed module (#12) * add: daed module * chore: add daed module to doc * chore: expand default overlay to both --- README.md | 19 +++++++- daed/module.nix | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 27 +++++++----- 3 files changed, 147 insertions(+), 12 deletions(-) create mode 100644 daed/module.nix diff --git a/README.md b/README.md index 387d120..2a113c5 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Modify flake.nix nixosConfigurations.HOSTNAME = nixpkgs.lib.nixosSystem { modules = [ inputs.daeuniverse.nixosModules.dae + inputs.daeuniverse.nixosModules.daed ]; }; } @@ -32,6 +33,20 @@ Modify flake.nix # configuration.nix {inputs, pkgs, ...}: { + + +# with daed + services.daed = { + enable = true; + configDir = "/etc/daed"; + listen = "0.0.0.0:2023"; + openFirewall = { + enable = true; + port = 12345; + }; + }; + +# or with dae services.dae = { enable = true; disableTxChecksumIpGeneric = false; @@ -41,8 +56,8 @@ Modify flake.nix enable = true; port = 12345; }; - }; -} + }; +}; # use packages diff --git a/daed/module.nix b/daed/module.nix new file mode 100644 index 0000000..7347f22 --- /dev/null +++ b/daed/module.nix @@ -0,0 +1,113 @@ +inputs: { config, lib, pkgs, ... }: + +let + cfg = config.services.daed; + defaultDaedPackage = inputs.self.packages.${pkgs.stdenv.hostPlatform.system}.daed; +in +{ + # disables Nixpkgs daed module to avoid conflicts + disabledModules = [ "services/networking/daed.nix" ]; + + options = { + services.daed = with lib;{ + enable = mkEnableOption + (mdDoc "A modern dashboard for dae"); + + package = mkOption { + type = types.path; + default = defaultDaedPackage; + defaultText = literalExpression '' + daed.packages.${pkgs.stdenv.hostPlatform.system}.daed + ''; + example = literalExpression "pkgs.daed"; + description = mdDoc '' + The daed package to use. + ''; + }; + + configDir = mkOption { + type = types.str; + default = "/etc/daed"; + description = mdDoc '' + The daed work directory. + ''; + }; + + listen = mkOption { + type = types.str; + default = "0.0.0.0:2023"; + description = mdDoc '' + The daed listen address. + ''; + }; + + openFirewall = mkOption { + type = with types; submodule { + options = { + enable = mkEnableOption "enable"; + port = mkOption { + type = types.int; + description = '' + Port to be opened. Consist with field `tproxy_port` in config file. + ''; + }; + }; + }; + default = { + enable = true; + port = 12345; + }; + defaultText = literalExpression '' + { + enable = true; + port = 12345; + } + ''; + description = mdDoc '' + Open the firewall port. + ''; + }; + }; + }; + + config = lib.mkIf cfg.enable + + { + environment.systemPackages = [ cfg.package ]; + systemd.packages = [ cfg.package ]; + + networking = lib.mkIf cfg.openFirewall.enable { + firewall = + let portToOpen = cfg.openFirewall.port; + in + { + allowedTCPPorts = [ portToOpen ]; + allowedUDPPorts = [ portToOpen ]; + }; + }; + + systemd.services.daed = + let + daedBin = lib.getExe cfg.package; + in + { + wantedBy = [ "multi-user.target" ]; + after = [ + "network-online.target" + "systemd-sysctl.service" + ]; + wants = [ + "network-online.target" + ]; + + serviceConfig = { + Type = "simple"; + User = "root"; + LimitNPROC = 512; + LimitNOFILE = 1048576; + ExecStart = "${daedBin} run -c ${cfg.configDir} -l ${cfg.listen}"; + Restart = "on-abnormal"; + }; + }; + }; +} diff --git a/flake.nix b/flake.nix index 9861bbb..747fef0 100644 --- a/flake.nix +++ b/flake.nix @@ -38,16 +38,23 @@ }; }; }; - flake = { - nixosModules = { dae = import ./dae/module.nix inputs; }; - overlays = { - default = final: prev: { - dae = inputs.self.packages.dae; - daed = inputs.self.packages.daed; + flake = + let + moduleName = [ "dae" "daed" ]; + genFlake = n: { + nixosModules = { + ${n} = import ./${n}/module.nix inputs; + }; + overlays = { + ${n} = final: prev: { ${n} = inputs.self.packages.${n}; }; + }; }; - dae = final: prev: { dae = inputs.self.packages.dae; }; - daed = final: prev: { daed = inputs.self.packages.daed; }; - }; - }; + in + inputs.nixpkgs.lib.mkMerge ( + (map genFlake moduleName) ++ [{ + overlays.default = final: prev: inputs.nixpkgs.lib.genAttrs moduleName + (n: { ${n} = inputs.self.packages.${n}; }); + }] + ); }; }