@@ -1,55 +1,17 @@
11{ config, lib, pkgs, ... }:
22with lib;
33let
4+ keysPath = "/var/lib/yggdrasil/keys.json";
5+
46 cfg = config.services.yggdrasil;
5− configProvided = (cfg.config != {});
6− configAsFile = (if configProvided then
7− toString (pkgs.writeTextFile {
8− name = "yggdrasil-conf";
9− text = builtins.toJSON cfg.config;
10− })
11− else null);
12− configFileProvided = (cfg.configFile != null);
13− generateConfig = (
14− if configProvided && configFileProvided then
15− "${pkgs.jq}/bin/jq -s add ${configAsFile} ${cfg.configFile}"
16− else if configProvided then
17− "cat ${configAsFile}"
18− else if configFileProvided then
19− "cat ${cfg.configFile}"
20− else
21− "${cfg.package}/bin/yggdrasil -genconf"
22− );
7+ configProvided = cfg.config != { };
8+ configFileProvided = cfg.configFile != null;
239
2410in {
2511 options = with types; {
2612 services.yggdrasil = {
2713 enable = mkEnableOption "the yggdrasil system service";
2814
29− configFile = mkOption {
30− type = nullOr str;
31− default = null;
32− example = "/run/keys/yggdrasil.conf";
33− description = ''
34− A file which contains JSON configuration for yggdrasil.
35−
36− You do not have to supply a complete configuration, as
37− yggdrasil will use default values for anything which is
38− omitted. If the encryption and signing keys are omitted,
39− yggdrasil will generate new ones each time the service is
40− started, resulting in a random IPv6 address on the yggdrasil
41− network each time.
42−
43− If both this option and <option>config</option> are
44− supplied, they will be combined, with values from
45− <option>config</option> taking precedence.
46−
47− You can use the command <code>nix-shell -p yggdrasil --run
48− "yggdrasil -genconf -json"</code> to generate a default
49− JSON configuration.
50− '';
51− };
52−
5315 config = mkOption {
5416 type = attrs;
5517 default = {};
@@ -66,16 +28,21 @@ in {
6628 Configuration for yggdrasil, as a Nix attribute set.
6729
6830 Warning: this is stored in the WORLD-READABLE Nix store!
69− Therefore, it is not appropriate for private keys. If you
70− do not specify the keys, yggdrasil will generate a new set
71− each time the service is started, creating a random IPv6
72− address on the yggdrasil network each time.
31+ Therefore, it is not appropriate for private keys. If you
32+ wish to specify the keys, use <option>configFile</option>.
33+
34+ If the <option>persistentKeys</option> is enabled then the
35+ keys that are generated during activation will override
36+ those in <option>config</option> or
37+ <option>configFile</option>.
38+
39+ If no keys are specified then ephemeral keys are generated
40+ and the Yggdrasil interface will have a random IPv6 address
41+ each time the service is started, this is the default.
7342
74− If you wish to specify the keys, use
75− <option>configFile</option>. If both
76− <option>configFile</option> and <option>config</option> are
77− supplied, they will be combined, with values from
78− <option>config</option> taking precedence.
43+ If both <option>configFile</option> and <option>config</option>
44+ are supplied, they will be combined, with values from
45+ <option>configFile</option> taking precedence.
7946
8047 You can use the command <code>nix-shell -p yggdrasil --run
8148 "yggdrasil -genconf"</code> to generate default
@@ -83,12 +50,21 @@ in {
8350 '';
8451 };
8552
53+ configFile = mkOption {
54+ type = nullOr path;
55+ default = null;
56+ example = "/run/keys/yggdrasil.conf";
57+ description = ''
58+ A file which contains JSON configuration for yggdrasil.
59+ See the <option>config</option> option for more information.
60+ '';
61+ };
62+
8663 group = mkOption {
8764 type = types.str;
8865 default = "root";
8966 example = "wheel";
90− description =
91− "Group to grant acces to the Yggdrasil control socket.";
67+ description = "Group to grant acces to the Yggdrasil control socket.";
9268 };
9369
9470 openMulticastPort = mkOption {
@@ -126,37 +102,64 @@ in {
126102 defaultText = "pkgs.yggdrasil";
127103 description = "Yggdrasil package to use.";
128104 };
105+
106+ persistentKeys = mkEnableOption ''
107+ If enabled then keys will be generated once and Yggdrasil
108+ will retain the same IPv6 address when the service is
109+ restarted. Keys are stored at ${keysPath}.
110+ '';
111+
129112 };
130113 };
131114
132− config = mkIf cfg.enable {
133− assertions = [
134− { assertion = config.networking.enableIPv6;
135− message = "networking.enableIPv6 must be true for yggdrasil to work";
136− }
137− ];
115+ config = mkIf cfg.enable (let binYggdrasil = cfg.package + "/bin/yggdrasil";
116+ in {
117+ assertions = [{
118+ assertion = config.networking.enableIPv6;
119+ message = "networking.enableIPv6 must be true for yggdrasil to work";
120+ }];
121+
122+ system.activationScripts.yggdrasil = mkIf cfg.persistentKeys ''
123+ if [ ! -e ${keysPath} ]
124+ then
125+ mkdir -p ${builtins.dirOf keysPath}
126+ ${binYggdrasil} -genconf -json \
127+ | ${pkgs.jq}/bin/jq \
128+ 'to_entries|map(select(.key|endswith("Key")))|from_entries' \
129+ > ${keysPath}
130+ chmod 600 ${keysPath}
131+ fi
132+ '';
138133
139134 systemd.services.yggdrasil = {
140135 description = "Yggdrasil Network Service";
141− path = [ cfg.package ] ++ optional (configProvided && configFileProvided) pkgs.jq;
142136 bindsTo = [ "network-online.target" ];
143137 after = [ "network-online.target" ];
144138 wantedBy = [ "multi-user.target" ];
145139
146− preStart = ''
147− ${generateConfig} | yggdrasil -normaliseconf -useconf > /run/yggdrasil/yggdrasil.conf
148− '';
140+ preStart =
141+ (if configProvided || configFileProvided || cfg.persistentKeys then
142+ "echo "
143+
144+ + (lib.optionalString configProvided
145+ "'${builtins.toJSON cfg.config}'")
146+ + (lib.optionalString configFileProvided "$(cat ${cfg.configFile})")
147+ + (lib.optionalString cfg.persistentKeys "$(cat ${keysPath})")
148+ + " | ${pkgs.jq}/bin/jq -s add | ${binYggdrasil} -normaliseconf -useconf"
149+ else
150+ "${binYggdrasil} -genconf") + " > /run/yggdrasil/yggdrasil.conf";
149151
150152 serviceConfig = {
151− ExecStart = "${cfg.package}/bin/yggdrasil -useconffile /run/yggdrasil/yggdrasil.conf";
153+ ExecStart =
154+ "${binYggdrasil} -useconffile /run/yggdrasil/yggdrasil.conf";
152155 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
153156 Restart = "always";
154157
155158 Group = cfg.group;
156159 RuntimeDirectory = "yggdrasil";
157160 RuntimeDirectoryMode = "0750";
158− BindReadOnlyPaths = mkIf configFileProvided
159− [ "${cfg.configFile}" ];
161+ BindReadOnlyPaths = lib.optional configFileProvided cfg.configFile
162+ ++ lib.optional cfg.persistentKeys keysPath;
160163
161164 # TODO: as of yggdrasil 0.3.8 and systemd 243, yggdrasil fails
162165 # to set up the network adapter when DynamicUser is set. See
@@ -191,6 +194,6 @@ in {
191194
192195 # Make yggdrasilctl available on the command line.
193196 environment.systemPackages = [ cfg.package ];
194− };
195− meta.maintainers = with lib.maintainers; [ gazally ];
197+ });
198+ meta.maintainers = with lib.maintainers; [ gazally ehmry ];
196199}