Overriding a parameter for a config option

I want to use nixos’s services.terraria to start a terraria server, but I want to use pkgs.tshock as the server exec. In the source terraria.nix I see this:

    systemd.services.terraria = {
      description = "Terraria Server Service";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      serviceConfig = {
        User = "terraria";
        Group = "terraria";
        Type = "forking";
        GuessMainPID = true;
        UMask = 7;
        ExecStart = "${tmuxCmd} new -d ${pkgs.terraria-server}/bin/TerrariaServer ${lib.concatStringsSep " " flags}";
        ExecStop = "${stopScript} $MAINPID";
      };
    };

Is there a way to override this with custom exec?

Sure you can set systemd.services.terraria.serviceConfig.ExecStart directly, but you need to provide it as a list and “clear” the existing command by setting "" as the first command in the list.

The problem is that it has a custom ${tmuxCmd} and is there a way to fetch that from the original .nix file (defined with let)?

No, there isn’t. You’ll have to copy that manually.

I’d argue:

{ lib, ... }: {
  systemd.services.terraria.serviceConfig.ExecStart = lib.mkForce "your command here";
}

is a bit cleaner. No need to use counterintuitive systemd-specific features just to clear a list that’s properly encoded :wink:

Defining too much with let is arguably a bit of a code smell for exactly this reason, values defined in let blocks cannot be interacted with downstream. You can only touch stuff that ends up in the options or config attrsets.

If you want to expose this value (perhaps through a read-only option, or maybe by adding support for changing the binary by adding a services.terraria.package option so that you can just change the terraria package the module uses), I’m sure a PR is welcome.

4 Likes