feat: nix: do not hardcode headplane-agent configuration environment variables

To make the module more flexible and to reduce the dependency on the
config API.
This commit is contained in:
Igor Ramazanov 2025-03-12 13:31:11 +00:00
parent 7ad851d49e
commit 654d09b44d

View File

@ -1,20 +1,36 @@
{ {
config, config,
pkgs,
lib, lib,
pkgs,
... ...
}: let }: let
inherit inherit
(lib) (lib)
attrsToList
listToAttrs
map
mkEnableOption mkEnableOption
mkIf mkIf
mkOption mkOption
mkPackageOption mkPackageOption
typeOf
types types
; ;
cfg = config.services.headplane; cfg = config.services.headplane;
settingsFormat = pkgs.formats.yaml {}; settingsFormat = pkgs.formats.yaml {};
settingsFile = settingsFormat.generate "headplane-config.yaml" cfg.settings; settingsFile = settingsFormat.generate "headplane-config.yaml" cfg.settings;
agentEnv = listToAttrs (map (n: {
name = n.name;
value =
if ((typeOf n.value) == "bool")
then
(
if (n.value)
then "true"
else "false"
)
else n.value;
}) (attrsToList cfg.agent.settings));
in { in {
options.services.headplane = { options.services.headplane = {
enable = mkEnableOption "headplane"; enable = mkEnableOption "headplane";
@ -25,6 +41,7 @@ in {
freeformType = settingsFormat.type; freeformType = settingsFormat.type;
}; };
default = {}; default = {};
description = "Headplane config, generates a YAML config. See: https://github.com/tale/headplane/blob/main/config.example.yaml.";
}; };
agent = mkOption { agent = mkOption {
@ -32,29 +49,10 @@ in {
options = { options = {
enable = mkEnableOption "headplane-agent"; enable = mkEnableOption "headplane-agent";
package = mkPackageOption pkgs "headplane-agent" {}; package = mkPackageOption pkgs "headplane-agent" {};
debug = mkOption { settings = mkOption {
type = types.bool; type = types.attrsOf [types.str types.bool];
description = "Enable debug logging if true."; description = "Headplane agent env vars config. See: https://github.com/tale/headplane/blob/main/docs/Headplane-Agent.md";
}; default = {};
hostname = mkOption {
type = types.str;
description = "A hostname you want to use for the agent.";
};
tsServer = mkOption {
type = types.str;
description = "The URL to your Headscale instance.";
};
tsAuthKey = mkOption {
type = types.str;
description = "An authorization key to authenticate with Headscale (see below).";
};
hpServer = mkOption {
type = types.str;
description = "The URL to your Headplane instance, including the subpath (eg. https://headplane.example.com/admin).";
};
hpAuthKey = mkOption {
type = types.str;
description = "The generated auth key to authenticate with Headplane.";
}; };
}; };
}; };
@ -62,9 +60,10 @@ in {
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
environment.systemPackages = [cfg.package]; environment = {
systemPackages = [cfg.package];
environment.etc."headplane/config.yaml".source = "${settingsFile}"; etc."headplane/config.yaml".source = "${settingsFile}";
};
systemd.services.headplane-agent = systemd.services.headplane-agent =
mkIf cfg.agent.enable mkIf cfg.agent.enable
@ -75,17 +74,7 @@ in {
after = ["headplane.service"]; after = ["headplane.service"];
requires = ["headplane.service"]; requires = ["headplane.service"];
environment = { environment = agentEnv;
HEADPLANE_AGENT_DEBUG =
if cfg.agent.debug
then "true"
else "false";
HEADPLANE_AGENT_HOSTNAME = cfg.agent.hostname;
HEADPLANE_AGENT_TS_SERVER = cfg.agent.tsServer;
HEADPLANE_AGENT_TS_AUTHKEY = cfg.agent.tsAuthkey;
HEADPLANE_AGENT_HP_SERVER = cfg.agent.hpServer;
HEADPLANE_AGENT_HP_AUTHKEY = cfg.agent.hpAuthkey;
};
serviceConfig = { serviceConfig = {
User = config.services.headscale.user; User = config.services.headscale.user;
@ -96,7 +85,7 @@ in {
RestartSec = 5; RestartSec = 5;
# TODO: Harden `systemd` security according to the "The Principle of Least Power". # TODO: Harden `systemd` security according to the "The Principle of Least Power".
# See: `$ systemd-analyze security headplane`. # See: `$ systemd-analyze security headplane-agent`.
}; };
}; };