this repo has no description
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 secretFile = mkOption {
103 type = lib.types.path;
104 example = "KNOT_SERVER_SECRET=<hash>";
105 description = "File containing secret key provided by appview (required)";
106 };
107
108 dbPath = mkOption {
109 type = types.path;
110 default = "${cfg.stateDir}/knotserver.db";
111 description = "Path to the database file";
112 };
113
114 hostname = mkOption {
115 type = types.str;
116 example = "knot.tangled.sh";
117 description = "Hostname for the server (required)";
118 };
119
120 dev = mkOption {
121 type = types.bool;
122 default = false;
123 description = "Enable development mode (disables signature verification)";
124 };
125 };
126 };
127 };
128
129 config = mkIf cfg.enable {
130 environment.systemPackages = [
131 pkgs.git
132 cfg.package
133 ];
134
135 users.users.${cfg.gitUser} = {
136 isSystemUser = true;
137 useDefaultShell = true;
138 home = cfg.stateDir;
139 createHome = true;
140 group = cfg.gitUser;
141 };
142
143 users.groups.${cfg.gitUser} = {};
144
145 services.openssh = {
146 enable = true;
147 extraConfig = ''
148 Match User ${cfg.gitUser}
149 AuthorizedKeysCommand /etc/ssh/keyfetch_wrapper
150 AuthorizedKeysCommandUser nobody
151 '';
152 };
153
154 environment.etc."ssh/keyfetch_wrapper" = {
155 mode = "0555";
156 text = ''
157 #!${pkgs.stdenv.shell}
158 ${cfg.package}/bin/knot keys \
159 -output authorized-keys \
160 -internal-api "http://${cfg.server.internalListenAddr}" \
161 -git-dir "${cfg.repo.scanPath}" \
162 -log-path /tmp/knotguard.log
163 '';
164 };
165
166 systemd.services.knot = {
167 description = "knot service";
168 after = ["network.target" "sshd.service"];
169 wantedBy = ["multi-user.target"];
170 enableStrictShellChecks = true;
171
172 preStart = let
173 setMotd =
174 if cfg.motdFile != null && cfg.motd != null
175 then throw "motdFile and motd cannot be both set"
176 else ''
177 ${optionalString (cfg.motdFile != null) "cat ${cfg.motdFile} > ${cfg.stateDir}/motd"}
178 ${optionalString (cfg.motd != null) ''printf "${cfg.motd}" > ${cfg.stateDir}/motd''}
179 '';
180 in ''
181 mkdir -p "${cfg.repo.scanPath}"
182 chown -R ${cfg.gitUser}:${cfg.gitUser} "${cfg.repo.scanPath}"
183
184 mkdir -p "${cfg.stateDir}/.config/git"
185 cat > "${cfg.stateDir}/.config/git/config" << EOF
186 [user]
187 name = Git User
188 email = git@example.com
189 [receive]
190 advertisePushOptions = true
191 EOF
192 ${setMotd}
193 chown -R ${cfg.gitUser}:${cfg.gitUser} "${cfg.stateDir}"
194 '';
195
196 serviceConfig = {
197 User = cfg.gitUser;
198 PermissionsStartOnly = true;
199 WorkingDirectory = cfg.stateDir;
200 Environment = [
201 "KNOT_REPO_SCAN_PATH=${cfg.repo.scanPath}"
202 "KNOT_REPO_MAIN_BRANCH=${cfg.repo.mainBranch}"
203 "APPVIEW_ENDPOINT=${cfg.appviewEndpoint}"
204 "KNOT_SERVER_INTERNAL_LISTEN_ADDR=${cfg.server.internalListenAddr}"
205 "KNOT_SERVER_LISTEN_ADDR=${cfg.server.listenAddr}"
206 "KNOT_SERVER_DB_PATH=${cfg.server.dbPath}"
207 "KNOT_SERVER_HOSTNAME=${cfg.server.hostname}"
208 "KNOT_SERVER_OWNER=${cfg.server.owner}"
209 ];
210 EnvironmentFile = cfg.server.secretFile;
211 ExecStart = "${cfg.package}/bin/knot server";
212 Restart = "always";
213 };
214 };
215
216 networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [22];
217 };
218 }