There are both wg-quick and wireguard modules in NixOS. Their options are slightly different, but mostly the same, and they seem to do the same thing (I tried both).
Which one is better for when using wireguard as a vpn client?
I have currently configured networking.wireguard, but have two issues with its systemd service.
I don’t want it to start automatically, but didn’t figure out how to stop that. (systemd.services.wireguard-wg0.wantedBy = lib.mkForce [] didn’t seem to work)
The connection doesn’t come up properly after laptop sleep (probably the routes need to added again).
nmcli connection import type wireguard file wg0.conf
which converts the wg0.conf into /etc/NetworkManager/system-connections/wg0.nmconnection and can from then on be managed with the nm-connection-editor. Of course, you can also deploy this file declaratively using
Thanks @hmenke. Why did you choose networking.wireguard over networking.wg-quick?
In my configuration, I have all the peers, addresses, etc., defined in an attrset, and I generate either a networking.wireguard.interfaces configuration, or .nmconnection file from that.
networking.wireguard generates one unit per peer, whereas networking.wg-quick generates one unit per interface. This means that with networking.wireguard you can add more peers in configuration.nix without restarting the interface. Personally I use networking.wireguard because it was the first thing that I found when searching the docs for Wireguard
How do you generate the .nmconnection file? I’ve been reading the Wireguard wiki page and it recommends importing the wireguard file with nmcli, but when declaring the wireguard config it would be awesome to run the import on a nix rebuild.
I’m now using systemd-networkd for wireguard. It works well and is better documented than NetworkManager. The main disadvantage of networkd for me is that there is no GUI integrated with GNOME which I can use to bring the wireguard link up/down.
So I don’t generate .nmconnection files any more, but previously I was using something like this:
{ config, lib, pkgs, ... }: let
settingsFormat = let
mkValueString = lib.generators.mkValueStringDefault {};
in pkgs.formats.ini {
mkKeyValue = lib.generators.mkKeyValueDefault { inherit mkValueString; } "=";
listToValue = lib.concatMapStringsSep ";" mkValueString;
};
in lib.mkIf config.networking.networkmanager.enable {
environment.etc = lib.mapAttrs' (name: settings: let
profile = "${name}.nmconnection";
in {
name = "NetworkManager/system-connections/${profile}";
value = {
mode = "0600";
source = settingsFormat.generate profile settings;
};
}) MY_CONFIG_HERE_ELIDED_FOR_BREVITY;
}