Skip to content

Commit

Permalink
nixos/hardware/stlink: add ST-Link udev rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandapip1 committed Jan 14, 2025
1 parent ed890bd commit 496b7da
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions nixos/modules/hardware/stlink.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
lib,
config,
options,
...
}:
let
cfg = config.hardware.stlink;
opt = options.hardware.stlink;
in
{
options.hardware.stlink = {
enable = lib.mkEnableOption "udev rules for ST-Link programmer devices";
owner = lib.mkOption {
type = lib.types.str;
default = "root";
example = "nobody";
description = "Owner of ST-Link devices";
};
group = lib.mkOption {
type = lib.types.str;
default = "plugdev";
example = "nobody";
description = "Group of ST-Link devices";
};
mode = lib.mkOption {
type = lib.types.str;
default = "0660";
example = "0640";
description = "Mode of ST-Link devices";
};
};

config = lib.mkMerge [
(lib.mkIf cfg.enable {
assertions = [
{
assertion = builtins.hasAttr cfg.owner config.users.users;
message = "Owner '${cfg.owner}' set in `${opt.owner}` is not configured via `${options.users.users}.\"${cfg.owner}\"`.";
}
{
assertion = (cfg.group == opt.group.default) || (builtins.hasAttr cfg.group config.users.groups);
message = "Group '${cfg.group}' set in `${opt.group}` is not configured via `${options.users.groups}.\"${cfg.group}\"`.";
}
];
# Source: https://www.linux-usb.org/usb.ids
services.udev.extraRules = ''
# ST-Link V1
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="3744", OWNER="${cfg.owner}", GROUP="${cfg.group}", MODE="${cfg.mode}"
# ST-Link V2
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="3748", OWNER="${cfg.owner}", GROUP="${cfg.group}", MODE="${cfg.mode}"
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="3752", OWNER="${cfg.owner}", GROUP="${cfg.group}", MODE="${cfg.mode}"
# ST-Link V2.1
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="374b", OWNER="${cfg.owner}", GROUP="${cfg.group}", MODE="${cfg.mode}"
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="3752", OWNER="${cfg.owner}", GROUP="${cfg.group}", MODE="${cfg.mode}"
# ST-Link V3
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="374e", OWNER="${cfg.owner}", GROUP="${cfg.group}", MODE="${cfg.mode}"
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="374f", OWNER="${cfg.owner}", GROUP="${cfg.group}", MODE="${cfg.mode}"
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="3753", OWNER="${cfg.owner}", GROUP="${cfg.group}", MODE="${cfg.mode}"
# ST-Link V3 Loader (what is this?)
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="374d", OWNER="${cfg.owner}", GROUP="${cfg.group}", MODE="${cfg.mode}"
'';
users.groups.plugdev = lib.mkIf (config.group == "plugdev") { };
})
];

meta.maintainers = with lib.maintainers; [ pandapip1 ];
}

0 comments on commit 496b7da

Please sign in to comment.