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

Offer NixOS module #63

Open
fzakaria opened this issue Aug 9, 2024 · 2 comments · May be fixed by #64
Open

Offer NixOS module #63

fzakaria opened this issue Aug 9, 2024 · 2 comments · May be fixed by #64
Assignees
Labels
enhancement New feature or request

Comments

@fzakaria
Copy link

fzakaria commented Aug 9, 2024

A nixos module similar to what is offered in golink would be great.

Here is a starter version I am using

{
  config,
  pkgs,
  lib,
  inputs,
  ...
}:
with lib; let
  cfg = config.services.tclip;
in {
  options.services.tclip = {
    enable = mkEnableOption "Enable tclip service";

    package = mkOption {
      type = types.package;
      description = ''
        tclip package to use
      '';
      default = inputs.tailscale-tclip.packages."${pkgs.system}".tclipd;
    };

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/tclip";
      description = "Path to data dir";
    };

    hostname = mkOption {
      type = types.str;
      default = "paste";
      description = "Hostname to use on your tailnet";
    };

    funnel = mkOption {
      type = types.bool;
      default = false;
      description = "if set, expose individual pastes to the public internet with Funnel";
    };

    user = mkOption {
      type = types.str;
      default = "tclip";
      description = "User account under which tclip runs.";
    };

    group = mkOption {
      type = types.str;
      default = "tclip";
      description = "Group account under which tclip runs.";
    };

    tailscaleAuthKeyFile = mkOption {
      type = types.path;
      description = "Path to file containing the Tailscale Auth Key";
    };

    verbose = mkOption {
      type = types.bool;
      default = false;
    };
  };
  config = mkIf cfg.enable {
    environment.systemPackages = [
      inputs.tailscale-tclip.packages."${pkgs.system}".tclip
    ];

    users.users."${cfg.user}" = {
      home = cfg.dataDir;
      createHome = true;
      group = "${cfg.group}";
      isSystemUser = true;
      isNormalUser = false;
      description = "User for tclip service";
    };
    users.groups."${cfg.group}" = {};

    systemd.services.tclip = {
      enable = true;
      script = let
        args =
          [
            "--data-dir"
            cfg.dataDir
            "--hostname"
            cfg.hostname
          ]
          ++ lib.optionals cfg.verbose ["--tsnet-verbose"]
          ++ lib.optionals cfg.funnel ["--use-funnel"];
      in ''
        ${lib.optionalString (cfg.tailscaleAuthKeyFile != null) ''
          export TS_AUTHKEY="$(head -n1 ${lib.escapeShellArg cfg.tailscaleAuthKeyFile})"
        ''}
        ${cfg.package}/bin/tclipd ${builtins.concatStringsSep " " args};
      '';
      wantedBy = ["multi-user.target"];
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        Restart = "always";
        RestartSec = "15";
        WorkingDirectory = "${cfg.dataDir}";
      };
    };
  };
}
@Erisa Erisa added the enhancement New feature or request label Aug 9, 2024
@fzakaria
Copy link
Author

Here is my final module with a few more options exposed:

{
  config,
  pkgs,
  lib,
  inputs,
  ...
}:
with lib; let
  cfg = config.services.tclip;
in {
  options.services.tclip = {
    enable = mkEnableOption "Enable tclip service";

    package = mkOption {
      type = types.package;
      description = ''
        tclip package to use
      '';
      default = pkgs.tclipd;
    };

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/tclip";
      description = "Path to data dir";
    };

    hostname = mkOption {
      type = types.str;
      default = "paste";
      description = "Hostname to use on your tailnet";
    };

    funnel = mkOption {
      type = types.bool;
      default = false;
      description = "if set, expose individual pastes to the public internet with Funnel";
    };

    user = mkOption {
      type = types.str;
      default = "tclip";
      description = "User account under which tclip runs.";
    };

    group = mkOption {
      type = types.str;
      default = "tclip";
      description = "Group account under which tclip runs.";
    };

    tailscaleAuthKeyFile = mkOption {
      type = types.path;
      description = "Path to file containing the Tailscale Auth Key";
    };

    verbose = mkOption {
      type = types.bool;
      default = false;
    };
  };
  config = mkIf cfg.enable {
    environment.systemPackages = [
      pkgs.tclip
    ];

    users.users."${cfg.user}" = {
      home = cfg.dataDir;
      createHome = true;
      group = "${cfg.group}";
      isSystemUser = true;
      isNormalUser = false;
      description = "User for tclip service";
    };
    users.groups."${cfg.group}" = {};

    systemd.services.tclip = {
      enable = true;
      script = let
        args =
          [
            "--data-location"
            cfg.dataDir
            "--hostname"
            cfg.hostname
          ]
          ++ lib.optionals cfg.verbose ["--tsnet-verbose"]
          ++ lib.optionals cfg.funnel ["--use-funnel"];
      in ''
        ${lib.optionalString (cfg.tailscaleAuthKeyFile != null) ''
          export TS_AUTHKEY="$(head -n1 ${lib.escapeShellArg cfg.tailscaleAuthKeyFile})"
        ''}
        ${cfg.package}/bin/tclipd ${builtins.concatStringsSep " " args};
      '';
      wantedBy = ["multi-user.target"];
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        Restart = "always";
        RestartSec = "15";
        WorkingDirectory = "${cfg.dataDir}";
      };
    };
  };
}

@Erisa
Copy link
Member

Erisa commented Aug 12, 2024

Thank you @fzakaria! Would you be able to submit a PR with this?

fzakaria added a commit to fzakaria/tclip that referenced this issue Aug 12, 2024
* Add a nixosModule
* Add an overlay

fixes tailscale-dev#63
@fzakaria fzakaria linked a pull request Aug 12, 2024 that will close this issue
fzakaria added a commit to fzakaria/tclip that referenced this issue Aug 12, 2024
* Add a nixosModule
* Add an overlay

fixes tailscale-dev#63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants