diff --git a/nixos/doc/manual/release-notes/rl-2105.xml b/nixos/doc/manual/release-notes/rl-2105.xml
index 2886c4c5f75..2b0a265cd98 100644
--- a/nixos/doc/manual/release-notes/rl-2105.xml
+++ b/nixos/doc/manual/release-notes/rl-2105.xml
@@ -333,6 +333,17 @@
vim and neovim switched to Python 3, dropping all Python 2 support.
+
+
+ networking.wireguard.interfaces.<name>.generatePrivateKeyFile,
+ which is off by default, had a chmod race condition
+ fixed. As an aside, the parent directory's permissions were widened,
+ and the key files were made owner-writable.
+ This only affects newly created keys.
+ However, if the exact permissions are important for your setup, read
+ #121294.
+
+
boot.zfs.forceImportAll
diff --git a/nixos/modules/services/networking/wireguard.nix b/nixos/modules/services/networking/wireguard.nix
index 34c86934535..043bce16e54 100644
--- a/nixos/modules/services/networking/wireguard.nix
+++ b/nixos/modules/services/networking/wireguard.nix
@@ -246,12 +246,15 @@ let
};
script = ''
- mkdir --mode 0644 -p "${dirOf values.privateKeyFile}"
+ set -e
+
+ # If the parent dir does not already exist, create it.
+ # Otherwise, does nothing, keeping existing permisions intact.
+ mkdir -p --mode 0755 "${dirOf values.privateKeyFile}"
+
if [ ! -f "${values.privateKeyFile}" ]; then
- touch "${values.privateKeyFile}"
- chmod 0600 "${values.privateKeyFile}"
- wg genkey > "${values.privateKeyFile}"
- chmod 0400 "${values.privateKeyFile}"
+ # Write private key file with atomically-correct permissions.
+ (set -e; umask 077; wg genkey > "${values.privateKeyFile}")
fi
'';
};