2012-03-15 08:19:17 +01:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
2013-01-03 18:55:56 +01:00
|
|
|
with pkgs;
|
2012-03-15 08:19:17 +01:00
|
|
|
|
|
|
|
let
|
2012-09-19 14:13:34 +02:00
|
|
|
cfg = config.networking.networkmanager;
|
|
|
|
|
|
|
|
stateDirs = "/var/lib/NetworkManager /var/lib/dhclient";
|
|
|
|
|
2013-01-03 18:55:56 +01:00
|
|
|
configFile = writeText "NetworkManager.conf" ''
|
2012-09-19 14:13:34 +02:00
|
|
|
[main]
|
|
|
|
plugins=keyfile
|
|
|
|
|
|
|
|
[keyfile]
|
|
|
|
${optionalString (config.networking.hostName != "") ''
|
|
|
|
hostname=${config.networking.hostName}
|
|
|
|
''}
|
|
|
|
|
|
|
|
[logging]
|
|
|
|
level=WARN
|
|
|
|
'';
|
|
|
|
|
|
|
|
polkitConf = ''
|
|
|
|
[network-manager]
|
|
|
|
Identity=unix-group:networkmanager
|
|
|
|
Action=org.freedesktop.NetworkManager.*
|
|
|
|
ResultAny=yes
|
|
|
|
ResultInactive=no
|
|
|
|
ResultActive=yes
|
|
|
|
|
|
|
|
[modem-manager]
|
|
|
|
Identity=unix-group:networkmanager
|
|
|
|
Action=org.freedesktop.ModemManager.*
|
|
|
|
ResultAny=yes
|
|
|
|
ResultInactive=no
|
|
|
|
ResultActive=yes
|
|
|
|
'';
|
|
|
|
|
2013-01-03 18:55:56 +01:00
|
|
|
ipUpScript = writeScript "01nixos-ip-up" ''
|
2012-09-19 14:13:34 +02:00
|
|
|
#!/bin/sh
|
|
|
|
if test "$2" = "up"; then
|
2013-01-16 13:17:57 +01:00
|
|
|
${config.systemd.package}/bin/systemctl start ip-up.target
|
2012-09-19 14:13:34 +02:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2013-08-16 00:35:57 +02:00
|
|
|
overrideNameserversScript = writeScript "02overridedns" ''
|
|
|
|
#!/bin/sh
|
2013-08-20 13:36:01 +02:00
|
|
|
${optionalString cfg.overrideNameservers "${gnused}/bin/sed -i '/nameserver /d' /etc/resolv.conf"}
|
2013-09-23 16:01:56 +02:00
|
|
|
${concatStrings (map (s: ''
|
|
|
|
${optionalString cfg.appendNameservers
|
|
|
|
"${gnused}/bin/sed -i '/nameserver ${s}/d' /etc/resolv.conf"
|
|
|
|
}
|
|
|
|
echo 'nameserver ${s}' >> /etc/resolv.conf
|
|
|
|
'') config.networking.nameservers)}
|
2013-08-16 00:35:57 +02:00
|
|
|
'';
|
|
|
|
|
2012-09-19 14:13:34 +02:00
|
|
|
in {
|
2012-03-15 08:19:17 +01:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2013-08-16 00:35:57 +02:00
|
|
|
networking.networkmanager = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
merge = mergeEnableOption;
|
|
|
|
description = ''
|
|
|
|
Whether to use NetworkManager to obtain an IP address and other
|
|
|
|
configuration for all network interfaces that are not manually
|
|
|
|
configured. If enabled, a group <literal>networkmanager</literal>
|
|
|
|
will be created. Add all users that should have permission
|
|
|
|
to change network settings to this group.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
packages = mkOption {
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
Extra packages that provide NetworkManager plugins.
|
|
|
|
'';
|
|
|
|
merge = mergeListOption;
|
|
|
|
apply = list: [ networkmanager modemmanager wpa_supplicant ] ++ list;
|
|
|
|
};
|
|
|
|
|
|
|
|
overrideNameservers = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
If enabled, any nameservers received by DHCP or configured in
|
|
|
|
NetworkManager will be replaced by the nameservers configured
|
2013-08-20 13:36:01 +02:00
|
|
|
in the <literal>networking.nameservers</literal> option. This
|
|
|
|
option overrides the <literal>appendNameservers</literal> option
|
|
|
|
if both are enabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
appendNameservers = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
If enabled, the name servers configured in the
|
|
|
|
<literal>networking.nameservers</literal> option will be appended
|
|
|
|
to the ones configured in NetworkManager or received by DHCP.
|
2013-08-16 00:35:57 +02:00
|
|
|
'';
|
|
|
|
};
|
2012-03-15 08:19:17 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2012-09-19 14:13:34 +02:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2013-03-31 21:18:51 +02:00
|
|
|
assertions = [{
|
|
|
|
assertion = config.networking.wireless.enable == false;
|
|
|
|
message = "You can not use networking.networkmanager with services.networking.wireless";
|
|
|
|
}];
|
|
|
|
|
2013-01-03 18:55:56 +01:00
|
|
|
environment.etc = [
|
|
|
|
{ source = ipUpScript;
|
|
|
|
target = "NetworkManager/dispatcher.d/01nixos-ip-up";
|
|
|
|
}
|
|
|
|
{ source = configFile;
|
|
|
|
target = "NetworkManager/NetworkManager.conf";
|
|
|
|
}
|
2013-05-13 17:52:19 +02:00
|
|
|
{ source = "${networkmanager_openvpn}/etc/NetworkManager/VPN/nm-openvpn-service.name";
|
|
|
|
target = "NetworkManager/VPN/nm-openvpn-service.name";
|
|
|
|
}
|
2013-07-10 16:43:26 +02:00
|
|
|
{ source = "${networkmanager_vpnc}/etc/NetworkManager/VPN/nm-vpnc-service.name";
|
|
|
|
target = "NetworkManager/VPN/nm-vpnc-service.name";
|
|
|
|
}
|
|
|
|
{ source = "${networkmanager_openconnect}/etc/NetworkManager/VPN/nm-openconnect-service.name";
|
|
|
|
target = "NetworkManager/VPN/nm-openconnect-service.name";
|
|
|
|
}
|
2013-08-20 13:36:01 +02:00
|
|
|
] ++ pkgs.lib.optional (cfg.overrideNameservers || cfg.appendNameservers)
|
2013-08-16 00:35:57 +02:00
|
|
|
{ source = overrideNameserversScript;
|
|
|
|
target = "NetworkManager/dispatcher.d/02overridedns";
|
|
|
|
};
|
2012-09-19 14:13:34 +02:00
|
|
|
|
2013-07-10 16:43:26 +02:00
|
|
|
environment.systemPackages = cfg.packages ++ [
|
|
|
|
networkmanager_openvpn
|
|
|
|
networkmanager_vpnc
|
|
|
|
networkmanager_openconnect
|
|
|
|
];
|
2012-03-15 08:19:17 +01:00
|
|
|
|
2012-09-19 14:13:34 +02:00
|
|
|
users.extraGroups = singleton {
|
|
|
|
name = "networkmanager";
|
|
|
|
gid = config.ids.gids.networkmanager;
|
|
|
|
};
|
|
|
|
|
2013-01-03 18:55:56 +01:00
|
|
|
systemd.packages = cfg.packages;
|
|
|
|
|
|
|
|
# Create an initialisation service that both starts
|
|
|
|
# NetworkManager when network.target is reached,
|
|
|
|
# and sets up necessary directories for NM.
|
2013-01-17 13:37:54 +01:00
|
|
|
systemd.services."networkmanager-init" = {
|
2013-01-03 18:55:56 +01:00
|
|
|
description = "NetworkManager initialisation";
|
|
|
|
wantedBy = [ "network.target" ];
|
|
|
|
partOf = [ "NetworkManager.service" ];
|
|
|
|
wants = [ "NetworkManager.service" ];
|
|
|
|
before = [ "NetworkManager.service" ];
|
|
|
|
script = ''
|
2012-09-19 14:13:34 +02:00
|
|
|
mkdir -m 700 -p /etc/NetworkManager/system-connections
|
|
|
|
mkdir -m 755 -p ${stateDirs}
|
|
|
|
'';
|
2013-01-03 18:55:56 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
};
|
2012-09-19 14:13:34 +02:00
|
|
|
};
|
2012-03-15 08:19:17 +01:00
|
|
|
|
2013-01-03 18:55:56 +01:00
|
|
|
# Turn off NixOS' network management
|
|
|
|
networking = {
|
|
|
|
useDHCP = false;
|
|
|
|
wireless.enable = false;
|
|
|
|
};
|
2012-09-19 14:13:34 +02:00
|
|
|
|
2013-04-22 19:36:14 +02:00
|
|
|
powerManagement.resumeCommands = ''
|
|
|
|
systemctl restart NetworkManager
|
|
|
|
'';
|
|
|
|
|
2012-09-19 14:13:34 +02:00
|
|
|
security.polkit.permissions = polkitConf;
|
|
|
|
|
2013-05-11 19:25:14 +02:00
|
|
|
# openvpn plugin has only dbus interface
|
2013-07-10 16:43:26 +02:00
|
|
|
services.dbus.packages = cfg.packages ++ [
|
|
|
|
networkmanager_openvpn
|
|
|
|
networkmanager_vpnc
|
|
|
|
networkmanager_openconnect
|
|
|
|
];
|
2012-09-19 14:13:34 +02:00
|
|
|
|
|
|
|
services.udev.packages = cfg.packages;
|
2012-03-15 08:19:17 +01:00
|
|
|
};
|
|
|
|
}
|