-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathflake.nix
159 lines (142 loc) · 4.58 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
description = "golink - A private shortlink service for tailnets";
inputs = {
nixpkgs.url = "nixpkgs/nixpkgs-unstable";
systems.url = "github:nix-systems/default";
};
outputs =
{ self
, nixpkgs
, systems
,
}:
let
eachSystem = f: nixpkgs.lib.genAttrs (import systems) (s: f nixpkgs.legacyPackages.${s});
in
{
formatter = eachSystem (pkgs: pkgs.nixpkgs-fmt);
devShells = eachSystem (pkgs: {
default = pkgs.mkShell { buildInputs = [ pkgs.go_1_24 ]; };
});
packages = eachSystem (pkgs: {
default = pkgs.buildGo124Module {
pname = "golink";
version = if (self ? shortRev) then self.shortRev else "dev";
src = pkgs.nix-gitignore.gitignoreSource [ ] ./.;
ldflags =
let
tsVersion =
with builtins;
head (match ".*tailscale.com v([0-9]+\.[0-9]+\.[0-9]+-?[a-zA-Z]?).*" (readFile ./go.mod));
in
[
"-w"
"-s"
"-X tailscale.com/version.longStamp=${tsVersion}"
"-X tailscale.com/version.shortStamp=${tsVersion}"
];
vendorHash = "sha256-RqKsIgwkCbIMQdCKtSHqW9eiZuNcZLCYeXFhPbgxjEU="; # SHA based on vendoring go.mod
};
});
overlays.default = final: prev: {
golink = self.packages.${prev.system}.default;
};
nixosModules.default =
{ config
, lib
, pkgs
, ...
}:
let
cfg = config.services.golink;
inherit (lib)
concatStringsSep
escapeShellArg
mkEnableOption
mkIf
mkOption
optionalString
optionals
types
;
in
{
options.services.golink = {
enable = mkEnableOption "Enable golink";
package = mkOption {
type = types.package;
description = ''
golink package to use
'';
default = pkgs.golink;
};
dataDir = mkOption {
type = types.path;
default = "/var/lib/golink";
description = "Path to data dir";
};
user = mkOption {
type = types.str;
default = "golink";
description = "User account under which golink runs.";
};
group = mkOption {
type = types.str;
default = "golink";
description = "Group account under which golink runs.";
};
databaseFile = mkOption {
type = types.path;
default = "/var/lib/golink/golink.db";
description = "Path to SQLite database";
};
tailscaleAuthKeyFile = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Path to the file containing the Tailscale Auth Key.
If null, manual authorization with the tailnet is required.
Check `journalctl -eu golink` for the login link.
'';
};
verbose = mkOption {
type = types.bool;
default = false;
};
};
config = mkIf cfg.enable {
nixpkgs.overlays = [ self.overlays.default ];
users.groups."${cfg.group}" = { };
users.users."${cfg.user}" = {
home = cfg.dataDir;
createHome = true;
group = "${cfg.group}";
isSystemUser = true;
isNormalUser = false;
description = "user for golink service";
};
systemd.services.golink = {
enable = true;
script =
let
args = [ "--sqlitedb ${cfg.databaseFile}" ] ++ optionals cfg.verbose [ "--verbose" ];
in
''
${optionalString (cfg.tailscaleAuthKeyFile != null) ''
export TS_AUTHKEY="$(head -n1 ${escapeShellArg cfg.tailscaleAuthKeyFile})"
''}
${cfg.package}/bin/golink ${concatStringsSep " " args}
'';
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
Restart = "always";
RestartSec = "15";
WorkingDirectory = "${cfg.dataDir}";
};
};
};
};
};
}