nixos pykms: run via DynamicUser

This commit is contained in:
Peter Hoeg 2018-03-26 15:16:22 +08:00
parent 2f94acd9ae
commit 1c30532b6d
2 changed files with 29 additions and 42 deletions

View File

@ -306,7 +306,7 @@
rslsync = 279; rslsync = 279;
minio = 280; minio = 280;
kanboard = 281; kanboard = 281;
pykms = 282; # pykms = 282; # DynamicUser = true
kodi = 283; kodi = 283;
restya-board = 284; restya-board = 284;
mighttpd2 = 285; mighttpd2 = 285;
@ -597,7 +597,7 @@
rslsync = 279; rslsync = 279;
minio = 280; minio = 280;
kanboard = 281; kanboard = 281;
pykms = 282; # pykms = 282; # DynamicUser = true
kodi = 283; kodi = 283;
restya-board = 284; restya-board = 284;
mighttpd2 = 285; mighttpd2 = 285;

View File

@ -5,20 +5,8 @@ with lib;
let let
cfg = config.services.pykms; cfg = config.services.pykms;
home = "/var/lib/pykms";
services = {
serviceConfig = {
Restart = "on-failure";
RestartSec = "10s";
StartLimitInterval = "1min";
PrivateTmp = true;
ProtectSystem = "full";
ProtectHome = true;
};
};
in { in {
meta.maintainers = with lib.maintainers; [ peterhoeg ];
options = { options = {
services.pykms = rec { services.pykms = rec {
@ -51,39 +39,38 @@ in {
default = false; default = false;
description = "Whether the listening port should be opened automatically."; description = "Whether the listening port should be opened automatically.";
}; };
memoryLimit = mkOption {
type = types.str;
default = "64M";
description = "How much memory to use at most.";
};
}; };
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewallPort [ cfg.port ]; networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewallPort [ cfg.port ];
systemd.services = { systemd.services.pykms = let
pykms = services // { home = "/var/lib/pykms";
description = "Python KMS"; in {
wantedBy = [ "multi-user.target" ]; description = "Python KMS";
serviceConfig = with pkgs; { after = [ "network.target" ];
User = "pykms"; wantedBy = [ "multi-user.target" ];
Group = "pykms"; # python programs with DynamicUser = true require HOME to be set
ExecStartPre = "${getBin pykms}/bin/create_pykms_db.sh ${home}/clients.db"; environment.HOME = home;
ExecStart = "${getBin pykms}/bin/server.py ${optionalString cfg.verbose "--verbose"} ${cfg.listenAddress} ${toString cfg.port}"; serviceConfig = with pkgs; {
WorkingDirectory = home; DynamicUser = true;
MemoryLimit = "64M"; StateDirectory = baseNameOf home;
}; ExecStartPre = "${getBin pykms}/bin/create_pykms_db.sh ${home}/clients.db";
}; ExecStart = lib.concatStringsSep " " ([
}; "${getBin pykms}/bin/server.py"
cfg.listenAddress
users = { (toString cfg.port)
users.pykms = { ] ++ lib.optional cfg.verbose "--verbose");
name = "pykms"; WorkingDirectory = home;
group = "pykms"; Restart = "on-failure";
home = home; MemoryLimit = cfg.memoryLimit;
createHome = true;
uid = config.ids.uids.pykms;
description = "PyKMS daemon user";
};
groups.pykms = {
gid = config.ids.gids.pykms;
}; };
}; };
}; };