From b6974d7e1c0dba7158beb9f7ac6d6aa91bbcf031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domen=20Ko=C5=BEar?= Date: Sun, 24 Nov 2024 15:32:11 +0000 Subject: [PATCH] add tailscale serve --- devenv.lock | 57 +++++++++++++------------- src/modules/services/tailscale.nix | 64 ++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 src/modules/services/tailscale.nix diff --git a/devenv.lock b/devenv.lock index 322f63949..6c9743b56 100644 --- a/devenv.lock +++ b/devenv.lock @@ -63,32 +63,10 @@ "type": "github" } }, - "git-hooks": { - "inputs": { - "flake-compat": "flake-compat", - "gitignore": "gitignore", - "nixpkgs": [ - "nixpkgs" - ], - "nixpkgs-stable": "nixpkgs-stable" - }, - "locked": { - "lastModified": 1730302582, - "owner": "cachix", - "repo": "git-hooks.nix", - "rev": "af8a16fe5c264f5e9e18bcee2859b40a656876cf", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "git-hooks.nix", - "type": "github" - } - }, "gitignore": { "inputs": { "nixpkgs": [ - "git-hooks", + "pre-commit-hooks", "nixpkgs" ] }, @@ -122,7 +100,7 @@ }, "nix": { "inputs": { - "flake-compat": "flake-compat_2", + "flake-compat": "flake-compat", "flake-parts": "flake-parts", "libgit2": "libgit2", "nixpkgs": "nixpkgs", @@ -191,10 +169,10 @@ }, "nixpkgs-stable": { "locked": { - "lastModified": 1730327045, + "lastModified": 1731797254, "owner": "NixOS", "repo": "nixpkgs", - "rev": "080166c15633801df010977d9d7474b4a6c549d7", + "rev": "e8c38b73aeb218e27163376a2d617e61a2ad9b59", "type": "github" }, "original": { @@ -249,15 +227,34 @@ "type": "github" } }, + "pre-commit-hooks_2": { + "inputs": { + "flake-compat": "flake-compat_2", + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ], + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1732021966, + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "3308484d1a443fc5bc92012435d79e80458fe43c", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, "root": { "inputs": { "devenv": "devenv", - "git-hooks": "git-hooks", "nix": "nix", "nixpkgs": "nixpkgs_2", - "pre-commit-hooks": [ - "git-hooks" - ] + "pre-commit-hooks": "pre-commit-hooks_2" } } }, diff --git a/src/modules/services/tailscale.nix b/src/modules/services/tailscale.nix new file mode 100644 index 000000000..7110e8b7d --- /dev/null +++ b/src/modules/services/tailscale.nix @@ -0,0 +1,64 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.services.tailscale; + + serveOpts = { + options = { + target = mkOption { + type = types.nullOr types.str; + default = null; + description = "Target for serve command (URL, file path, text content)"; + }; + + port = mkOption { + type = types.nullOr types.int; + default = null; + description = "Port to expose service on"; + }; + + protocol = mkOption { + type = types.enum [ "https" "http" "tcp" "tls-terminated-tcp" ]; + default = "https"; + description = "Protocol to expose service with"; + }; + + path = mkOption { + type = types.nullOr types.str; + default = null; + description = "Path to append to base URL"; + }; + }; + }; +in +{ + options = { + services.tailscale = { + enable = mkEnableOption "Tailscale serve service"; + + serve = mkOption { + type = types.attrsOf (types.submodule serveOpts); + default = { }; + description = "Tailscale serve configurations"; + }; + }; + }; + + config = lib.mkIf cfg.enable { + packages = [ pkgs.tailscale ]; + processes = mapAttrs' + (name: serveCfg: + nameValuePair "tailscale-serve-${name}" { + exec = concatStringsSep " " ([ + "${pkgs.tailscale}/bin/tailscale serve" + ] + ++ optional (serverCfg.path != null) "--set-path=${serveCfg.path}" + ++ optional (serverCfg.port != null) "--${serveCfg.protocol}=${toString serveCfg.port}" + ++ optional (serverCfg.target != null) serveCfg.target); + } + ) + cfg.serve; + }; +}