Skip to content

Commit 20f8422

Browse files
committed
Introduce nixos module for tclip
* Add a nixosModule * Add an overlay fixes #63
1 parent 3de85c3 commit 20f8422

File tree

1 file changed

+189
-65
lines changed

1 file changed

+189
-65
lines changed

flake.nix

Lines changed: 189 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,92 +12,216 @@
1212
};
1313
};
1414

15-
outputs = { self, nixpkgs, utils, gomod2nix }:
16-
utils.lib.eachSystem [
15+
outputs = {
16+
self,
17+
nixpkgs,
18+
utils,
19+
gomod2nix,
20+
}:
21+
{
22+
overlays.default = final: prev: {
23+
tclip = self.packages.tclip;
24+
tclipd = self.packages.tclipd;
25+
};
26+
27+
nixosModules.tclip = {
28+
config,
29+
lib,
30+
...
31+
}:
32+
with lib; let
33+
cfg = config.services.tclip;
34+
in {
35+
options.services.tclip = {
36+
enable = mkEnableOption "Enable tclip service";
37+
38+
package = mkOption {
39+
type = types.package;
40+
description = ''
41+
tclip package to use
42+
'';
43+
default = self.packages."${system}".tclipd;
44+
};
45+
46+
dataDir = mkOption {
47+
type = types.path;
48+
default = "/var/lib/tclip";
49+
description = "Path to data dir";
50+
};
51+
52+
hostname = mkOption {
53+
type = types.str;
54+
default = "paste";
55+
description = "Hostname to use on your tailnet";
56+
};
57+
58+
funnel = mkOption {
59+
type = types.bool;
60+
default = false;
61+
description = "if set, expose individual pastes to the public internet with Funnel";
62+
};
63+
64+
user = mkOption {
65+
type = types.str;
66+
default = "tclip";
67+
description = "User account under which tclip runs.";
68+
};
69+
70+
group = mkOption {
71+
type = types.str;
72+
default = "tclip";
73+
description = "Group account under which tclip runs.";
74+
};
75+
76+
tailscaleAuthKeyFile = mkOption {
77+
type = types.path;
78+
description = "Path to file containing the Tailscale Auth Key";
79+
};
80+
81+
verbose = mkOption {
82+
type = types.bool;
83+
default = false;
84+
};
85+
};
86+
config = mkIf cfg.enable {
87+
environment.systemPackages = [
88+
self.packages."${system}".tclip
89+
];
90+
91+
users.users."${cfg.user}" = {
92+
home = cfg.dataDir;
93+
createHome = true;
94+
group = "${cfg.group}";
95+
isSystemUser = true;
96+
isNormalUser = false;
97+
description = "User for tclip service";
98+
};
99+
users.groups."${cfg.group}" = {};
100+
101+
systemd.services.tclip = {
102+
enable = true;
103+
script = let
104+
args =
105+
[
106+
"--data-dir"
107+
cfg.dataDir
108+
"--hostname"
109+
cfg.hostname
110+
]
111+
++ lib.optionals cfg.verbose ["--tsnet-verbose"]
112+
++ lib.optionals cfg.funnel ["--use-funnel"];
113+
in ''
114+
${lib.optionalString (cfg.tailscaleAuthKeyFile != null) ''
115+
export TS_AUTHKEY="$(head -n1 ${lib.escapeShellArg cfg.tailscaleAuthKeyFile})"
116+
''}
117+
${cfg.package}/bin/tclipd ${builtins.concatStringsSep " " args};
118+
'';
119+
wantedBy = ["multi-user.target"];
120+
serviceConfig = {
121+
User = cfg.user;
122+
Group = cfg.group;
123+
Restart = "always";
124+
RestartSec = "15";
125+
WorkingDirectory = "${cfg.dataDir}";
126+
};
127+
};
128+
};
129+
};
130+
131+
nixosModules.default = self.nixosModules.tclip;
132+
}
133+
// utils.lib.eachSystem [
17134
"x86_64-linux"
18135
"aarch64-linux"
19136
"x86_64-darwin"
20137
"aarch64-darwin"
21-
] (system:
22-
let
23-
graft = pkgs: pkg: pkg.override {
24-
buildGoModule = pkgs.buildGo122Module;
25-
};
26-
pkgs = import nixpkgs {
27-
inherit system;
28-
overlays = [ gomod2nix.overlays.default (final: prev: {
138+
] (system: let
139+
graft = pkgs: pkg:
140+
pkg.override {
141+
buildGoModule = pkgs.buildGo122Module;
142+
};
143+
pkgs = import nixpkgs {
144+
inherit system;
145+
overlays = [
146+
gomod2nix.overlays.default
147+
(final: prev: {
29148
go = prev.go;
30149
go-tools = graft prev prev.go-tools;
31150
gotools = graft prev prev.gotools;
32151
gopls = graft prev prev.gopls;
33-
}) ];
152+
})
153+
];
154+
};
155+
version = builtins.substring 0 8 self.lastModifiedDate;
156+
in {
157+
packages = rec {
158+
tclipd = pkgs.buildGoApplication {
159+
pname = "tclipd";
160+
version = "0.1.0-${version}";
161+
go = pkgs.go;
162+
src = ./.;
163+
subPackages = "cmd/tclipd";
164+
modules = ./gomod2nix.toml;
34165
};
35-
version = builtins.substring 0 8 self.lastModifiedDate;
36-
in {
37-
packages = rec {
38-
tclipd = pkgs.buildGoApplication {
39-
pname = "tclipd";
40-
version = "0.1.0-${version}";
41-
go = pkgs.go;
42-
src = ./.;
43-
subPackages = "cmd/tclipd";
44-
modules = ./gomod2nix.toml;
45-
};
46166

47-
tclip = pkgs.buildGoApplication {
48-
pname = "tclip";
49-
inherit (tclipd) src version modules;
50-
subPackages = "cmd/tclip";
51-
go = pkgs.go;
167+
tclip = pkgs.buildGoApplication {
168+
pname = "tclip";
169+
inherit (tclipd) src version modules;
170+
subPackages = "cmd/tclip";
171+
go = pkgs.go;
52172

53-
CGO_ENABLED = "0";
54-
};
173+
CGO_ENABLED = "0";
174+
};
55175

56-
docker = pkgs.dockerTools.buildLayeredImage {
57-
name = "ghcr.io/tailscale-dev/tclip";
58-
tag = "latest";
59-
config.Cmd = [ "${tclipd}/bin/tclipd" ];
60-
contents = [ pkgs.cacert ];
61-
};
176+
docker = pkgs.dockerTools.buildLayeredImage {
177+
name = "ghcr.io/tailscale-dev/tclip";
178+
tag = "latest";
179+
config.Cmd = ["${tclipd}/bin/tclipd"];
180+
contents = [pkgs.cacert];
181+
};
62182

63-
portable-service = let
64-
web-service = pkgs.substituteAll {
65-
name = "tclip.service";
66-
src = ./run/portable-service/tclip.service.in;
67-
inherit tclipd;
68-
};
69-
in pkgs.portableService {
183+
portable-service = let
184+
web-service = pkgs.substituteAll {
185+
name = "tclip.service";
186+
src = ./run/portable-service/tclip.service.in;
187+
inherit tclipd;
188+
};
189+
in
190+
pkgs.portableService {
70191
inherit (tclipd) version;
71192
pname = "tclip";
72193
description = "The tclip service";
73194
homepage = "https://github.com/tailscale-dev/tclip";
74-
units = [ web-service ];
75-
symlinks = [{
76-
object = "${pkgs.cacert}/etc/ssl";
77-
symlink = "/etc/ssl";
78-
}];
195+
units = [web-service];
196+
symlinks = [
197+
{
198+
object = "${pkgs.cacert}/etc/ssl";
199+
symlink = "/etc/ssl";
200+
}
201+
];
79202
};
80203

81-
default = docker;
82-
};
204+
default = docker;
205+
};
83206

84-
apps.default =
85-
utils.lib.mkApp { drv = self.packages.${system}.default; };
207+
apps.default =
208+
utils.lib.mkApp {drv = self.packages.${system}.default;};
86209

87-
devShells.default = pkgs.mkShell {
88-
buildInputs = with pkgs; [
89-
go
90-
gopls
91-
gotools
92-
go-tools
93-
gomod2nix.packages.${system}.default
94-
sqlite-interactive
210+
devShells.default = pkgs.mkShell {
211+
buildInputs = with pkgs; [
212+
go
213+
gopls
214+
gotools
215+
go-tools
216+
gomod2nix.packages.${system}.default
217+
sqlite-interactive
95218

96-
yarn
97-
nodejs
98-
];
219+
yarn
220+
nodejs
221+
];
99222

100-
TSNET_HOSTNAME = "paste-devel";
101-
};
102-
}) // {};
223+
TSNET_HOSTNAME = "paste-devel";
224+
};
225+
})
226+
// {};
103227
}

0 commit comments

Comments
 (0)