forked from tailscale/golink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
162 lines (146 loc) · 4.37 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
160
161
162
{
description = "golink - A private shortlink service for tailnets";
inputs = {
nixpkgs.url = "nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{ self
, nixpkgs
, flake-utils
, ...
}:
let
golinkVersion =
if (self ? shortRev)
then self.shortRev
else "dev";
in
{
overlay = final: prev:
let
pkgs = nixpkgs.legacyPackages.${prev.system};
in
rec {
golink = pkgs.buildGo121Module rec {
pname = "golink";
version = golinkVersion;
src = pkgs.nix-gitignore.gitignoreSource [ ] ./.;
vendorSha256 = "sha256-+DC7ybg+2hBdhX7SyAA2zUTCJlUXQwxzT1CjLf3drv4="; # SHA based on vendoring go.mod
};
};
}
// flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
overlays = [ self.overlay ];
inherit system;
};
in
rec {
# `nix develop`
devShell = pkgs.mkShell { buildInputs = [ pkgs.go_1_21 ]; };
# `nix build`
packages = with pkgs; {
inherit golink;
};
defaultPackage = pkgs.golink;
# `nix run`
apps.golink = flake-utils.lib.mkApp {
drv = packages.golink;
};
defaultApp = apps.golink;
overlays.default = self.overlay;
})
// {
nixosModules.default =
{ pkgs
, lib
, config
, ...
}:
let
cfg = config.services.golink;
in
{
options = with lib; {
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.path;
description = "Path to file containing the Tailscale Auth Key";
};
verbose = mkOption {
type = types.bool;
default = false;
};
};
};
config = lib.mkIf cfg.enable {
users.users."${cfg.user}" = {
home = cfg.dataDir;
createHome = true;
group = "${cfg.group}";
isSystemUser = true;
isNormalUser = false;
description = "user for golink service";
};
users.groups."${cfg.group}" = { };
systemd.services.golink = {
enable = true;
script =
let
args =
[
"--sqlitedb ${cfg.databaseFile}"
]
++ lib.optionals cfg.verbose [ "--verbose" ];
in
''
${lib.optionalString (cfg.tailscaleAuthKeyFile != null) ''
export TS_AUTHKEY="$(head -n1 ${lib.escapeShellArg cfg.tailscaleAuthKeyFile})"
''}
${cfg.package}/bin/golink ${builtins.concatStringsSep " " args}
'';
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
Restart = "always";
RestartSec = "15";
WorkingDirectory = "${cfg.dataDir}";
};
};
};
};
};
}