Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpc: 0.34 -> 0.35 #347387

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 62 additions & 36 deletions nixos/modules/services/hardware/triggerhappy.nix
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}:
let

cfg = config.services.triggerhappy;

socket = "/run/thd.socket";

configFile = pkgs.writeText "triggerhappy.conf" ''
${lib.concatMapStringsSep "\n"
({ keys, event, cmd, ... }:
''${lib.concatMapStringsSep "+" (x: "KEY_" + x) keys} ${toString { press = 1; hold = 2; release = 0; }.${event}} ${cmd}''
)
cfg.bindings}
${lib.concatMapStringsSep "\n" (
{
keys,
event,
cmd,
...
}:
''${lib.concatMapStringsSep "+" (x: "KEY_" + x) keys} ${
toString
{
press = 1;
hold = 2;
release = 0;
}
.${event}
} ${cmd}''
) cfg.bindings}
${cfg.extraConfig}
'';

bindingCfg = { ... }: {
options = {

keys = lib.mkOption {
type = lib.types.listOf lib.types.str;
description = "List of keys to match. Key names as defined in linux/input-event-codes.h";
};

event = lib.mkOption {
type = lib.types.enum ["press" "hold" "release"];
default = "press";
description = "Event to match.";
};
bindingCfg =
{ ... }:
{
options = {

keys = lib.mkOption {
type = lib.types.listOf lib.types.str;
description = "List of keys to match. Key names as defined in linux/input-event-codes.h";
};

event = lib.mkOption {
type = lib.types.enum [
"press"
"hold"
"release"
];
default = "press";
description = "Event to match.";
};

cmd = lib.mkOption {
type = lib.types.str;
description = "What to run.";
};

cmd = lib.mkOption {
type = lib.types.str;
description = "What to run.";
};

};
};

in

Expand Down Expand Up @@ -65,9 +88,9 @@ in

bindings = lib.mkOption {
type = lib.types.listOf (lib.types.submodule bindingCfg);
default = [];
default = [ ];
example = lib.literalExpression ''
[ { keys = ["PLAYPAUSE"]; cmd = "''${pkgs.mpc-cli}/bin/mpc -q toggle"; } ]
[ { keys = ["PLAYPAUSE"]; cmd = "''${lib.getExe pkgs.mpc} -q toggle"; } ]
'';
description = ''
Key bindings for {command}`triggerhappy`.
Expand All @@ -86,7 +109,6 @@ in

};


###### implementation

config = lib.mkIf cfg.enable {
Expand All @@ -101,18 +123,22 @@ in
wantedBy = [ "multi-user.target" ];
description = "Global hotkey daemon";
serviceConfig = {
ExecStart = "${pkgs.triggerhappy}/bin/thd ${lib.optionalString (cfg.user != "root") "--user ${cfg.user}"} --socket ${socket} --triggers ${configFile} --deviceglob /dev/input/event*";
ExecStart = "${pkgs.triggerhappy}/bin/thd ${
lib.optionalString (cfg.user != "root") "--user ${cfg.user}"
} --socket ${socket} --triggers ${configFile} --deviceglob /dev/input/event*";
};
};

services.udev.packages = lib.singleton (pkgs.writeTextFile {
name = "triggerhappy-udev-rules";
destination = "/etc/udev/rules.d/61-triggerhappy.rules";
text = ''
ACTION=="add", SUBSYSTEM=="input", KERNEL=="event[0-9]*", ATTRS{name}!="triggerhappy", \
RUN+="${pkgs.triggerhappy}/bin/th-cmd --socket ${socket} --passfd --udev"
'';
});
services.udev.packages = lib.singleton (
pkgs.writeTextFile {
name = "triggerhappy-udev-rules";
destination = "/etc/udev/rules.d/61-triggerhappy.rules";
text = ''
ACTION=="add", SUBSYSTEM=="input", KERNEL=="event[0-9]*", ATTRS{name}!="triggerhappy", \
RUN+="${pkgs.triggerhappy}/bin/th-cmd --socket ${socket} --passfd --udev"
'';
}
);

};

Expand Down
127 changes: 72 additions & 55 deletions nixos/tests/mpd.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import ./make-test-python.nix ({ pkgs, lib, ... }:
import ./make-test-python.nix (
{ pkgs, lib, ... }:
let
track = pkgs.fetchurl {
# Sourced from http://freemusicarchive.org/music/Blue_Wave_Theory/Surf_Music_Month_Challenge/Skyhawk_Beach_fade_in
# License: http://creativecommons.org/licenses/by-sa/4.0/

name = "Blue_Wave_Theory-Skyhawk_Beach.mp3";
url = "https://freemusicarchive.org/file/music/ccCommunity/Blue_Wave_Theory/Surf_Music_Month_Challenge/Blue_Wave_Theory_-_04_-_Skyhawk_Beach.mp3";
sha256 = "0xw417bxkx4gqqy139bb21yldi37xx8xjfxrwaqa0gyw19dl6mgp";
hash = "sha256-91VDWwrcP6Cw4rk72VHvZ8RGfRBrpRE8xo/02dcJhHc=";
meta.license = lib.licenses.cc-by-nc-sa-40;
};

defaultCfg = rec {
Expand All @@ -16,42 +17,56 @@ import ./make-test-python.nix ({ pkgs, lib, ... }:
musicDirectory = "${dataDir}/music";
};

defaultMpdCfg = with defaultCfg; {
inherit dataDir musicDirectory user group;
defaultMpdCfg = {
inherit (defaultCfg)
dataDir
musicDirectory
user
group
;
enable = true;
};

musicService = { user, group, musicDirectory }: {
description = "Sets up the music file(s) for MPD to use.";
requires = [ "mpd.service" ];
after = [ "mpd.service" ];
wantedBy = [ "default.target" ];
script = ''
cp ${track} ${musicDirectory}
'';
serviceConfig = {
User = user;
Group = group;
musicService =
{
user,
group,
musicDirectory,
}:
{
description = "Sets up the music file(s) for MPD to use.";
requires = [ "mpd.service" ];
after = [ "mpd.service" ];
wantedBy = [ "default.target" ];
script = ''
cp ${track} ${musicDirectory}
'';
serviceConfig = {
User = user;
Group = group;
};
};
};

mkServer = { mpd, musicService, }:
{ boot.kernelModules = [ "snd-dummy" ];
mkServer =
{ mpd, musicService }:
{
boot.kernelModules = [ "snd-dummy" ];
services.mpd = mpd;
systemd.services.musicService = musicService;
};
in {
in
{
name = "mpd";
meta = with pkgs.lib.maintainers; {
maintainers = [ emmanuelrosa ];
meta = {
maintainers = with lib.maintainers; [ emmanuelrosa ];
};

nodes =
{ client =
{ ... }: { };
nodes = {
client = { ... }: { };

serverALSA =
{ ... }: lib.mkMerge [
{ ... }:
lib.mkMerge [
(mkServer {
mpd = defaultMpdCfg // {
network.listenAddress = "any";
Expand All @@ -63,13 +78,14 @@ import ./make-test-python.nix ({ pkgs, lib, ... }:
}
'';
};
musicService = with defaultMpdCfg; musicService { inherit user group musicDirectory; };
musicService = musicService { inherit (defaultMpdCfg) user group musicDirectory; };
})
{ networking.firewall.allowedTCPPorts = [ 6600 ]; }
];

serverPulseAudio =
{ ... }: lib.mkMerge [
{ ... }:
lib.mkMerge [
(mkServer {
mpd = defaultMpdCfg // {
extraConfig = ''
Expand All @@ -80,7 +96,7 @@ import ./make-test-python.nix ({ pkgs, lib, ... }:
'';
};

musicService = with defaultCfg; musicService { inherit user group musicDirectory; };
musicService = musicService { inherit (defaultMpdCfg) user group musicDirectory; };
})
{
hardware.pulseaudio = {
Expand All @@ -94,40 +110,41 @@ import ./make-test-python.nix ({ pkgs, lib, ... }:
];
};

testScript = ''
mpc = "${pkgs.mpc-cli}/bin/mpc --wait"
testScript = ''
mpc = "${lib.getExe pkgs.mpc} --wait"

# Connects to the given server and attempts to play a tune.
def play_some_music(server):
server.wait_for_unit("mpd.service")
server.succeed(f"{mpc} update")
_, tracks = server.execute(f"{mpc} ls")
# Connects to the given server and attempts to play a tune.
def play_some_music(server):
server.wait_for_unit("mpd.service")
server.succeed(f"{mpc} update")
_, tracks = server.execute(f"{mpc} ls")

for track in tracks.splitlines():
server.succeed(f"{mpc} add {track}")
for track in tracks.splitlines():
server.succeed(f"{mpc} add {track}")

_, added_tracks = server.execute(f"{mpc} playlist")
_, added_tracks = server.execute(f"{mpc} playlist")

# Check we succeeded adding audio tracks to the playlist
assert len(added_tracks.splitlines()) > 0
# Check we succeeded adding audio tracks to the playlist
assert len(added_tracks.splitlines()) > 0

server.succeed(f"{mpc} play")
server.succeed(f"{mpc} play")

_, output = server.execute(f"{mpc} status")
# Assure audio track is playing
assert "playing" in output
_, output = server.execute(f"{mpc} status")
# Assure audio track is playing
assert "playing" in output

server.succeed(f"{mpc} stop")
server.succeed(f"{mpc} stop")


play_some_music(serverALSA)
play_some_music(serverPulseAudio)
play_some_music(serverALSA)
play_some_music(serverPulseAudio)

client.wait_for_unit("multi-user.target")
client.succeed(f"{mpc} -h serverALSA status")
client.wait_for_unit("multi-user.target")
client.succeed(f"{mpc} -h serverALSA status")

# The PulseAudio-based server is configured not to accept external client connections
# to perform the following test:
client.fail(f"{mpc} -h serverPulseAudio status")
'';
})
# The PulseAudio-based server is configured not to accept external client connections
# to perform the following test:
client.fail(f"{mpc} -h serverPulseAudio status")
'';
}
)
Loading