-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.nix
59 lines (54 loc) · 1.76 KB
/
default.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{ config, lib, pkgs, ... }:
with lib;
let cfg = config.profiles.sparky-sd-mac;
in {
options.profiles.sparky-sd-mac = {
enable = mkEnableOption (mdDoc ''
Enable the SPARKY SD-MAC profile.
Use the serial number of the SD-Card for generating a unique and persistend MAC address.
Useful for devices that don't have a unique MAC address like the NanoPi R2S.
'');
macPrefix = mkOption {
type = types.str;
default = "aa:91:36";
description = mdDoc ''
Prefix (OUI) of the generated MAC addresses.
'';
};
blockDeviceName = mkOption {
type = types.str;
default = "mmcblk0";
description = mdDoc ''
Name of the block device of the SD-Card (on the device that will be the probe later).
'';
};
macInterfaceName = mkOption {
type = types.str;
description = mdDoc ''
The MAC address of this interface will be set to the generated MAC.
'';
};
};
config = mkIf cfg.enable {
# SD-MAC setup
systemd.services.sdmac-setup = {
description = "SD-MAC Setup";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
path = with pkgs; [ iproute2 gnused gawk ];
serviceConfig = {
Restart = "on-failure";
RestartSec = 2;
Type = "oneshot";
};
script = ''
set -euo pipefail
MAC_SUFFIX=$(cat /sys/block/${cfg.blockDeviceName}/device/serial | md5sum | awk '{ print $1 }' | head -c 6 | sed -e 's/./&:/2' -e 's/./&:/5' | tr -d '\n')
MAC_ADDRESS="${cfg.macPrefix}:$MAC_SUFFIX"
ip link set dev ${cfg.macInterfaceName} down
ip link set dev ${cfg.macInterfaceName} address $MAC_ADDRESS
ip link set dev ${cfg.macInterfaceName} up
'';
};
};
}