Do you guys always manually type wpa_supplicant command every time connecting to a wifi?

When I enabled networking.wireless, rebuild reported the wpa configuration not found.

I found a post here saying disabling it and enabling networking.networkmanager instead, and the author was still left in confusion why. I followed his suggestion, and rebuild works. I also still wonder why.

now I have to manually run wpa_supplicant -B -i interface -c <(wpa_phrase bssid key).
Do you guys always manually type this command every time connecting to a wifi?

Thanks.

1 Like

No. You can use any networking utility you prefer to manage wireless connections just as on any other distro. As you already mentioned you can use Network Manager or something like it, or just define declarative wifi networks in your configuration using the networking.wireless.networks option.

It’s up to you. On my laptop, I prefer to use network manager so I can easily switch between networks when traveling, but for a box at home which doesn’t move at all, a declarative approach is nice.

Thanks. Why

? Can you give some example in the configuration?

https://nixos.org/manual/nixos/stable/#sec-wireless

You can also use Network Manager; this way you won’t have to specify the networks in your NixOS configuration, and Network Manager will connect automatically after sleep/reboot/etc. Jon Ringer’s answer is a visually pleasing intro.

The way I set it up:

  1. In /etc/nixos/configuration.nix, I had to add my user to the networkmanager group. The example from Chapter 7. User Management in the NixOS manual shows how:

    users.users.alice = {
      home = "/home/alice";
      extraGroups = [ "wheel" "networkmanager" ];
      # ...
    };
    

    (Here’s the reference docs for users.users.<your-username>.extraGroups NixOS option.)

  2. Next (from Chris Martin’s Installing NixOS post):

    Now you’ll want to turn on the network manager which can manage your WPA keys so you don’t have to keep manually messing with wpa_supplicant .

    Replace

    networking.wireless.enable = true;
    

    with

    networking.networkmanager.enable = true;
    

    Also add kde4.networkmanagement to the package list to get a GUI for it.

  3. sudo nixos-rebuild switch

Then the commands that I usually use:

  • nmcli device wifi list
  • nmcli device wifi connect "wifi-name" password 'password'

I use an old Lenovo T520 laptop, and the Network Manager sometimes acts up with different error messages every time after coming back from sleep or reboot, but then this usually helps:

sudo systemctl restart wpa_supplicant.service dhcpcd.service NetworkManager.service
1 Like

I choose Network Manager too, to be able to easily add new network connections.

However, I implemented the autogeneration of /etc/NetworkManager/system-connections/*.nmconnection files, by using the following module :

{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.networking.networkmanager;

  getFileName = stringAsChars (x: if x == " " then "-" else x);

  createWifi = ssid: opt: {
    name = ''
      NetworkManager/system-connections/${getFileName ssid}.nmconnection
    '';
    value = {
      mode = "0400";
      source = pkgs.writeText "${ssid}.nmconnection" ''
        [connection]
        id=${ssid}
        type=wifi

        [wifi]
        ssid=${ssid}

        [wifi-security]
        ${optionalString (opt.psk != null) ''
        key-mgmt=wpa-psk
        psk=${opt.psk}''}
      '';
    };
  };

  keyFiles = mapAttrs' createWifi config.networking.wireless.networks;
in {
  config = mkIf cfg.enable {
    environment.etc = keyFiles;

    systemd.services.NetworkManager-predefined-connections = {
      restartTriggers = mapAttrsToList (name: value: value.source) keyFiles;
      serviceConfig = {
        Type = "oneshot";
        RemainAfterExit = true;
        ExecStart = "${pkgs.coreutils}/bin/true";
        ExecReload = "${pkgs.networkmanager}/bin/nmcli connection reload";
      };
      reloadIfChanged = true;
      wantedBy = [ "multi-user.target" ];
    };
  };
}

That way, I can also add declaratively some networks with

networking.networkmanager.enable = true;
networking.wireless.networks."SSID" = { psk = "KEY" };
2 Likes

I use the wpa-supplicant approach. I don’t like using the complexity of NetworkManager for something that simple and I prefer the declarative approach as much as possible (besides things that need passwords).

1 Like

If you’re corcerned about this, in NixOS 21.11 (or unstable even) there is an option for safely configuring the network without including the password in the configuration file.

3 Likes