From 42f5ecde9d8e2fd4d072b648b885087a267029cc Mon Sep 17 00:00:00 2001 From: James Atkins Date: Sun, 6 Oct 2024 16:52:42 -0500 Subject: [PATCH] nixos/networkd: support systemd-creds in WireGuard systemd 256 supports network.wireguard.* credentials (https://github.com/systemd/systemd/pull/30826). Check whether PrivateKey / PresharedKey starts with an @, if so it is a credential. --- nixos/lib/systemd-lib.nix | 5 +++++ nixos/modules/system/boot/networkd.nix | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/nixos/lib/systemd-lib.nix b/nixos/lib/systemd-lib.nix index fedd85f09b80..886bbd301db6 100644 --- a/nixos/lib/systemd-lib.nix +++ b/nixos/lib/systemd-lib.nix @@ -17,6 +17,7 @@ let filterAttrs flatten flip + hasPrefix head isInt isFloat @@ -196,6 +197,10 @@ in rec { optional (attr ? ${name}) "Systemd ${group} field `${name}' has been removed. See ${see}"; + assertKeyIsSystemdCredential = name: group: attr: + optional (attr ? ${name} && !(hasPrefix "@" attr.${name})) + "Systemd ${group} field `${name}' is not a systemd credential"; + checkUnitConfig = group: checks: attrs: let # We're applied at the top-level type (attrsOf unitOption), so the actual # unit options might contain attributes from mkOverride and mkIf that we need to diff --git a/nixos/modules/system/boot/networkd.nix b/nixos/modules/system/boot/networkd.nix index 94a5de7e2bd4..c56a5b3bfdfa 100644 --- a/nixos/modules/system/boot/networkd.nix +++ b/nixos/modules/system/boot/networkd.nix @@ -411,11 +411,14 @@ let (assertValueOneOf "Layer2SpecificHeader" [ "none" "default" ]) ]; - # NOTE The PrivateKey directive is missing on purpose here, please - # do not add it to this list. The nix store is world-readable let's - # refrain ourselves from providing a footgun. + # NOTE Check whether the key starts with an @, in which case it is + # interpreted as the name of the credential from which the actual key + # shall be read by systemd-creds. + # Do not remove this check as the nix store is world-readable. sectionWireGuard = checkUnitConfig "WireGuard" [ + (assertKeyIsSystemdCredential "PrivateKey") (assertOnlyFields [ + "PrivateKey" "PrivateKeyFile" "ListenPort" "FirewallMark" @@ -426,12 +429,15 @@ let (assertRange "FirewallMark" 1 4294967295) ]; - # NOTE The PresharedKey directive is missing on purpose here, please - # do not add it to this list. The nix store is world-readable,let's - # refrain ourselves from providing a footgun. + # NOTE Check whether the key starts with an @, in which case it is + # interpreted as the name of the credential from which the actual key + # shall be read by systemd-creds. + # Do not remove this check as the nix store is world-readable. sectionWireGuardPeer = checkUnitConfigWithLegacyKey "wireguardPeerConfig" "WireGuardPeer" [ + (assertKeyIsSystemdCredential "PresharedKey") (assertOnlyFields [ "PublicKey" + "PresharedKey" "PresharedKeyFile" "AllowedIPs" "Endpoint"