Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/minecraft-server: add fifo controll and stop warning #88432

Closed
wants to merge 1 commit into from

Conversation

Kloenk
Copy link
Member

@Kloenk Kloenk commented May 20, 2020

Motivation for this change

Providing a way to control the Minecraft server without the use of rcon, or running the Minecraft server in a tmux session.

Also, be able to see when the server restarts when you are playing.

Things done

implement services.minecraft-server.fifo and services.minecraft-server.warn

  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/minecraft-server-controll-socket/7286/5

@Kloenk Kloenk requested a review from infinisil May 23, 2020 09:46
nixos/modules/services/games/minecraft-server.nix Outdated Show resolved Hide resolved
nixos/modules/services/games/minecraft-server.nix Outdated Show resolved Hide resolved
nixos/modules/services/games/minecraft-server.nix Outdated Show resolved Hide resolved
nixos/modules/services/games/minecraft-server.nix Outdated Show resolved Hide resolved
nixos/modules/services/games/minecraft-server.nix Outdated Show resolved Hide resolved
@Kloenk Kloenk force-pushed the feature/mc-fifo branch 2 times, most recently from 17bd691 to 1719c3d Compare May 28, 2020 19:05
@Kloenk
Copy link
Member Author

Kloenk commented May 28, 2020

Thanks, @infinisil. Applied all changes.

@Kloenk Kloenk requested a review from infinisil May 28, 2020 19:05
@Kloenk
Copy link
Member Author

Kloenk commented May 29, 2020

Fixed, sorry missed it.

@Kloenk Kloenk requested a review from infinisil May 29, 2020 11:38
Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the minor bits in the review, my only other concern is:

$ systemctl stop minecraft-server
Warning: Stopping minecraft-server.service, but it can still be activated by:
  minecraft-server.socket

For what we use the socket for, I don't think it makes sense for it to be allowed to start the service. If there's no way to turn this off then I guess we'll just roll with it though.

nixos/modules/services/games/minecraft-server.nix Outdated Show resolved Hide resolved
nixos/modules/services/games/minecraft-server.nix Outdated Show resolved Hide resolved
@Kloenk
Copy link
Member Author

Kloenk commented Jun 1, 2020

Other than the minor bits in the review, my only other concern is:

$ systemctl stop minecraft-server
Warning: Stopping minecraft-server.service, but it can still be activated by:
  minecraft-server.socket

For what we use the socket for, I don't think it makes sense for it to be allowed to start the service. If there's no way to turn this off then I guess we'll just roll with it though.

The only way I see is to run a PostStop script which calls systemd to stop minecraft-server.socket. Unsure if we want this

@Kloenk Kloenk requested a review from infinisil June 1, 2020 13:44
@infinisil
Copy link
Member

infinisil commented Aug 11, 2020

I wasn't really happy with the current solution, so I tried some alternate approaches, and I found one that works quite well: Instead of having systemd create the socket, let the minecraft service itself create it (and destroy it), then we don't need to deal with socket unit weirdness. And then just pipe the socket to the minecraft command. Here's the diff:

diff --git a/nixos/modules/services/games/minecraft-server.nix b/nixos/modules/services/games/minecraft-server.nix
index c097e50b3e3..1f87557b630 100644
--- a/nixos/modules/services/games/minecraft-server.nix
+++ b/nixos/modules/services/games/minecraft-server.nix
@@ -161,6 +161,7 @@ in {
 
       standardInputFifo = mkOption {
         type = types.str;
+        readOnly = true;
         default = "/run/minecraft/stdin";
         description = ''
           The path to a FIFO for sending commands to standard input of the
@@ -205,18 +206,24 @@ in {
       description   = "Minecraft Server Service";
       wantedBy      = [ "multi-user.target" ];
       after         = [ "network.target" ];
-      requires = [ "minecraft-server.socket" ];
 
       serviceConfig = {
-        ExecStart = "${cfg.package}/bin/minecraft-server ${cfg.jvmOpts}";
         Restart = "always";
         User = "minecraft";
+        # Allow group to write to created files
+        UMask = "0002";
         WorkingDirectory = cfg.dataDir;
-        StandardInput = "socket";
+        RuntimeDirectory = "minecraft";
         StandardOutput = mkDefault "journal";
       };
 
+      script = ''
+        ${cfg.package}/bin/minecraft-server ${cfg.jvmOpts} <>${cfg.standardInputFifo}
+      '';
+
       preStart = ''
+        mkfifo ${cfg.standardInputFifo}
+
         ln -sf ${eulaFile} eula.txt
       '' + (if cfg.declarative then ''
 
@@ -245,25 +252,13 @@ in {
         fi
       '');
 
-      preStop = let 
+      preStop = let
         script = pkgs.writeShellScript "minecraft-stop-commands" cfg.stopCommands;
       in optionalString (cfg.stopCommands != null) ''
         ${script} > ${cfg.standardInputFifo}
       '';
     };
 
-    systemd.sockets.minecraft-server = {
-      description = "Minecraft stdin fifo file";
-      socketConfig = {
-        ListenFIFO = cfg.standardInputFifo;
-        SocketUser = "minecraft";
-        SocketGroup = "minecraft";
-        SocketMode = "0660";
-      };
-      #wantedBy = [ "sockets.target" ];
-
-    };
-
     networking.firewall = mkIf cfg.openFirewall (if cfg.declarative then {
       allowedUDPPorts = [ serverPort ];
       allowedTCPPorts = [ serverPort ]

Notes:

  • readOnly because I used RuntimeDirectory which automatically creates /run/minecraft and the /run prefix can't be changed.
  • RuntimeDirectory automatically removes /run/minecraft after the service is stopped, so no need to clean anything up manually
  • UMask = "0002" so that the socket can be written to by the group
  • <>${cfg.standardInputFifo} is a bit of an odd bash incantation that pipes the fifo to the commands stdin without closing the fifo after the first line

@stale
Copy link

stale bot commented Feb 7, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Feb 7, 2021
@Kloenk
Copy link
Member Author

Kloenk commented Feb 7, 2021

Forgot about this, and currently not hosting a mc Server. Will close

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Feb 7, 2021
@Kloenk Kloenk closed this Feb 7, 2021
@PeterHindes
Copy link
Contributor

Did this ever get finished.

@Kloenk
Copy link
Member Author

Kloenk commented Dec 24, 2021

Not past this state. So usable, but not clean enough to push it. Feel free to continue work on it, if you have questions feel free to ask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants