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 secretFile = mkOption {
97 type = lib.types.path;
98 example = "KNOT_SERVER_SECRET=<hash>";
99 description = "File containing secret key provided by appview (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 system.activationScripts.gitConfig = let
130 setMotd =
131 if cfg.motdFile != null && cfg.motd != null
132 then throw "motdFile and motd cannot be both set"
133 else ''
134 ${optionalString (cfg.motdFile != null) "cat ${cfg.motdFile} > ${cfg.stateDir}/motd"}
135 ${optionalString (cfg.motd != null) ''printf "${cfg.motd}" > ${cfg.stateDir}/motd''}
136 '';
137 in ''
138 mkdir -p "${cfg.repo.scanPath}"
139 chown -R ${cfg.gitUser}:${cfg.gitUser} "${cfg.repo.scanPath}"
140
141 mkdir -p "${cfg.stateDir}/.config/git"
142 cat > "${cfg.stateDir}/.config/git/config" << EOF
143 [user]
144 name = Git User
145 email = git@example.com
146 [receive]
147 advertisePushOptions = true
148 EOF
149 ${setMotd}
150 chown -R ${cfg.gitUser}:${cfg.gitUser} "${cfg.stateDir}"
151 '';
152
153 users.users.${cfg.gitUser} = {
154 isSystemUser = true;
155 useDefaultShell = true;
156 home = cfg.stateDir;
157 createHome = true;
158 group = cfg.gitUser;
159 };
160
161 users.groups.${cfg.gitUser} = {};
162
163 services.openssh = {
164 enable = true;
165 extraConfig = ''
166 Match User ${cfg.gitUser}
167 AuthorizedKeysCommand /etc/ssh/keyfetch_wrapper
168 AuthorizedKeysCommandUser nobody
169 '';
170 };
171
172 environment.etc."ssh/keyfetch_wrapper" = {
173 mode = "0555";
174 text = ''
175 #!${pkgs.stdenv.shell}
176 ${cfg.package}/bin/knot keys \
177 -output authorized-keys \
178 -internal-api "http://${cfg.server.internalListenAddr}" \
179 -git-dir "${cfg.repo.scanPath}" \
180 -log-path /tmp/knotguard.log
181 '';
182 };
183
184 systemd.services.knot = {
185 description = "knot service";
186 after = ["network.target" "sshd.service"];
187 wantedBy = ["multi-user.target"];
188 serviceConfig = {
189 User = cfg.gitUser;
190 WorkingDirectory = cfg.stateDir;
191 Environment = [
192 "KNOT_REPO_SCAN_PATH=${cfg.repo.scanPath}"
193 "KNOT_REPO_MAIN_BRANCH=${cfg.repo.mainBranch}"
194 "APPVIEW_ENDPOINT=${cfg.appviewEndpoint}"
195 "KNOT_SERVER_INTERNAL_LISTEN_ADDR=${cfg.server.internalListenAddr}"
196 "KNOT_SERVER_LISTEN_ADDR=${cfg.server.listenAddr}"
197 "KNOT_SERVER_DB_PATH=${cfg.server.dbPath}"
198 "KNOT_SERVER_HOSTNAME=${cfg.server.hostname}"
199 ];
200 EnvironmentFile = cfg.server.secretFile;
201 ExecStart = "${cfg.package}/bin/knot server";
202 Restart = "always";
203 };
204 };
205
206 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [22];
207 };
208 }