1{
2 config,
3 pkgs,
4 lib,
5 ...
6}: let
7 cfg = config.services.tangled-knot;
8in
9 with lib; {
10 options = {
11 services.tangled-knot = {
12 enable = mkOption {
13 type = types.bool;
14 default = false;
15 description = "Enable a tangled knot";
16 };
17
18 package = mkOption {
19 type = types.package;
20 description = "Package to use for the knot";
21 };
22
23 appviewEndpoint = mkOption {
24 type = types.str;
25 default = "https://tangled.sh";
26 description = "Appview endpoint";
27 };
28
29 gitUser = mkOption {
30 type = types.str;
31 default = "git";
32 description = "User that hosts git repos and performs git operations";
33 };
34
35 openFirewall = mkOption {
36 type = types.bool;
37 default = true;
38 description = "Open port 22 in the firewall for ssh";
39 };
40
41 stateDir = mkOption {
42 type = types.path;
43 default = "/home/${cfg.gitUser}";
44 description = "Tangled knot data directory";
45 };
46
47 repo = {
48 scanPath = mkOption {
49 type = types.path;
50 default = cfg.stateDir;
51 description = "Path where repositories are scanned from";
52 };
53
54 mainBranch = mkOption {
55 type = types.str;
56 default = "main";
57 description = "Default branch name for repositories";
58 };
59 };
60
61 motd = mkOption {
62 type = types.nullOr types.str;
63 default = null;
64 description = ''
65 Message of the day
66
67 The contents are shown as-is; eg. you will want to add a newline if
68 setting a non-empty message since the knot won't do this for you.
69 '';
70 };
71
72 motdFile = mkOption {
73 type = types.nullOr types.path;
74 default = null;
75 description = ''
76 File containing message of the day
77
78 The contents are shown as-is; eg. you will want to add a newline if
79 setting a non-empty message since the knot won't do this for you.
80 '';
81 };
82
83 server = {
84 listenAddr = mkOption {
85 type = types.str;
86 default = "0.0.0.0:5555";
87 description = "Address to listen on";
88 };
89
90 internalListenAddr = mkOption {
91 type = types.str;
92 default = "127.0.0.1:5444";
93 description = "Internal address for inter-service communication";
94 };
95
96 owner = mkOption {
97 type = types.str;
98 example = "did:plc:qfpnj4og54vl56wngdriaxug";
99 description = "DID of owner (required)";
100 };
101
102 dbPath = mkOption {
103 type = types.path;
104 default = "${cfg.stateDir}/knotserver.db";
105 description = "Path to the database file";
106 };
107
108 hostname = mkOption {
109 type = types.str;
110 example = "knot.tangled.sh";
111 description = "Hostname for the server (required)";
112 };
113
114 dev = mkOption {
115 type = types.bool;
116 default = false;
117 description = "Enable development mode (disables signature verification)";
118 };
119 };
120 };
121 };
122
123 config = mkIf cfg.enable {
124 environment.systemPackages = [
125 pkgs.git
126 cfg.package
127 ];
128
129 users.users.${cfg.gitUser} = {
130 isSystemUser = true;
131 useDefaultShell = true;
132 home = cfg.stateDir;
133 createHome = true;
134 group = cfg.gitUser;
135 };
136
137 users.groups.${cfg.gitUser} = {};
138
139 services.openssh = {
140 enable = true;
141 extraConfig = ''
142 Match User ${cfg.gitUser}
143 AuthorizedKeysCommand /etc/ssh/keyfetch_wrapper
144 AuthorizedKeysCommandUser nobody
145 '';
146 };
147
148 environment.etc."ssh/keyfetch_wrapper" = {
149 mode = "0555";
150 text = ''
151 #!${pkgs.stdenv.shell}
152 ${cfg.package}/bin/knot keys \
153 -output authorized-keys \
154 -internal-api "http://${cfg.server.internalListenAddr}" \
155 -git-dir "${cfg.repo.scanPath}" \
156 -log-path /tmp/knotguard.log
157 '';
158 };
159
160 systemd.services.knot = {
161 description = "knot service";
162 after = ["network.target" "sshd.service"];
163 wantedBy = ["multi-user.target"];
164 enableStrictShellChecks = true;
165
166 preStart = let
167 setMotd =
168 if cfg.motdFile != null && cfg.motd != null
169 then throw "motdFile and motd cannot be both set"
170 else ''
171 ${optionalString (cfg.motdFile != null) "cat ${cfg.motdFile} > ${cfg.stateDir}/motd"}
172 ${optionalString (cfg.motd != null) ''printf "${cfg.motd}" > ${cfg.stateDir}/motd''}
173 '';
174 in ''
175 mkdir -p "${cfg.repo.scanPath}"
176 chown -R ${cfg.gitUser}:${cfg.gitUser} "${cfg.repo.scanPath}"
177
178 mkdir -p "${cfg.stateDir}/.config/git"
179 cat > "${cfg.stateDir}/.config/git/config" << EOF
180 [user]
181 name = Git User
182 email = git@example.com
183 [receive]
184 advertisePushOptions = true
185 EOF
186 ${setMotd}
187 chown -R ${cfg.gitUser}:${cfg.gitUser} "${cfg.stateDir}"
188 '';
189
190 serviceConfig = {
191 User = cfg.gitUser;
192 PermissionsStartOnly = true;
193 WorkingDirectory = cfg.stateDir;
194 Environment = [
195 "KNOT_REPO_SCAN_PATH=${cfg.repo.scanPath}"
196 "KNOT_REPO_MAIN_BRANCH=${cfg.repo.mainBranch}"
197 "APPVIEW_ENDPOINT=${cfg.appviewEndpoint}"
198 "KNOT_SERVER_INTERNAL_LISTEN_ADDR=${cfg.server.internalListenAddr}"
199 "KNOT_SERVER_LISTEN_ADDR=${cfg.server.listenAddr}"
200 "KNOT_SERVER_DB_PATH=${cfg.server.dbPath}"
201 "KNOT_SERVER_HOSTNAME=${cfg.server.hostname}"
202 "KNOT_SERVER_OWNER=${cfg.server.owner}"
203 ];
204 ExecStart = "${cfg.package}/bin/knot server";
205 Restart = "always";
206 };
207 };
208
209 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [22];
210 };
211 }