Skip to content

Commit 136d5ad

Browse files
kradalbyXe
andauthored
Add Flake file (Nix package, NixOS module) (#39)
* Add Flake file (Nix package, NixOS module) This commit adds a Flake.nix file allowing Nix users to consume this repo with `nix build`, `nix run` and adds a NixOS module to quickly spin up the service. * add github action file for nix build --------- Signed-off-by: Kristoffer Dalby <[email protected]> Co-authored-by: Xe Iaso <[email protected]>
1 parent cada6f6 commit 136d5ad

File tree

4 files changed

+230
-0
lines changed

4 files changed

+230
-0
lines changed

.github/workflows/nix.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Nix build
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v3
17+
with:
18+
fetch-depth: 2
19+
20+
- uses: cachix/install-nix-action@v16
21+
22+
- name: Run build
23+
run: nix build

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
node_modules
22
package-lock.json
3+
.direnv
4+
.envrc
5+
result

flake.lock

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
description = "golink - A private shortlink service for tailnets";
3+
4+
inputs = {
5+
nixpkgs.url = "nixpkgs/nixpkgs-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs =
10+
{ self
11+
, nixpkgs
12+
, flake-utils
13+
, ...
14+
}:
15+
let
16+
golinkVersion =
17+
if (self ? shortRev)
18+
then self.shortRev
19+
else "dev";
20+
in
21+
{
22+
overlay = final: prev:
23+
let
24+
pkgs = nixpkgs.legacyPackages.${prev.system};
25+
in
26+
rec {
27+
golink = pkgs.buildGo119Module rec {
28+
pname = "golink";
29+
version = golinkVersion;
30+
src = pkgs.nix-gitignore.gitignoreSource [ ] ./.;
31+
32+
vendorSha256 = "sha256-uho3VxVpCmHBVg6zcB+HhiplG7qfl6iJxMacRnzAR/0=";
33+
};
34+
};
35+
}
36+
// flake-utils.lib.eachDefaultSystem
37+
(system:
38+
let
39+
pkgs = import nixpkgs {
40+
overlays = [ self.overlay ];
41+
inherit system;
42+
};
43+
in
44+
rec {
45+
# `nix develop`
46+
devShell = pkgs.mkShell { buildInputs = [ pkgs.go ]; };
47+
48+
# `nix build`
49+
packages = with pkgs; {
50+
inherit golink;
51+
};
52+
53+
defaultPackage = pkgs.golink;
54+
55+
# `nix run`
56+
apps.golink = flake-utils.lib.mkApp {
57+
drv = packages.golink;
58+
};
59+
defaultApp = apps.golink;
60+
61+
overlays.default = self.overlay;
62+
})
63+
// {
64+
nixosModules.default =
65+
{ pkgs
66+
, lib
67+
, config
68+
, ...
69+
}:
70+
let
71+
cfg = config.services.golink;
72+
in
73+
{
74+
options = with lib; {
75+
services.golink = {
76+
enable = mkEnableOption "Enable golink";
77+
78+
package = mkOption {
79+
type = types.package;
80+
description = ''
81+
golink package to use
82+
'';
83+
default = pkgs.golink;
84+
};
85+
86+
dataDir = mkOption {
87+
type = types.path;
88+
default = "/var/lib/golink";
89+
description = "Path to data dir";
90+
};
91+
92+
user = mkOption {
93+
type = types.str;
94+
default = "golink";
95+
description = "User account under which golink runs.";
96+
};
97+
98+
group = mkOption {
99+
type = types.str;
100+
default = "golink";
101+
description = "Group account under which golink runs.";
102+
};
103+
104+
databaseFile = mkOption {
105+
type = types.path;
106+
default = "/var/lib/golink/golink.db";
107+
description = "Path to SQLite database";
108+
};
109+
110+
tailscaleAuthKeyFile = mkOption {
111+
type = types.path;
112+
description = "Path to file containing the Tailscale Auth Key";
113+
};
114+
115+
verbose = mkOption {
116+
type = types.bool;
117+
default = false;
118+
};
119+
};
120+
};
121+
config = lib.mkIf cfg.enable {
122+
users.users."${cfg.user}" = {
123+
home = cfg.dataDir;
124+
createHome = true;
125+
group = "${cfg.group}";
126+
isSystemUser = true;
127+
isNormalUser = false;
128+
description = "user for golink service";
129+
};
130+
users.groups."${cfg.group}" = { };
131+
132+
systemd.services.golink = {
133+
enable = true;
134+
script =
135+
let
136+
args =
137+
[
138+
"--sqlitedb ${cfg.databaseFile}"
139+
]
140+
++ lib.optionals cfg.verbose [ "--verbose" ];
141+
in
142+
''
143+
${lib.optionalString (cfg.tailscaleAuthKeyFile != null) ''
144+
export TS_AUTHKEY="$(head -n1 ${lib.escapeShellArg cfg.tailscaleAuthKeyFile})"
145+
''}
146+
147+
${cfg.package}/bin/golink ${builtins.concatStringsSep " " args}
148+
'';
149+
wantedBy = [ "multi-user.target" ];
150+
after = [ "network-online.target" ];
151+
serviceConfig = {
152+
User = cfg.user;
153+
Group = cfg.group;
154+
Restart = "always";
155+
RestartSec = "15";
156+
WorkingDirectory = "${cfg.dataDir}";
157+
};
158+
};
159+
};
160+
};
161+
};
162+
}

0 commit comments

Comments
 (0)