21 cfg = config.services.rosenpass;
22 opt = options.services.rosenpass;
23 settingsFormat = pkgs.formats.toml { };
26 options.services.rosenpass =
42 enable = lib.mkEnableOption "Rosenpass";
44 package = lib.mkPackageOption pkgs "rosenpass" { };
46 defaultDevice = mkOption {
48 description = "Name of the network interface to use for all peers by default.";
54 freeformType = settingsFormat.type;
57 public_key = mkOption {
59 description = "Path to a file containing the public key of the local Rosenpass peer. Generate this by running {command}`rosenpass gen-keys`.";
62 secret_key = mkOption {
64 description = "Path to a file containing the secret key of the local Rosenpass peer. Generate this by running {command}`rosenpass gen-keys`.";
69 description = "List of local endpoints to listen for connections.";
71 example = literalExpression "[ \"0.0.0.0:10000\" ]";
74 verbosity = mkOption {
80 description = "Verbosity of output produced by the service.";
86 freeformType = settingsFormat.type;
89 public_key = mkOption {
91 description = "Path to a file containing the public key of the remote Rosenpass peer.";
97 description = "Endpoint of the remote Rosenpass peer.";
102 default = cfg.defaultDevice;
103 defaultText = literalExpression "config.${opt.defaultDevice}";
104 description = "Name of the local WireGuard interface to use for this peer.";
109 description = "WireGuard public key corresponding to the remote Rosenpass peer.";
116 description = "List of peers to exchange keys with.";
122 description = "Configuration for Rosenpass, see <https://rosenpass.eu/> for further information.";
126 config = mkIf cfg.enable {
129 # NOTE: In the descriptions below, we tried to refer to e.g.
130 # options.systemd.network.netdevs."<name>".wireguardPeers.*.PublicKey
131 # directly, but don't know how to traverse "<name>" and * in this path.
134 relevant = config.systemd.network.enable;
135 root = config.systemd.network.netdevs;
136 peer = (x: x.wireguardPeers);
137 key = x: x.PublicKey or null;
138 description = "${options.systemd.network.netdevs}.\"<name>\".wireguardPeers.*.PublicKey";
141 relevant = config.networking.wireguard.enable;
142 root = config.networking.wireguard.interfaces;
144 key = (x: x.publicKey);
145 description = "${options.networking.wireguard.interfaces}.\"<name>\".peers.*.publicKey";
148 relevant = root != { };
149 root = config.networking.wg-quick.interfaces;
151 key = (x: x.publicKey);
152 description = "${options.networking.wg-quick.interfaces}.\"<name>\".peers.*.publicKey";
155 relevantExtractions = filter (x: x.relevant) extractions;
163 filter (x: x != null) (flatten (concatMap (x: (map key (peer x))) (attrValues root)));
164 configuredKeys = flatten (map extract relevantExtractions);
165 itemize = xs: concatLines (map (x: " - ${x}") xs);
166 descriptions = map (x: "`${x.description}`");
167 missingKeys = filter (key: !builtins.elem key configuredKeys) (map (x: x.peer) cfg.settings.peers);
169 While this may work as expected, e.g. you want to manually configure WireGuard,
170 such a scenario is unusual. Please double-check your configuration.
173 (optional (relevantExtractions != [ ] && missingKeys != [ ]) ''
174 You have configured Rosenpass peers with the WireGuard public keys:
175 ${itemize missingKeys}
176 But there is no corresponding active Wireguard peer configuration in any of:
177 ${itemize (descriptions relevantExtractions)}
180 ++ optional (relevantExtractions == [ ]) ''
181 You have configured Rosenpass, but you have not configured Wireguard via any of:
182 ${itemize (descriptions extractions)}
186 environment.systemPackages = [
191 systemd.services.rosenpass =
193 filterNonNull = filterAttrsRecursive (_: v: v != null);
194 config = settingsFormat.generate "config.toml" (
199 credentialPath = id: "$CREDENTIALS_DIRECTORY/${id}";
200 # NOTE: We would like to remove all `null` values inside `cfg.settings`
201 # recursively, since `settingsFormat.generate` cannot handle `null`.
202 # This would require to traverse both attribute sets and lists recursively.
203 # `filterAttrsRecursive` only recurses into attribute sets, but not
204 # into values that might contain other attribute sets (such as lists,
205 # e.g. `cfg.settings.peers`). Here, we just specialize on `cfg.settings.peers`,
206 # and this may break unexpectedly whenever a `null` value is contained
207 # in a list in `cfg.settings`, other than `cfg.settings.peers`.
208 peersWithoutNulls = map filterNonNull cfg.settings.peers;
211 secret_key = credentialPath "pqsk";
212 public_key = credentialPath "pqpk";
213 peers = peersWithoutNulls;
220 wantedBy = [ "multi-user.target" ];
221 wants = [ "network-online.target" ];
222 after = [ "network-online.target" ];
231 RuntimeDirectory = "rosenpass";
233 AmbientCapabilities = [ "CAP_NET_ADMIN" ];
235 "pqsk:${cfg.settings.secret_key}"
236 "pqpk:${cfg.settings.public_key}"
240 # See <https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Specifiers>
241 environment.CONFIG = "%t/${serviceConfig.RuntimeDirectory}/config.toml";
244 ${getExe pkgs.envsubst} -i ${config} -o "$CONFIG"
245 rosenpass exchange-config "$CONFIG"